blob: 139887011ba2c4b387050e5a041878d4e4f09c84 [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
Takashi Iwai877211f2005-11-17 13:59:38 +0100489static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
490 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Takashi Iwai877211f2005-11-17 13:59:38 +0100492 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700493 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 unsigned int bits;
495 snd_pcm_uframes_t frames;
496
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200497 if (PCM_RUNTIME_CHECK(substream))
498 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 snd_pcm_stream_lock_irq(substream);
501 switch (runtime->status->state) {
502 case SNDRV_PCM_STATE_OPEN:
503 case SNDRV_PCM_STATE_SETUP:
504 case SNDRV_PCM_STATE_PREPARED:
505 break;
506 default:
507 snd_pcm_stream_unlock_irq(substream);
508 return -EBADFD;
509 }
510 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100511#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!substream->oss.oss)
513#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200514 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EBADFD;
516
517 params->rmask = ~0U;
518 err = snd_pcm_hw_refine(substream, params);
519 if (err < 0)
520 goto _error;
521
522 err = snd_pcm_hw_params_choose(substream, params);
523 if (err < 0)
524 goto _error;
525
526 if (substream->ops->hw_params != NULL) {
527 err = substream->ops->hw_params(substream, params);
528 if (err < 0)
529 goto _error;
530 }
531
532 runtime->access = params_access(params);
533 runtime->format = params_format(params);
534 runtime->subformat = params_subformat(params);
535 runtime->channels = params_channels(params);
536 runtime->rate = params_rate(params);
537 runtime->period_size = params_period_size(params);
538 runtime->periods = params_periods(params);
539 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 runtime->info = params->info;
541 runtime->rate_num = params->rate_num;
542 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100543 runtime->no_period_wakeup =
544 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
545 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 bits = snd_pcm_format_physical_width(runtime->format);
548 runtime->sample_bits = bits;
549 bits *= runtime->channels;
550 runtime->frame_bits = bits;
551 frames = 1;
552 while (bits % 8 != 0) {
553 bits *= 2;
554 frames *= 2;
555 }
556 runtime->byte_align = bits / 8;
557 runtime->min_align = frames;
558
559 /* Default sw params */
560 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
561 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 runtime->start_threshold = 1;
564 runtime->stop_threshold = runtime->buffer_size;
565 runtime->silence_threshold = 0;
566 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200567 runtime->boundary = runtime->buffer_size;
568 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
569 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200572 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700573
James Bottomley82f68252010-07-05 22:53:06 +0200574 if (pm_qos_request_active(&substream->latency_pm_qos_req))
575 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700576 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200577 pm_qos_add_request(&substream->latency_pm_qos_req,
578 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300581 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 so we force application to retry to set
583 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200584 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (substream->ops->hw_free != NULL)
586 substream->ops->hw_free(substream);
587 return err;
588}
589
Takashi Iwai877211f2005-11-17 13:59:38 +0100590static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
591 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Takashi Iwai877211f2005-11-17 13:59:38 +0100593 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int err;
595
Li Zefanef44a1e2009-04-10 09:43:08 +0800596 params = memdup_user(_params, sizeof(*params));
597 if (IS_ERR(params))
598 return PTR_ERR(params);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 err = snd_pcm_hw_params(substream, params);
601 if (copy_to_user(_params, params, sizeof(*params))) {
602 if (!err)
603 err = -EFAULT;
604 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 kfree(params);
607 return err;
608}
609
Takashi Iwai877211f2005-11-17 13:59:38 +0100610static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwai877211f2005-11-17 13:59:38 +0100612 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int result = 0;
614
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200615 if (PCM_RUNTIME_CHECK(substream))
616 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 snd_pcm_stream_lock_irq(substream);
619 switch (runtime->status->state) {
620 case SNDRV_PCM_STATE_SETUP:
621 case SNDRV_PCM_STATE_PREPARED:
622 break;
623 default:
624 snd_pcm_stream_unlock_irq(substream);
625 return -EBADFD;
626 }
627 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200628 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -EBADFD;
630 if (substream->ops->hw_free)
631 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200632 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200633 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return result;
635}
636
Takashi Iwai877211f2005-11-17 13:59:38 +0100637static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
638 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Takashi Iwai877211f2005-11-17 13:59:38 +0100640 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100641 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200643 if (PCM_RUNTIME_CHECK(substream))
644 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 snd_pcm_stream_lock_irq(substream);
647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
648 snd_pcm_stream_unlock_irq(substream);
649 return -EBADFD;
650 }
651 snd_pcm_stream_unlock_irq(substream);
652
Dan Carpenter145d92e2015-09-23 12:42:28 +0300653 if (params->tstamp_mode < 0 ||
654 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -EINVAL;
Takashi Iwai58900812014-07-16 17:45:27 +0200656 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
657 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200658 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (params->avail_min == 0)
660 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (params->silence_size >= runtime->boundary) {
662 if (params->silence_threshold != 0)
663 return -EINVAL;
664 } else {
665 if (params->silence_size > params->silence_threshold)
666 return -EINVAL;
667 if (params->silence_threshold > runtime->buffer_size)
668 return -EINVAL;
669 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100670 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 snd_pcm_stream_lock_irq(substream);
672 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai58900812014-07-16 17:45:27 +0200673 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
674 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 runtime->period_step = params->period_step;
676 runtime->control->avail_min = params->avail_min;
677 runtime->start_threshold = params->start_threshold;
678 runtime->stop_threshold = params->stop_threshold;
679 runtime->silence_threshold = params->silence_threshold;
680 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 params->boundary = runtime->boundary;
682 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
684 runtime->silence_size > 0)
685 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100686 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100689 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Takashi Iwai877211f2005-11-17 13:59:38 +0100692static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
693 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Takashi Iwai877211f2005-11-17 13:59:38 +0100695 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int err;
697 if (copy_from_user(&params, _params, sizeof(params)))
698 return -EFAULT;
699 err = snd_pcm_sw_params(substream, &params);
700 if (copy_to_user(_params, &params, sizeof(params)))
701 return -EFAULT;
702 return err;
703}
704
Takashi Iwai877211f2005-11-17 13:59:38 +0100705int snd_pcm_status(struct snd_pcm_substream *substream,
706 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Takashi Iwai877211f2005-11-17 13:59:38 +0100708 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600711
712 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
713 &runtime->audio_tstamp_config);
714
715 /* backwards compatible behavior */
716 if (runtime->audio_tstamp_config.type_requested ==
717 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
718 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
719 runtime->audio_tstamp_config.type_requested =
720 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
721 else
722 runtime->audio_tstamp_config.type_requested =
723 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
724 runtime->audio_tstamp_report.valid = 0;
725 } else
726 runtime->audio_tstamp_report.valid = 1;
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 status->state = runtime->status->state;
729 status->suspended_state = runtime->status->suspended_state;
730 if (status->state == SNDRV_PCM_STATE_OPEN)
731 goto _end;
732 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100733 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100735 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
736 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600737 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500738 status->audio_tstamp =
739 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600740 if (runtime->audio_tstamp_report.valid == 1)
741 /* backwards compatibility, no report provided in COMPAT mode */
742 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
743 &status->audio_tstamp_accuracy,
744 &runtime->audio_tstamp_report);
745
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100746 goto _tstamp_end;
747 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600748 } else {
749 /* get tstamp only in fallback mode and only if enabled */
750 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
751 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100752 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100753 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 status->appl_ptr = runtime->control->appl_ptr;
755 status->hw_ptr = runtime->status->hw_ptr;
756 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
757 status->avail = snd_pcm_playback_avail(runtime);
758 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200759 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200761 status->delay += runtime->delay;
762 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 status->delay = 0;
764 } else {
765 status->avail = snd_pcm_capture_avail(runtime);
766 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200767 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 else
769 status->delay = 0;
770 }
771 status->avail_max = runtime->avail_max;
772 status->overrange = runtime->overrange;
773 runtime->avail_max = 0;
774 runtime->overrange = 0;
775 _end:
776 snd_pcm_stream_unlock_irq(substream);
777 return 0;
778}
779
Takashi Iwai877211f2005-11-17 13:59:38 +0100780static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600781 struct snd_pcm_status __user * _status,
782 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Takashi Iwai877211f2005-11-17 13:59:38 +0100784 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600788 /*
789 * with extension, parameters are read/write,
790 * get audio_tstamp_data from user,
791 * ignore rest of status structure
792 */
793 if (ext && get_user(status.audio_tstamp_data,
794 (u32 __user *)(&_status->audio_tstamp_data)))
795 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 res = snd_pcm_status(substream, &status);
797 if (res < 0)
798 return res;
799 if (copy_to_user(_status, &status, sizeof(status)))
800 return -EFAULT;
801 return 0;
802}
803
Takashi Iwai877211f2005-11-17 13:59:38 +0100804static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
805 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Takashi Iwai877211f2005-11-17 13:59:38 +0100807 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 unsigned int channel;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 channel = info->channel;
811 runtime = substream->runtime;
812 snd_pcm_stream_lock_irq(substream);
813 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
814 snd_pcm_stream_unlock_irq(substream);
815 return -EBADFD;
816 }
817 snd_pcm_stream_unlock_irq(substream);
818 if (channel >= runtime->channels)
819 return -EINVAL;
820 memset(info, 0, sizeof(*info));
821 info->channel = channel;
822 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
823}
824
Takashi Iwai877211f2005-11-17 13:59:38 +0100825static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
826 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Takashi Iwai877211f2005-11-17 13:59:38 +0100828 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int res;
830
831 if (copy_from_user(&info, _info, sizeof(info)))
832 return -EFAULT;
833 res = snd_pcm_channel_info(substream, &info);
834 if (res < 0)
835 return res;
836 if (copy_to_user(_info, &info, sizeof(info)))
837 return -EFAULT;
838 return 0;
839}
840
Takashi Iwai877211f2005-11-17 13:59:38 +0100841static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Takashi Iwai877211f2005-11-17 13:59:38 +0100843 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (runtime->trigger_master == NULL)
845 return;
846 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600847 if (!runtime->trigger_tstamp_latched)
848 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 } else {
850 snd_pcm_trigger_tstamp(runtime->trigger_master);
851 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
852 }
853 runtime->trigger_master = NULL;
854}
855
856struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100857 int (*pre_action)(struct snd_pcm_substream *substream, int state);
858 int (*do_action)(struct snd_pcm_substream *substream, int state);
859 void (*undo_action)(struct snd_pcm_substream *substream, int state);
860 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861};
862
863/*
864 * this functions is core for handling of linked stream
865 * Note: the stream state might be changed also on failure
866 * Note2: call with calling stream lock + link lock
867 */
868static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100869 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 int state, int do_lock)
871{
Takashi Iwai877211f2005-11-17 13:59:38 +0100872 struct snd_pcm_substream *s = NULL;
873 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200874 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Takashi Iwaief991b92007-02-22 12:52:53 +0100876 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200877 if (do_lock && s != substream) {
878 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200879 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200880 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200881 spin_lock_nested(&s->self_group.lock, depth);
882 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 res = ops->pre_action(s, state);
885 if (res < 0)
886 goto _unlock;
887 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100888 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 res = ops->do_action(s, state);
890 if (res < 0) {
891 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100892 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (s1 == s) /* failed stream */
894 break;
895 ops->undo_action(s1, state);
896 }
897 }
898 s = NULL; /* unlock all */
899 goto _unlock;
900 }
901 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100902 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 ops->post_action(s, state);
904 }
905 _unlock:
906 if (do_lock) {
907 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100908 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200909 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200910 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200911 mutex_unlock(&s1->self_group.mutex);
912 else
913 spin_unlock(&s1->self_group.lock);
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (s1 == s) /* end */
916 break;
917 }
918 }
919 return res;
920}
921
922/*
923 * Note: call with stream lock
924 */
925static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100926 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int state)
928{
929 int res;
930
931 res = ops->pre_action(substream, state);
932 if (res < 0)
933 return res;
934 res = ops->do_action(substream, state);
935 if (res == 0)
936 ops->post_action(substream, state);
937 else if (ops->undo_action)
938 ops->undo_action(substream, state);
939 return res;
940}
941
942/*
943 * Note: call with stream lock
944 */
945static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100946 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 int state)
948{
949 int res;
950
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100951 if (!snd_pcm_stream_linked(substream))
952 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200953
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100954 if (substream->pcm->nonatomic) {
955 if (!mutex_trylock(&substream->group->mutex)) {
956 mutex_unlock(&substream->self_group.mutex);
957 mutex_lock(&substream->group->mutex);
958 mutex_lock(&substream->self_group.mutex);
959 }
960 res = snd_pcm_action_group(ops, substream, state, 1);
961 mutex_unlock(&substream->group->mutex);
962 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!spin_trylock(&substream->group->lock)) {
964 spin_unlock(&substream->self_group.lock);
965 spin_lock(&substream->group->lock);
966 spin_lock(&substream->self_group.lock);
967 }
968 res = snd_pcm_action_group(ops, substream, state, 1);
969 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 return res;
972}
973
974/*
975 * Note: don't use any locks before
976 */
977static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100978 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 int state)
980{
981 int res;
982
Takashi Iwaie3a4bd52014-10-31 14:45:04 +0100983 snd_pcm_stream_lock_irq(substream);
984 res = snd_pcm_action(ops, substream, state);
985 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return res;
987}
988
989/*
990 */
991static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100992 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 int state)
994{
995 int res;
996
997 down_read(&snd_pcm_link_rwsem);
998 if (snd_pcm_stream_linked(substream))
999 res = snd_pcm_action_group(ops, substream, state, 0);
1000 else
1001 res = snd_pcm_action_single(ops, substream, state);
1002 up_read(&snd_pcm_link_rwsem);
1003 return res;
1004}
1005
1006/*
1007 * start callbacks
1008 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001009static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Takashi Iwai877211f2005-11-17 13:59:38 +01001011 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1013 return -EBADFD;
1014 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1015 !snd_pcm_playback_data(substream))
1016 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001017 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 runtime->trigger_master = substream;
1019 return 0;
1020}
1021
Takashi Iwai877211f2005-11-17 13:59:38 +01001022static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 if (substream->runtime->trigger_master != substream)
1025 return 0;
1026 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1027}
1028
Takashi Iwai877211f2005-11-17 13:59:38 +01001029static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 if (substream->runtime->trigger_master == substream)
1032 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1033}
1034
Takashi Iwai877211f2005-11-17 13:59:38 +01001035static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Takashi Iwai877211f2005-11-17 13:59:38 +01001037 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001039 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001040 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1041 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 runtime->status->state = state;
1043 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1044 runtime->silence_size > 0)
1045 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001047 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
1048 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051static struct action_ops snd_pcm_action_start = {
1052 .pre_action = snd_pcm_pre_start,
1053 .do_action = snd_pcm_do_start,
1054 .undo_action = snd_pcm_undo_start,
1055 .post_action = snd_pcm_post_start
1056};
1057
1058/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001059 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001060 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001061 *
1062 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001064int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Takashi Iwai877211f2005-11-17 13:59:38 +01001066 return snd_pcm_action(&snd_pcm_action_start, substream,
1067 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070/*
1071 * stop callbacks
1072 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001073static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Takashi Iwai877211f2005-11-17 13:59:38 +01001075 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1077 return -EBADFD;
1078 runtime->trigger_master = substream;
1079 return 0;
1080}
1081
Takashi Iwai877211f2005-11-17 13:59:38 +01001082static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 if (substream->runtime->trigger_master == substream &&
1085 snd_pcm_running(substream))
1086 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1087 return 0; /* unconditonally stop all substreams */
1088}
1089
Takashi Iwai877211f2005-11-17 13:59:38 +01001090static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Takashi Iwai877211f2005-11-17 13:59:38 +01001092 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (runtime->status->state != state) {
1094 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001095 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001097 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
1098 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001101 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104static struct action_ops snd_pcm_action_stop = {
1105 .pre_action = snd_pcm_pre_stop,
1106 .do_action = snd_pcm_do_stop,
1107 .post_action = snd_pcm_post_stop
1108};
1109
1110/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001111 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001112 * @substream: the PCM substream instance
1113 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001115 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001116 *
Masanari Iida0a114582014-02-09 00:47:36 +09001117 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001119int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1122}
1123
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001124EXPORT_SYMBOL(snd_pcm_stop);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001127 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001128 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001130 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001132 *
1133 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001135int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Takashi Iwai877211f2005-11-17 13:59:38 +01001137 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1138 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Takashi Iwai1fb85102014-11-07 17:08:28 +01001141/**
1142 * snd_pcm_stop_xrun - stop the running streams as XRUN
1143 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001144 *
1145 * This stops the given running substream (and all linked substreams) as XRUN.
1146 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1147 *
1148 * Return: Zero if successful, or a negative error code.
1149 */
1150int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1151{
1152 unsigned long flags;
1153 int ret = 0;
1154
1155 snd_pcm_stream_lock_irqsave(substream, flags);
1156 if (snd_pcm_running(substream))
1157 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1158 snd_pcm_stream_unlock_irqrestore(substream, flags);
1159 return ret;
1160}
1161EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
1164 * pause callbacks
1165 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001166static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Takashi Iwai877211f2005-11-17 13:59:38 +01001168 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1170 return -ENOSYS;
1171 if (push) {
1172 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1173 return -EBADFD;
1174 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1175 return -EBADFD;
1176 runtime->trigger_master = substream;
1177 return 0;
1178}
1179
Takashi Iwai877211f2005-11-17 13:59:38 +01001180static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 if (substream->runtime->trigger_master != substream)
1183 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001184 /* some drivers might use hw_ptr to recover from the pause -
1185 update the hw_ptr now */
1186 if (push)
1187 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001188 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001189 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001190 * delta, effectively to skip the check once.
1191 */
1192 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return substream->ops->trigger(substream,
1194 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1195 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1196}
1197
Takashi Iwai877211f2005-11-17 13:59:38 +01001198static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 if (substream->runtime->trigger_master == substream)
1201 substream->ops->trigger(substream,
1202 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1203 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1204}
1205
Takashi Iwai877211f2005-11-17 13:59:38 +01001206static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Takashi Iwai877211f2005-11-17 13:59:38 +01001208 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 snd_pcm_trigger_tstamp(substream);
1210 if (push) {
1211 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1212 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001213 snd_timer_notify(substream->timer,
1214 SNDRV_TIMER_EVENT_MPAUSE,
1215 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001217 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 } else {
1219 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001221 snd_timer_notify(substream->timer,
1222 SNDRV_TIMER_EVENT_MCONTINUE,
1223 &runtime->trigger_tstamp);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001272 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1273 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001275 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278static struct action_ops snd_pcm_action_suspend = {
1279 .pre_action = snd_pcm_pre_suspend,
1280 .do_action = snd_pcm_do_suspend,
1281 .post_action = snd_pcm_post_suspend
1282};
1283
1284/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001285 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001286 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001289 *
1290 * Return: Zero if successful (or @substream is %NULL), or a negative error
1291 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001293int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 int err;
1296 unsigned long flags;
1297
Takashi Iwai603bf522005-11-17 15:59:14 +01001298 if (! substream)
1299 return 0;
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 snd_pcm_stream_lock_irqsave(substream, flags);
1302 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1303 snd_pcm_stream_unlock_irqrestore(substream, flags);
1304 return err;
1305}
1306
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001307EXPORT_SYMBOL(snd_pcm_suspend);
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001310 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001311 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001314 *
1315 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001317int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Takashi Iwai877211f2005-11-17 13:59:38 +01001319 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int stream, err = 0;
1321
Takashi Iwai603bf522005-11-17 15:59:14 +01001322 if (! pcm)
1323 return 0;
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001326 for (substream = pcm->streams[stream].substream;
1327 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /* FIXME: the open/close code should lock this as well */
1329 if (substream->runtime == NULL)
1330 continue;
1331 err = snd_pcm_suspend(substream);
1332 if (err < 0 && err != -EBUSY)
1333 return err;
1334 }
1335 }
1336 return 0;
1337}
1338
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001339EXPORT_SYMBOL(snd_pcm_suspend_all);
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/* resume */
1342
Takashi Iwai877211f2005-11-17 13:59:38 +01001343static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Takashi Iwai877211f2005-11-17 13:59:38 +01001345 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1347 return -ENOSYS;
1348 runtime->trigger_master = substream;
1349 return 0;
1350}
1351
Takashi Iwai877211f2005-11-17 13:59:38 +01001352static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Takashi Iwai877211f2005-11-17 13:59:38 +01001354 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (runtime->trigger_master != substream)
1356 return 0;
1357 /* DMA not running previously? */
1358 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1359 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1360 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1361 return 0;
1362 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1363}
1364
Takashi Iwai877211f2005-11-17 13:59:38 +01001365static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 if (substream->runtime->trigger_master == substream &&
1368 snd_pcm_running(substream))
1369 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1370}
1371
Takashi Iwai877211f2005-11-17 13:59:38 +01001372static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Takashi Iwai877211f2005-11-17 13:59:38 +01001374 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001376 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001378 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1379 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382static struct action_ops snd_pcm_action_resume = {
1383 .pre_action = snd_pcm_pre_resume,
1384 .do_action = snd_pcm_do_resume,
1385 .undo_action = snd_pcm_undo_resume,
1386 .post_action = snd_pcm_post_resume
1387};
1388
Takashi Iwai877211f2005-11-17 13:59:38 +01001389static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Takashi Iwai877211f2005-11-17 13:59:38 +01001391 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int res;
1393
1394 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001395 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1397 snd_power_unlock(card);
1398 return res;
1399}
1400
1401#else
1402
Takashi Iwai877211f2005-11-17 13:59:38 +01001403static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 return -ENOSYS;
1406}
1407
1408#endif /* CONFIG_PM */
1409
1410/*
1411 * xrun ioctl
1412 *
1413 * Change the RUNNING stream(s) to XRUN state.
1414 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001415static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Takashi Iwai877211f2005-11-17 13:59:38 +01001417 struct snd_card *card = substream->pcm->card;
1418 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 int result;
1420
1421 snd_power_lock(card);
1422 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001423 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (result < 0)
1425 goto _unlock;
1426 }
1427
1428 snd_pcm_stream_lock_irq(substream);
1429 switch (runtime->status->state) {
1430 case SNDRV_PCM_STATE_XRUN:
1431 result = 0; /* already there */
1432 break;
1433 case SNDRV_PCM_STATE_RUNNING:
1434 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1435 break;
1436 default:
1437 result = -EBADFD;
1438 }
1439 snd_pcm_stream_unlock_irq(substream);
1440 _unlock:
1441 snd_power_unlock(card);
1442 return result;
1443}
1444
1445/*
1446 * reset ioctl
1447 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001448static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Takashi Iwai877211f2005-11-17 13:59:38 +01001450 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 switch (runtime->status->state) {
1452 case SNDRV_PCM_STATE_RUNNING:
1453 case SNDRV_PCM_STATE_PREPARED:
1454 case SNDRV_PCM_STATE_PAUSED:
1455 case SNDRV_PCM_STATE_SUSPENDED:
1456 return 0;
1457 default:
1458 return -EBADFD;
1459 }
1460}
1461
Takashi Iwai877211f2005-11-17 13:59:38 +01001462static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Takashi Iwai877211f2005-11-17 13:59:38 +01001464 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1466 if (err < 0)
1467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001469 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1470 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 runtime->silence_start = runtime->status->hw_ptr;
1472 runtime->silence_filled = 0;
1473 return 0;
1474}
1475
Takashi Iwai877211f2005-11-17 13:59:38 +01001476static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Takashi Iwai877211f2005-11-17 13:59:38 +01001478 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 runtime->control->appl_ptr = runtime->status->hw_ptr;
1480 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1481 runtime->silence_size > 0)
1482 snd_pcm_playback_silence(substream, ULONG_MAX);
1483}
1484
1485static struct action_ops snd_pcm_action_reset = {
1486 .pre_action = snd_pcm_pre_reset,
1487 .do_action = snd_pcm_do_reset,
1488 .post_action = snd_pcm_post_reset
1489};
1490
Takashi Iwai877211f2005-11-17 13:59:38 +01001491static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1494}
1495
1496/*
1497 * prepare ioctl
1498 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001499/* we use the second argument for updating f_flags */
1500static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1501 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Takashi Iwai877211f2005-11-17 13:59:38 +01001503 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001504 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1505 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EBADFD;
1507 if (snd_pcm_running(substream))
1508 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001509 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return 0;
1511}
1512
Takashi Iwai877211f2005-11-17 13:59:38 +01001513static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 int err;
1516 err = substream->ops->prepare(substream);
1517 if (err < 0)
1518 return err;
1519 return snd_pcm_do_reset(substream, 0);
1520}
1521
Takashi Iwai877211f2005-11-17 13:59:38 +01001522static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Takashi Iwai877211f2005-11-17 13:59:38 +01001524 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001526 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
1529static struct action_ops snd_pcm_action_prepare = {
1530 .pre_action = snd_pcm_pre_prepare,
1531 .do_action = snd_pcm_do_prepare,
1532 .post_action = snd_pcm_post_prepare
1533};
1534
1535/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001536 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001537 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001538 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001539 *
1540 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001542static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1543 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001546 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001547 int f_flags;
1548
1549 if (file)
1550 f_flags = file->f_flags;
1551 else
1552 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001555 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001556 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1557 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 snd_power_unlock(card);
1559 return res;
1560}
1561
1562/*
1563 * drain ioctl
1564 */
1565
Takashi Iwai877211f2005-11-17 13:59:38 +01001566static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001568 struct snd_pcm_runtime *runtime = substream->runtime;
1569 switch (runtime->status->state) {
1570 case SNDRV_PCM_STATE_OPEN:
1571 case SNDRV_PCM_STATE_DISCONNECTED:
1572 case SNDRV_PCM_STATE_SUSPENDED:
1573 return -EBADFD;
1574 }
1575 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return 0;
1577}
1578
Takashi Iwai877211f2005-11-17 13:59:38 +01001579static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Takashi Iwai877211f2005-11-17 13:59:38 +01001581 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1583 switch (runtime->status->state) {
1584 case SNDRV_PCM_STATE_PREPARED:
1585 /* start playback stream if possible */
1586 if (! snd_pcm_playback_empty(substream)) {
1587 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1588 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001589 } else {
1590 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592 break;
1593 case SNDRV_PCM_STATE_RUNNING:
1594 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1595 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001596 case SNDRV_PCM_STATE_XRUN:
1597 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 default:
1600 break;
1601 }
1602 } else {
1603 /* stop running stream */
1604 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001605 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001607 snd_pcm_do_stop(substream, new_state);
1608 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610 }
Libin Yang48d88292014-12-31 22:09:54 +08001611
1612 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1613 runtime->trigger_master == substream &&
1614 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1615 return substream->ops->trigger(substream,
1616 SNDRV_PCM_TRIGGER_DRAIN);
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return 0;
1619}
1620
Takashi Iwai877211f2005-11-17 13:59:38 +01001621static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623}
1624
1625static struct action_ops snd_pcm_action_drain_init = {
1626 .pre_action = snd_pcm_pre_drain_init,
1627 .do_action = snd_pcm_do_drain_init,
1628 .post_action = snd_pcm_post_drain_init
1629};
1630
Takashi Iwai877211f2005-11-17 13:59:38 +01001631static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633/*
1634 * Drain the stream(s).
1635 * When the substream is linked, sync until the draining of all playback streams
1636 * is finished.
1637 * After this call, all streams are supposed to be either SETUP or DRAINING
1638 * (capture only) state.
1639 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001640static int snd_pcm_drain(struct snd_pcm_substream *substream,
1641 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Takashi Iwai877211f2005-11-17 13:59:38 +01001643 struct snd_card *card;
1644 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001645 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001646 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001648 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 card = substream->pcm->card;
1651 runtime = substream->runtime;
1652
1653 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1654 return -EBADFD;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 snd_power_lock(card);
1657 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001658 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001659 if (result < 0) {
1660 snd_power_unlock(card);
1661 return result;
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001665 if (file) {
1666 if (file->f_flags & O_NONBLOCK)
1667 nonblock = 1;
1668 } else if (substream->f_flags & O_NONBLOCK)
1669 nonblock = 1;
1670
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001671 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001672 snd_pcm_stream_lock_irq(substream);
1673 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001674 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001675 snd_pcm_pause(substream, 0);
1676
1677 /* pre-start/stop - all running streams are changed to DRAINING state */
1678 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001679 if (result < 0)
1680 goto unlock;
1681 /* in non-blocking, we don't wait in ioctl but let caller poll */
1682 if (nonblock) {
1683 result = -EAGAIN;
1684 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 for (;;) {
1688 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001689 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (signal_pending(current)) {
1691 result = -ERESTARTSYS;
1692 break;
1693 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001694 /* find a substream to drain */
1695 to_check = NULL;
1696 snd_pcm_group_for_each_entry(s, substream) {
1697 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1698 continue;
1699 runtime = s->runtime;
1700 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1701 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001702 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001703 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001704 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001705 if (!to_check)
1706 break; /* all drained */
1707 init_waitqueue_entry(&wait, current);
1708 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001710 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001712 if (runtime->no_period_wakeup)
1713 tout = MAX_SCHEDULE_TIMEOUT;
1714 else {
1715 tout = 10;
1716 if (runtime->rate) {
1717 long t = runtime->period_size * 2 / runtime->rate;
1718 tout = max(t, tout);
1719 }
1720 tout = msecs_to_jiffies(tout * 1000);
1721 }
1722 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001724 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001726 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001727 if (card->shutdown) {
1728 result = -ENODEV;
1729 break;
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (tout == 0) {
1732 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1733 result = -ESTRPIPE;
1734 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001735 dev_dbg(substream->pcm->card->dev,
1736 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1738 result = -EIO;
1739 }
1740 break;
1741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001743
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001744 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001745 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001746 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 return result;
1750}
1751
1752/*
1753 * drop ioctl
1754 *
1755 * Immediately put all linked substreams into SETUP state.
1756 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001757static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Takashi Iwai877211f2005-11-17 13:59:38 +01001759 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int result = 0;
1761
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001762 if (PCM_RUNTIME_CHECK(substream))
1763 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001766 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001767 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1768 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return -EBADFD;
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 snd_pcm_stream_lock_irq(substream);
1772 /* resume pause */
1773 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1774 snd_pcm_pause(substream, 0);
1775
1776 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1777 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1778 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return result;
1781}
1782
1783
Al Viro0888c322013-06-05 14:09:55 -04001784static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Al Viro0888c322013-06-05 14:09:55 -04001786 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001787 unsigned int minor;
1788
Al Viro0888c322013-06-05 14:09:55 -04001789 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1790 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001792 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1793 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
1796/*
1797 * PCM link handling
1798 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001799static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001802 struct snd_pcm_file *pcm_file;
1803 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001804 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001805 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Al Viro0888c322013-06-05 14:09:55 -04001807 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001809 if (!is_pcm_file(f.file)) {
1810 res = -EBADFD;
1811 goto _badf;
1812 }
1813 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001815 group = kmalloc(sizeof(*group), GFP_KERNEL);
1816 if (!group) {
1817 res = -ENOMEM;
1818 goto _nolock;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 down_write(&snd_pcm_link_rwsem);
1821 write_lock_irq(&snd_pcm_link_rwlock);
1822 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001823 substream->runtime->status->state != substream1->runtime->status->state ||
1824 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 res = -EBADFD;
1826 goto _end;
1827 }
1828 if (snd_pcm_stream_linked(substream1)) {
1829 res = -EALREADY;
1830 goto _end;
1831 }
1832 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001833 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001834 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001836 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 INIT_LIST_HEAD(&substream->group->substreams);
1838 list_add_tail(&substream->link_list, &substream->group->substreams);
1839 substream->group->count = 1;
1840 }
1841 list_add_tail(&substream1->link_list, &substream->group->substreams);
1842 substream->group->count++;
1843 substream1->group = substream->group;
1844 _end:
1845 write_unlock_irq(&snd_pcm_link_rwlock);
1846 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001847 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001848 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001849 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001850 _badf:
1851 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return res;
1853}
1854
Takashi Iwai877211f2005-11-17 13:59:38 +01001855static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 substream->group = &substream->self_group;
1858 INIT_LIST_HEAD(&substream->self_group.substreams);
1859 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1860}
1861
Takashi Iwai877211f2005-11-17 13:59:38 +01001862static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Takashi Iwaief991b92007-02-22 12:52:53 +01001864 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 int res = 0;
1866
1867 down_write(&snd_pcm_link_rwsem);
1868 write_lock_irq(&snd_pcm_link_rwlock);
1869 if (!snd_pcm_stream_linked(substream)) {
1870 res = -EALREADY;
1871 goto _end;
1872 }
1873 list_del(&substream->link_list);
1874 substream->group->count--;
1875 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001876 snd_pcm_group_for_each_entry(s, substream) {
1877 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
1879 }
1880 kfree(substream->group);
1881 }
1882 relink_to_local(substream);
1883 _end:
1884 write_unlock_irq(&snd_pcm_link_rwlock);
1885 up_write(&snd_pcm_link_rwsem);
1886 return res;
1887}
1888
1889/*
1890 * hw configurator
1891 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001892static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1893 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Takashi Iwai877211f2005-11-17 13:59:38 +01001895 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1897 hw_param_interval_c(params, rule->deps[1]), &t);
1898 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1899}
1900
Takashi Iwai877211f2005-11-17 13:59:38 +01001901static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1902 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Takashi Iwai877211f2005-11-17 13:59:38 +01001904 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1906 hw_param_interval_c(params, rule->deps[1]), &t);
1907 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1908}
1909
Takashi Iwai877211f2005-11-17 13:59:38 +01001910static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1911 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Takashi Iwai877211f2005-11-17 13:59:38 +01001913 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1915 hw_param_interval_c(params, rule->deps[1]),
1916 (unsigned long) rule->private, &t);
1917 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1918}
1919
Takashi Iwai877211f2005-11-17 13:59:38 +01001920static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1921 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Takashi Iwai877211f2005-11-17 13:59:38 +01001923 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1925 (unsigned long) rule->private,
1926 hw_param_interval_c(params, rule->deps[1]), &t);
1927 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1928}
1929
Takashi Iwai877211f2005-11-17 13:59:38 +01001930static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1931 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001934 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1935 struct snd_mask m;
1936 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 snd_mask_any(&m);
1938 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1939 int bits;
1940 if (! snd_mask_test(mask, k))
1941 continue;
1942 bits = snd_pcm_format_physical_width(k);
1943 if (bits <= 0)
1944 continue; /* ignore invalid formats */
1945 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1946 snd_mask_reset(&m, k);
1947 }
1948 return snd_mask_refine(mask, &m);
1949}
1950
Takashi Iwai877211f2005-11-17 13:59:38 +01001951static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1952 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Takashi Iwai877211f2005-11-17 13:59:38 +01001954 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 unsigned int k;
1956 t.min = UINT_MAX;
1957 t.max = 0;
1958 t.openmin = 0;
1959 t.openmax = 0;
1960 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1961 int bits;
1962 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1963 continue;
1964 bits = snd_pcm_format_physical_width(k);
1965 if (bits <= 0)
1966 continue; /* ignore invalid formats */
1967 if (t.min > (unsigned)bits)
1968 t.min = bits;
1969 if (t.max < (unsigned)bits)
1970 t.max = bits;
1971 }
1972 t.integer = 1;
1973 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1974}
1975
1976#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1977#error "Change this table"
1978#endif
1979
1980static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1981 48000, 64000, 88200, 96000, 176400, 192000 };
1982
Clemens Ladisch7653d552007-08-13 17:38:54 +02001983const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1984 .count = ARRAY_SIZE(rates),
1985 .list = rates,
1986};
1987
Takashi Iwai877211f2005-11-17 13:59:38 +01001988static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1989 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Takashi Iwai877211f2005-11-17 13:59:38 +01001991 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001993 snd_pcm_known_rates.count,
1994 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
Takashi Iwai877211f2005-11-17 13:59:38 +01001997static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1998 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Takashi Iwai877211f2005-11-17 13:59:38 +01002000 struct snd_interval t;
2001 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 t.min = 0;
2003 t.max = substream->buffer_bytes_max;
2004 t.openmin = 0;
2005 t.openmax = 0;
2006 t.integer = 1;
2007 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2008}
2009
Takashi Iwai877211f2005-11-17 13:59:38 +01002010int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Takashi Iwai877211f2005-11-17 13:59:38 +01002012 struct snd_pcm_runtime *runtime = substream->runtime;
2013 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 int k, err;
2015
2016 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2017 snd_mask_any(constrs_mask(constrs, k));
2018 }
2019
2020 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2021 snd_interval_any(constrs_interval(constrs, k));
2022 }
2023
2024 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2025 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2026 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2027 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2028 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2029
2030 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2031 snd_pcm_hw_rule_format, NULL,
2032 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2033 if (err < 0)
2034 return err;
2035 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2036 snd_pcm_hw_rule_sample_bits, NULL,
2037 SNDRV_PCM_HW_PARAM_FORMAT,
2038 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2039 if (err < 0)
2040 return err;
2041 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2042 snd_pcm_hw_rule_div, NULL,
2043 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2044 if (err < 0)
2045 return err;
2046 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2047 snd_pcm_hw_rule_mul, NULL,
2048 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2049 if (err < 0)
2050 return err;
2051 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2052 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2053 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2054 if (err < 0)
2055 return err;
2056 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2057 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2058 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2059 if (err < 0)
2060 return err;
2061 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2062 snd_pcm_hw_rule_div, NULL,
2063 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2064 if (err < 0)
2065 return err;
2066 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2067 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2068 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2069 if (err < 0)
2070 return err;
2071 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2072 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2073 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2074 if (err < 0)
2075 return err;
2076 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2077 snd_pcm_hw_rule_div, NULL,
2078 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2079 if (err < 0)
2080 return err;
2081 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2082 snd_pcm_hw_rule_div, NULL,
2083 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2084 if (err < 0)
2085 return err;
2086 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2087 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2088 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2089 if (err < 0)
2090 return err;
2091 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2092 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2093 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2094 if (err < 0)
2095 return err;
2096 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2097 snd_pcm_hw_rule_mul, NULL,
2098 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2099 if (err < 0)
2100 return err;
2101 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2102 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2103 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2104 if (err < 0)
2105 return err;
2106 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2107 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2108 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2109 if (err < 0)
2110 return err;
2111 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2112 snd_pcm_hw_rule_muldivk, (void*) 8,
2113 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2114 if (err < 0)
2115 return err;
2116 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2117 snd_pcm_hw_rule_muldivk, (void*) 8,
2118 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2119 if (err < 0)
2120 return err;
2121 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2122 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2123 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2124 if (err < 0)
2125 return err;
2126 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2127 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2128 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2129 if (err < 0)
2130 return err;
2131 return 0;
2132}
2133
Takashi Iwai877211f2005-11-17 13:59:38 +01002134int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Takashi Iwai877211f2005-11-17 13:59:38 +01002136 struct snd_pcm_runtime *runtime = substream->runtime;
2137 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 int err;
2139 unsigned int mask = 0;
2140
2141 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2142 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2143 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2144 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002145 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2147 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2148 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2149 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2150 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2151 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2152 }
2153 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
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_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
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_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002162 if (err < 0)
2163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2166 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002167 if (err < 0)
2168 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2171 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002172 if (err < 0)
2173 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2176 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002177 if (err < 0)
2178 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2181 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002182 if (err < 0)
2183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2186 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002187 if (err < 0)
2188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2191 snd_pcm_hw_rule_buffer_bytes_max, substream,
2192 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2193 if (err < 0)
2194 return err;
2195
2196 /* FIXME: remove */
2197 if (runtime->dma_bytes) {
2198 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002199 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202
2203 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2204 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2205 snd_pcm_hw_rule_rate, hw,
2206 SNDRV_PCM_HW_PARAM_RATE, -1);
2207 if (err < 0)
2208 return err;
2209 }
2210
2211 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2213
2214 return 0;
2215}
2216
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002217static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002220}
2221
2222void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2223{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002224 substream->ref_count--;
2225 if (substream->ref_count > 0)
2226 return;
2227
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002228 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002229 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002230 if (substream->ops->hw_free &&
2231 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 substream->ops->hw_free(substream);
2233 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002234 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002236 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2237 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002238 if (substream->pcm_release) {
2239 substream->pcm_release(substream);
2240 substream->pcm_release = NULL;
2241 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002242 snd_pcm_detach_substream(substream);
2243}
2244
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002245EXPORT_SYMBOL(snd_pcm_release_substream);
2246
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002247int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2248 struct file *file,
2249 struct snd_pcm_substream **rsubstream)
2250{
2251 struct snd_pcm_substream *substream;
2252 int err;
2253
2254 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2255 if (err < 0)
2256 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002257 if (substream->ref_count > 1) {
2258 *rsubstream = substream;
2259 return 0;
2260 }
2261
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002262 err = snd_pcm_hw_constraints_init(substream);
2263 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002264 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002265 goto error;
2266 }
2267
2268 if ((err = substream->ops->open(substream)) < 0)
2269 goto error;
2270
2271 substream->hw_opened = 1;
2272
2273 err = snd_pcm_hw_constraints_complete(substream);
2274 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002275 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002276 goto error;
2277 }
2278
2279 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002281
2282 error:
2283 snd_pcm_release_substream(substream);
2284 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002287EXPORT_SYMBOL(snd_pcm_open_substream);
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002290 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002291 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Takashi Iwai877211f2005-11-17 13:59:38 +01002293 struct snd_pcm_file *pcm_file;
2294 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002295 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002297 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2298 if (err < 0)
2299 return err;
2300
Takashi Iwai548a6482006-07-31 16:51:51 +02002301 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2302 if (pcm_file == NULL) {
2303 snd_pcm_release_substream(substream);
2304 return -ENOMEM;
2305 }
2306 pcm_file->substream = substream;
2307 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002308 substream->file = pcm_file;
2309 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return 0;
2314}
2315
Clemens Ladischf87135f2005-11-20 14:06:59 +01002316static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Takashi Iwai877211f2005-11-17 13:59:38 +01002318 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002319 int err = nonseekable_open(inode, file);
2320 if (err < 0)
2321 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002322 pcm = snd_lookup_minor_data(iminor(inode),
2323 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002324 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002325 if (pcm)
2326 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002327 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002328}
2329
2330static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2331{
2332 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002333 int err = nonseekable_open(inode, file);
2334 if (err < 0)
2335 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002336 pcm = snd_lookup_minor_data(iminor(inode),
2337 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002338 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002339 if (pcm)
2340 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002341 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002342}
2343
2344static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2345{
2346 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 wait_queue_t wait;
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (pcm == NULL) {
2350 err = -ENODEV;
2351 goto __error1;
2352 }
2353 err = snd_card_file_add(pcm->card, file);
2354 if (err < 0)
2355 goto __error1;
2356 if (!try_module_get(pcm->card->module)) {
2357 err = -EFAULT;
2358 goto __error2;
2359 }
2360 init_waitqueue_entry(&wait, current);
2361 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002362 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002364 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (err >= 0)
2366 break;
2367 if (err == -EAGAIN) {
2368 if (file->f_flags & O_NONBLOCK) {
2369 err = -EBUSY;
2370 break;
2371 }
2372 } else
2373 break;
2374 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002375 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002377 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002378 if (pcm->card->shutdown) {
2379 err = -ENODEV;
2380 break;
2381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (signal_pending(current)) {
2383 err = -ERESTARTSYS;
2384 break;
2385 }
2386 }
2387 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002388 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (err < 0)
2390 goto __error;
2391 return err;
2392
2393 __error:
2394 module_put(pcm->card->module);
2395 __error2:
2396 snd_card_file_remove(pcm->card, file);
2397 __error1:
2398 return err;
2399}
2400
2401static int snd_pcm_release(struct inode *inode, struct file *file)
2402{
Takashi Iwai877211f2005-11-17 13:59:38 +01002403 struct snd_pcm *pcm;
2404 struct snd_pcm_substream *substream;
2405 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 pcm_file = file->private_data;
2408 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002409 if (snd_BUG_ON(!substream))
2410 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002412 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002413 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002414 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002415 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 wake_up(&pcm->open_wait);
2417 module_put(pcm->card->module);
2418 snd_card_file_remove(pcm->card, file);
2419 return 0;
2420}
2421
Takashi Iwai877211f2005-11-17 13:59:38 +01002422static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2423 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Takashi Iwai877211f2005-11-17 13:59:38 +01002425 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 snd_pcm_sframes_t appl_ptr;
2427 snd_pcm_sframes_t ret;
2428 snd_pcm_sframes_t hw_avail;
2429
2430 if (frames == 0)
2431 return 0;
2432
2433 snd_pcm_stream_lock_irq(substream);
2434 switch (runtime->status->state) {
2435 case SNDRV_PCM_STATE_PREPARED:
2436 break;
2437 case SNDRV_PCM_STATE_DRAINING:
2438 case SNDRV_PCM_STATE_RUNNING:
2439 if (snd_pcm_update_hw_ptr(substream) >= 0)
2440 break;
2441 /* Fall through */
2442 case SNDRV_PCM_STATE_XRUN:
2443 ret = -EPIPE;
2444 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002445 case SNDRV_PCM_STATE_SUSPENDED:
2446 ret = -ESTRPIPE;
2447 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 default:
2449 ret = -EBADFD;
2450 goto __end;
2451 }
2452
2453 hw_avail = snd_pcm_playback_hw_avail(runtime);
2454 if (hw_avail <= 0) {
2455 ret = 0;
2456 goto __end;
2457 }
2458 if (frames > (snd_pcm_uframes_t)hw_avail)
2459 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 appl_ptr = runtime->control->appl_ptr - frames;
2461 if (appl_ptr < 0)
2462 appl_ptr += runtime->boundary;
2463 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 ret = frames;
2465 __end:
2466 snd_pcm_stream_unlock_irq(substream);
2467 return ret;
2468}
2469
Takashi Iwai877211f2005-11-17 13:59:38 +01002470static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2471 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Takashi Iwai877211f2005-11-17 13:59:38 +01002473 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 snd_pcm_sframes_t appl_ptr;
2475 snd_pcm_sframes_t ret;
2476 snd_pcm_sframes_t hw_avail;
2477
2478 if (frames == 0)
2479 return 0;
2480
2481 snd_pcm_stream_lock_irq(substream);
2482 switch (runtime->status->state) {
2483 case SNDRV_PCM_STATE_PREPARED:
2484 case SNDRV_PCM_STATE_DRAINING:
2485 break;
2486 case SNDRV_PCM_STATE_RUNNING:
2487 if (snd_pcm_update_hw_ptr(substream) >= 0)
2488 break;
2489 /* Fall through */
2490 case SNDRV_PCM_STATE_XRUN:
2491 ret = -EPIPE;
2492 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002493 case SNDRV_PCM_STATE_SUSPENDED:
2494 ret = -ESTRPIPE;
2495 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 default:
2497 ret = -EBADFD;
2498 goto __end;
2499 }
2500
2501 hw_avail = snd_pcm_capture_hw_avail(runtime);
2502 if (hw_avail <= 0) {
2503 ret = 0;
2504 goto __end;
2505 }
2506 if (frames > (snd_pcm_uframes_t)hw_avail)
2507 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 appl_ptr = runtime->control->appl_ptr - frames;
2509 if (appl_ptr < 0)
2510 appl_ptr += runtime->boundary;
2511 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 ret = frames;
2513 __end:
2514 snd_pcm_stream_unlock_irq(substream);
2515 return ret;
2516}
2517
Takashi Iwai877211f2005-11-17 13:59:38 +01002518static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2519 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Takashi Iwai877211f2005-11-17 13:59:38 +01002521 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 snd_pcm_sframes_t appl_ptr;
2523 snd_pcm_sframes_t ret;
2524 snd_pcm_sframes_t avail;
2525
2526 if (frames == 0)
2527 return 0;
2528
2529 snd_pcm_stream_lock_irq(substream);
2530 switch (runtime->status->state) {
2531 case SNDRV_PCM_STATE_PREPARED:
2532 case SNDRV_PCM_STATE_PAUSED:
2533 break;
2534 case SNDRV_PCM_STATE_DRAINING:
2535 case SNDRV_PCM_STATE_RUNNING:
2536 if (snd_pcm_update_hw_ptr(substream) >= 0)
2537 break;
2538 /* Fall through */
2539 case SNDRV_PCM_STATE_XRUN:
2540 ret = -EPIPE;
2541 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002542 case SNDRV_PCM_STATE_SUSPENDED:
2543 ret = -ESTRPIPE;
2544 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 default:
2546 ret = -EBADFD;
2547 goto __end;
2548 }
2549
2550 avail = snd_pcm_playback_avail(runtime);
2551 if (avail <= 0) {
2552 ret = 0;
2553 goto __end;
2554 }
2555 if (frames > (snd_pcm_uframes_t)avail)
2556 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 appl_ptr = runtime->control->appl_ptr + frames;
2558 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2559 appl_ptr -= runtime->boundary;
2560 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 ret = frames;
2562 __end:
2563 snd_pcm_stream_unlock_irq(substream);
2564 return ret;
2565}
2566
Takashi Iwai877211f2005-11-17 13:59:38 +01002567static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2568 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
Takashi Iwai877211f2005-11-17 13:59:38 +01002570 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 snd_pcm_sframes_t appl_ptr;
2572 snd_pcm_sframes_t ret;
2573 snd_pcm_sframes_t avail;
2574
2575 if (frames == 0)
2576 return 0;
2577
2578 snd_pcm_stream_lock_irq(substream);
2579 switch (runtime->status->state) {
2580 case SNDRV_PCM_STATE_PREPARED:
2581 case SNDRV_PCM_STATE_DRAINING:
2582 case SNDRV_PCM_STATE_PAUSED:
2583 break;
2584 case SNDRV_PCM_STATE_RUNNING:
2585 if (snd_pcm_update_hw_ptr(substream) >= 0)
2586 break;
2587 /* Fall through */
2588 case SNDRV_PCM_STATE_XRUN:
2589 ret = -EPIPE;
2590 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002591 case SNDRV_PCM_STATE_SUSPENDED:
2592 ret = -ESTRPIPE;
2593 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 default:
2595 ret = -EBADFD;
2596 goto __end;
2597 }
2598
2599 avail = snd_pcm_capture_avail(runtime);
2600 if (avail <= 0) {
2601 ret = 0;
2602 goto __end;
2603 }
2604 if (frames > (snd_pcm_uframes_t)avail)
2605 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 appl_ptr = runtime->control->appl_ptr + frames;
2607 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2608 appl_ptr -= runtime->boundary;
2609 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 ret = frames;
2611 __end:
2612 snd_pcm_stream_unlock_irq(substream);
2613 return ret;
2614}
2615
Takashi Iwai877211f2005-11-17 13:59:38 +01002616static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Takashi Iwai877211f2005-11-17 13:59:38 +01002618 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 int err;
2620
2621 snd_pcm_stream_lock_irq(substream);
2622 switch (runtime->status->state) {
2623 case SNDRV_PCM_STATE_DRAINING:
2624 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2625 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002626 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 case SNDRV_PCM_STATE_RUNNING:
2628 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2629 break;
2630 /* Fall through */
2631 case SNDRV_PCM_STATE_PREPARED:
2632 case SNDRV_PCM_STATE_SUSPENDED:
2633 err = 0;
2634 break;
2635 case SNDRV_PCM_STATE_XRUN:
2636 err = -EPIPE;
2637 break;
2638 default:
2639 __badfd:
2640 err = -EBADFD;
2641 break;
2642 }
2643 snd_pcm_stream_unlock_irq(substream);
2644 return err;
2645}
2646
Takashi Iwai877211f2005-11-17 13:59:38 +01002647static int snd_pcm_delay(struct snd_pcm_substream *substream,
2648 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Takashi Iwai877211f2005-11-17 13:59:38 +01002650 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 int err;
2652 snd_pcm_sframes_t n = 0;
2653
2654 snd_pcm_stream_lock_irq(substream);
2655 switch (runtime->status->state) {
2656 case SNDRV_PCM_STATE_DRAINING:
2657 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2658 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002659 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 case SNDRV_PCM_STATE_RUNNING:
2661 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2662 break;
2663 /* Fall through */
2664 case SNDRV_PCM_STATE_PREPARED:
2665 case SNDRV_PCM_STATE_SUSPENDED:
2666 err = 0;
2667 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2668 n = snd_pcm_playback_hw_avail(runtime);
2669 else
2670 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002671 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 break;
2673 case SNDRV_PCM_STATE_XRUN:
2674 err = -EPIPE;
2675 break;
2676 default:
2677 __badfd:
2678 err = -EBADFD;
2679 break;
2680 }
2681 snd_pcm_stream_unlock_irq(substream);
2682 if (!err)
2683 if (put_user(n, res))
2684 err = -EFAULT;
2685 return err;
2686}
2687
Takashi Iwai877211f2005-11-17 13:59:38 +01002688static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2689 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Takashi Iwai877211f2005-11-17 13:59:38 +01002691 struct snd_pcm_runtime *runtime = substream->runtime;
2692 struct snd_pcm_sync_ptr sync_ptr;
2693 volatile struct snd_pcm_mmap_status *status;
2694 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 int err;
2696
2697 memset(&sync_ptr, 0, sizeof(sync_ptr));
2698 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2699 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002700 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 -07002701 return -EFAULT;
2702 status = runtime->status;
2703 control = runtime->control;
2704 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2705 err = snd_pcm_hwsync(substream);
2706 if (err < 0)
2707 return err;
2708 }
2709 snd_pcm_stream_lock_irq(substream);
2710 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2711 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2712 else
2713 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2714 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2715 control->avail_min = sync_ptr.c.control.avail_min;
2716 else
2717 sync_ptr.c.control.avail_min = control->avail_min;
2718 sync_ptr.s.status.state = status->state;
2719 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2720 sync_ptr.s.status.tstamp = status->tstamp;
2721 sync_ptr.s.status.suspended_state = status->suspended_state;
2722 snd_pcm_stream_unlock_irq(substream);
2723 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2724 return -EFAULT;
2725 return 0;
2726}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002727
2728static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2729{
2730 struct snd_pcm_runtime *runtime = substream->runtime;
2731 int arg;
2732
2733 if (get_user(arg, _arg))
2734 return -EFAULT;
2735 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2736 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002737 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002738 return 0;
2739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Takashi Iwai0df63e42006-04-28 15:13:41 +02002741static int snd_pcm_common_ioctl1(struct file *file,
2742 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 unsigned int cmd, void __user *arg)
2744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 switch (cmd) {
2746 case SNDRV_PCM_IOCTL_PVERSION:
2747 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2748 case SNDRV_PCM_IOCTL_INFO:
2749 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002750 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2751 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002752 case SNDRV_PCM_IOCTL_TTSTAMP:
2753 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 case SNDRV_PCM_IOCTL_HW_REFINE:
2755 return snd_pcm_hw_refine_user(substream, arg);
2756 case SNDRV_PCM_IOCTL_HW_PARAMS:
2757 return snd_pcm_hw_params_user(substream, arg);
2758 case SNDRV_PCM_IOCTL_HW_FREE:
2759 return snd_pcm_hw_free(substream);
2760 case SNDRV_PCM_IOCTL_SW_PARAMS:
2761 return snd_pcm_sw_params_user(substream, arg);
2762 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002763 return snd_pcm_status_user(substream, arg, false);
2764 case SNDRV_PCM_IOCTL_STATUS_EXT:
2765 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2767 return snd_pcm_channel_info_user(substream, arg);
2768 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002769 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 case SNDRV_PCM_IOCTL_RESET:
2771 return snd_pcm_reset(substream);
2772 case SNDRV_PCM_IOCTL_START:
2773 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2774 case SNDRV_PCM_IOCTL_LINK:
2775 return snd_pcm_link(substream, (int)(unsigned long) arg);
2776 case SNDRV_PCM_IOCTL_UNLINK:
2777 return snd_pcm_unlink(substream);
2778 case SNDRV_PCM_IOCTL_RESUME:
2779 return snd_pcm_resume(substream);
2780 case SNDRV_PCM_IOCTL_XRUN:
2781 return snd_pcm_xrun(substream);
2782 case SNDRV_PCM_IOCTL_HWSYNC:
2783 return snd_pcm_hwsync(substream);
2784 case SNDRV_PCM_IOCTL_DELAY:
2785 return snd_pcm_delay(substream, arg);
2786 case SNDRV_PCM_IOCTL_SYNC_PTR:
2787 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002788#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2790 return snd_pcm_hw_refine_old_user(substream, arg);
2791 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2792 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002795 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 case SNDRV_PCM_IOCTL_DROP:
2797 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002798 case SNDRV_PCM_IOCTL_PAUSE:
2799 {
2800 int res;
2801 snd_pcm_stream_lock_irq(substream);
2802 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2803 snd_pcm_stream_unlock_irq(substream);
2804 return res;
2805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002807 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return -ENOTTY;
2809}
2810
Takashi Iwai0df63e42006-04-28 15:13:41 +02002811static int snd_pcm_playback_ioctl1(struct file *file,
2812 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 unsigned int cmd, void __user *arg)
2814{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002815 if (snd_BUG_ON(!substream))
2816 return -ENXIO;
2817 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2818 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 switch (cmd) {
2820 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2821 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002822 struct snd_xferi xferi;
2823 struct snd_xferi __user *_xferi = arg;
2824 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 snd_pcm_sframes_t result;
2826 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2827 return -EBADFD;
2828 if (put_user(0, &_xferi->result))
2829 return -EFAULT;
2830 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2831 return -EFAULT;
2832 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2833 __put_user(result, &_xferi->result);
2834 return result < 0 ? result : 0;
2835 }
2836 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2837 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002838 struct snd_xfern xfern;
2839 struct snd_xfern __user *_xfern = arg;
2840 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 void __user **bufs;
2842 snd_pcm_sframes_t result;
2843 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2844 return -EBADFD;
2845 if (runtime->channels > 128)
2846 return -EINVAL;
2847 if (put_user(0, &_xfern->result))
2848 return -EFAULT;
2849 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2850 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002851
2852 bufs = memdup_user(xfern.bufs,
2853 sizeof(void *) * runtime->channels);
2854 if (IS_ERR(bufs))
2855 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2857 kfree(bufs);
2858 __put_user(result, &_xfern->result);
2859 return result < 0 ? result : 0;
2860 }
2861 case SNDRV_PCM_IOCTL_REWIND:
2862 {
2863 snd_pcm_uframes_t frames;
2864 snd_pcm_uframes_t __user *_frames = arg;
2865 snd_pcm_sframes_t result;
2866 if (get_user(frames, _frames))
2867 return -EFAULT;
2868 if (put_user(0, _frames))
2869 return -EFAULT;
2870 result = snd_pcm_playback_rewind(substream, frames);
2871 __put_user(result, _frames);
2872 return result < 0 ? result : 0;
2873 }
2874 case SNDRV_PCM_IOCTL_FORWARD:
2875 {
2876 snd_pcm_uframes_t frames;
2877 snd_pcm_uframes_t __user *_frames = arg;
2878 snd_pcm_sframes_t result;
2879 if (get_user(frames, _frames))
2880 return -EFAULT;
2881 if (put_user(0, _frames))
2882 return -EFAULT;
2883 result = snd_pcm_playback_forward(substream, frames);
2884 __put_user(result, _frames);
2885 return result < 0 ? result : 0;
2886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002888 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
Takashi Iwai0df63e42006-04-28 15:13:41 +02002891static int snd_pcm_capture_ioctl1(struct file *file,
2892 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 unsigned int cmd, void __user *arg)
2894{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002895 if (snd_BUG_ON(!substream))
2896 return -ENXIO;
2897 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2898 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 switch (cmd) {
2900 case SNDRV_PCM_IOCTL_READI_FRAMES:
2901 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002902 struct snd_xferi xferi;
2903 struct snd_xferi __user *_xferi = arg;
2904 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 snd_pcm_sframes_t result;
2906 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2907 return -EBADFD;
2908 if (put_user(0, &_xferi->result))
2909 return -EFAULT;
2910 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2911 return -EFAULT;
2912 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2913 __put_user(result, &_xferi->result);
2914 return result < 0 ? result : 0;
2915 }
2916 case SNDRV_PCM_IOCTL_READN_FRAMES:
2917 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002918 struct snd_xfern xfern;
2919 struct snd_xfern __user *_xfern = arg;
2920 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 void *bufs;
2922 snd_pcm_sframes_t result;
2923 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2924 return -EBADFD;
2925 if (runtime->channels > 128)
2926 return -EINVAL;
2927 if (put_user(0, &_xfern->result))
2928 return -EFAULT;
2929 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2930 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002931
2932 bufs = memdup_user(xfern.bufs,
2933 sizeof(void *) * runtime->channels);
2934 if (IS_ERR(bufs))
2935 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2937 kfree(bufs);
2938 __put_user(result, &_xfern->result);
2939 return result < 0 ? result : 0;
2940 }
2941 case SNDRV_PCM_IOCTL_REWIND:
2942 {
2943 snd_pcm_uframes_t frames;
2944 snd_pcm_uframes_t __user *_frames = arg;
2945 snd_pcm_sframes_t result;
2946 if (get_user(frames, _frames))
2947 return -EFAULT;
2948 if (put_user(0, _frames))
2949 return -EFAULT;
2950 result = snd_pcm_capture_rewind(substream, frames);
2951 __put_user(result, _frames);
2952 return result < 0 ? result : 0;
2953 }
2954 case SNDRV_PCM_IOCTL_FORWARD:
2955 {
2956 snd_pcm_uframes_t frames;
2957 snd_pcm_uframes_t __user *_frames = arg;
2958 snd_pcm_sframes_t result;
2959 if (get_user(frames, _frames))
2960 return -EFAULT;
2961 if (put_user(0, _frames))
2962 return -EFAULT;
2963 result = snd_pcm_capture_forward(substream, frames);
2964 __put_user(result, _frames);
2965 return result < 0 ? result : 0;
2966 }
2967 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002968 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
Takashi Iwai877211f2005-11-17 13:59:38 +01002971static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2972 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Takashi Iwai877211f2005-11-17 13:59:38 +01002974 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 pcm_file = file->private_data;
2977
2978 if (((cmd >> 8) & 0xff) != 'A')
2979 return -ENOTTY;
2980
Takashi Iwai0df63e42006-04-28 15:13:41 +02002981 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2982 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
Takashi Iwai877211f2005-11-17 13:59:38 +01002985static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2986 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
Takashi Iwai877211f2005-11-17 13:59:38 +01002988 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990 pcm_file = file->private_data;
2991
2992 if (((cmd >> 8) & 0xff) != 'A')
2993 return -ENOTTY;
2994
Takashi Iwai0df63e42006-04-28 15:13:41 +02002995 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2996 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
Takashi Iwai877211f2005-11-17 13:59:38 +01002999int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 unsigned int cmd, void *arg)
3001{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003002 mm_segment_t fs;
3003 int result;
3004
3005 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 switch (substream->stream) {
3007 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003008 result = snd_pcm_playback_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 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003012 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3013 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003014 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003016 result = -EINVAL;
3017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003019 snd_leave_user(fs);
3020 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021}
3022
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003023EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3024
Takashi Iwai877211f2005-11-17 13:59:38 +01003025static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3026 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Takashi Iwai877211f2005-11-17 13:59:38 +01003028 struct snd_pcm_file *pcm_file;
3029 struct snd_pcm_substream *substream;
3030 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 snd_pcm_sframes_t result;
3032
3033 pcm_file = file->private_data;
3034 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003035 if (PCM_RUNTIME_CHECK(substream))
3036 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 runtime = substream->runtime;
3038 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3039 return -EBADFD;
3040 if (!frame_aligned(runtime, count))
3041 return -EINVAL;
3042 count = bytes_to_frames(runtime, count);
3043 result = snd_pcm_lib_read(substream, buf, count);
3044 if (result > 0)
3045 result = frames_to_bytes(runtime, result);
3046 return result;
3047}
3048
Takashi Iwai877211f2005-11-17 13:59:38 +01003049static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3050 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Takashi Iwai877211f2005-11-17 13:59:38 +01003052 struct snd_pcm_file *pcm_file;
3053 struct snd_pcm_substream *substream;
3054 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 snd_pcm_sframes_t result;
3056
3057 pcm_file = file->private_data;
3058 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003059 if (PCM_RUNTIME_CHECK(substream))
3060 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003062 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3063 return -EBADFD;
3064 if (!frame_aligned(runtime, count))
3065 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 count = bytes_to_frames(runtime, count);
3067 result = snd_pcm_lib_write(substream, buf, count);
3068 if (result > 0)
3069 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return result;
3071}
3072
Al Viro1c65d982015-04-04 00:19:32 -04003073static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Takashi Iwai877211f2005-11-17 13:59:38 +01003075 struct snd_pcm_file *pcm_file;
3076 struct snd_pcm_substream *substream;
3077 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 snd_pcm_sframes_t result;
3079 unsigned long i;
3080 void __user **bufs;
3081 snd_pcm_uframes_t frames;
3082
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003083 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003085 if (PCM_RUNTIME_CHECK(substream))
3086 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 runtime = substream->runtime;
3088 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3089 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003090 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003092 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003094 if (!frame_aligned(runtime, to->iov->iov_len))
3095 return -EINVAL;
3096 frames = bytes_to_samples(runtime, to->iov->iov_len);
3097 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 if (bufs == NULL)
3099 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003100 for (i = 0; i < to->nr_segs; ++i)
3101 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 result = snd_pcm_lib_readv(substream, bufs, frames);
3103 if (result > 0)
3104 result = frames_to_bytes(runtime, result);
3105 kfree(bufs);
3106 return result;
3107}
3108
Al Viro1c65d982015-04-04 00:19:32 -04003109static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Takashi Iwai877211f2005-11-17 13:59:38 +01003111 struct snd_pcm_file *pcm_file;
3112 struct snd_pcm_substream *substream;
3113 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 snd_pcm_sframes_t result;
3115 unsigned long i;
3116 void __user **bufs;
3117 snd_pcm_uframes_t frames;
3118
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003119 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003121 if (PCM_RUNTIME_CHECK(substream))
3122 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003124 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3125 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003126 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003127 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003128 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3129 !frame_aligned(runtime, from->iov->iov_len))
3130 return -EINVAL;
3131 frames = bytes_to_samples(runtime, from->iov->iov_len);
3132 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (bufs == NULL)
3134 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003135 for (i = 0; i < from->nr_segs; ++i)
3136 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 result = snd_pcm_lib_writev(substream, bufs, frames);
3138 if (result > 0)
3139 result = frames_to_bytes(runtime, result);
3140 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return result;
3142}
3143
3144static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3145{
Takashi Iwai877211f2005-11-17 13:59:38 +01003146 struct snd_pcm_file *pcm_file;
3147 struct snd_pcm_substream *substream;
3148 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 unsigned int mask;
3150 snd_pcm_uframes_t avail;
3151
3152 pcm_file = file->private_data;
3153
3154 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003155 if (PCM_RUNTIME_CHECK(substream))
3156 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 runtime = substream->runtime;
3158
3159 poll_wait(file, &runtime->sleep, wait);
3160
3161 snd_pcm_stream_lock_irq(substream);
3162 avail = snd_pcm_playback_avail(runtime);
3163 switch (runtime->status->state) {
3164 case SNDRV_PCM_STATE_RUNNING:
3165 case SNDRV_PCM_STATE_PREPARED:
3166 case SNDRV_PCM_STATE_PAUSED:
3167 if (avail >= runtime->control->avail_min) {
3168 mask = POLLOUT | POLLWRNORM;
3169 break;
3170 }
3171 /* Fall through */
3172 case SNDRV_PCM_STATE_DRAINING:
3173 mask = 0;
3174 break;
3175 default:
3176 mask = POLLOUT | POLLWRNORM | POLLERR;
3177 break;
3178 }
3179 snd_pcm_stream_unlock_irq(substream);
3180 return mask;
3181}
3182
3183static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3184{
Takashi Iwai877211f2005-11-17 13:59:38 +01003185 struct snd_pcm_file *pcm_file;
3186 struct snd_pcm_substream *substream;
3187 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 unsigned int mask;
3189 snd_pcm_uframes_t avail;
3190
3191 pcm_file = file->private_data;
3192
3193 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003194 if (PCM_RUNTIME_CHECK(substream))
3195 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 runtime = substream->runtime;
3197
3198 poll_wait(file, &runtime->sleep, wait);
3199
3200 snd_pcm_stream_lock_irq(substream);
3201 avail = snd_pcm_capture_avail(runtime);
3202 switch (runtime->status->state) {
3203 case SNDRV_PCM_STATE_RUNNING:
3204 case SNDRV_PCM_STATE_PREPARED:
3205 case SNDRV_PCM_STATE_PAUSED:
3206 if (avail >= runtime->control->avail_min) {
3207 mask = POLLIN | POLLRDNORM;
3208 break;
3209 }
3210 mask = 0;
3211 break;
3212 case SNDRV_PCM_STATE_DRAINING:
3213 if (avail > 0) {
3214 mask = POLLIN | POLLRDNORM;
3215 break;
3216 }
3217 /* Fall through */
3218 default:
3219 mask = POLLIN | POLLRDNORM | POLLERR;
3220 break;
3221 }
3222 snd_pcm_stream_unlock_irq(substream);
3223 return mask;
3224}
3225
3226/*
3227 * mmap support
3228 */
3229
3230/*
3231 * Only on coherent architectures, we can mmap the status and the control records
3232 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3233 */
3234#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3235/*
3236 * mmap status record
3237 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003238static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3239 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Takashi Iwai877211f2005-11-17 13:59:38 +01003241 struct snd_pcm_substream *substream = area->vm_private_data;
3242 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
3244 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003245 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003247 vmf->page = virt_to_page(runtime->status);
3248 get_page(vmf->page);
3249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003252static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003254 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255};
3256
Takashi Iwai877211f2005-11-17 13:59:38 +01003257static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 struct vm_area_struct *area)
3259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 long size;
3261 if (!(area->vm_flags & VM_READ))
3262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003264 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return -EINVAL;
3266 area->vm_ops = &snd_pcm_vm_ops_status;
3267 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003268 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return 0;
3270}
3271
3272/*
3273 * mmap control record
3274 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003275static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3276 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
Takashi Iwai877211f2005-11-17 13:59:38 +01003278 struct snd_pcm_substream *substream = area->vm_private_data;
3279 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003282 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003284 vmf->page = virt_to_page(runtime->control);
3285 get_page(vmf->page);
3286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003289static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003291 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292};
3293
Takashi Iwai877211f2005-11-17 13:59:38 +01003294static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 struct vm_area_struct *area)
3296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 long size;
3298 if (!(area->vm_flags & VM_READ))
3299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003301 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return -EINVAL;
3303 area->vm_ops = &snd_pcm_vm_ops_control;
3304 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003305 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return 0;
3307}
3308#else /* ! coherent mmap */
3309/*
3310 * don't support mmap for status and control records.
3311 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003312static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct vm_area_struct *area)
3314{
3315 return -ENXIO;
3316}
Takashi Iwai877211f2005-11-17 13:59:38 +01003317static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 struct vm_area_struct *area)
3319{
3320 return -ENXIO;
3321}
3322#endif /* coherent mmap */
3323
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003324static inline struct page *
3325snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3326{
3327 void *vaddr = substream->runtime->dma_area + ofs;
3328 return virt_to_page(vaddr);
3329}
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003332 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003334static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3335 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Takashi Iwai877211f2005-11-17 13:59:38 +01003337 struct snd_pcm_substream *substream = area->vm_private_data;
3338 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 unsigned long offset;
3340 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 size_t dma_bytes;
3342
3343 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003344 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003346 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3348 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003349 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003350 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003352 else
3353 page = snd_pcm_default_page_ops(substream, offset);
3354 if (!page)
3355 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003356 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003357 vmf->page = page;
3358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
Takashi Iwai657b1982009-11-26 12:40:21 +01003361static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3362 .open = snd_pcm_mmap_data_open,
3363 .close = snd_pcm_mmap_data_close,
3364};
3365
3366static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 .open = snd_pcm_mmap_data_open,
3368 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003369 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370};
3371
3372/*
3373 * mmap the DMA buffer on RAM
3374 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003375
3376/**
3377 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3378 * @substream: PCM substream
3379 * @area: VMA
3380 *
3381 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3382 * this function is invoked implicitly.
3383 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003384int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3385 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003387 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003388#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003389 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3390 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3391 return remap_pfn_range(area, area->vm_start,
3392 substream->dma_buffer.addr >> PAGE_SHIFT,
3393 area->vm_end - area->vm_start, area->vm_page_prot);
3394 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003395#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003396#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003397 if (!substream->ops->page &&
3398 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3399 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3400 area,
3401 substream->runtime->dma_area,
3402 substream->runtime->dma_addr,
3403 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003404#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003405 /* mmap with fault handler */
3406 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return 0;
3408}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003409EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
3411/*
3412 * mmap the DMA buffer on I/O memory area
3413 */
3414#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003415/**
3416 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3417 * @substream: PCM substream
3418 * @area: VMA
3419 *
3420 * When your hardware uses the iomapped pages as the hardware buffer and
3421 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3422 * is supposed to work only on limited architectures.
3423 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003424int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3425 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003427 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003430 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003432
3433EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434#endif /* SNDRV_PCM_INFO_MMAP */
3435
3436/*
3437 * mmap DMA buffer
3438 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003439int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 struct vm_area_struct *area)
3441{
Takashi Iwai877211f2005-11-17 13:59:38 +01003442 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 long size;
3444 unsigned long offset;
3445 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003446 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
3448 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3449 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3450 return -EINVAL;
3451 } else {
3452 if (!(area->vm_flags & VM_READ))
3453 return -EINVAL;
3454 }
3455 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3457 return -EBADFD;
3458 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3459 return -ENXIO;
3460 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3461 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3462 return -EINVAL;
3463 size = area->vm_end - area->vm_start;
3464 offset = area->vm_pgoff << PAGE_SHIFT;
3465 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3466 if ((size_t)size > dma_bytes)
3467 return -EINVAL;
3468 if (offset > dma_bytes - size)
3469 return -EINVAL;
3470
Takashi Iwai657b1982009-11-26 12:40:21 +01003471 area->vm_ops = &snd_pcm_vm_ops_data;
3472 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003474 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003476 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003477 if (!err)
3478 atomic_inc(&substream->mmap_count);
3479 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480}
3481
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003482EXPORT_SYMBOL(snd_pcm_mmap_data);
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3485{
Takashi Iwai877211f2005-11-17 13:59:38 +01003486 struct snd_pcm_file * pcm_file;
3487 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 unsigned long offset;
3489
3490 pcm_file = file->private_data;
3491 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003492 if (PCM_RUNTIME_CHECK(substream))
3493 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
3495 offset = area->vm_pgoff << PAGE_SHIFT;
3496 switch (offset) {
3497 case SNDRV_PCM_MMAP_OFFSET_STATUS:
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_status(substream, file, area);
3501 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003502 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return -ENXIO;
3504 return snd_pcm_mmap_control(substream, file, area);
3505 default:
3506 return snd_pcm_mmap_data(substream, file, area);
3507 }
3508 return 0;
3509}
3510
3511static int snd_pcm_fasync(int fd, struct file * file, int on)
3512{
Takashi Iwai877211f2005-11-17 13:59:38 +01003513 struct snd_pcm_file * pcm_file;
3514 struct snd_pcm_substream *substream;
3515 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 pcm_file = file->private_data;
3518 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003519 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003520 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003522 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
3525/*
3526 * ioctl32 compat
3527 */
3528#ifdef CONFIG_COMPAT
3529#include "pcm_compat.c"
3530#else
3531#define snd_pcm_ioctl_compat NULL
3532#endif
3533
3534/*
3535 * To be removed helpers to keep binary compatibility
3536 */
3537
Takashi Iwai59d48582005-12-01 10:51:58 +01003538#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3540#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3541
Takashi Iwai877211f2005-11-17 13:59:38 +01003542static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3543 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
3545 unsigned int i;
3546
3547 memset(params, 0, sizeof(*params));
3548 params->flags = oparams->flags;
3549 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3550 params->masks[i].bits[0] = oparams->masks[i];
3551 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3552 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3553 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3554 params->info = oparams->info;
3555 params->msbits = oparams->msbits;
3556 params->rate_num = oparams->rate_num;
3557 params->rate_den = oparams->rate_den;
3558 params->fifo_size = oparams->fifo_size;
3559}
3560
Takashi Iwai877211f2005-11-17 13:59:38 +01003561static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3562 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
3564 unsigned int i;
3565
3566 memset(oparams, 0, sizeof(*oparams));
3567 oparams->flags = params->flags;
3568 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3569 oparams->masks[i] = params->masks[i].bits[0];
3570 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3571 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3572 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3573 oparams->info = params->info;
3574 oparams->msbits = params->msbits;
3575 oparams->rate_num = params->rate_num;
3576 oparams->rate_den = params->rate_den;
3577 oparams->fifo_size = params->fifo_size;
3578}
3579
Takashi Iwai877211f2005-11-17 13:59:38 +01003580static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3581 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582{
Takashi Iwai877211f2005-11-17 13:59:38 +01003583 struct snd_pcm_hw_params *params;
3584 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 int err;
3586
3587 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003588 if (!params)
3589 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Li Zefanef44a1e2009-04-10 09:43:08 +08003591 oparams = memdup_user(_oparams, sizeof(*oparams));
3592 if (IS_ERR(oparams)) {
3593 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 goto out;
3595 }
3596 snd_pcm_hw_convert_from_old_params(params, oparams);
3597 err = snd_pcm_hw_refine(substream, params);
3598 snd_pcm_hw_convert_to_old_params(oparams, params);
3599 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3600 if (!err)
3601 err = -EFAULT;
3602 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003603
3604 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605out:
3606 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 return err;
3608}
3609
Takashi Iwai877211f2005-11-17 13:59:38 +01003610static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3611 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
Takashi Iwai877211f2005-11-17 13:59:38 +01003613 struct snd_pcm_hw_params *params;
3614 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 int err;
3616
3617 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003618 if (!params)
3619 return -ENOMEM;
3620
3621 oparams = memdup_user(_oparams, sizeof(*oparams));
3622 if (IS_ERR(oparams)) {
3623 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 goto out;
3625 }
3626 snd_pcm_hw_convert_from_old_params(params, oparams);
3627 err = snd_pcm_hw_params(substream, params);
3628 snd_pcm_hw_convert_to_old_params(oparams, params);
3629 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3630 if (!err)
3631 err = -EFAULT;
3632 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003633
3634 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635out:
3636 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 return err;
3638}
Takashi Iwai59d48582005-12-01 10:51:58 +01003639#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Cliff Cai70036092008-09-03 10:54:36 +02003641#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003642static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3643 unsigned long addr,
3644 unsigned long len,
3645 unsigned long pgoff,
3646 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003647{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003648 struct snd_pcm_file *pcm_file = file->private_data;
3649 struct snd_pcm_substream *substream = pcm_file->substream;
3650 struct snd_pcm_runtime *runtime = substream->runtime;
3651 unsigned long offset = pgoff << PAGE_SHIFT;
3652
3653 switch (offset) {
3654 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3655 return (unsigned long)runtime->status;
3656 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3657 return (unsigned long)runtime->control;
3658 default:
3659 return (unsigned long)runtime->dma_area + offset;
3660 }
Cliff Cai70036092008-09-03 10:54:36 +02003661}
3662#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003663# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003664#endif
3665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666/*
3667 * Register section
3668 */
3669
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003670const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003672 .owner = THIS_MODULE,
3673 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003674 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003675 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003676 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003677 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003678 .poll = snd_pcm_playback_poll,
3679 .unlocked_ioctl = snd_pcm_playback_ioctl,
3680 .compat_ioctl = snd_pcm_ioctl_compat,
3681 .mmap = snd_pcm_mmap,
3682 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003683 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 },
3685 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003686 .owner = THIS_MODULE,
3687 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003688 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003689 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003690 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003691 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003692 .poll = snd_pcm_capture_poll,
3693 .unlocked_ioctl = snd_pcm_capture_ioctl,
3694 .compat_ioctl = snd_pcm_ioctl_compat,
3695 .mmap = snd_pcm_mmap,
3696 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003697 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
3699};