blob: a8b27cdc2844855fb2d030a246bf2374e7e27269 [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) {
Takashi Iwai67756e32015-07-17 15:22:33 +020088 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +020089 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
Jie Yang90bbaf62015-10-16 17:57:46 +0800489static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
490 int event)
491{
492#ifdef CONFIG_SND_PCM_TIMER
493 if (substream->timer)
494 snd_timer_notify(substream->timer, event,
495 &substream->runtime->trigger_tstamp);
496#endif
497}
498
Takashi Iwai877211f2005-11-17 13:59:38 +0100499static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
500 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Takashi Iwai877211f2005-11-17 13:59:38 +0100502 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700503 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned int bits;
505 snd_pcm_uframes_t frames;
506
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200507 if (PCM_RUNTIME_CHECK(substream))
508 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 snd_pcm_stream_lock_irq(substream);
511 switch (runtime->status->state) {
512 case SNDRV_PCM_STATE_OPEN:
513 case SNDRV_PCM_STATE_SETUP:
514 case SNDRV_PCM_STATE_PREPARED:
515 break;
516 default:
517 snd_pcm_stream_unlock_irq(substream);
518 return -EBADFD;
519 }
520 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100521#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!substream->oss.oss)
523#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200524 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -EBADFD;
526
527 params->rmask = ~0U;
528 err = snd_pcm_hw_refine(substream, params);
529 if (err < 0)
530 goto _error;
531
532 err = snd_pcm_hw_params_choose(substream, params);
533 if (err < 0)
534 goto _error;
535
536 if (substream->ops->hw_params != NULL) {
537 err = substream->ops->hw_params(substream, params);
538 if (err < 0)
539 goto _error;
540 }
541
542 runtime->access = params_access(params);
543 runtime->format = params_format(params);
544 runtime->subformat = params_subformat(params);
545 runtime->channels = params_channels(params);
546 runtime->rate = params_rate(params);
547 runtime->period_size = params_period_size(params);
548 runtime->periods = params_periods(params);
549 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 runtime->info = params->info;
551 runtime->rate_num = params->rate_num;
552 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100553 runtime->no_period_wakeup =
554 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
555 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 bits = snd_pcm_format_physical_width(runtime->format);
558 runtime->sample_bits = bits;
559 bits *= runtime->channels;
560 runtime->frame_bits = bits;
561 frames = 1;
562 while (bits % 8 != 0) {
563 bits *= 2;
564 frames *= 2;
565 }
566 runtime->byte_align = bits / 8;
567 runtime->min_align = frames;
568
569 /* Default sw params */
570 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
571 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 runtime->start_threshold = 1;
574 runtime->stop_threshold = runtime->buffer_size;
575 runtime->silence_threshold = 0;
576 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200577 runtime->boundary = runtime->buffer_size;
578 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
579 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200582 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700583
James Bottomley82f68252010-07-05 22:53:06 +0200584 if (pm_qos_request_active(&substream->latency_pm_qos_req))
585 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700586 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200587 pm_qos_add_request(&substream->latency_pm_qos_req,
588 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
590 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300591 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 so we force application to retry to set
593 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200594 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (substream->ops->hw_free != NULL)
596 substream->ops->hw_free(substream);
597 return err;
598}
599
Takashi Iwai877211f2005-11-17 13:59:38 +0100600static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
601 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Takashi Iwai877211f2005-11-17 13:59:38 +0100603 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int err;
605
Li Zefanef44a1e2009-04-10 09:43:08 +0800606 params = memdup_user(_params, sizeof(*params));
607 if (IS_ERR(params))
608 return PTR_ERR(params);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 err = snd_pcm_hw_params(substream, params);
611 if (copy_to_user(_params, params, sizeof(*params))) {
612 if (!err)
613 err = -EFAULT;
614 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 kfree(params);
617 return err;
618}
619
Takashi Iwai877211f2005-11-17 13:59:38 +0100620static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Takashi Iwai877211f2005-11-17 13:59:38 +0100622 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int result = 0;
624
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200625 if (PCM_RUNTIME_CHECK(substream))
626 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 snd_pcm_stream_lock_irq(substream);
629 switch (runtime->status->state) {
630 case SNDRV_PCM_STATE_SETUP:
631 case SNDRV_PCM_STATE_PREPARED:
632 break;
633 default:
634 snd_pcm_stream_unlock_irq(substream);
635 return -EBADFD;
636 }
637 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200638 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -EBADFD;
640 if (substream->ops->hw_free)
641 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200642 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200643 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return result;
645}
646
Takashi Iwai877211f2005-11-17 13:59:38 +0100647static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
648 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Takashi Iwai877211f2005-11-17 13:59:38 +0100650 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100651 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200653 if (PCM_RUNTIME_CHECK(substream))
654 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 snd_pcm_stream_lock_irq(substream);
657 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
658 snd_pcm_stream_unlock_irq(substream);
659 return -EBADFD;
660 }
661 snd_pcm_stream_unlock_irq(substream);
662
Dan Carpenter145d92e2015-09-23 12:42:28 +0300663 if (params->tstamp_mode < 0 ||
664 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -EINVAL;
Takashi Iwai58900812014-07-16 17:45:27 +0200666 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
667 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200668 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (params->avail_min == 0)
670 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (params->silence_size >= runtime->boundary) {
672 if (params->silence_threshold != 0)
673 return -EINVAL;
674 } else {
675 if (params->silence_size > params->silence_threshold)
676 return -EINVAL;
677 if (params->silence_threshold > runtime->buffer_size)
678 return -EINVAL;
679 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100680 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 snd_pcm_stream_lock_irq(substream);
682 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai58900812014-07-16 17:45:27 +0200683 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
684 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 runtime->period_step = params->period_step;
686 runtime->control->avail_min = params->avail_min;
687 runtime->start_threshold = params->start_threshold;
688 runtime->stop_threshold = params->stop_threshold;
689 runtime->silence_threshold = params->silence_threshold;
690 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 params->boundary = runtime->boundary;
692 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
694 runtime->silence_size > 0)
695 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100696 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Takashi Iwai877211f2005-11-17 13:59:38 +0100702static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
703 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Takashi Iwai877211f2005-11-17 13:59:38 +0100705 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int err;
707 if (copy_from_user(&params, _params, sizeof(params)))
708 return -EFAULT;
709 err = snd_pcm_sw_params(substream, &params);
710 if (copy_to_user(_params, &params, sizeof(params)))
711 return -EFAULT;
712 return err;
713}
714
Takashi Iwai877211f2005-11-17 13:59:38 +0100715int snd_pcm_status(struct snd_pcm_substream *substream,
716 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Takashi Iwai877211f2005-11-17 13:59:38 +0100718 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600721
722 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
723 &runtime->audio_tstamp_config);
724
725 /* backwards compatible behavior */
726 if (runtime->audio_tstamp_config.type_requested ==
727 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
728 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
729 runtime->audio_tstamp_config.type_requested =
730 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
731 else
732 runtime->audio_tstamp_config.type_requested =
733 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
734 runtime->audio_tstamp_report.valid = 0;
735 } else
736 runtime->audio_tstamp_report.valid = 1;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 status->state = runtime->status->state;
739 status->suspended_state = runtime->status->suspended_state;
740 if (status->state == SNDRV_PCM_STATE_OPEN)
741 goto _end;
742 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100743 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100745 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
746 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600747 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500748 status->audio_tstamp =
749 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600750 if (runtime->audio_tstamp_report.valid == 1)
751 /* backwards compatibility, no report provided in COMPAT mode */
752 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
753 &status->audio_tstamp_accuracy,
754 &runtime->audio_tstamp_report);
755
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100756 goto _tstamp_end;
757 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600758 } else {
759 /* get tstamp only in fallback mode and only if enabled */
760 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
761 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100762 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100763 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 status->appl_ptr = runtime->control->appl_ptr;
765 status->hw_ptr = runtime->status->hw_ptr;
766 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
767 status->avail = snd_pcm_playback_avail(runtime);
768 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200769 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200771 status->delay += runtime->delay;
772 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 status->delay = 0;
774 } else {
775 status->avail = snd_pcm_capture_avail(runtime);
776 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200777 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 else
779 status->delay = 0;
780 }
781 status->avail_max = runtime->avail_max;
782 status->overrange = runtime->overrange;
783 runtime->avail_max = 0;
784 runtime->overrange = 0;
785 _end:
786 snd_pcm_stream_unlock_irq(substream);
787 return 0;
788}
789
Takashi Iwai877211f2005-11-17 13:59:38 +0100790static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600791 struct snd_pcm_status __user * _status,
792 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Takashi Iwai877211f2005-11-17 13:59:38 +0100794 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600798 /*
799 * with extension, parameters are read/write,
800 * get audio_tstamp_data from user,
801 * ignore rest of status structure
802 */
803 if (ext && get_user(status.audio_tstamp_data,
804 (u32 __user *)(&_status->audio_tstamp_data)))
805 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 res = snd_pcm_status(substream, &status);
807 if (res < 0)
808 return res;
809 if (copy_to_user(_status, &status, sizeof(status)))
810 return -EFAULT;
811 return 0;
812}
813
Takashi Iwai877211f2005-11-17 13:59:38 +0100814static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
815 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwai877211f2005-11-17 13:59:38 +0100817 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 unsigned int channel;
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 channel = info->channel;
821 runtime = substream->runtime;
822 snd_pcm_stream_lock_irq(substream);
823 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
824 snd_pcm_stream_unlock_irq(substream);
825 return -EBADFD;
826 }
827 snd_pcm_stream_unlock_irq(substream);
828 if (channel >= runtime->channels)
829 return -EINVAL;
830 memset(info, 0, sizeof(*info));
831 info->channel = channel;
832 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
833}
834
Takashi Iwai877211f2005-11-17 13:59:38 +0100835static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
836 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Takashi Iwai877211f2005-11-17 13:59:38 +0100838 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int res;
840
841 if (copy_from_user(&info, _info, sizeof(info)))
842 return -EFAULT;
843 res = snd_pcm_channel_info(substream, &info);
844 if (res < 0)
845 return res;
846 if (copy_to_user(_info, &info, sizeof(info)))
847 return -EFAULT;
848 return 0;
849}
850
Takashi Iwai877211f2005-11-17 13:59:38 +0100851static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Takashi Iwai877211f2005-11-17 13:59:38 +0100853 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (runtime->trigger_master == NULL)
855 return;
856 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600857 if (!runtime->trigger_tstamp_latched)
858 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 } else {
860 snd_pcm_trigger_tstamp(runtime->trigger_master);
861 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
862 }
863 runtime->trigger_master = NULL;
864}
865
866struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100867 int (*pre_action)(struct snd_pcm_substream *substream, int state);
868 int (*do_action)(struct snd_pcm_substream *substream, int state);
869 void (*undo_action)(struct snd_pcm_substream *substream, int state);
870 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871};
872
873/*
874 * this functions is core for handling of linked stream
875 * Note: the stream state might be changed also on failure
876 * Note2: call with calling stream lock + link lock
877 */
878static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100879 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int state, int do_lock)
881{
Takashi Iwai877211f2005-11-17 13:59:38 +0100882 struct snd_pcm_substream *s = NULL;
883 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200884 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Takashi Iwaief991b92007-02-22 12:52:53 +0100886 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200887 if (do_lock && s != substream) {
888 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200889 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200890 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200891 spin_lock_nested(&s->self_group.lock, depth);
892 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 res = ops->pre_action(s, state);
895 if (res < 0)
896 goto _unlock;
897 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100898 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 res = ops->do_action(s, state);
900 if (res < 0) {
901 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100902 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (s1 == s) /* failed stream */
904 break;
905 ops->undo_action(s1, state);
906 }
907 }
908 s = NULL; /* unlock all */
909 goto _unlock;
910 }
911 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100912 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 ops->post_action(s, state);
914 }
915 _unlock:
916 if (do_lock) {
917 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100918 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200919 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200920 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200921 mutex_unlock(&s1->self_group.mutex);
922 else
923 spin_unlock(&s1->self_group.lock);
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (s1 == s) /* end */
926 break;
927 }
928 }
929 return res;
930}
931
932/*
933 * Note: call with stream lock
934 */
935static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100936 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int state)
938{
939 int res;
940
941 res = ops->pre_action(substream, state);
942 if (res < 0)
943 return res;
944 res = ops->do_action(substream, state);
945 if (res == 0)
946 ops->post_action(substream, state);
947 else if (ops->undo_action)
948 ops->undo_action(substream, state);
949 return res;
950}
951
952/*
953 * Note: call with stream lock
954 */
955static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100956 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int state)
958{
959 int res;
960
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100961 if (!snd_pcm_stream_linked(substream))
962 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200963
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100964 if (substream->pcm->nonatomic) {
965 if (!mutex_trylock(&substream->group->mutex)) {
966 mutex_unlock(&substream->self_group.mutex);
967 mutex_lock(&substream->group->mutex);
968 mutex_lock(&substream->self_group.mutex);
969 }
970 res = snd_pcm_action_group(ops, substream, state, 1);
971 mutex_unlock(&substream->group->mutex);
972 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!spin_trylock(&substream->group->lock)) {
974 spin_unlock(&substream->self_group.lock);
975 spin_lock(&substream->group->lock);
976 spin_lock(&substream->self_group.lock);
977 }
978 res = snd_pcm_action_group(ops, substream, state, 1);
979 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 return res;
982}
983
984/*
985 * Note: don't use any locks before
986 */
987static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100988 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int state)
990{
991 int res;
992
Takashi Iwaie3a4bd52014-10-31 14:45:04 +0100993 snd_pcm_stream_lock_irq(substream);
994 res = snd_pcm_action(ops, substream, state);
995 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return res;
997}
998
999/*
1000 */
1001static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001002 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int state)
1004{
1005 int res;
1006
1007 down_read(&snd_pcm_link_rwsem);
1008 if (snd_pcm_stream_linked(substream))
1009 res = snd_pcm_action_group(ops, substream, state, 0);
1010 else
1011 res = snd_pcm_action_single(ops, substream, state);
1012 up_read(&snd_pcm_link_rwsem);
1013 return res;
1014}
1015
1016/*
1017 * start callbacks
1018 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001019static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Takashi Iwai877211f2005-11-17 13:59:38 +01001021 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1023 return -EBADFD;
1024 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1025 !snd_pcm_playback_data(substream))
1026 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001027 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 runtime->trigger_master = substream;
1029 return 0;
1030}
1031
Takashi Iwai877211f2005-11-17 13:59:38 +01001032static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 if (substream->runtime->trigger_master != substream)
1035 return 0;
1036 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1037}
1038
Takashi Iwai877211f2005-11-17 13:59:38 +01001039static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 if (substream->runtime->trigger_master == substream)
1042 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1043}
1044
Takashi Iwai877211f2005-11-17 13:59:38 +01001045static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Takashi Iwai877211f2005-11-17 13:59:38 +01001047 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001049 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001050 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1051 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 runtime->status->state = state;
1053 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1054 runtime->silence_size > 0)
1055 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001056 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059static struct action_ops snd_pcm_action_start = {
1060 .pre_action = snd_pcm_pre_start,
1061 .do_action = snd_pcm_do_start,
1062 .undo_action = snd_pcm_undo_start,
1063 .post_action = snd_pcm_post_start
1064};
1065
1066/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001067 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001068 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001069 *
1070 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001072int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Takashi Iwai877211f2005-11-17 13:59:38 +01001074 return snd_pcm_action(&snd_pcm_action_start, substream,
1075 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078/*
1079 * stop callbacks
1080 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001081static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Takashi Iwai877211f2005-11-17 13:59:38 +01001083 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1085 return -EBADFD;
1086 runtime->trigger_master = substream;
1087 return 0;
1088}
1089
Takashi Iwai877211f2005-11-17 13:59:38 +01001090static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 if (substream->runtime->trigger_master == substream &&
1093 snd_pcm_running(substream))
1094 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1095 return 0; /* unconditonally stop all substreams */
1096}
1097
Takashi Iwai877211f2005-11-17 13:59:38 +01001098static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Takashi Iwai877211f2005-11-17 13:59:38 +01001100 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (runtime->status->state != state) {
1102 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001103 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001104 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001107 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
1110static struct action_ops snd_pcm_action_stop = {
1111 .pre_action = snd_pcm_pre_stop,
1112 .do_action = snd_pcm_do_stop,
1113 .post_action = snd_pcm_post_stop
1114};
1115
1116/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001117 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001118 * @substream: the PCM substream instance
1119 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001121 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001122 *
Masanari Iida0a114582014-02-09 00:47:36 +09001123 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001125int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1128}
1129
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001130EXPORT_SYMBOL(snd_pcm_stop);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001133 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001134 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001136 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001138 *
1139 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001141int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Takashi Iwai877211f2005-11-17 13:59:38 +01001143 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1144 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Takashi Iwai1fb85102014-11-07 17:08:28 +01001147/**
1148 * snd_pcm_stop_xrun - stop the running streams as XRUN
1149 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001150 *
1151 * This stops the given running substream (and all linked substreams) as XRUN.
1152 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1153 *
1154 * Return: Zero if successful, or a negative error code.
1155 */
1156int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1157{
1158 unsigned long flags;
1159 int ret = 0;
1160
1161 snd_pcm_stream_lock_irqsave(substream, flags);
1162 if (snd_pcm_running(substream))
1163 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1164 snd_pcm_stream_unlock_irqrestore(substream, flags);
1165 return ret;
1166}
1167EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/*
1170 * pause callbacks
1171 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001172static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Takashi Iwai877211f2005-11-17 13:59:38 +01001174 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1176 return -ENOSYS;
1177 if (push) {
1178 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1179 return -EBADFD;
1180 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1181 return -EBADFD;
1182 runtime->trigger_master = substream;
1183 return 0;
1184}
1185
Takashi Iwai877211f2005-11-17 13:59:38 +01001186static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 if (substream->runtime->trigger_master != substream)
1189 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001190 /* some drivers might use hw_ptr to recover from the pause -
1191 update the hw_ptr now */
1192 if (push)
1193 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001194 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001195 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001196 * delta, effectively to skip the check once.
1197 */
1198 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return substream->ops->trigger(substream,
1200 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1201 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1202}
1203
Takashi Iwai877211f2005-11-17 13:59:38 +01001204static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 if (substream->runtime->trigger_master == substream)
1207 substream->ops->trigger(substream,
1208 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1209 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1210}
1211
Takashi Iwai877211f2005-11-17 13:59:38 +01001212static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwai877211f2005-11-17 13:59:38 +01001214 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 snd_pcm_trigger_tstamp(substream);
1216 if (push) {
1217 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001218 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001220 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 } else {
1222 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001223 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225}
1226
1227static struct action_ops snd_pcm_action_pause = {
1228 .pre_action = snd_pcm_pre_pause,
1229 .do_action = snd_pcm_do_pause,
1230 .undo_action = snd_pcm_undo_pause,
1231 .post_action = snd_pcm_post_pause
1232};
1233
1234/*
1235 * Push/release the pause for all linked streams.
1236 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001237static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1240}
1241
1242#ifdef CONFIG_PM
1243/* suspend */
1244
Takashi Iwai877211f2005-11-17 13:59:38 +01001245static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Takashi Iwai877211f2005-11-17 13:59:38 +01001247 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1249 return -EBUSY;
1250 runtime->trigger_master = substream;
1251 return 0;
1252}
1253
Takashi Iwai877211f2005-11-17 13:59:38 +01001254static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Takashi Iwai877211f2005-11-17 13:59:38 +01001256 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (runtime->trigger_master != substream)
1258 return 0;
1259 if (! snd_pcm_running(substream))
1260 return 0;
1261 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1262 return 0; /* suspend unconditionally */
1263}
1264
Takashi Iwai877211f2005-11-17 13:59:38 +01001265static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Takashi Iwai877211f2005-11-17 13:59:38 +01001267 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001269 runtime->status->suspended_state = runtime->status->state;
1270 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001271 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001273 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276static struct action_ops snd_pcm_action_suspend = {
1277 .pre_action = snd_pcm_pre_suspend,
1278 .do_action = snd_pcm_do_suspend,
1279 .post_action = snd_pcm_post_suspend
1280};
1281
1282/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001283 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001284 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001287 *
1288 * Return: Zero if successful (or @substream is %NULL), or a negative error
1289 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001291int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 int err;
1294 unsigned long flags;
1295
Takashi Iwai603bf522005-11-17 15:59:14 +01001296 if (! substream)
1297 return 0;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 snd_pcm_stream_lock_irqsave(substream, flags);
1300 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1301 snd_pcm_stream_unlock_irqrestore(substream, flags);
1302 return err;
1303}
1304
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001305EXPORT_SYMBOL(snd_pcm_suspend);
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001308 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001309 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001312 *
1313 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001315int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Takashi Iwai877211f2005-11-17 13:59:38 +01001317 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int stream, err = 0;
1319
Takashi Iwai603bf522005-11-17 15:59:14 +01001320 if (! pcm)
1321 return 0;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001324 for (substream = pcm->streams[stream].substream;
1325 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* FIXME: the open/close code should lock this as well */
1327 if (substream->runtime == NULL)
1328 continue;
1329 err = snd_pcm_suspend(substream);
1330 if (err < 0 && err != -EBUSY)
1331 return err;
1332 }
1333 }
1334 return 0;
1335}
1336
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001337EXPORT_SYMBOL(snd_pcm_suspend_all);
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/* resume */
1340
Takashi Iwai877211f2005-11-17 13:59:38 +01001341static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Takashi Iwai877211f2005-11-17 13:59:38 +01001343 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1345 return -ENOSYS;
1346 runtime->trigger_master = substream;
1347 return 0;
1348}
1349
Takashi Iwai877211f2005-11-17 13:59:38 +01001350static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Takashi Iwai877211f2005-11-17 13:59:38 +01001352 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (runtime->trigger_master != substream)
1354 return 0;
1355 /* DMA not running previously? */
1356 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1357 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1358 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1359 return 0;
1360 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1361}
1362
Takashi Iwai877211f2005-11-17 13:59:38 +01001363static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 if (substream->runtime->trigger_master == substream &&
1366 snd_pcm_running(substream))
1367 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1368}
1369
Takashi Iwai877211f2005-11-17 13:59:38 +01001370static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Takashi Iwai877211f2005-11-17 13:59:38 +01001372 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001374 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001375 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
1378static struct action_ops snd_pcm_action_resume = {
1379 .pre_action = snd_pcm_pre_resume,
1380 .do_action = snd_pcm_do_resume,
1381 .undo_action = snd_pcm_undo_resume,
1382 .post_action = snd_pcm_post_resume
1383};
1384
Takashi Iwai877211f2005-11-17 13:59:38 +01001385static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Takashi Iwai877211f2005-11-17 13:59:38 +01001387 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int res;
1389
1390 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001391 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1393 snd_power_unlock(card);
1394 return res;
1395}
1396
1397#else
1398
Takashi Iwai877211f2005-11-17 13:59:38 +01001399static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 return -ENOSYS;
1402}
1403
1404#endif /* CONFIG_PM */
1405
1406/*
1407 * xrun ioctl
1408 *
1409 * Change the RUNNING stream(s) to XRUN state.
1410 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001411static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Takashi Iwai877211f2005-11-17 13:59:38 +01001413 struct snd_card *card = substream->pcm->card;
1414 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 int result;
1416
1417 snd_power_lock(card);
1418 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001419 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (result < 0)
1421 goto _unlock;
1422 }
1423
1424 snd_pcm_stream_lock_irq(substream);
1425 switch (runtime->status->state) {
1426 case SNDRV_PCM_STATE_XRUN:
1427 result = 0; /* already there */
1428 break;
1429 case SNDRV_PCM_STATE_RUNNING:
1430 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1431 break;
1432 default:
1433 result = -EBADFD;
1434 }
1435 snd_pcm_stream_unlock_irq(substream);
1436 _unlock:
1437 snd_power_unlock(card);
1438 return result;
1439}
1440
1441/*
1442 * reset ioctl
1443 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001444static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Takashi Iwai877211f2005-11-17 13:59:38 +01001446 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 switch (runtime->status->state) {
1448 case SNDRV_PCM_STATE_RUNNING:
1449 case SNDRV_PCM_STATE_PREPARED:
1450 case SNDRV_PCM_STATE_PAUSED:
1451 case SNDRV_PCM_STATE_SUSPENDED:
1452 return 0;
1453 default:
1454 return -EBADFD;
1455 }
1456}
1457
Takashi Iwai877211f2005-11-17 13:59:38 +01001458static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Takashi Iwai877211f2005-11-17 13:59:38 +01001460 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1462 if (err < 0)
1463 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001465 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1466 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 runtime->silence_start = runtime->status->hw_ptr;
1468 runtime->silence_filled = 0;
1469 return 0;
1470}
1471
Takashi Iwai877211f2005-11-17 13:59:38 +01001472static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Takashi Iwai877211f2005-11-17 13:59:38 +01001474 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 runtime->control->appl_ptr = runtime->status->hw_ptr;
1476 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1477 runtime->silence_size > 0)
1478 snd_pcm_playback_silence(substream, ULONG_MAX);
1479}
1480
1481static struct action_ops snd_pcm_action_reset = {
1482 .pre_action = snd_pcm_pre_reset,
1483 .do_action = snd_pcm_do_reset,
1484 .post_action = snd_pcm_post_reset
1485};
1486
Takashi Iwai877211f2005-11-17 13:59:38 +01001487static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1490}
1491
1492/*
1493 * prepare ioctl
1494 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001495/* we use the second argument for updating f_flags */
1496static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1497 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Takashi Iwai877211f2005-11-17 13:59:38 +01001499 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001500 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1501 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return -EBADFD;
1503 if (snd_pcm_running(substream))
1504 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001505 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 0;
1507}
1508
Takashi Iwai877211f2005-11-17 13:59:38 +01001509static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 int err;
1512 err = substream->ops->prepare(substream);
1513 if (err < 0)
1514 return err;
1515 return snd_pcm_do_reset(substream, 0);
1516}
1517
Takashi Iwai877211f2005-11-17 13:59:38 +01001518static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Takashi Iwai877211f2005-11-17 13:59:38 +01001520 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001522 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525static struct action_ops snd_pcm_action_prepare = {
1526 .pre_action = snd_pcm_pre_prepare,
1527 .do_action = snd_pcm_do_prepare,
1528 .post_action = snd_pcm_post_prepare
1529};
1530
1531/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001532 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001533 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001534 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001535 *
1536 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001538static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1539 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001542 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001543 int f_flags;
1544
1545 if (file)
1546 f_flags = file->f_flags;
1547 else
1548 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001551 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001552 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1553 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 snd_power_unlock(card);
1555 return res;
1556}
1557
1558/*
1559 * drain ioctl
1560 */
1561
Takashi Iwai877211f2005-11-17 13:59:38 +01001562static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001564 struct snd_pcm_runtime *runtime = substream->runtime;
1565 switch (runtime->status->state) {
1566 case SNDRV_PCM_STATE_OPEN:
1567 case SNDRV_PCM_STATE_DISCONNECTED:
1568 case SNDRV_PCM_STATE_SUSPENDED:
1569 return -EBADFD;
1570 }
1571 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return 0;
1573}
1574
Takashi Iwai877211f2005-11-17 13:59:38 +01001575static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Takashi Iwai877211f2005-11-17 13:59:38 +01001577 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1579 switch (runtime->status->state) {
1580 case SNDRV_PCM_STATE_PREPARED:
1581 /* start playback stream if possible */
1582 if (! snd_pcm_playback_empty(substream)) {
1583 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1584 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001585 } else {
1586 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588 break;
1589 case SNDRV_PCM_STATE_RUNNING:
1590 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1591 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001592 case SNDRV_PCM_STATE_XRUN:
1593 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 default:
1596 break;
1597 }
1598 } else {
1599 /* stop running stream */
1600 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001601 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001603 snd_pcm_do_stop(substream, new_state);
1604 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606 }
Libin Yang48d88292014-12-31 22:09:54 +08001607
1608 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1609 runtime->trigger_master == substream &&
1610 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1611 return substream->ops->trigger(substream,
1612 SNDRV_PCM_TRIGGER_DRAIN);
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return 0;
1615}
1616
Takashi Iwai877211f2005-11-17 13:59:38 +01001617static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619}
1620
1621static struct action_ops snd_pcm_action_drain_init = {
1622 .pre_action = snd_pcm_pre_drain_init,
1623 .do_action = snd_pcm_do_drain_init,
1624 .post_action = snd_pcm_post_drain_init
1625};
1626
Takashi Iwai877211f2005-11-17 13:59:38 +01001627static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629/*
1630 * Drain the stream(s).
1631 * When the substream is linked, sync until the draining of all playback streams
1632 * is finished.
1633 * After this call, all streams are supposed to be either SETUP or DRAINING
1634 * (capture only) state.
1635 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001636static int snd_pcm_drain(struct snd_pcm_substream *substream,
1637 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Takashi Iwai877211f2005-11-17 13:59:38 +01001639 struct snd_card *card;
1640 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001641 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001642 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001644 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 card = substream->pcm->card;
1647 runtime = substream->runtime;
1648
1649 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1650 return -EBADFD;
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 snd_power_lock(card);
1653 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001654 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001655 if (result < 0) {
1656 snd_power_unlock(card);
1657 return result;
1658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001661 if (file) {
1662 if (file->f_flags & O_NONBLOCK)
1663 nonblock = 1;
1664 } else if (substream->f_flags & O_NONBLOCK)
1665 nonblock = 1;
1666
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001667 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001668 snd_pcm_stream_lock_irq(substream);
1669 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001670 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001671 snd_pcm_pause(substream, 0);
1672
1673 /* pre-start/stop - all running streams are changed to DRAINING state */
1674 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001675 if (result < 0)
1676 goto unlock;
1677 /* in non-blocking, we don't wait in ioctl but let caller poll */
1678 if (nonblock) {
1679 result = -EAGAIN;
1680 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 for (;;) {
1684 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001685 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (signal_pending(current)) {
1687 result = -ERESTARTSYS;
1688 break;
1689 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001690 /* find a substream to drain */
1691 to_check = NULL;
1692 snd_pcm_group_for_each_entry(s, substream) {
1693 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1694 continue;
1695 runtime = s->runtime;
1696 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1697 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001698 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001699 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001700 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001701 if (!to_check)
1702 break; /* all drained */
1703 init_waitqueue_entry(&wait, current);
1704 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001706 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001708 if (runtime->no_period_wakeup)
1709 tout = MAX_SCHEDULE_TIMEOUT;
1710 else {
1711 tout = 10;
1712 if (runtime->rate) {
1713 long t = runtime->period_size * 2 / runtime->rate;
1714 tout = max(t, tout);
1715 }
1716 tout = msecs_to_jiffies(tout * 1000);
1717 }
1718 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001720 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001722 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001723 if (card->shutdown) {
1724 result = -ENODEV;
1725 break;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (tout == 0) {
1728 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1729 result = -ESTRPIPE;
1730 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001731 dev_dbg(substream->pcm->card->dev,
1732 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1734 result = -EIO;
1735 }
1736 break;
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001739
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001740 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001741 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001742 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 return result;
1746}
1747
1748/*
1749 * drop ioctl
1750 *
1751 * Immediately put all linked substreams into SETUP state.
1752 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001753static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Takashi Iwai877211f2005-11-17 13:59:38 +01001755 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int result = 0;
1757
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001758 if (PCM_RUNTIME_CHECK(substream))
1759 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001762 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001763 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1764 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return -EBADFD;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 snd_pcm_stream_lock_irq(substream);
1768 /* resume pause */
1769 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1770 snd_pcm_pause(substream, 0);
1771
1772 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1773 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1774 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return result;
1777}
1778
1779
Al Viro0888c322013-06-05 14:09:55 -04001780static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Al Viro0888c322013-06-05 14:09:55 -04001782 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001783 unsigned int minor;
1784
Al Viro0888c322013-06-05 14:09:55 -04001785 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1786 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001788 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1789 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
1792/*
1793 * PCM link handling
1794 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001795static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001798 struct snd_pcm_file *pcm_file;
1799 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001800 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001801 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Al Viro0888c322013-06-05 14:09:55 -04001803 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001805 if (!is_pcm_file(f.file)) {
1806 res = -EBADFD;
1807 goto _badf;
1808 }
1809 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001811 group = kmalloc(sizeof(*group), GFP_KERNEL);
1812 if (!group) {
1813 res = -ENOMEM;
1814 goto _nolock;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 down_write(&snd_pcm_link_rwsem);
1817 write_lock_irq(&snd_pcm_link_rwlock);
1818 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001819 substream->runtime->status->state != substream1->runtime->status->state ||
1820 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 res = -EBADFD;
1822 goto _end;
1823 }
1824 if (snd_pcm_stream_linked(substream1)) {
1825 res = -EALREADY;
1826 goto _end;
1827 }
1828 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001829 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001830 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001832 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 INIT_LIST_HEAD(&substream->group->substreams);
1834 list_add_tail(&substream->link_list, &substream->group->substreams);
1835 substream->group->count = 1;
1836 }
1837 list_add_tail(&substream1->link_list, &substream->group->substreams);
1838 substream->group->count++;
1839 substream1->group = substream->group;
1840 _end:
1841 write_unlock_irq(&snd_pcm_link_rwlock);
1842 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001843 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001844 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001845 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001846 _badf:
1847 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return res;
1849}
1850
Takashi Iwai877211f2005-11-17 13:59:38 +01001851static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 substream->group = &substream->self_group;
1854 INIT_LIST_HEAD(&substream->self_group.substreams);
1855 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1856}
1857
Takashi Iwai877211f2005-11-17 13:59:38 +01001858static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Takashi Iwaief991b92007-02-22 12:52:53 +01001860 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 int res = 0;
1862
1863 down_write(&snd_pcm_link_rwsem);
1864 write_lock_irq(&snd_pcm_link_rwlock);
1865 if (!snd_pcm_stream_linked(substream)) {
1866 res = -EALREADY;
1867 goto _end;
1868 }
1869 list_del(&substream->link_list);
1870 substream->group->count--;
1871 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001872 snd_pcm_group_for_each_entry(s, substream) {
1873 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 break;
1875 }
1876 kfree(substream->group);
1877 }
1878 relink_to_local(substream);
1879 _end:
1880 write_unlock_irq(&snd_pcm_link_rwlock);
1881 up_write(&snd_pcm_link_rwsem);
1882 return res;
1883}
1884
1885/*
1886 * hw configurator
1887 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001888static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1889 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Takashi Iwai877211f2005-11-17 13:59:38 +01001891 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1893 hw_param_interval_c(params, rule->deps[1]), &t);
1894 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1895}
1896
Takashi Iwai877211f2005-11-17 13:59:38 +01001897static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1898 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Takashi Iwai877211f2005-11-17 13:59:38 +01001900 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1902 hw_param_interval_c(params, rule->deps[1]), &t);
1903 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1904}
1905
Takashi Iwai877211f2005-11-17 13:59:38 +01001906static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1907 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Takashi Iwai877211f2005-11-17 13:59:38 +01001909 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1911 hw_param_interval_c(params, rule->deps[1]),
1912 (unsigned long) rule->private, &t);
1913 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1914}
1915
Takashi Iwai877211f2005-11-17 13:59:38 +01001916static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1917 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Takashi Iwai877211f2005-11-17 13:59:38 +01001919 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1921 (unsigned long) rule->private,
1922 hw_param_interval_c(params, rule->deps[1]), &t);
1923 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1924}
1925
Takashi Iwai877211f2005-11-17 13:59:38 +01001926static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1927 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001930 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1931 struct snd_mask m;
1932 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 snd_mask_any(&m);
1934 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1935 int bits;
1936 if (! snd_mask_test(mask, k))
1937 continue;
1938 bits = snd_pcm_format_physical_width(k);
1939 if (bits <= 0)
1940 continue; /* ignore invalid formats */
1941 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1942 snd_mask_reset(&m, k);
1943 }
1944 return snd_mask_refine(mask, &m);
1945}
1946
Takashi Iwai877211f2005-11-17 13:59:38 +01001947static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1948 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Takashi Iwai877211f2005-11-17 13:59:38 +01001950 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 unsigned int k;
1952 t.min = UINT_MAX;
1953 t.max = 0;
1954 t.openmin = 0;
1955 t.openmax = 0;
1956 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1957 int bits;
1958 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1959 continue;
1960 bits = snd_pcm_format_physical_width(k);
1961 if (bits <= 0)
1962 continue; /* ignore invalid formats */
1963 if (t.min > (unsigned)bits)
1964 t.min = bits;
1965 if (t.max < (unsigned)bits)
1966 t.max = bits;
1967 }
1968 t.integer = 1;
1969 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1970}
1971
1972#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1973#error "Change this table"
1974#endif
1975
1976static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1977 48000, 64000, 88200, 96000, 176400, 192000 };
1978
Clemens Ladisch7653d552007-08-13 17:38:54 +02001979const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1980 .count = ARRAY_SIZE(rates),
1981 .list = rates,
1982};
1983
Takashi Iwai877211f2005-11-17 13:59:38 +01001984static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1985 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Takashi Iwai877211f2005-11-17 13:59:38 +01001987 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001989 snd_pcm_known_rates.count,
1990 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Takashi Iwai877211f2005-11-17 13:59:38 +01001993static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1994 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
Takashi Iwai877211f2005-11-17 13:59:38 +01001996 struct snd_interval t;
1997 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 t.min = 0;
1999 t.max = substream->buffer_bytes_max;
2000 t.openmin = 0;
2001 t.openmax = 0;
2002 t.integer = 1;
2003 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2004}
2005
Takashi Iwai877211f2005-11-17 13:59:38 +01002006int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
Takashi Iwai877211f2005-11-17 13:59:38 +01002008 struct snd_pcm_runtime *runtime = substream->runtime;
2009 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 int k, err;
2011
2012 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2013 snd_mask_any(constrs_mask(constrs, k));
2014 }
2015
2016 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2017 snd_interval_any(constrs_interval(constrs, k));
2018 }
2019
2020 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2021 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2022 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2023 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2024 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2025
2026 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2027 snd_pcm_hw_rule_format, NULL,
2028 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2029 if (err < 0)
2030 return err;
2031 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2032 snd_pcm_hw_rule_sample_bits, NULL,
2033 SNDRV_PCM_HW_PARAM_FORMAT,
2034 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2035 if (err < 0)
2036 return err;
2037 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2038 snd_pcm_hw_rule_div, NULL,
2039 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2040 if (err < 0)
2041 return err;
2042 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2043 snd_pcm_hw_rule_mul, NULL,
2044 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2045 if (err < 0)
2046 return err;
2047 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2048 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2049 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2050 if (err < 0)
2051 return err;
2052 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2053 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2054 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2055 if (err < 0)
2056 return err;
2057 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2058 snd_pcm_hw_rule_div, NULL,
2059 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2060 if (err < 0)
2061 return err;
2062 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2063 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2064 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2065 if (err < 0)
2066 return err;
2067 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2068 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2069 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2070 if (err < 0)
2071 return err;
2072 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2073 snd_pcm_hw_rule_div, NULL,
2074 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2075 if (err < 0)
2076 return err;
2077 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2078 snd_pcm_hw_rule_div, NULL,
2079 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2080 if (err < 0)
2081 return err;
2082 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2083 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2084 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2085 if (err < 0)
2086 return err;
2087 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2088 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2089 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2090 if (err < 0)
2091 return err;
2092 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2093 snd_pcm_hw_rule_mul, NULL,
2094 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2095 if (err < 0)
2096 return err;
2097 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2098 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2099 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2100 if (err < 0)
2101 return err;
2102 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2103 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2104 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2105 if (err < 0)
2106 return err;
2107 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2108 snd_pcm_hw_rule_muldivk, (void*) 8,
2109 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2110 if (err < 0)
2111 return err;
2112 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2113 snd_pcm_hw_rule_muldivk, (void*) 8,
2114 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2115 if (err < 0)
2116 return err;
2117 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2118 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2119 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2120 if (err < 0)
2121 return err;
2122 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2123 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2124 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2125 if (err < 0)
2126 return err;
2127 return 0;
2128}
2129
Takashi Iwai877211f2005-11-17 13:59:38 +01002130int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Takashi Iwai877211f2005-11-17 13:59:38 +01002132 struct snd_pcm_runtime *runtime = substream->runtime;
2133 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 int err;
2135 unsigned int mask = 0;
2136
2137 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2138 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2139 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2140 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002141 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2143 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2144 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2145 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2146 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2147 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2148 }
2149 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002150 if (err < 0)
2151 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002154 if (err < 0)
2155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002158 if (err < 0)
2159 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2162 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002163 if (err < 0)
2164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2167 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002168 if (err < 0)
2169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2172 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002173 if (err < 0)
2174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2177 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002178 if (err < 0)
2179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2182 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002183 if (err < 0)
2184 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2187 snd_pcm_hw_rule_buffer_bytes_max, substream,
2188 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2189 if (err < 0)
2190 return err;
2191
2192 /* FIXME: remove */
2193 if (runtime->dma_bytes) {
2194 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002195 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302196 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198
2199 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2200 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2201 snd_pcm_hw_rule_rate, hw,
2202 SNDRV_PCM_HW_PARAM_RATE, -1);
2203 if (err < 0)
2204 return err;
2205 }
2206
2207 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2209
2210 return 0;
2211}
2212
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002213static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002216}
2217
2218void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2219{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002220 substream->ref_count--;
2221 if (substream->ref_count > 0)
2222 return;
2223
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002224 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002225 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002226 if (substream->ops->hw_free &&
2227 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 substream->ops->hw_free(substream);
2229 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002230 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002232 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2233 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002234 if (substream->pcm_release) {
2235 substream->pcm_release(substream);
2236 substream->pcm_release = NULL;
2237 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002238 snd_pcm_detach_substream(substream);
2239}
2240
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002241EXPORT_SYMBOL(snd_pcm_release_substream);
2242
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002243int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2244 struct file *file,
2245 struct snd_pcm_substream **rsubstream)
2246{
2247 struct snd_pcm_substream *substream;
2248 int err;
2249
2250 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2251 if (err < 0)
2252 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002253 if (substream->ref_count > 1) {
2254 *rsubstream = substream;
2255 return 0;
2256 }
2257
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002258 err = snd_pcm_hw_constraints_init(substream);
2259 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002260 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002261 goto error;
2262 }
2263
2264 if ((err = substream->ops->open(substream)) < 0)
2265 goto error;
2266
2267 substream->hw_opened = 1;
2268
2269 err = snd_pcm_hw_constraints_complete(substream);
2270 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002271 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002272 goto error;
2273 }
2274
2275 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002277
2278 error:
2279 snd_pcm_release_substream(substream);
2280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002283EXPORT_SYMBOL(snd_pcm_open_substream);
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002286 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002287 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Takashi Iwai877211f2005-11-17 13:59:38 +01002289 struct snd_pcm_file *pcm_file;
2290 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002291 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002293 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2294 if (err < 0)
2295 return err;
2296
Takashi Iwai548a6482006-07-31 16:51:51 +02002297 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2298 if (pcm_file == NULL) {
2299 snd_pcm_release_substream(substream);
2300 return -ENOMEM;
2301 }
2302 pcm_file->substream = substream;
2303 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002304 substream->file = pcm_file;
2305 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
2310}
2311
Clemens Ladischf87135f2005-11-20 14:06:59 +01002312static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Takashi Iwai877211f2005-11-17 13:59:38 +01002314 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002315 int err = nonseekable_open(inode, file);
2316 if (err < 0)
2317 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002318 pcm = snd_lookup_minor_data(iminor(inode),
2319 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002320 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002321 if (pcm)
2322 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002323 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002324}
2325
2326static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2327{
2328 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002329 int err = nonseekable_open(inode, file);
2330 if (err < 0)
2331 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002332 pcm = snd_lookup_minor_data(iminor(inode),
2333 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002334 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002335 if (pcm)
2336 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002337 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002338}
2339
2340static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2341{
2342 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 wait_queue_t wait;
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (pcm == NULL) {
2346 err = -ENODEV;
2347 goto __error1;
2348 }
2349 err = snd_card_file_add(pcm->card, file);
2350 if (err < 0)
2351 goto __error1;
2352 if (!try_module_get(pcm->card->module)) {
2353 err = -EFAULT;
2354 goto __error2;
2355 }
2356 init_waitqueue_entry(&wait, current);
2357 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002358 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002360 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (err >= 0)
2362 break;
2363 if (err == -EAGAIN) {
2364 if (file->f_flags & O_NONBLOCK) {
2365 err = -EBUSY;
2366 break;
2367 }
2368 } else
2369 break;
2370 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002371 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002373 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002374 if (pcm->card->shutdown) {
2375 err = -ENODEV;
2376 break;
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (signal_pending(current)) {
2379 err = -ERESTARTSYS;
2380 break;
2381 }
2382 }
2383 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002384 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (err < 0)
2386 goto __error;
2387 return err;
2388
2389 __error:
2390 module_put(pcm->card->module);
2391 __error2:
2392 snd_card_file_remove(pcm->card, file);
2393 __error1:
2394 return err;
2395}
2396
2397static int snd_pcm_release(struct inode *inode, struct file *file)
2398{
Takashi Iwai877211f2005-11-17 13:59:38 +01002399 struct snd_pcm *pcm;
2400 struct snd_pcm_substream *substream;
2401 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 pcm_file = file->private_data;
2404 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002405 if (snd_BUG_ON(!substream))
2406 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002408 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002409 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002410 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002411 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 wake_up(&pcm->open_wait);
2413 module_put(pcm->card->module);
2414 snd_card_file_remove(pcm->card, file);
2415 return 0;
2416}
2417
Takashi Iwai877211f2005-11-17 13:59:38 +01002418static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2419 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Takashi Iwai877211f2005-11-17 13:59:38 +01002421 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 snd_pcm_sframes_t appl_ptr;
2423 snd_pcm_sframes_t ret;
2424 snd_pcm_sframes_t hw_avail;
2425
2426 if (frames == 0)
2427 return 0;
2428
2429 snd_pcm_stream_lock_irq(substream);
2430 switch (runtime->status->state) {
2431 case SNDRV_PCM_STATE_PREPARED:
2432 break;
2433 case SNDRV_PCM_STATE_DRAINING:
2434 case SNDRV_PCM_STATE_RUNNING:
2435 if (snd_pcm_update_hw_ptr(substream) >= 0)
2436 break;
2437 /* Fall through */
2438 case SNDRV_PCM_STATE_XRUN:
2439 ret = -EPIPE;
2440 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002441 case SNDRV_PCM_STATE_SUSPENDED:
2442 ret = -ESTRPIPE;
2443 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 default:
2445 ret = -EBADFD;
2446 goto __end;
2447 }
2448
2449 hw_avail = snd_pcm_playback_hw_avail(runtime);
2450 if (hw_avail <= 0) {
2451 ret = 0;
2452 goto __end;
2453 }
2454 if (frames > (snd_pcm_uframes_t)hw_avail)
2455 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 appl_ptr = runtime->control->appl_ptr - frames;
2457 if (appl_ptr < 0)
2458 appl_ptr += runtime->boundary;
2459 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 ret = frames;
2461 __end:
2462 snd_pcm_stream_unlock_irq(substream);
2463 return ret;
2464}
2465
Takashi Iwai877211f2005-11-17 13:59:38 +01002466static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2467 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Takashi Iwai877211f2005-11-17 13:59:38 +01002469 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 snd_pcm_sframes_t appl_ptr;
2471 snd_pcm_sframes_t ret;
2472 snd_pcm_sframes_t hw_avail;
2473
2474 if (frames == 0)
2475 return 0;
2476
2477 snd_pcm_stream_lock_irq(substream);
2478 switch (runtime->status->state) {
2479 case SNDRV_PCM_STATE_PREPARED:
2480 case SNDRV_PCM_STATE_DRAINING:
2481 break;
2482 case SNDRV_PCM_STATE_RUNNING:
2483 if (snd_pcm_update_hw_ptr(substream) >= 0)
2484 break;
2485 /* Fall through */
2486 case SNDRV_PCM_STATE_XRUN:
2487 ret = -EPIPE;
2488 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002489 case SNDRV_PCM_STATE_SUSPENDED:
2490 ret = -ESTRPIPE;
2491 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 default:
2493 ret = -EBADFD;
2494 goto __end;
2495 }
2496
2497 hw_avail = snd_pcm_capture_hw_avail(runtime);
2498 if (hw_avail <= 0) {
2499 ret = 0;
2500 goto __end;
2501 }
2502 if (frames > (snd_pcm_uframes_t)hw_avail)
2503 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 appl_ptr = runtime->control->appl_ptr - frames;
2505 if (appl_ptr < 0)
2506 appl_ptr += runtime->boundary;
2507 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 ret = frames;
2509 __end:
2510 snd_pcm_stream_unlock_irq(substream);
2511 return ret;
2512}
2513
Takashi Iwai877211f2005-11-17 13:59:38 +01002514static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2515 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
Takashi Iwai877211f2005-11-17 13:59:38 +01002517 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 snd_pcm_sframes_t appl_ptr;
2519 snd_pcm_sframes_t ret;
2520 snd_pcm_sframes_t avail;
2521
2522 if (frames == 0)
2523 return 0;
2524
2525 snd_pcm_stream_lock_irq(substream);
2526 switch (runtime->status->state) {
2527 case SNDRV_PCM_STATE_PREPARED:
2528 case SNDRV_PCM_STATE_PAUSED:
2529 break;
2530 case SNDRV_PCM_STATE_DRAINING:
2531 case SNDRV_PCM_STATE_RUNNING:
2532 if (snd_pcm_update_hw_ptr(substream) >= 0)
2533 break;
2534 /* Fall through */
2535 case SNDRV_PCM_STATE_XRUN:
2536 ret = -EPIPE;
2537 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002538 case SNDRV_PCM_STATE_SUSPENDED:
2539 ret = -ESTRPIPE;
2540 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 default:
2542 ret = -EBADFD;
2543 goto __end;
2544 }
2545
2546 avail = snd_pcm_playback_avail(runtime);
2547 if (avail <= 0) {
2548 ret = 0;
2549 goto __end;
2550 }
2551 if (frames > (snd_pcm_uframes_t)avail)
2552 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 appl_ptr = runtime->control->appl_ptr + frames;
2554 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2555 appl_ptr -= runtime->boundary;
2556 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 ret = frames;
2558 __end:
2559 snd_pcm_stream_unlock_irq(substream);
2560 return ret;
2561}
2562
Takashi Iwai877211f2005-11-17 13:59:38 +01002563static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2564 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Takashi Iwai877211f2005-11-17 13:59:38 +01002566 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 snd_pcm_sframes_t appl_ptr;
2568 snd_pcm_sframes_t ret;
2569 snd_pcm_sframes_t avail;
2570
2571 if (frames == 0)
2572 return 0;
2573
2574 snd_pcm_stream_lock_irq(substream);
2575 switch (runtime->status->state) {
2576 case SNDRV_PCM_STATE_PREPARED:
2577 case SNDRV_PCM_STATE_DRAINING:
2578 case SNDRV_PCM_STATE_PAUSED:
2579 break;
2580 case SNDRV_PCM_STATE_RUNNING:
2581 if (snd_pcm_update_hw_ptr(substream) >= 0)
2582 break;
2583 /* Fall through */
2584 case SNDRV_PCM_STATE_XRUN:
2585 ret = -EPIPE;
2586 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002587 case SNDRV_PCM_STATE_SUSPENDED:
2588 ret = -ESTRPIPE;
2589 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 default:
2591 ret = -EBADFD;
2592 goto __end;
2593 }
2594
2595 avail = snd_pcm_capture_avail(runtime);
2596 if (avail <= 0) {
2597 ret = 0;
2598 goto __end;
2599 }
2600 if (frames > (snd_pcm_uframes_t)avail)
2601 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 appl_ptr = runtime->control->appl_ptr + frames;
2603 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2604 appl_ptr -= runtime->boundary;
2605 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 ret = frames;
2607 __end:
2608 snd_pcm_stream_unlock_irq(substream);
2609 return ret;
2610}
2611
Takashi Iwai877211f2005-11-17 13:59:38 +01002612static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
Takashi Iwai877211f2005-11-17 13:59:38 +01002614 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 int err;
2616
2617 snd_pcm_stream_lock_irq(substream);
2618 switch (runtime->status->state) {
2619 case SNDRV_PCM_STATE_DRAINING:
2620 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2621 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002622 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 case SNDRV_PCM_STATE_RUNNING:
2624 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2625 break;
2626 /* Fall through */
2627 case SNDRV_PCM_STATE_PREPARED:
2628 case SNDRV_PCM_STATE_SUSPENDED:
2629 err = 0;
2630 break;
2631 case SNDRV_PCM_STATE_XRUN:
2632 err = -EPIPE;
2633 break;
2634 default:
2635 __badfd:
2636 err = -EBADFD;
2637 break;
2638 }
2639 snd_pcm_stream_unlock_irq(substream);
2640 return err;
2641}
2642
Takashi Iwai877211f2005-11-17 13:59:38 +01002643static int snd_pcm_delay(struct snd_pcm_substream *substream,
2644 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Takashi Iwai877211f2005-11-17 13:59:38 +01002646 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 int err;
2648 snd_pcm_sframes_t n = 0;
2649
2650 snd_pcm_stream_lock_irq(substream);
2651 switch (runtime->status->state) {
2652 case SNDRV_PCM_STATE_DRAINING:
2653 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2654 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002655 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 case SNDRV_PCM_STATE_RUNNING:
2657 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2658 break;
2659 /* Fall through */
2660 case SNDRV_PCM_STATE_PREPARED:
2661 case SNDRV_PCM_STATE_SUSPENDED:
2662 err = 0;
2663 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2664 n = snd_pcm_playback_hw_avail(runtime);
2665 else
2666 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002667 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 break;
2669 case SNDRV_PCM_STATE_XRUN:
2670 err = -EPIPE;
2671 break;
2672 default:
2673 __badfd:
2674 err = -EBADFD;
2675 break;
2676 }
2677 snd_pcm_stream_unlock_irq(substream);
2678 if (!err)
2679 if (put_user(n, res))
2680 err = -EFAULT;
2681 return err;
2682}
2683
Takashi Iwai877211f2005-11-17 13:59:38 +01002684static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2685 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Takashi Iwai877211f2005-11-17 13:59:38 +01002687 struct snd_pcm_runtime *runtime = substream->runtime;
2688 struct snd_pcm_sync_ptr sync_ptr;
2689 volatile struct snd_pcm_mmap_status *status;
2690 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 int err;
2692
2693 memset(&sync_ptr, 0, sizeof(sync_ptr));
2694 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2695 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002696 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 -07002697 return -EFAULT;
2698 status = runtime->status;
2699 control = runtime->control;
2700 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2701 err = snd_pcm_hwsync(substream);
2702 if (err < 0)
2703 return err;
2704 }
2705 snd_pcm_stream_lock_irq(substream);
2706 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2707 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2708 else
2709 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2710 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2711 control->avail_min = sync_ptr.c.control.avail_min;
2712 else
2713 sync_ptr.c.control.avail_min = control->avail_min;
2714 sync_ptr.s.status.state = status->state;
2715 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2716 sync_ptr.s.status.tstamp = status->tstamp;
2717 sync_ptr.s.status.suspended_state = status->suspended_state;
2718 snd_pcm_stream_unlock_irq(substream);
2719 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2720 return -EFAULT;
2721 return 0;
2722}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002723
2724static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2725{
2726 struct snd_pcm_runtime *runtime = substream->runtime;
2727 int arg;
2728
2729 if (get_user(arg, _arg))
2730 return -EFAULT;
2731 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2732 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002733 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002734 return 0;
2735}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Takashi Iwai0df63e42006-04-28 15:13:41 +02002737static int snd_pcm_common_ioctl1(struct file *file,
2738 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 unsigned int cmd, void __user *arg)
2740{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 switch (cmd) {
2742 case SNDRV_PCM_IOCTL_PVERSION:
2743 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2744 case SNDRV_PCM_IOCTL_INFO:
2745 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002746 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2747 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002748 case SNDRV_PCM_IOCTL_TTSTAMP:
2749 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 case SNDRV_PCM_IOCTL_HW_REFINE:
2751 return snd_pcm_hw_refine_user(substream, arg);
2752 case SNDRV_PCM_IOCTL_HW_PARAMS:
2753 return snd_pcm_hw_params_user(substream, arg);
2754 case SNDRV_PCM_IOCTL_HW_FREE:
2755 return snd_pcm_hw_free(substream);
2756 case SNDRV_PCM_IOCTL_SW_PARAMS:
2757 return snd_pcm_sw_params_user(substream, arg);
2758 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002759 return snd_pcm_status_user(substream, arg, false);
2760 case SNDRV_PCM_IOCTL_STATUS_EXT:
2761 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2763 return snd_pcm_channel_info_user(substream, arg);
2764 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002765 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 case SNDRV_PCM_IOCTL_RESET:
2767 return snd_pcm_reset(substream);
2768 case SNDRV_PCM_IOCTL_START:
2769 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2770 case SNDRV_PCM_IOCTL_LINK:
2771 return snd_pcm_link(substream, (int)(unsigned long) arg);
2772 case SNDRV_PCM_IOCTL_UNLINK:
2773 return snd_pcm_unlink(substream);
2774 case SNDRV_PCM_IOCTL_RESUME:
2775 return snd_pcm_resume(substream);
2776 case SNDRV_PCM_IOCTL_XRUN:
2777 return snd_pcm_xrun(substream);
2778 case SNDRV_PCM_IOCTL_HWSYNC:
2779 return snd_pcm_hwsync(substream);
2780 case SNDRV_PCM_IOCTL_DELAY:
2781 return snd_pcm_delay(substream, arg);
2782 case SNDRV_PCM_IOCTL_SYNC_PTR:
2783 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002784#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2786 return snd_pcm_hw_refine_old_user(substream, arg);
2787 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2788 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002789#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002791 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 case SNDRV_PCM_IOCTL_DROP:
2793 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002794 case SNDRV_PCM_IOCTL_PAUSE:
2795 {
2796 int res;
2797 snd_pcm_stream_lock_irq(substream);
2798 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2799 snd_pcm_stream_unlock_irq(substream);
2800 return res;
2801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002803 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return -ENOTTY;
2805}
2806
Takashi Iwai0df63e42006-04-28 15:13:41 +02002807static int snd_pcm_playback_ioctl1(struct file *file,
2808 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 unsigned int cmd, void __user *arg)
2810{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002811 if (snd_BUG_ON(!substream))
2812 return -ENXIO;
2813 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2814 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 switch (cmd) {
2816 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2817 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002818 struct snd_xferi xferi;
2819 struct snd_xferi __user *_xferi = arg;
2820 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 snd_pcm_sframes_t result;
2822 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2823 return -EBADFD;
2824 if (put_user(0, &_xferi->result))
2825 return -EFAULT;
2826 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2827 return -EFAULT;
2828 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2829 __put_user(result, &_xferi->result);
2830 return result < 0 ? result : 0;
2831 }
2832 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2833 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002834 struct snd_xfern xfern;
2835 struct snd_xfern __user *_xfern = arg;
2836 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 void __user **bufs;
2838 snd_pcm_sframes_t result;
2839 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2840 return -EBADFD;
2841 if (runtime->channels > 128)
2842 return -EINVAL;
2843 if (put_user(0, &_xfern->result))
2844 return -EFAULT;
2845 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2846 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002847
2848 bufs = memdup_user(xfern.bufs,
2849 sizeof(void *) * runtime->channels);
2850 if (IS_ERR(bufs))
2851 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2853 kfree(bufs);
2854 __put_user(result, &_xfern->result);
2855 return result < 0 ? result : 0;
2856 }
2857 case SNDRV_PCM_IOCTL_REWIND:
2858 {
2859 snd_pcm_uframes_t frames;
2860 snd_pcm_uframes_t __user *_frames = arg;
2861 snd_pcm_sframes_t result;
2862 if (get_user(frames, _frames))
2863 return -EFAULT;
2864 if (put_user(0, _frames))
2865 return -EFAULT;
2866 result = snd_pcm_playback_rewind(substream, frames);
2867 __put_user(result, _frames);
2868 return result < 0 ? result : 0;
2869 }
2870 case SNDRV_PCM_IOCTL_FORWARD:
2871 {
2872 snd_pcm_uframes_t frames;
2873 snd_pcm_uframes_t __user *_frames = arg;
2874 snd_pcm_sframes_t result;
2875 if (get_user(frames, _frames))
2876 return -EFAULT;
2877 if (put_user(0, _frames))
2878 return -EFAULT;
2879 result = snd_pcm_playback_forward(substream, frames);
2880 __put_user(result, _frames);
2881 return result < 0 ? result : 0;
2882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002884 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885}
2886
Takashi Iwai0df63e42006-04-28 15:13:41 +02002887static int snd_pcm_capture_ioctl1(struct file *file,
2888 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 unsigned int cmd, void __user *arg)
2890{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002891 if (snd_BUG_ON(!substream))
2892 return -ENXIO;
2893 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2894 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 switch (cmd) {
2896 case SNDRV_PCM_IOCTL_READI_FRAMES:
2897 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002898 struct snd_xferi xferi;
2899 struct snd_xferi __user *_xferi = arg;
2900 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 snd_pcm_sframes_t result;
2902 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2903 return -EBADFD;
2904 if (put_user(0, &_xferi->result))
2905 return -EFAULT;
2906 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2907 return -EFAULT;
2908 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2909 __put_user(result, &_xferi->result);
2910 return result < 0 ? result : 0;
2911 }
2912 case SNDRV_PCM_IOCTL_READN_FRAMES:
2913 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002914 struct snd_xfern xfern;
2915 struct snd_xfern __user *_xfern = arg;
2916 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 void *bufs;
2918 snd_pcm_sframes_t result;
2919 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2920 return -EBADFD;
2921 if (runtime->channels > 128)
2922 return -EINVAL;
2923 if (put_user(0, &_xfern->result))
2924 return -EFAULT;
2925 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2926 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002927
2928 bufs = memdup_user(xfern.bufs,
2929 sizeof(void *) * runtime->channels);
2930 if (IS_ERR(bufs))
2931 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2933 kfree(bufs);
2934 __put_user(result, &_xfern->result);
2935 return result < 0 ? result : 0;
2936 }
2937 case SNDRV_PCM_IOCTL_REWIND:
2938 {
2939 snd_pcm_uframes_t frames;
2940 snd_pcm_uframes_t __user *_frames = arg;
2941 snd_pcm_sframes_t result;
2942 if (get_user(frames, _frames))
2943 return -EFAULT;
2944 if (put_user(0, _frames))
2945 return -EFAULT;
2946 result = snd_pcm_capture_rewind(substream, frames);
2947 __put_user(result, _frames);
2948 return result < 0 ? result : 0;
2949 }
2950 case SNDRV_PCM_IOCTL_FORWARD:
2951 {
2952 snd_pcm_uframes_t frames;
2953 snd_pcm_uframes_t __user *_frames = arg;
2954 snd_pcm_sframes_t result;
2955 if (get_user(frames, _frames))
2956 return -EFAULT;
2957 if (put_user(0, _frames))
2958 return -EFAULT;
2959 result = snd_pcm_capture_forward(substream, frames);
2960 __put_user(result, _frames);
2961 return result < 0 ? result : 0;
2962 }
2963 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002964 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Takashi Iwai877211f2005-11-17 13:59:38 +01002967static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2968 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Takashi Iwai877211f2005-11-17 13:59:38 +01002970 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 pcm_file = file->private_data;
2973
2974 if (((cmd >> 8) & 0xff) != 'A')
2975 return -ENOTTY;
2976
Takashi Iwai0df63e42006-04-28 15:13:41 +02002977 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2978 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980
Takashi Iwai877211f2005-11-17 13:59:38 +01002981static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2982 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
Takashi Iwai877211f2005-11-17 13:59:38 +01002984 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 pcm_file = file->private_data;
2987
2988 if (((cmd >> 8) & 0xff) != 'A')
2989 return -ENOTTY;
2990
Takashi Iwai0df63e42006-04-28 15:13:41 +02002991 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2992 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
Takashi Iwai877211f2005-11-17 13:59:38 +01002995int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 unsigned int cmd, void *arg)
2997{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002998 mm_segment_t fs;
2999 int result;
3000
3001 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 switch (substream->stream) {
3003 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003004 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3005 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003006 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003008 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3009 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003010 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003012 result = -EINVAL;
3013 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003015 snd_leave_user(fs);
3016 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003019EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3020
Takashi Iwai877211f2005-11-17 13:59:38 +01003021static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3022 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
Takashi Iwai877211f2005-11-17 13:59:38 +01003024 struct snd_pcm_file *pcm_file;
3025 struct snd_pcm_substream *substream;
3026 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 snd_pcm_sframes_t result;
3028
3029 pcm_file = file->private_data;
3030 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003031 if (PCM_RUNTIME_CHECK(substream))
3032 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 runtime = substream->runtime;
3034 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3035 return -EBADFD;
3036 if (!frame_aligned(runtime, count))
3037 return -EINVAL;
3038 count = bytes_to_frames(runtime, count);
3039 result = snd_pcm_lib_read(substream, buf, count);
3040 if (result > 0)
3041 result = frames_to_bytes(runtime, result);
3042 return result;
3043}
3044
Takashi Iwai877211f2005-11-17 13:59:38 +01003045static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3046 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Takashi Iwai877211f2005-11-17 13:59:38 +01003048 struct snd_pcm_file *pcm_file;
3049 struct snd_pcm_substream *substream;
3050 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 snd_pcm_sframes_t result;
3052
3053 pcm_file = file->private_data;
3054 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003055 if (PCM_RUNTIME_CHECK(substream))
3056 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003058 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3059 return -EBADFD;
3060 if (!frame_aligned(runtime, count))
3061 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 count = bytes_to_frames(runtime, count);
3063 result = snd_pcm_lib_write(substream, buf, count);
3064 if (result > 0)
3065 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return result;
3067}
3068
Al Viro1c65d982015-04-04 00:19:32 -04003069static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Takashi Iwai877211f2005-11-17 13:59:38 +01003071 struct snd_pcm_file *pcm_file;
3072 struct snd_pcm_substream *substream;
3073 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 snd_pcm_sframes_t result;
3075 unsigned long i;
3076 void __user **bufs;
3077 snd_pcm_uframes_t frames;
3078
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003079 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003081 if (PCM_RUNTIME_CHECK(substream))
3082 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 runtime = substream->runtime;
3084 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3085 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003086 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003088 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003090 if (!frame_aligned(runtime, to->iov->iov_len))
3091 return -EINVAL;
3092 frames = bytes_to_samples(runtime, to->iov->iov_len);
3093 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (bufs == NULL)
3095 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003096 for (i = 0; i < to->nr_segs; ++i)
3097 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 result = snd_pcm_lib_readv(substream, bufs, frames);
3099 if (result > 0)
3100 result = frames_to_bytes(runtime, result);
3101 kfree(bufs);
3102 return result;
3103}
3104
Al Viro1c65d982015-04-04 00:19:32 -04003105static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Takashi Iwai877211f2005-11-17 13:59:38 +01003107 struct snd_pcm_file *pcm_file;
3108 struct snd_pcm_substream *substream;
3109 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 snd_pcm_sframes_t result;
3111 unsigned long i;
3112 void __user **bufs;
3113 snd_pcm_uframes_t frames;
3114
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003115 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003117 if (PCM_RUNTIME_CHECK(substream))
3118 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003120 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3121 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003122 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003123 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003124 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3125 !frame_aligned(runtime, from->iov->iov_len))
3126 return -EINVAL;
3127 frames = bytes_to_samples(runtime, from->iov->iov_len);
3128 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (bufs == NULL)
3130 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003131 for (i = 0; i < from->nr_segs; ++i)
3132 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 result = snd_pcm_lib_writev(substream, bufs, frames);
3134 if (result > 0)
3135 result = frames_to_bytes(runtime, result);
3136 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return result;
3138}
3139
3140static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3141{
Takashi Iwai877211f2005-11-17 13:59:38 +01003142 struct snd_pcm_file *pcm_file;
3143 struct snd_pcm_substream *substream;
3144 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 unsigned int mask;
3146 snd_pcm_uframes_t avail;
3147
3148 pcm_file = file->private_data;
3149
3150 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003151 if (PCM_RUNTIME_CHECK(substream))
3152 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 runtime = substream->runtime;
3154
3155 poll_wait(file, &runtime->sleep, wait);
3156
3157 snd_pcm_stream_lock_irq(substream);
3158 avail = snd_pcm_playback_avail(runtime);
3159 switch (runtime->status->state) {
3160 case SNDRV_PCM_STATE_RUNNING:
3161 case SNDRV_PCM_STATE_PREPARED:
3162 case SNDRV_PCM_STATE_PAUSED:
3163 if (avail >= runtime->control->avail_min) {
3164 mask = POLLOUT | POLLWRNORM;
3165 break;
3166 }
3167 /* Fall through */
3168 case SNDRV_PCM_STATE_DRAINING:
3169 mask = 0;
3170 break;
3171 default:
3172 mask = POLLOUT | POLLWRNORM | POLLERR;
3173 break;
3174 }
3175 snd_pcm_stream_unlock_irq(substream);
3176 return mask;
3177}
3178
3179static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3180{
Takashi Iwai877211f2005-11-17 13:59:38 +01003181 struct snd_pcm_file *pcm_file;
3182 struct snd_pcm_substream *substream;
3183 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 unsigned int mask;
3185 snd_pcm_uframes_t avail;
3186
3187 pcm_file = file->private_data;
3188
3189 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003190 if (PCM_RUNTIME_CHECK(substream))
3191 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 runtime = substream->runtime;
3193
3194 poll_wait(file, &runtime->sleep, wait);
3195
3196 snd_pcm_stream_lock_irq(substream);
3197 avail = snd_pcm_capture_avail(runtime);
3198 switch (runtime->status->state) {
3199 case SNDRV_PCM_STATE_RUNNING:
3200 case SNDRV_PCM_STATE_PREPARED:
3201 case SNDRV_PCM_STATE_PAUSED:
3202 if (avail >= runtime->control->avail_min) {
3203 mask = POLLIN | POLLRDNORM;
3204 break;
3205 }
3206 mask = 0;
3207 break;
3208 case SNDRV_PCM_STATE_DRAINING:
3209 if (avail > 0) {
3210 mask = POLLIN | POLLRDNORM;
3211 break;
3212 }
3213 /* Fall through */
3214 default:
3215 mask = POLLIN | POLLRDNORM | POLLERR;
3216 break;
3217 }
3218 snd_pcm_stream_unlock_irq(substream);
3219 return mask;
3220}
3221
3222/*
3223 * mmap support
3224 */
3225
3226/*
3227 * Only on coherent architectures, we can mmap the status and the control records
3228 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3229 */
3230#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3231/*
3232 * mmap status record
3233 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003234static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3235 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
Takashi Iwai877211f2005-11-17 13:59:38 +01003237 struct snd_pcm_substream *substream = area->vm_private_data;
3238 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
3240 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003241 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003243 vmf->page = virt_to_page(runtime->status);
3244 get_page(vmf->page);
3245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003248static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003250 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251};
3252
Takashi Iwai877211f2005-11-17 13:59:38 +01003253static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 struct vm_area_struct *area)
3255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 long size;
3257 if (!(area->vm_flags & VM_READ))
3258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003260 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return -EINVAL;
3262 area->vm_ops = &snd_pcm_vm_ops_status;
3263 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003264 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return 0;
3266}
3267
3268/*
3269 * mmap control record
3270 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003271static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3272 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Takashi Iwai877211f2005-11-17 13:59:38 +01003274 struct snd_pcm_substream *substream = area->vm_private_data;
3275 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003278 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003280 vmf->page = virt_to_page(runtime->control);
3281 get_page(vmf->page);
3282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003285static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003287 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288};
3289
Takashi Iwai877211f2005-11-17 13:59:38 +01003290static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 struct vm_area_struct *area)
3292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 long size;
3294 if (!(area->vm_flags & VM_READ))
3295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003297 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 return -EINVAL;
3299 area->vm_ops = &snd_pcm_vm_ops_control;
3300 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003301 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return 0;
3303}
3304#else /* ! coherent mmap */
3305/*
3306 * don't support mmap for status and control records.
3307 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003308static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 struct vm_area_struct *area)
3310{
3311 return -ENXIO;
3312}
Takashi Iwai877211f2005-11-17 13:59:38 +01003313static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 struct vm_area_struct *area)
3315{
3316 return -ENXIO;
3317}
3318#endif /* coherent mmap */
3319
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003320static inline struct page *
3321snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3322{
3323 void *vaddr = substream->runtime->dma_area + ofs;
3324 return virt_to_page(vaddr);
3325}
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003328 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003330static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3331 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
Takashi Iwai877211f2005-11-17 13:59:38 +01003333 struct snd_pcm_substream *substream = area->vm_private_data;
3334 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 unsigned long offset;
3336 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 size_t dma_bytes;
3338
3339 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003340 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003342 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3344 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003345 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003346 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003348 else
3349 page = snd_pcm_default_page_ops(substream, offset);
3350 if (!page)
3351 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003352 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003353 vmf->page = page;
3354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
Takashi Iwai657b1982009-11-26 12:40:21 +01003357static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3358 .open = snd_pcm_mmap_data_open,
3359 .close = snd_pcm_mmap_data_close,
3360};
3361
3362static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 .open = snd_pcm_mmap_data_open,
3364 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003365 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366};
3367
3368/*
3369 * mmap the DMA buffer on RAM
3370 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003371
3372/**
3373 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3374 * @substream: PCM substream
3375 * @area: VMA
3376 *
3377 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3378 * this function is invoked implicitly.
3379 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003380int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3381 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003383 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003384#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003385 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3386 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3387 return remap_pfn_range(area, area->vm_start,
3388 substream->dma_buffer.addr >> PAGE_SHIFT,
3389 area->vm_end - area->vm_start, area->vm_page_prot);
3390 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003391#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003392#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003393 if (!substream->ops->page &&
3394 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3395 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3396 area,
3397 substream->runtime->dma_area,
3398 substream->runtime->dma_addr,
3399 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003400#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003401 /* mmap with fault handler */
3402 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 return 0;
3404}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003405EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
3407/*
3408 * mmap the DMA buffer on I/O memory area
3409 */
3410#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003411/**
3412 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3413 * @substream: PCM substream
3414 * @area: VMA
3415 *
3416 * When your hardware uses the iomapped pages as the hardware buffer and
3417 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3418 * is supposed to work only on limited architectures.
3419 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003420int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3421 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003423 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003426 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003428
3429EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430#endif /* SNDRV_PCM_INFO_MMAP */
3431
3432/*
3433 * mmap DMA buffer
3434 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003435int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 struct vm_area_struct *area)
3437{
Takashi Iwai877211f2005-11-17 13:59:38 +01003438 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 long size;
3440 unsigned long offset;
3441 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003442 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
3444 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3445 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3446 return -EINVAL;
3447 } else {
3448 if (!(area->vm_flags & VM_READ))
3449 return -EINVAL;
3450 }
3451 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3453 return -EBADFD;
3454 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3455 return -ENXIO;
3456 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3457 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3458 return -EINVAL;
3459 size = area->vm_end - area->vm_start;
3460 offset = area->vm_pgoff << PAGE_SHIFT;
3461 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3462 if ((size_t)size > dma_bytes)
3463 return -EINVAL;
3464 if (offset > dma_bytes - size)
3465 return -EINVAL;
3466
Takashi Iwai657b1982009-11-26 12:40:21 +01003467 area->vm_ops = &snd_pcm_vm_ops_data;
3468 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003470 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003472 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003473 if (!err)
3474 atomic_inc(&substream->mmap_count);
3475 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003478EXPORT_SYMBOL(snd_pcm_mmap_data);
3479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3481{
Takashi Iwai877211f2005-11-17 13:59:38 +01003482 struct snd_pcm_file * pcm_file;
3483 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 unsigned long offset;
3485
3486 pcm_file = file->private_data;
3487 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003488 if (PCM_RUNTIME_CHECK(substream))
3489 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
3491 offset = area->vm_pgoff << PAGE_SHIFT;
3492 switch (offset) {
3493 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003494 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 return -ENXIO;
3496 return snd_pcm_mmap_status(substream, file, area);
3497 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003498 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return -ENXIO;
3500 return snd_pcm_mmap_control(substream, file, area);
3501 default:
3502 return snd_pcm_mmap_data(substream, file, area);
3503 }
3504 return 0;
3505}
3506
3507static int snd_pcm_fasync(int fd, struct file * file, int on)
3508{
Takashi Iwai877211f2005-11-17 13:59:38 +01003509 struct snd_pcm_file * pcm_file;
3510 struct snd_pcm_substream *substream;
3511 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 pcm_file = file->private_data;
3514 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003515 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003516 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003518 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
3521/*
3522 * ioctl32 compat
3523 */
3524#ifdef CONFIG_COMPAT
3525#include "pcm_compat.c"
3526#else
3527#define snd_pcm_ioctl_compat NULL
3528#endif
3529
3530/*
3531 * To be removed helpers to keep binary compatibility
3532 */
3533
Takashi Iwai59d48582005-12-01 10:51:58 +01003534#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3536#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3537
Takashi Iwai877211f2005-11-17 13:59:38 +01003538static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3539 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
3541 unsigned int i;
3542
3543 memset(params, 0, sizeof(*params));
3544 params->flags = oparams->flags;
3545 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3546 params->masks[i].bits[0] = oparams->masks[i];
3547 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3548 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3549 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3550 params->info = oparams->info;
3551 params->msbits = oparams->msbits;
3552 params->rate_num = oparams->rate_num;
3553 params->rate_den = oparams->rate_den;
3554 params->fifo_size = oparams->fifo_size;
3555}
3556
Takashi Iwai877211f2005-11-17 13:59:38 +01003557static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3558 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
3560 unsigned int i;
3561
3562 memset(oparams, 0, sizeof(*oparams));
3563 oparams->flags = params->flags;
3564 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3565 oparams->masks[i] = params->masks[i].bits[0];
3566 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3567 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3568 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3569 oparams->info = params->info;
3570 oparams->msbits = params->msbits;
3571 oparams->rate_num = params->rate_num;
3572 oparams->rate_den = params->rate_den;
3573 oparams->fifo_size = params->fifo_size;
3574}
3575
Takashi Iwai877211f2005-11-17 13:59:38 +01003576static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3577 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Takashi Iwai877211f2005-11-17 13:59:38 +01003579 struct snd_pcm_hw_params *params;
3580 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 int err;
3582
3583 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003584 if (!params)
3585 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Li Zefanef44a1e2009-04-10 09:43:08 +08003587 oparams = memdup_user(_oparams, sizeof(*oparams));
3588 if (IS_ERR(oparams)) {
3589 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 goto out;
3591 }
3592 snd_pcm_hw_convert_from_old_params(params, oparams);
3593 err = snd_pcm_hw_refine(substream, params);
3594 snd_pcm_hw_convert_to_old_params(oparams, params);
3595 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3596 if (!err)
3597 err = -EFAULT;
3598 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003599
3600 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601out:
3602 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 return err;
3604}
3605
Takashi Iwai877211f2005-11-17 13:59:38 +01003606static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3607 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
Takashi Iwai877211f2005-11-17 13:59:38 +01003609 struct snd_pcm_hw_params *params;
3610 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 int err;
3612
3613 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003614 if (!params)
3615 return -ENOMEM;
3616
3617 oparams = memdup_user(_oparams, sizeof(*oparams));
3618 if (IS_ERR(oparams)) {
3619 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 goto out;
3621 }
3622 snd_pcm_hw_convert_from_old_params(params, oparams);
3623 err = snd_pcm_hw_params(substream, params);
3624 snd_pcm_hw_convert_to_old_params(oparams, params);
3625 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3626 if (!err)
3627 err = -EFAULT;
3628 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003629
3630 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631out:
3632 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 return err;
3634}
Takashi Iwai59d48582005-12-01 10:51:58 +01003635#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Cliff Cai70036092008-09-03 10:54:36 +02003637#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003638static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3639 unsigned long addr,
3640 unsigned long len,
3641 unsigned long pgoff,
3642 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003643{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003644 struct snd_pcm_file *pcm_file = file->private_data;
3645 struct snd_pcm_substream *substream = pcm_file->substream;
3646 struct snd_pcm_runtime *runtime = substream->runtime;
3647 unsigned long offset = pgoff << PAGE_SHIFT;
3648
3649 switch (offset) {
3650 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3651 return (unsigned long)runtime->status;
3652 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3653 return (unsigned long)runtime->control;
3654 default:
3655 return (unsigned long)runtime->dma_area + offset;
3656 }
Cliff Cai70036092008-09-03 10:54:36 +02003657}
3658#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003659# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003660#endif
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662/*
3663 * Register section
3664 */
3665
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003666const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003668 .owner = THIS_MODULE,
3669 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003670 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003671 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003672 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003673 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003674 .poll = snd_pcm_playback_poll,
3675 .unlocked_ioctl = snd_pcm_playback_ioctl,
3676 .compat_ioctl = snd_pcm_ioctl_compat,
3677 .mmap = snd_pcm_mmap,
3678 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003679 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 },
3681 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003682 .owner = THIS_MODULE,
3683 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003684 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003685 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003686 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003687 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003688 .poll = snd_pcm_capture_poll,
3689 .unlocked_ioctl = snd_pcm_capture_ioctl,
3690 .compat_ioctl = snd_pcm_ioctl_compat,
3691 .mmap = snd_pcm_mmap,
3692 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003693 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 }
3695};