blob: 2bde07a4a87fb3283a2b940ebedece1b90dcc3df [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/time.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020028#include <linux/pm_qos.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010029#include <linux/io.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010030#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/timer.h>
37#include <sound/minors.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080038#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Sakamoto2c4842d2017-05-26 09:30:46 +090040#include "pcm_local.h"
41
Takashi Sakamoto37567c52017-06-07 08:46:44 +090042#ifdef CONFIG_SND_DEBUG
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090043#define CREATE_TRACE_POINTS
44#include "pcm_param_trace.h"
Takashi Sakamoto37567c52017-06-07 08:46:44 +090045#else
46#define trace_hw_mask_param_enabled() 0
47#define trace_hw_interval_param_enabled() 0
48#define trace_hw_mask_param(substream, type, index, prev, curr)
49#define trace_hw_interval_param(substream, type, index, prev, curr)
50#endif
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * Compatibility
54 */
55
Takashi Iwai877211f2005-11-17 13:59:38 +010056struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned int flags;
58 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
59 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010060 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
62 unsigned int rmask;
63 unsigned int cmask;
64 unsigned int info;
65 unsigned int msbits;
66 unsigned int rate_num;
67 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010068 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned char reserved[64];
70};
71
Takashi Iwai59d48582005-12-01 10:51:58 +010072#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010073#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
74#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Takashi Iwai877211f2005-11-17 13:59:38 +010076static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
77 struct snd_pcm_hw_params_old __user * _oparams);
78static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
79 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010080#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010081static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 *
85 */
86
Takashi Iwai7af142f2014-09-01 11:19:37 +020087static DEFINE_RWLOCK(snd_pcm_link_rwlock);
88static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwai67ec10722016-02-17 14:30:26 +010090/* Writer in rwsem may block readers even during its waiting in queue,
91 * and this may lead to a deadlock when the code path takes read sem
92 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
93 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
94 * spin until it gets the lock.
95 */
96static inline void down_write_nonblock(struct rw_semaphore *lock)
97{
98 while (!down_write_trylock(lock))
99 cond_resched();
100}
101
Takashi Iwai30b771c2014-10-30 15:02:50 +0100102/**
103 * snd_pcm_stream_lock - Lock the PCM stream
104 * @substream: PCM substream
105 *
106 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
107 * flag of the given substream. This also takes the global link rw lock
108 * (or rw sem), too, for avoiding the race with linked streams.
109 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200110void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
111{
112 if (substream->pcm->nonatomic) {
Takashi Iwai67756e32015-07-17 15:22:33 +0200113 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200114 mutex_lock(&substream->self_group.mutex);
115 } else {
116 read_lock(&snd_pcm_link_rwlock);
117 spin_lock(&substream->self_group.lock);
118 }
119}
120EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
121
Takashi Iwai30b771c2014-10-30 15:02:50 +0100122/**
123 * snd_pcm_stream_lock - Unlock the PCM stream
124 * @substream: PCM substream
125 *
126 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
127 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200128void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
129{
130 if (substream->pcm->nonatomic) {
131 mutex_unlock(&substream->self_group.mutex);
132 up_read(&snd_pcm_link_rwsem);
133 } else {
134 spin_unlock(&substream->self_group.lock);
135 read_unlock(&snd_pcm_link_rwlock);
136 }
137}
138EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
139
Takashi Iwai30b771c2014-10-30 15:02:50 +0100140/**
141 * snd_pcm_stream_lock_irq - Lock the PCM stream
142 * @substream: PCM substream
143 *
144 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
145 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
146 * as snd_pcm_stream_lock().
147 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200148void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
149{
150 if (!substream->pcm->nonatomic)
151 local_irq_disable();
152 snd_pcm_stream_lock(substream);
153}
154EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
155
Takashi Iwai30b771c2014-10-30 15:02:50 +0100156/**
157 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
158 * @substream: PCM substream
159 *
160 * This is a counter-part of snd_pcm_stream_lock_irq().
161 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200162void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
163{
164 snd_pcm_stream_unlock(substream);
165 if (!substream->pcm->nonatomic)
166 local_irq_enable();
167}
168EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
169
170unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
171{
172 unsigned long flags = 0;
173 if (!substream->pcm->nonatomic)
174 local_irq_save(flags);
175 snd_pcm_stream_lock(substream);
176 return flags;
177}
178EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
179
Takashi Iwai30b771c2014-10-30 15:02:50 +0100180/**
181 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
182 * @substream: PCM substream
183 * @flags: irq flags
184 *
185 * This is a counter-part of snd_pcm_stream_lock_irqsave().
186 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200187void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
188 unsigned long flags)
189{
190 snd_pcm_stream_unlock(substream);
191 if (!substream->pcm->nonatomic)
192 local_irq_restore(flags);
193}
194EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Takashi Iwai877211f2005-11-17 13:59:38 +0100196int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Takashi Iwai877211f2005-11-17 13:59:38 +0100198 struct snd_pcm_runtime *runtime;
199 struct snd_pcm *pcm = substream->pcm;
200 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 memset(info, 0, sizeof(*info));
203 info->card = pcm->card->number;
204 info->device = pcm->device;
205 info->stream = substream->stream;
206 info->subdevice = substream->number;
207 strlcpy(info->id, pcm->id, sizeof(info->id));
208 strlcpy(info->name, pcm->name, sizeof(info->name));
209 info->dev_class = pcm->dev_class;
210 info->dev_subclass = pcm->dev_subclass;
211 info->subdevices_count = pstr->substream_count;
212 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
213 strlcpy(info->subname, substream->name, sizeof(info->subname));
214 runtime = substream->runtime;
215 /* AB: FIXME!!! This is definitely nonsense */
216 if (runtime) {
217 info->sync = runtime->sync;
218 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
219 }
220 return 0;
221}
222
Takashi Iwai877211f2005-11-17 13:59:38 +0100223int snd_pcm_info_user(struct snd_pcm_substream *substream,
224 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Takashi Iwai877211f2005-11-17 13:59:38 +0100226 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int err;
228
229 info = kmalloc(sizeof(*info), GFP_KERNEL);
230 if (! info)
231 return -ENOMEM;
232 err = snd_pcm_info(substream, info);
233 if (err >= 0) {
234 if (copy_to_user(_info, info, sizeof(*info)))
235 err = -EFAULT;
236 }
237 kfree(info);
238 return err;
239}
240
Takashi Iwai63825f32014-10-22 12:04:46 +0200241static bool hw_support_mmap(struct snd_pcm_substream *substream)
242{
243 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
244 return false;
245 /* check architectures that return -EINVAL from dma_mmap_coherent() */
246 /* FIXME: this should be some global flag */
247#if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
248 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
249 if (!substream->ops->mmap &&
250 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
251 return false;
252#endif
253 return true;
254}
255
Takashi Iwai877211f2005-11-17 13:59:38 +0100256int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
257 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100260 struct snd_pcm_hardware *hw;
261 struct snd_interval *i = NULL;
262 struct snd_mask *m = NULL;
263 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 unsigned int rstamps[constrs->rules_num];
265 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
266 unsigned int stamp = 2;
267 int changed, again;
268
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900269 struct snd_mask __maybe_unused old_mask;
270 struct snd_interval __maybe_unused old_interval;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 params->info = 0;
273 params->fifo_size = 0;
274 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
275 params->msbits = 0;
276 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
277 params->rate_num = 0;
278 params->rate_den = 0;
279 }
280
281 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
282 m = hw_param_mask(params, k);
283 if (snd_mask_empty(m))
284 return -EINVAL;
285 if (!(params->rmask & (1 << k)))
286 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900287
288 if (trace_hw_mask_param_enabled())
289 old_mask = *m;
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 changed = snd_mask_refine(m, constrs_mask(constrs, k));
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900292
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900293 trace_hw_mask_param(substream, k, 0, &old_mask, m);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (changed)
296 params->cmask |= 1 << k;
297 if (changed < 0)
298 return changed;
299 }
300
301 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
302 i = hw_param_interval(params, k);
303 if (snd_interval_empty(i))
304 return -EINVAL;
305 if (!(params->rmask & (1 << k)))
306 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900307
308 if (trace_hw_interval_param_enabled())
309 old_interval = *i;
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 changed = snd_interval_refine(i, constrs_interval(constrs, k));
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900312
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900313 trace_hw_interval_param(substream, k, 0, &old_interval, i);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (changed)
316 params->cmask |= 1 << k;
317 if (changed < 0)
318 return changed;
319 }
320
321 for (k = 0; k < constrs->rules_num; k++)
322 rstamps[k] = 0;
323 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
324 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
325 do {
326 again = 0;
327 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100328 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned int d;
330 int doit = 0;
331 if (r->cond && !(r->cond & params->flags))
332 continue;
333 for (d = 0; r->deps[d] >= 0; d++) {
334 if (vstamps[r->deps[d]] > rstamps[k]) {
335 doit = 1;
336 break;
337 }
338 }
339 if (!doit)
340 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900341
342 if (trace_hw_mask_param_enabled()) {
343 if (hw_is_mask(r->var))
344 old_mask = *hw_param_mask(params, r->var);
345 }
346 if (trace_hw_interval_param_enabled()) {
347 if (hw_is_interval(r->var))
348 old_interval = *hw_param_interval(params, r->var);
349 }
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 changed = r->func(params, r);
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900352
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900353 if (hw_is_mask(r->var)) {
354 trace_hw_mask_param(substream, r->var, k + 1,
355 &old_mask, hw_param_mask(params, r->var));
356 }
357 if (hw_is_interval(r->var)) {
358 trace_hw_interval_param(substream, r->var, k + 1,
359 &old_interval, hw_param_interval(params, r->var));
360 }
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 rstamps[k] = stamp;
363 if (changed && r->var >= 0) {
364 params->cmask |= (1 << r->var);
365 vstamps[r->var] = stamp;
366 again = 1;
367 }
368 if (changed < 0)
369 return changed;
370 stamp++;
371 }
372 } while (again);
373 if (!params->msbits) {
374 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
375 if (snd_interval_single(i))
376 params->msbits = snd_interval_value(i);
377 }
378
379 if (!params->rate_den) {
380 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
381 if (snd_interval_single(i)) {
382 params->rate_num = snd_interval_value(i);
383 params->rate_den = 1;
384 }
385 }
386
387 hw = &substream->runtime->hw;
Takashi Iwai63825f32014-10-22 12:04:46 +0200388 if (!params->info) {
Libin Yang48d88292014-12-31 22:09:54 +0800389 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
390 SNDRV_PCM_INFO_DRAIN_TRIGGER);
Takashi Iwai63825f32014-10-22 12:04:46 +0200391 if (!hw_support_mmap(substream))
392 params->info &= ~(SNDRV_PCM_INFO_MMAP |
393 SNDRV_PCM_INFO_MMAP_VALID);
394 }
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200395 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100396 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
397 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
398 if (snd_mask_min(m) == snd_mask_max(m) &&
399 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200400 changed = substream->ops->ioctl(substream,
401 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200402 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200403 return changed;
404 }
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 params->rmask = 0;
407 return 0;
408}
409
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200410EXPORT_SYMBOL(snd_pcm_hw_refine);
411
Takashi Iwai877211f2005-11-17 13:59:38 +0100412static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
413 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Takashi Iwai877211f2005-11-17 13:59:38 +0100415 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int err;
417
Li Zefanef44a1e2009-04-10 09:43:08 +0800418 params = memdup_user(_params, sizeof(*params));
419 if (IS_ERR(params))
420 return PTR_ERR(params);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 err = snd_pcm_hw_refine(substream, params);
423 if (copy_to_user(_params, params, sizeof(*params))) {
424 if (!err)
425 err = -EFAULT;
426 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 kfree(params);
429 return err;
430}
431
Takashi Iwai9442e692006-09-30 23:27:19 -0700432static int period_to_usecs(struct snd_pcm_runtime *runtime)
433{
434 int usecs;
435
436 if (! runtime->rate)
437 return -1; /* invalid */
438
439 /* take 75% of period time as the deadline */
440 usecs = (750000 / runtime->rate) * runtime->period_size;
441 usecs += ((750000 % runtime->rate) * runtime->period_size) /
442 runtime->rate;
443
444 return usecs;
445}
446
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200447static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
448{
449 snd_pcm_stream_lock_irq(substream);
450 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
451 substream->runtime->status->state = state;
452 snd_pcm_stream_unlock_irq(substream);
453}
454
Jie Yang90bbaf62015-10-16 17:57:46 +0800455static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
456 int event)
457{
458#ifdef CONFIG_SND_PCM_TIMER
459 if (substream->timer)
460 snd_timer_notify(substream->timer, event,
461 &substream->runtime->trigger_tstamp);
462#endif
463}
464
Takashi Iwai877211f2005-11-17 13:59:38 +0100465static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
466 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Takashi Iwai877211f2005-11-17 13:59:38 +0100468 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700469 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 unsigned int bits;
471 snd_pcm_uframes_t frames;
472
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200473 if (PCM_RUNTIME_CHECK(substream))
474 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 snd_pcm_stream_lock_irq(substream);
477 switch (runtime->status->state) {
478 case SNDRV_PCM_STATE_OPEN:
479 case SNDRV_PCM_STATE_SETUP:
480 case SNDRV_PCM_STATE_PREPARED:
481 break;
482 default:
483 snd_pcm_stream_unlock_irq(substream);
484 return -EBADFD;
485 }
486 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100487#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!substream->oss.oss)
489#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200490 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EBADFD;
492
493 params->rmask = ~0U;
494 err = snd_pcm_hw_refine(substream, params);
495 if (err < 0)
496 goto _error;
497
498 err = snd_pcm_hw_params_choose(substream, params);
499 if (err < 0)
500 goto _error;
501
502 if (substream->ops->hw_params != NULL) {
503 err = substream->ops->hw_params(substream, params);
504 if (err < 0)
505 goto _error;
506 }
507
508 runtime->access = params_access(params);
509 runtime->format = params_format(params);
510 runtime->subformat = params_subformat(params);
511 runtime->channels = params_channels(params);
512 runtime->rate = params_rate(params);
513 runtime->period_size = params_period_size(params);
514 runtime->periods = params_periods(params);
515 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 runtime->info = params->info;
517 runtime->rate_num = params->rate_num;
518 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100519 runtime->no_period_wakeup =
520 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
521 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 bits = snd_pcm_format_physical_width(runtime->format);
524 runtime->sample_bits = bits;
525 bits *= runtime->channels;
526 runtime->frame_bits = bits;
527 frames = 1;
528 while (bits % 8 != 0) {
529 bits *= 2;
530 frames *= 2;
531 }
532 runtime->byte_align = bits / 8;
533 runtime->min_align = frames;
534
535 /* Default sw params */
536 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
537 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 runtime->start_threshold = 1;
540 runtime->stop_threshold = runtime->buffer_size;
541 runtime->silence_threshold = 0;
542 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200543 runtime->boundary = runtime->buffer_size;
544 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
545 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200548 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700549
James Bottomley82f68252010-07-05 22:53:06 +0200550 if (pm_qos_request_active(&substream->latency_pm_qos_req))
551 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700552 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200553 pm_qos_add_request(&substream->latency_pm_qos_req,
554 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 0;
556 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300557 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 so we force application to retry to set
559 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200560 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (substream->ops->hw_free != NULL)
562 substream->ops->hw_free(substream);
563 return err;
564}
565
Takashi Iwai877211f2005-11-17 13:59:38 +0100566static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
567 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Takashi Iwai877211f2005-11-17 13:59:38 +0100569 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int err;
571
Li Zefanef44a1e2009-04-10 09:43:08 +0800572 params = memdup_user(_params, sizeof(*params));
573 if (IS_ERR(params))
574 return PTR_ERR(params);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 err = snd_pcm_hw_params(substream, params);
577 if (copy_to_user(_params, params, sizeof(*params))) {
578 if (!err)
579 err = -EFAULT;
580 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 kfree(params);
583 return err;
584}
585
Takashi Iwai877211f2005-11-17 13:59:38 +0100586static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Takashi Iwai877211f2005-11-17 13:59:38 +0100588 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int result = 0;
590
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200591 if (PCM_RUNTIME_CHECK(substream))
592 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 snd_pcm_stream_lock_irq(substream);
595 switch (runtime->status->state) {
596 case SNDRV_PCM_STATE_SETUP:
597 case SNDRV_PCM_STATE_PREPARED:
598 break;
599 default:
600 snd_pcm_stream_unlock_irq(substream);
601 return -EBADFD;
602 }
603 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200604 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -EBADFD;
606 if (substream->ops->hw_free)
607 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200608 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200609 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return result;
611}
612
Takashi Iwai877211f2005-11-17 13:59:38 +0100613static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
614 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwai877211f2005-11-17 13:59:38 +0100616 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100617 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200619 if (PCM_RUNTIME_CHECK(substream))
620 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 snd_pcm_stream_lock_irq(substream);
623 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
624 snd_pcm_stream_unlock_irq(substream);
625 return -EBADFD;
626 }
627 snd_pcm_stream_unlock_irq(substream);
628
Dan Carpenter145d92e2015-09-23 12:42:28 +0300629 if (params->tstamp_mode < 0 ||
630 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return -EINVAL;
Takashi Iwai58900812014-07-16 17:45:27 +0200632 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
633 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (params->avail_min == 0)
636 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (params->silence_size >= runtime->boundary) {
638 if (params->silence_threshold != 0)
639 return -EINVAL;
640 } else {
641 if (params->silence_size > params->silence_threshold)
642 return -EINVAL;
643 if (params->silence_threshold > runtime->buffer_size)
644 return -EINVAL;
645 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100646 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 snd_pcm_stream_lock_irq(substream);
648 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai58900812014-07-16 17:45:27 +0200649 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
650 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 runtime->period_step = params->period_step;
652 runtime->control->avail_min = params->avail_min;
653 runtime->start_threshold = params->start_threshold;
654 runtime->stop_threshold = params->stop_threshold;
655 runtime->silence_threshold = params->silence_threshold;
656 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 params->boundary = runtime->boundary;
658 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
660 runtime->silence_size > 0)
661 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100662 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Takashi Iwai877211f2005-11-17 13:59:38 +0100668static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
669 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Takashi Iwai877211f2005-11-17 13:59:38 +0100671 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int err;
673 if (copy_from_user(&params, _params, sizeof(params)))
674 return -EFAULT;
675 err = snd_pcm_sw_params(substream, &params);
676 if (copy_to_user(_params, &params, sizeof(params)))
677 return -EFAULT;
678 return err;
679}
680
Takashi Iwai877211f2005-11-17 13:59:38 +0100681int snd_pcm_status(struct snd_pcm_substream *substream,
682 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Takashi Iwai877211f2005-11-17 13:59:38 +0100684 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600687
688 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
689 &runtime->audio_tstamp_config);
690
691 /* backwards compatible behavior */
692 if (runtime->audio_tstamp_config.type_requested ==
693 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
694 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
695 runtime->audio_tstamp_config.type_requested =
696 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
697 else
698 runtime->audio_tstamp_config.type_requested =
699 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
700 runtime->audio_tstamp_report.valid = 0;
701 } else
702 runtime->audio_tstamp_report.valid = 1;
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 status->state = runtime->status->state;
705 status->suspended_state = runtime->status->suspended_state;
706 if (status->state == SNDRV_PCM_STATE_OPEN)
707 goto _end;
708 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100709 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100711 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
712 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600713 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500714 status->audio_tstamp =
715 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600716 if (runtime->audio_tstamp_report.valid == 1)
717 /* backwards compatibility, no report provided in COMPAT mode */
718 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
719 &status->audio_tstamp_accuracy,
720 &runtime->audio_tstamp_report);
721
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100722 goto _tstamp_end;
723 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600724 } else {
725 /* get tstamp only in fallback mode and only if enabled */
726 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
727 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100728 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100729 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 status->appl_ptr = runtime->control->appl_ptr;
731 status->hw_ptr = runtime->status->hw_ptr;
732 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
733 status->avail = snd_pcm_playback_avail(runtime);
734 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200735 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200737 status->delay += runtime->delay;
738 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 status->delay = 0;
740 } else {
741 status->avail = snd_pcm_capture_avail(runtime);
742 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200743 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 else
745 status->delay = 0;
746 }
747 status->avail_max = runtime->avail_max;
748 status->overrange = runtime->overrange;
749 runtime->avail_max = 0;
750 runtime->overrange = 0;
751 _end:
752 snd_pcm_stream_unlock_irq(substream);
753 return 0;
754}
755
Takashi Iwai877211f2005-11-17 13:59:38 +0100756static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600757 struct snd_pcm_status __user * _status,
758 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Takashi Iwai877211f2005-11-17 13:59:38 +0100760 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600764 /*
765 * with extension, parameters are read/write,
766 * get audio_tstamp_data from user,
767 * ignore rest of status structure
768 */
769 if (ext && get_user(status.audio_tstamp_data,
770 (u32 __user *)(&_status->audio_tstamp_data)))
771 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 res = snd_pcm_status(substream, &status);
773 if (res < 0)
774 return res;
775 if (copy_to_user(_status, &status, sizeof(status)))
776 return -EFAULT;
777 return 0;
778}
779
Takashi Iwai877211f2005-11-17 13:59:38 +0100780static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
781 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Takashi Iwai877211f2005-11-17 13:59:38 +0100783 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 unsigned int channel;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 channel = info->channel;
787 runtime = substream->runtime;
788 snd_pcm_stream_lock_irq(substream);
789 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
790 snd_pcm_stream_unlock_irq(substream);
791 return -EBADFD;
792 }
793 snd_pcm_stream_unlock_irq(substream);
794 if (channel >= runtime->channels)
795 return -EINVAL;
796 memset(info, 0, sizeof(*info));
797 info->channel = channel;
798 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
799}
800
Takashi Iwai877211f2005-11-17 13:59:38 +0100801static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
802 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Takashi Iwai877211f2005-11-17 13:59:38 +0100804 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int res;
806
807 if (copy_from_user(&info, _info, sizeof(info)))
808 return -EFAULT;
809 res = snd_pcm_channel_info(substream, &info);
810 if (res < 0)
811 return res;
812 if (copy_to_user(_info, &info, sizeof(info)))
813 return -EFAULT;
814 return 0;
815}
816
Takashi Iwai877211f2005-11-17 13:59:38 +0100817static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Takashi Iwai877211f2005-11-17 13:59:38 +0100819 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (runtime->trigger_master == NULL)
821 return;
822 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600823 if (!runtime->trigger_tstamp_latched)
824 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 } else {
826 snd_pcm_trigger_tstamp(runtime->trigger_master);
827 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
828 }
829 runtime->trigger_master = NULL;
830}
831
832struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100833 int (*pre_action)(struct snd_pcm_substream *substream, int state);
834 int (*do_action)(struct snd_pcm_substream *substream, int state);
835 void (*undo_action)(struct snd_pcm_substream *substream, int state);
836 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837};
838
839/*
840 * this functions is core for handling of linked stream
841 * Note: the stream state might be changed also on failure
842 * Note2: call with calling stream lock + link lock
843 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100844static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100845 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int state, int do_lock)
847{
Takashi Iwai877211f2005-11-17 13:59:38 +0100848 struct snd_pcm_substream *s = NULL;
849 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200850 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Takashi Iwaief991b92007-02-22 12:52:53 +0100852 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200853 if (do_lock && s != substream) {
854 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200855 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200856 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200857 spin_lock_nested(&s->self_group.lock, depth);
858 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 res = ops->pre_action(s, state);
861 if (res < 0)
862 goto _unlock;
863 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100864 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 res = ops->do_action(s, state);
866 if (res < 0) {
867 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100868 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (s1 == s) /* failed stream */
870 break;
871 ops->undo_action(s1, state);
872 }
873 }
874 s = NULL; /* unlock all */
875 goto _unlock;
876 }
877 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100878 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 ops->post_action(s, state);
880 }
881 _unlock:
882 if (do_lock) {
883 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100884 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200885 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200886 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200887 mutex_unlock(&s1->self_group.mutex);
888 else
889 spin_unlock(&s1->self_group.lock);
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (s1 == s) /* end */
892 break;
893 }
894 }
895 return res;
896}
897
898/*
899 * Note: call with stream lock
900 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100901static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100902 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 int state)
904{
905 int res;
906
907 res = ops->pre_action(substream, state);
908 if (res < 0)
909 return res;
910 res = ops->do_action(substream, state);
911 if (res == 0)
912 ops->post_action(substream, state);
913 else if (ops->undo_action)
914 ops->undo_action(substream, state);
915 return res;
916}
917
918/*
919 * Note: call with stream lock
920 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100921static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100922 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int state)
924{
925 int res;
926
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100927 if (!snd_pcm_stream_linked(substream))
928 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200929
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100930 if (substream->pcm->nonatomic) {
931 if (!mutex_trylock(&substream->group->mutex)) {
932 mutex_unlock(&substream->self_group.mutex);
933 mutex_lock(&substream->group->mutex);
934 mutex_lock(&substream->self_group.mutex);
935 }
936 res = snd_pcm_action_group(ops, substream, state, 1);
937 mutex_unlock(&substream->group->mutex);
938 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!spin_trylock(&substream->group->lock)) {
940 spin_unlock(&substream->self_group.lock);
941 spin_lock(&substream->group->lock);
942 spin_lock(&substream->self_group.lock);
943 }
944 res = snd_pcm_action_group(ops, substream, state, 1);
945 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 return res;
948}
949
950/*
951 * Note: don't use any locks before
952 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100953static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100954 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int state)
956{
957 int res;
958
Takashi Iwaie3a4bd52014-10-31 14:45:04 +0100959 snd_pcm_stream_lock_irq(substream);
960 res = snd_pcm_action(ops, substream, state);
961 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return res;
963}
964
965/*
966 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100967static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100968 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int state)
970{
971 int res;
972
973 down_read(&snd_pcm_link_rwsem);
974 if (snd_pcm_stream_linked(substream))
975 res = snd_pcm_action_group(ops, substream, state, 0);
976 else
977 res = snd_pcm_action_single(ops, substream, state);
978 up_read(&snd_pcm_link_rwsem);
979 return res;
980}
981
982/*
983 * start callbacks
984 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100985static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Takashi Iwai877211f2005-11-17 13:59:38 +0100987 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
989 return -EBADFD;
990 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
991 !snd_pcm_playback_data(substream))
992 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600993 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 runtime->trigger_master = substream;
995 return 0;
996}
997
Takashi Iwai877211f2005-11-17 13:59:38 +0100998static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 if (substream->runtime->trigger_master != substream)
1001 return 0;
1002 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1003}
1004
Takashi Iwai877211f2005-11-17 13:59:38 +01001005static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 if (substream->runtime->trigger_master == substream)
1008 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1009}
1010
Takashi Iwai877211f2005-11-17 13:59:38 +01001011static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Takashi Iwai877211f2005-11-17 13:59:38 +01001013 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001015 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001016 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1017 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 runtime->status->state = state;
1019 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1020 runtime->silence_size > 0)
1021 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001022 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Julia Lawallb17154c2015-11-29 16:36:40 +01001025static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 .pre_action = snd_pcm_pre_start,
1027 .do_action = snd_pcm_do_start,
1028 .undo_action = snd_pcm_undo_start,
1029 .post_action = snd_pcm_post_start
1030};
1031
1032/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001033 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001034 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001035 *
1036 * Return: Zero if successful, or a negative error code.
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001037 * The stream lock must be acquired before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001039int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Takashi Iwai877211f2005-11-17 13:59:38 +01001041 return snd_pcm_action(&snd_pcm_action_start, substream,
1042 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001045/* take the stream lock and start the streams */
1046static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1047{
1048 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1049 SNDRV_PCM_STATE_RUNNING);
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
1053 * stop callbacks
1054 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001055static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Takashi Iwai877211f2005-11-17 13:59:38 +01001057 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1059 return -EBADFD;
1060 runtime->trigger_master = substream;
1061 return 0;
1062}
1063
Takashi Iwai877211f2005-11-17 13:59:38 +01001064static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 if (substream->runtime->trigger_master == substream &&
1067 snd_pcm_running(substream))
1068 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1069 return 0; /* unconditonally stop all substreams */
1070}
1071
Takashi Iwai877211f2005-11-17 13:59:38 +01001072static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Takashi Iwai877211f2005-11-17 13:59:38 +01001074 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (runtime->status->state != state) {
1076 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001077 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001078 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001081 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Julia Lawallb17154c2015-11-29 16:36:40 +01001084static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 .pre_action = snd_pcm_pre_stop,
1086 .do_action = snd_pcm_do_stop,
1087 .post_action = snd_pcm_post_stop
1088};
1089
1090/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001091 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001092 * @substream: the PCM substream instance
1093 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001095 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001096 *
Masanari Iida0a114582014-02-09 00:47:36 +09001097 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001099int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1102}
1103
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001104EXPORT_SYMBOL(snd_pcm_stop);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001107 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001108 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001110 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001112 *
1113 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001115int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Takashi Iwai877211f2005-11-17 13:59:38 +01001117 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1118 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Takashi Iwai1fb85102014-11-07 17:08:28 +01001121/**
1122 * snd_pcm_stop_xrun - stop the running streams as XRUN
1123 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001124 *
1125 * This stops the given running substream (and all linked substreams) as XRUN.
1126 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1127 *
1128 * Return: Zero if successful, or a negative error code.
1129 */
1130int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1131{
1132 unsigned long flags;
1133 int ret = 0;
1134
1135 snd_pcm_stream_lock_irqsave(substream, flags);
1136 if (snd_pcm_running(substream))
1137 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1138 snd_pcm_stream_unlock_irqrestore(substream, flags);
1139 return ret;
1140}
1141EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/*
1144 * pause callbacks
1145 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001146static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Takashi Iwai877211f2005-11-17 13:59:38 +01001148 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1150 return -ENOSYS;
1151 if (push) {
1152 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1153 return -EBADFD;
1154 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1155 return -EBADFD;
1156 runtime->trigger_master = substream;
1157 return 0;
1158}
1159
Takashi Iwai877211f2005-11-17 13:59:38 +01001160static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 if (substream->runtime->trigger_master != substream)
1163 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001164 /* some drivers might use hw_ptr to recover from the pause -
1165 update the hw_ptr now */
1166 if (push)
1167 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001168 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001169 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001170 * delta, effectively to skip the check once.
1171 */
1172 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return substream->ops->trigger(substream,
1174 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1175 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1176}
1177
Takashi Iwai877211f2005-11-17 13:59:38 +01001178static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 if (substream->runtime->trigger_master == substream)
1181 substream->ops->trigger(substream,
1182 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1183 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1184}
1185
Takashi Iwai877211f2005-11-17 13:59:38 +01001186static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Takashi Iwai877211f2005-11-17 13:59:38 +01001188 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 snd_pcm_trigger_tstamp(substream);
1190 if (push) {
1191 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001192 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001194 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 } else {
1196 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001197 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199}
1200
Julia Lawallb17154c2015-11-29 16:36:40 +01001201static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 .pre_action = snd_pcm_pre_pause,
1203 .do_action = snd_pcm_do_pause,
1204 .undo_action = snd_pcm_undo_pause,
1205 .post_action = snd_pcm_post_pause
1206};
1207
1208/*
1209 * Push/release the pause for all linked streams.
1210 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001211static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1214}
1215
1216#ifdef CONFIG_PM
1217/* suspend */
1218
Takashi Iwai877211f2005-11-17 13:59:38 +01001219static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Takashi Iwai877211f2005-11-17 13:59:38 +01001221 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1223 return -EBUSY;
1224 runtime->trigger_master = substream;
1225 return 0;
1226}
1227
Takashi Iwai877211f2005-11-17 13:59:38 +01001228static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Takashi Iwai877211f2005-11-17 13:59:38 +01001230 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (runtime->trigger_master != substream)
1232 return 0;
1233 if (! snd_pcm_running(substream))
1234 return 0;
1235 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1236 return 0; /* suspend unconditionally */
1237}
1238
Takashi Iwai877211f2005-11-17 13:59:38 +01001239static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Takashi Iwai877211f2005-11-17 13:59:38 +01001241 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001243 runtime->status->suspended_state = runtime->status->state;
1244 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001245 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001247 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Julia Lawallb17154c2015-11-29 16:36:40 +01001250static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 .pre_action = snd_pcm_pre_suspend,
1252 .do_action = snd_pcm_do_suspend,
1253 .post_action = snd_pcm_post_suspend
1254};
1255
1256/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001257 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001258 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001261 *
1262 * Return: Zero if successful (or @substream is %NULL), or a negative error
1263 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001265int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 int err;
1268 unsigned long flags;
1269
Takashi Iwai603bf522005-11-17 15:59:14 +01001270 if (! substream)
1271 return 0;
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 snd_pcm_stream_lock_irqsave(substream, flags);
1274 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1275 snd_pcm_stream_unlock_irqrestore(substream, flags);
1276 return err;
1277}
1278
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001279EXPORT_SYMBOL(snd_pcm_suspend);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001282 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001283 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001286 *
1287 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001289int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Takashi Iwai877211f2005-11-17 13:59:38 +01001291 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int stream, err = 0;
1293
Takashi Iwai603bf522005-11-17 15:59:14 +01001294 if (! pcm)
1295 return 0;
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001298 for (substream = pcm->streams[stream].substream;
1299 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /* FIXME: the open/close code should lock this as well */
1301 if (substream->runtime == NULL)
1302 continue;
1303 err = snd_pcm_suspend(substream);
1304 if (err < 0 && err != -EBUSY)
1305 return err;
1306 }
1307 }
1308 return 0;
1309}
1310
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001311EXPORT_SYMBOL(snd_pcm_suspend_all);
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313/* resume */
1314
Takashi Iwai877211f2005-11-17 13:59:38 +01001315static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Takashi Iwai877211f2005-11-17 13:59:38 +01001317 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1319 return -ENOSYS;
1320 runtime->trigger_master = substream;
1321 return 0;
1322}
1323
Takashi Iwai877211f2005-11-17 13:59:38 +01001324static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Takashi Iwai877211f2005-11-17 13:59:38 +01001326 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (runtime->trigger_master != substream)
1328 return 0;
1329 /* DMA not running previously? */
1330 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1331 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1332 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1333 return 0;
1334 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1335}
1336
Takashi Iwai877211f2005-11-17 13:59:38 +01001337static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 if (substream->runtime->trigger_master == substream &&
1340 snd_pcm_running(substream))
1341 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1342}
1343
Takashi Iwai877211f2005-11-17 13:59:38 +01001344static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Takashi Iwai877211f2005-11-17 13:59:38 +01001346 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001348 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001349 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Julia Lawallb17154c2015-11-29 16:36:40 +01001352static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 .pre_action = snd_pcm_pre_resume,
1354 .do_action = snd_pcm_do_resume,
1355 .undo_action = snd_pcm_undo_resume,
1356 .post_action = snd_pcm_post_resume
1357};
1358
Takashi Iwai877211f2005-11-17 13:59:38 +01001359static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Takashi Iwai877211f2005-11-17 13:59:38 +01001361 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 int res;
1363
1364 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001365 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1367 snd_power_unlock(card);
1368 return res;
1369}
1370
1371#else
1372
Takashi Iwai877211f2005-11-17 13:59:38 +01001373static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 return -ENOSYS;
1376}
1377
1378#endif /* CONFIG_PM */
1379
1380/*
1381 * xrun ioctl
1382 *
1383 * Change the RUNNING stream(s) to XRUN state.
1384 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001385static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Takashi Iwai877211f2005-11-17 13:59:38 +01001387 struct snd_card *card = substream->pcm->card;
1388 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int result;
1390
1391 snd_power_lock(card);
1392 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001393 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (result < 0)
1395 goto _unlock;
1396 }
1397
1398 snd_pcm_stream_lock_irq(substream);
1399 switch (runtime->status->state) {
1400 case SNDRV_PCM_STATE_XRUN:
1401 result = 0; /* already there */
1402 break;
1403 case SNDRV_PCM_STATE_RUNNING:
1404 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1405 break;
1406 default:
1407 result = -EBADFD;
1408 }
1409 snd_pcm_stream_unlock_irq(substream);
1410 _unlock:
1411 snd_power_unlock(card);
1412 return result;
1413}
1414
1415/*
1416 * reset ioctl
1417 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001418static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Takashi Iwai877211f2005-11-17 13:59:38 +01001420 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 switch (runtime->status->state) {
1422 case SNDRV_PCM_STATE_RUNNING:
1423 case SNDRV_PCM_STATE_PREPARED:
1424 case SNDRV_PCM_STATE_PAUSED:
1425 case SNDRV_PCM_STATE_SUSPENDED:
1426 return 0;
1427 default:
1428 return -EBADFD;
1429 }
1430}
1431
Takashi Iwai877211f2005-11-17 13:59:38 +01001432static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwai877211f2005-11-17 13:59:38 +01001434 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1436 if (err < 0)
1437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001439 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1440 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 runtime->silence_start = runtime->status->hw_ptr;
1442 runtime->silence_filled = 0;
1443 return 0;
1444}
1445
Takashi Iwai877211f2005-11-17 13:59:38 +01001446static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Takashi Iwai877211f2005-11-17 13:59:38 +01001448 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 runtime->control->appl_ptr = runtime->status->hw_ptr;
1450 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1451 runtime->silence_size > 0)
1452 snd_pcm_playback_silence(substream, ULONG_MAX);
1453}
1454
Julia Lawallb17154c2015-11-29 16:36:40 +01001455static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 .pre_action = snd_pcm_pre_reset,
1457 .do_action = snd_pcm_do_reset,
1458 .post_action = snd_pcm_post_reset
1459};
1460
Takashi Iwai877211f2005-11-17 13:59:38 +01001461static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1464}
1465
1466/*
1467 * prepare ioctl
1468 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001469/* we use the second argument for updating f_flags */
1470static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1471 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Takashi Iwai877211f2005-11-17 13:59:38 +01001473 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001474 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1475 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return -EBADFD;
1477 if (snd_pcm_running(substream))
1478 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001479 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481}
1482
Takashi Iwai877211f2005-11-17 13:59:38 +01001483static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 int err;
1486 err = substream->ops->prepare(substream);
1487 if (err < 0)
1488 return err;
1489 return snd_pcm_do_reset(substream, 0);
1490}
1491
Takashi Iwai877211f2005-11-17 13:59:38 +01001492static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Takashi Iwai877211f2005-11-17 13:59:38 +01001494 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001496 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Julia Lawallb17154c2015-11-29 16:36:40 +01001499static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 .pre_action = snd_pcm_pre_prepare,
1501 .do_action = snd_pcm_do_prepare,
1502 .post_action = snd_pcm_post_prepare
1503};
1504
1505/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001506 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001507 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001508 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001509 *
1510 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001512static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1513 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001516 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001517 int f_flags;
1518
1519 if (file)
1520 f_flags = file->f_flags;
1521 else
1522 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001525 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001526 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1527 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 snd_power_unlock(card);
1529 return res;
1530}
1531
1532/*
1533 * drain ioctl
1534 */
1535
Takashi Iwai877211f2005-11-17 13:59:38 +01001536static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001538 struct snd_pcm_runtime *runtime = substream->runtime;
1539 switch (runtime->status->state) {
1540 case SNDRV_PCM_STATE_OPEN:
1541 case SNDRV_PCM_STATE_DISCONNECTED:
1542 case SNDRV_PCM_STATE_SUSPENDED:
1543 return -EBADFD;
1544 }
1545 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return 0;
1547}
1548
Takashi Iwai877211f2005-11-17 13:59:38 +01001549static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Takashi Iwai877211f2005-11-17 13:59:38 +01001551 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1553 switch (runtime->status->state) {
1554 case SNDRV_PCM_STATE_PREPARED:
1555 /* start playback stream if possible */
1556 if (! snd_pcm_playback_empty(substream)) {
1557 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1558 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001559 } else {
1560 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562 break;
1563 case SNDRV_PCM_STATE_RUNNING:
1564 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1565 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001566 case SNDRV_PCM_STATE_XRUN:
1567 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1568 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 default:
1570 break;
1571 }
1572 } else {
1573 /* stop running stream */
1574 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001575 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001577 snd_pcm_do_stop(substream, new_state);
1578 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 }
Libin Yang48d88292014-12-31 22:09:54 +08001581
1582 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1583 runtime->trigger_master == substream &&
1584 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1585 return substream->ops->trigger(substream,
1586 SNDRV_PCM_TRIGGER_DRAIN);
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return 0;
1589}
1590
Takashi Iwai877211f2005-11-17 13:59:38 +01001591static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593}
1594
Julia Lawallb17154c2015-11-29 16:36:40 +01001595static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 .pre_action = snd_pcm_pre_drain_init,
1597 .do_action = snd_pcm_do_drain_init,
1598 .post_action = snd_pcm_post_drain_init
1599};
1600
Takashi Iwai877211f2005-11-17 13:59:38 +01001601static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603/*
1604 * Drain the stream(s).
1605 * When the substream is linked, sync until the draining of all playback streams
1606 * is finished.
1607 * After this call, all streams are supposed to be either SETUP or DRAINING
1608 * (capture only) state.
1609 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001610static int snd_pcm_drain(struct snd_pcm_substream *substream,
1611 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Takashi Iwai877211f2005-11-17 13:59:38 +01001613 struct snd_card *card;
1614 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001615 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001616 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001618 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 card = substream->pcm->card;
1621 runtime = substream->runtime;
1622
1623 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1624 return -EBADFD;
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 snd_power_lock(card);
1627 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001628 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001629 if (result < 0) {
1630 snd_power_unlock(card);
1631 return result;
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001635 if (file) {
1636 if (file->f_flags & O_NONBLOCK)
1637 nonblock = 1;
1638 } else if (substream->f_flags & O_NONBLOCK)
1639 nonblock = 1;
1640
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001641 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001642 snd_pcm_stream_lock_irq(substream);
1643 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001644 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001645 snd_pcm_pause(substream, 0);
1646
1647 /* pre-start/stop - all running streams are changed to DRAINING state */
1648 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001649 if (result < 0)
1650 goto unlock;
1651 /* in non-blocking, we don't wait in ioctl but let caller poll */
1652 if (nonblock) {
1653 result = -EAGAIN;
1654 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 for (;;) {
1658 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001659 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (signal_pending(current)) {
1661 result = -ERESTARTSYS;
1662 break;
1663 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001664 /* find a substream to drain */
1665 to_check = NULL;
1666 snd_pcm_group_for_each_entry(s, substream) {
1667 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1668 continue;
1669 runtime = s->runtime;
1670 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1671 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001672 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001673 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001674 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001675 if (!to_check)
1676 break; /* all drained */
1677 init_waitqueue_entry(&wait, current);
1678 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001680 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001682 if (runtime->no_period_wakeup)
1683 tout = MAX_SCHEDULE_TIMEOUT;
1684 else {
1685 tout = 10;
1686 if (runtime->rate) {
1687 long t = runtime->period_size * 2 / runtime->rate;
1688 tout = max(t, tout);
1689 }
1690 tout = msecs_to_jiffies(tout * 1000);
1691 }
1692 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001694 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001696 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001697 if (card->shutdown) {
1698 result = -ENODEV;
1699 break;
1700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (tout == 0) {
1702 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1703 result = -ESTRPIPE;
1704 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001705 dev_dbg(substream->pcm->card->dev,
1706 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1708 result = -EIO;
1709 }
1710 break;
1711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001713
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001714 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001715 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001716 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 return result;
1720}
1721
1722/*
1723 * drop ioctl
1724 *
1725 * Immediately put all linked substreams into SETUP state.
1726 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001727static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Takashi Iwai877211f2005-11-17 13:59:38 +01001729 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 int result = 0;
1731
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001732 if (PCM_RUNTIME_CHECK(substream))
1733 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001736 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001737 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1738 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -EBADFD;
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 snd_pcm_stream_lock_irq(substream);
1742 /* resume pause */
1743 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1744 snd_pcm_pause(substream, 0);
1745
1746 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1747 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1748 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return result;
1751}
1752
1753
Al Viro0888c322013-06-05 14:09:55 -04001754static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Al Viro0888c322013-06-05 14:09:55 -04001756 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001757 unsigned int minor;
1758
Al Viro0888c322013-06-05 14:09:55 -04001759 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1760 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001762 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1763 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
1766/*
1767 * PCM link handling
1768 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001769static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001772 struct snd_pcm_file *pcm_file;
1773 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001774 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001775 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Al Viro0888c322013-06-05 14:09:55 -04001777 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001779 if (!is_pcm_file(f.file)) {
1780 res = -EBADFD;
1781 goto _badf;
1782 }
1783 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001785 group = kmalloc(sizeof(*group), GFP_KERNEL);
1786 if (!group) {
1787 res = -ENOMEM;
1788 goto _nolock;
1789 }
Takashi Iwai67ec10722016-02-17 14:30:26 +01001790 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 write_lock_irq(&snd_pcm_link_rwlock);
1792 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001793 substream->runtime->status->state != substream1->runtime->status->state ||
1794 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 res = -EBADFD;
1796 goto _end;
1797 }
1798 if (snd_pcm_stream_linked(substream1)) {
1799 res = -EALREADY;
1800 goto _end;
1801 }
1802 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001803 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001804 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001806 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 INIT_LIST_HEAD(&substream->group->substreams);
1808 list_add_tail(&substream->link_list, &substream->group->substreams);
1809 substream->group->count = 1;
1810 }
1811 list_add_tail(&substream1->link_list, &substream->group->substreams);
1812 substream->group->count++;
1813 substream1->group = substream->group;
1814 _end:
1815 write_unlock_irq(&snd_pcm_link_rwlock);
1816 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001817 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001818 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001819 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001820 _badf:
1821 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return res;
1823}
1824
Takashi Iwai877211f2005-11-17 13:59:38 +01001825static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 substream->group = &substream->self_group;
1828 INIT_LIST_HEAD(&substream->self_group.substreams);
1829 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1830}
1831
Takashi Iwai877211f2005-11-17 13:59:38 +01001832static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Takashi Iwaief991b92007-02-22 12:52:53 +01001834 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 int res = 0;
1836
Takashi Iwai67ec10722016-02-17 14:30:26 +01001837 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 write_lock_irq(&snd_pcm_link_rwlock);
1839 if (!snd_pcm_stream_linked(substream)) {
1840 res = -EALREADY;
1841 goto _end;
1842 }
1843 list_del(&substream->link_list);
1844 substream->group->count--;
1845 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001846 snd_pcm_group_for_each_entry(s, substream) {
1847 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 break;
1849 }
1850 kfree(substream->group);
1851 }
1852 relink_to_local(substream);
1853 _end:
1854 write_unlock_irq(&snd_pcm_link_rwlock);
1855 up_write(&snd_pcm_link_rwsem);
1856 return res;
1857}
1858
1859/*
1860 * hw configurator
1861 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001862static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1863 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Takashi Iwai877211f2005-11-17 13:59:38 +01001865 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1867 hw_param_interval_c(params, rule->deps[1]), &t);
1868 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1869}
1870
Takashi Iwai877211f2005-11-17 13:59:38 +01001871static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1872 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Takashi Iwai877211f2005-11-17 13:59:38 +01001874 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1876 hw_param_interval_c(params, rule->deps[1]), &t);
1877 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1878}
1879
Takashi Iwai877211f2005-11-17 13:59:38 +01001880static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1881 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Takashi Iwai877211f2005-11-17 13:59:38 +01001883 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1885 hw_param_interval_c(params, rule->deps[1]),
1886 (unsigned long) rule->private, &t);
1887 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1888}
1889
Takashi Iwai877211f2005-11-17 13:59:38 +01001890static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1891 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Takashi Iwai877211f2005-11-17 13:59:38 +01001893 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1895 (unsigned long) rule->private,
1896 hw_param_interval_c(params, rule->deps[1]), &t);
1897 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1898}
1899
Takashi Iwai877211f2005-11-17 13:59:38 +01001900static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1901 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 unsigned int k;
Takashi Sakamotob55f9fd2017-05-17 08:48:20 +09001904 const struct snd_interval *i =
1905 hw_param_interval_c(params, rule->deps[0]);
Takashi Iwai877211f2005-11-17 13:59:38 +01001906 struct snd_mask m;
1907 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 snd_mask_any(&m);
1909 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1910 int bits;
1911 if (! snd_mask_test(mask, k))
1912 continue;
1913 bits = snd_pcm_format_physical_width(k);
1914 if (bits <= 0)
1915 continue; /* ignore invalid formats */
1916 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1917 snd_mask_reset(&m, k);
1918 }
1919 return snd_mask_refine(mask, &m);
1920}
1921
Takashi Iwai877211f2005-11-17 13:59:38 +01001922static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1923 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Takashi Iwai877211f2005-11-17 13:59:38 +01001925 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 unsigned int k;
1927 t.min = UINT_MAX;
1928 t.max = 0;
1929 t.openmin = 0;
1930 t.openmax = 0;
1931 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1932 int bits;
1933 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1934 continue;
1935 bits = snd_pcm_format_physical_width(k);
1936 if (bits <= 0)
1937 continue; /* ignore invalid formats */
1938 if (t.min > (unsigned)bits)
1939 t.min = bits;
1940 if (t.max < (unsigned)bits)
1941 t.max = bits;
1942 }
1943 t.integer = 1;
1944 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1945}
1946
1947#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1948#error "Change this table"
1949#endif
1950
Takashi Sakamoto8b674302017-05-17 08:48:17 +09001951static const unsigned int rates[] = {
1952 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1953 48000, 64000, 88200, 96000, 176400, 192000
1954};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Clemens Ladisch7653d552007-08-13 17:38:54 +02001956const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1957 .count = ARRAY_SIZE(rates),
1958 .list = rates,
1959};
1960
Takashi Iwai877211f2005-11-17 13:59:38 +01001961static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1962 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Takashi Iwai877211f2005-11-17 13:59:38 +01001964 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001966 snd_pcm_known_rates.count,
1967 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Takashi Iwai877211f2005-11-17 13:59:38 +01001970static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1971 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Takashi Iwai877211f2005-11-17 13:59:38 +01001973 struct snd_interval t;
1974 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 t.min = 0;
1976 t.max = substream->buffer_bytes_max;
1977 t.openmin = 0;
1978 t.openmax = 0;
1979 t.integer = 1;
1980 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1981}
1982
Takashi Iwai877211f2005-11-17 13:59:38 +01001983int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Takashi Iwai877211f2005-11-17 13:59:38 +01001985 struct snd_pcm_runtime *runtime = substream->runtime;
1986 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 int k, err;
1988
1989 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1990 snd_mask_any(constrs_mask(constrs, k));
1991 }
1992
1993 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1994 snd_interval_any(constrs_interval(constrs, k));
1995 }
1996
1997 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1998 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1999 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2000 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2001 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2002
2003 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2004 snd_pcm_hw_rule_format, NULL,
2005 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2006 if (err < 0)
2007 return err;
2008 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2009 snd_pcm_hw_rule_sample_bits, NULL,
2010 SNDRV_PCM_HW_PARAM_FORMAT,
2011 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2012 if (err < 0)
2013 return err;
2014 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2015 snd_pcm_hw_rule_div, NULL,
2016 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2017 if (err < 0)
2018 return err;
2019 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2020 snd_pcm_hw_rule_mul, NULL,
2021 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2022 if (err < 0)
2023 return err;
2024 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2025 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2026 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2027 if (err < 0)
2028 return err;
2029 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2030 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2031 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2032 if (err < 0)
2033 return err;
2034 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2035 snd_pcm_hw_rule_div, NULL,
2036 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2037 if (err < 0)
2038 return err;
2039 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2040 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2041 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2042 if (err < 0)
2043 return err;
2044 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2045 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2046 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2047 if (err < 0)
2048 return err;
2049 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2050 snd_pcm_hw_rule_div, NULL,
2051 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2052 if (err < 0)
2053 return err;
2054 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2055 snd_pcm_hw_rule_div, NULL,
2056 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2057 if (err < 0)
2058 return err;
2059 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2060 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2061 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2062 if (err < 0)
2063 return err;
2064 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2065 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2066 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2067 if (err < 0)
2068 return err;
2069 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2070 snd_pcm_hw_rule_mul, NULL,
2071 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2072 if (err < 0)
2073 return err;
2074 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2075 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2076 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2077 if (err < 0)
2078 return err;
2079 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2080 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2081 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2082 if (err < 0)
2083 return err;
2084 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2085 snd_pcm_hw_rule_muldivk, (void*) 8,
2086 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2087 if (err < 0)
2088 return err;
2089 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2090 snd_pcm_hw_rule_muldivk, (void*) 8,
2091 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2092 if (err < 0)
2093 return err;
2094 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2095 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2096 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2097 if (err < 0)
2098 return err;
2099 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2100 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2101 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2102 if (err < 0)
2103 return err;
2104 return 0;
2105}
2106
Takashi Iwai877211f2005-11-17 13:59:38 +01002107int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Takashi Iwai877211f2005-11-17 13:59:38 +01002109 struct snd_pcm_runtime *runtime = substream->runtime;
2110 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 int err;
2112 unsigned int mask = 0;
2113
2114 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2115 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2116 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2117 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002118 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2120 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2121 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2122 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2123 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2124 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2125 }
2126 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002127 if (err < 0)
2128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002131 if (err < 0)
2132 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002135 if (err < 0)
2136 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2139 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002140 if (err < 0)
2141 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2144 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002145 if (err < 0)
2146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2149 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002150 if (err < 0)
2151 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2154 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002155 if (err < 0)
2156 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2159 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002160 if (err < 0)
2161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2164 snd_pcm_hw_rule_buffer_bytes_max, substream,
2165 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2166 if (err < 0)
2167 return err;
2168
2169 /* FIXME: remove */
2170 if (runtime->dma_bytes) {
2171 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002172 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302173 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175
2176 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2177 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2178 snd_pcm_hw_rule_rate, hw,
2179 SNDRV_PCM_HW_PARAM_RATE, -1);
2180 if (err < 0)
2181 return err;
2182 }
2183
2184 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2186
2187 return 0;
2188}
2189
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002190static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002193}
2194
2195void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2196{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002197 substream->ref_count--;
2198 if (substream->ref_count > 0)
2199 return;
2200
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002201 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002202 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002203 if (substream->ops->hw_free &&
2204 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 substream->ops->hw_free(substream);
2206 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002207 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002209 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2210 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002211 if (substream->pcm_release) {
2212 substream->pcm_release(substream);
2213 substream->pcm_release = NULL;
2214 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002215 snd_pcm_detach_substream(substream);
2216}
2217
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002218EXPORT_SYMBOL(snd_pcm_release_substream);
2219
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002220int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2221 struct file *file,
2222 struct snd_pcm_substream **rsubstream)
2223{
2224 struct snd_pcm_substream *substream;
2225 int err;
2226
2227 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2228 if (err < 0)
2229 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002230 if (substream->ref_count > 1) {
2231 *rsubstream = substream;
2232 return 0;
2233 }
2234
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002235 err = snd_pcm_hw_constraints_init(substream);
2236 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002237 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002238 goto error;
2239 }
2240
2241 if ((err = substream->ops->open(substream)) < 0)
2242 goto error;
2243
2244 substream->hw_opened = 1;
2245
2246 err = snd_pcm_hw_constraints_complete(substream);
2247 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002248 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002249 goto error;
2250 }
2251
2252 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002254
2255 error:
2256 snd_pcm_release_substream(substream);
2257 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002260EXPORT_SYMBOL(snd_pcm_open_substream);
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002263 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002264 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Takashi Iwai877211f2005-11-17 13:59:38 +01002266 struct snd_pcm_file *pcm_file;
2267 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002268 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002270 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2271 if (err < 0)
2272 return err;
2273
Takashi Iwai548a6482006-07-31 16:51:51 +02002274 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2275 if (pcm_file == NULL) {
2276 snd_pcm_release_substream(substream);
2277 return -ENOMEM;
2278 }
2279 pcm_file->substream = substream;
2280 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002281 substream->file = pcm_file;
2282 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return 0;
2287}
2288
Clemens Ladischf87135f2005-11-20 14:06:59 +01002289static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Takashi Iwai877211f2005-11-17 13:59:38 +01002291 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002292 int err = nonseekable_open(inode, file);
2293 if (err < 0)
2294 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002295 pcm = snd_lookup_minor_data(iminor(inode),
2296 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002297 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002298 if (pcm)
2299 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002300 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002301}
2302
2303static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2304{
2305 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002306 int err = nonseekable_open(inode, file);
2307 if (err < 0)
2308 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002309 pcm = snd_lookup_minor_data(iminor(inode),
2310 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002311 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002312 if (pcm)
2313 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002314 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002315}
2316
2317static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2318{
2319 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 wait_queue_t wait;
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (pcm == NULL) {
2323 err = -ENODEV;
2324 goto __error1;
2325 }
2326 err = snd_card_file_add(pcm->card, file);
2327 if (err < 0)
2328 goto __error1;
2329 if (!try_module_get(pcm->card->module)) {
2330 err = -EFAULT;
2331 goto __error2;
2332 }
2333 init_waitqueue_entry(&wait, current);
2334 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002335 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002337 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (err >= 0)
2339 break;
2340 if (err == -EAGAIN) {
2341 if (file->f_flags & O_NONBLOCK) {
2342 err = -EBUSY;
2343 break;
2344 }
2345 } else
2346 break;
2347 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002348 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002350 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002351 if (pcm->card->shutdown) {
2352 err = -ENODEV;
2353 break;
2354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (signal_pending(current)) {
2356 err = -ERESTARTSYS;
2357 break;
2358 }
2359 }
2360 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002361 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (err < 0)
2363 goto __error;
2364 return err;
2365
2366 __error:
2367 module_put(pcm->card->module);
2368 __error2:
2369 snd_card_file_remove(pcm->card, file);
2370 __error1:
2371 return err;
2372}
2373
2374static int snd_pcm_release(struct inode *inode, struct file *file)
2375{
Takashi Iwai877211f2005-11-17 13:59:38 +01002376 struct snd_pcm *pcm;
2377 struct snd_pcm_substream *substream;
2378 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 pcm_file = file->private_data;
2381 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002382 if (snd_BUG_ON(!substream))
2383 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002385 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002386 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002387 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002388 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 wake_up(&pcm->open_wait);
2390 module_put(pcm->card->module);
2391 snd_card_file_remove(pcm->card, file);
2392 return 0;
2393}
2394
Takashi Iwaif839cc12017-05-19 20:04:23 +02002395/* check and update PCM state; return 0 or a negative error
2396 * call this inside PCM lock
2397 */
2398static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2399{
2400 switch (substream->runtime->status->state) {
2401 case SNDRV_PCM_STATE_DRAINING:
2402 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2403 return -EBADFD;
2404 /* Fall through */
2405 case SNDRV_PCM_STATE_RUNNING:
2406 return snd_pcm_update_hw_ptr(substream);
2407 case SNDRV_PCM_STATE_PREPARED:
2408 case SNDRV_PCM_STATE_PAUSED:
2409 return 0;
2410 case SNDRV_PCM_STATE_SUSPENDED:
2411 return -ESTRPIPE;
2412 case SNDRV_PCM_STATE_XRUN:
2413 return -EPIPE;
2414 default:
2415 return -EBADFD;
2416 }
2417}
2418
Takashi Iwai9027c462017-05-19 20:22:33 +02002419/* update to the given appl_ptr and call ack callback if needed;
2420 * when an error is returned, take back to the original value
2421 */
2422static int apply_appl_ptr(struct snd_pcm_substream *substream,
2423 snd_pcm_uframes_t appl_ptr)
2424{
2425 struct snd_pcm_runtime *runtime = substream->runtime;
2426 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2427 int ret;
2428
2429 runtime->control->appl_ptr = appl_ptr;
2430 if (substream->ops->ack) {
2431 ret = substream->ops->ack(substream);
2432 if (ret < 0) {
2433 runtime->control->appl_ptr = old_appl_ptr;
2434 return ret;
2435 }
2436 }
2437 return 0;
2438}
2439
2440/* increase the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002441static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2442 snd_pcm_uframes_t frames,
2443 snd_pcm_sframes_t avail)
2444{
2445 struct snd_pcm_runtime *runtime = substream->runtime;
2446 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002447 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002448
2449 if (avail <= 0)
2450 return 0;
2451 if (frames > (snd_pcm_uframes_t)avail)
2452 frames = avail;
2453 appl_ptr = runtime->control->appl_ptr + frames;
2454 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2455 appl_ptr -= runtime->boundary;
Takashi Iwai9027c462017-05-19 20:22:33 +02002456 ret = apply_appl_ptr(substream, appl_ptr);
2457 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002458}
2459
Takashi Iwai9027c462017-05-19 20:22:33 +02002460/* decrease the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002461static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2462 snd_pcm_uframes_t frames,
2463 snd_pcm_sframes_t avail)
2464{
2465 struct snd_pcm_runtime *runtime = substream->runtime;
2466 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002467 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002468
2469 if (avail <= 0)
2470 return 0;
2471 if (frames > (snd_pcm_uframes_t)avail)
2472 frames = avail;
2473 appl_ptr = runtime->control->appl_ptr - frames;
2474 if (appl_ptr < 0)
2475 appl_ptr += runtime->boundary;
Takashi Iwai9027c462017-05-19 20:22:33 +02002476 ret = apply_appl_ptr(substream, appl_ptr);
2477 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002478}
2479
Takashi Iwai877211f2005-11-17 13:59:38 +01002480static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2481 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
Takashi Iwai877211f2005-11-17 13:59:38 +01002483 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 if (frames == 0)
2487 return 0;
2488
2489 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002490 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002491 if (!ret)
2492 ret = rewind_appl_ptr(substream, frames,
2493 snd_pcm_playback_hw_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 snd_pcm_stream_unlock_irq(substream);
2495 return ret;
2496}
2497
Takashi Iwai877211f2005-11-17 13:59:38 +01002498static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2499 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
Takashi Iwai877211f2005-11-17 13:59:38 +01002501 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 if (frames == 0)
2505 return 0;
2506
2507 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002508 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002509 if (!ret)
2510 ret = rewind_appl_ptr(substream, frames,
2511 snd_pcm_capture_hw_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 snd_pcm_stream_unlock_irq(substream);
2513 return ret;
2514}
2515
Takashi Iwai877211f2005-11-17 13:59:38 +01002516static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2517 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Takashi Iwai877211f2005-11-17 13:59:38 +01002519 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 if (frames == 0)
2523 return 0;
2524
2525 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002526 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002527 if (!ret)
2528 ret = forward_appl_ptr(substream, frames,
2529 snd_pcm_playback_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 snd_pcm_stream_unlock_irq(substream);
2531 return ret;
2532}
2533
Takashi Iwai877211f2005-11-17 13:59:38 +01002534static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2535 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Takashi Iwai877211f2005-11-17 13:59:38 +01002537 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 if (frames == 0)
2541 return 0;
2542
2543 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002544 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002545 if (!ret)
2546 ret = forward_appl_ptr(substream, frames,
2547 snd_pcm_capture_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 snd_pcm_stream_unlock_irq(substream);
2549 return ret;
2550}
2551
Takashi Iwai877211f2005-11-17 13:59:38 +01002552static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 int err;
2555
2556 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002557 err = do_pcm_hwsync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 snd_pcm_stream_unlock_irq(substream);
2559 return err;
2560}
2561
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002562static snd_pcm_sframes_t snd_pcm_delay(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
Takashi Iwai877211f2005-11-17 13:59:38 +01002564 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 int err;
2566 snd_pcm_sframes_t n = 0;
2567
2568 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002569 err = do_pcm_hwsync(substream);
2570 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2572 n = snd_pcm_playback_hw_avail(runtime);
2573 else
2574 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002575 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002578 return err < 0 ? err : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Takashi Iwai877211f2005-11-17 13:59:38 +01002581static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2582 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Takashi Iwai877211f2005-11-17 13:59:38 +01002584 struct snd_pcm_runtime *runtime = substream->runtime;
2585 struct snd_pcm_sync_ptr sync_ptr;
2586 volatile struct snd_pcm_mmap_status *status;
2587 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 int err;
2589
2590 memset(&sync_ptr, 0, sizeof(sync_ptr));
2591 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2592 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002593 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 -07002594 return -EFAULT;
2595 status = runtime->status;
2596 control = runtime->control;
2597 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2598 err = snd_pcm_hwsync(substream);
2599 if (err < 0)
2600 return err;
2601 }
2602 snd_pcm_stream_lock_irq(substream);
Takashi Iwai9027c462017-05-19 20:22:33 +02002603 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2604 err = apply_appl_ptr(substream, sync_ptr.c.control.appl_ptr);
2605 if (err < 0) {
2606 snd_pcm_stream_unlock_irq(substream);
2607 return err;
2608 }
2609 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 sync_ptr.c.control.appl_ptr = control->appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2613 control->avail_min = sync_ptr.c.control.avail_min;
2614 else
2615 sync_ptr.c.control.avail_min = control->avail_min;
2616 sync_ptr.s.status.state = status->state;
2617 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2618 sync_ptr.s.status.tstamp = status->tstamp;
2619 sync_ptr.s.status.suspended_state = status->suspended_state;
2620 snd_pcm_stream_unlock_irq(substream);
2621 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2622 return -EFAULT;
2623 return 0;
2624}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002625
2626static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2627{
2628 struct snd_pcm_runtime *runtime = substream->runtime;
2629 int arg;
2630
2631 if (get_user(arg, _arg))
2632 return -EFAULT;
2633 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2634 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002635 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002636 return 0;
2637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Takashi Iwai0df63e42006-04-28 15:13:41 +02002639static int snd_pcm_common_ioctl1(struct file *file,
2640 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 unsigned int cmd, void __user *arg)
2642{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 switch (cmd) {
2644 case SNDRV_PCM_IOCTL_PVERSION:
2645 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2646 case SNDRV_PCM_IOCTL_INFO:
2647 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002648 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2649 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002650 case SNDRV_PCM_IOCTL_TTSTAMP:
2651 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 case SNDRV_PCM_IOCTL_HW_REFINE:
2653 return snd_pcm_hw_refine_user(substream, arg);
2654 case SNDRV_PCM_IOCTL_HW_PARAMS:
2655 return snd_pcm_hw_params_user(substream, arg);
2656 case SNDRV_PCM_IOCTL_HW_FREE:
2657 return snd_pcm_hw_free(substream);
2658 case SNDRV_PCM_IOCTL_SW_PARAMS:
2659 return snd_pcm_sw_params_user(substream, arg);
2660 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002661 return snd_pcm_status_user(substream, arg, false);
2662 case SNDRV_PCM_IOCTL_STATUS_EXT:
2663 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2665 return snd_pcm_channel_info_user(substream, arg);
2666 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002667 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 case SNDRV_PCM_IOCTL_RESET:
2669 return snd_pcm_reset(substream);
2670 case SNDRV_PCM_IOCTL_START:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002671 return snd_pcm_start_lock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 case SNDRV_PCM_IOCTL_LINK:
2673 return snd_pcm_link(substream, (int)(unsigned long) arg);
2674 case SNDRV_PCM_IOCTL_UNLINK:
2675 return snd_pcm_unlink(substream);
2676 case SNDRV_PCM_IOCTL_RESUME:
2677 return snd_pcm_resume(substream);
2678 case SNDRV_PCM_IOCTL_XRUN:
2679 return snd_pcm_xrun(substream);
2680 case SNDRV_PCM_IOCTL_HWSYNC:
2681 return snd_pcm_hwsync(substream);
2682 case SNDRV_PCM_IOCTL_DELAY:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002683 {
2684 snd_pcm_sframes_t delay = snd_pcm_delay(substream);
2685 snd_pcm_sframes_t __user *res = arg;
2686
2687 if (delay < 0)
2688 return delay;
2689 if (put_user(delay, res))
2690 return -EFAULT;
2691 return 0;
2692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 case SNDRV_PCM_IOCTL_SYNC_PTR:
2694 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002695#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2697 return snd_pcm_hw_refine_old_user(substream, arg);
2698 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2699 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002700#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002702 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 case SNDRV_PCM_IOCTL_DROP:
2704 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002705 case SNDRV_PCM_IOCTL_PAUSE:
2706 {
2707 int res;
2708 snd_pcm_stream_lock_irq(substream);
2709 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2710 snd_pcm_stream_unlock_irq(substream);
2711 return res;
2712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002714 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return -ENOTTY;
2716}
2717
Takashi Iwai0df63e42006-04-28 15:13:41 +02002718static int snd_pcm_playback_ioctl1(struct file *file,
2719 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 unsigned int cmd, void __user *arg)
2721{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002722 if (snd_BUG_ON(!substream))
2723 return -ENXIO;
2724 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2725 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 switch (cmd) {
2727 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2728 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002729 struct snd_xferi xferi;
2730 struct snd_xferi __user *_xferi = arg;
2731 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 snd_pcm_sframes_t result;
2733 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2734 return -EBADFD;
2735 if (put_user(0, &_xferi->result))
2736 return -EFAULT;
2737 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2738 return -EFAULT;
2739 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2740 __put_user(result, &_xferi->result);
2741 return result < 0 ? result : 0;
2742 }
2743 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2744 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002745 struct snd_xfern xfern;
2746 struct snd_xfern __user *_xfern = arg;
2747 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 void __user **bufs;
2749 snd_pcm_sframes_t result;
2750 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2751 return -EBADFD;
2752 if (runtime->channels > 128)
2753 return -EINVAL;
2754 if (put_user(0, &_xfern->result))
2755 return -EFAULT;
2756 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2757 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002758
2759 bufs = memdup_user(xfern.bufs,
2760 sizeof(void *) * runtime->channels);
2761 if (IS_ERR(bufs))
2762 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2764 kfree(bufs);
2765 __put_user(result, &_xfern->result);
2766 return result < 0 ? result : 0;
2767 }
2768 case SNDRV_PCM_IOCTL_REWIND:
2769 {
2770 snd_pcm_uframes_t frames;
2771 snd_pcm_uframes_t __user *_frames = arg;
2772 snd_pcm_sframes_t result;
2773 if (get_user(frames, _frames))
2774 return -EFAULT;
2775 if (put_user(0, _frames))
2776 return -EFAULT;
2777 result = snd_pcm_playback_rewind(substream, frames);
2778 __put_user(result, _frames);
2779 return result < 0 ? result : 0;
2780 }
2781 case SNDRV_PCM_IOCTL_FORWARD:
2782 {
2783 snd_pcm_uframes_t frames;
2784 snd_pcm_uframes_t __user *_frames = arg;
2785 snd_pcm_sframes_t result;
2786 if (get_user(frames, _frames))
2787 return -EFAULT;
2788 if (put_user(0, _frames))
2789 return -EFAULT;
2790 result = snd_pcm_playback_forward(substream, frames);
2791 __put_user(result, _frames);
2792 return result < 0 ? result : 0;
2793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002795 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
Takashi Iwai0df63e42006-04-28 15:13:41 +02002798static int snd_pcm_capture_ioctl1(struct file *file,
2799 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 unsigned int cmd, void __user *arg)
2801{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002802 if (snd_BUG_ON(!substream))
2803 return -ENXIO;
2804 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2805 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 switch (cmd) {
2807 case SNDRV_PCM_IOCTL_READI_FRAMES:
2808 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002809 struct snd_xferi xferi;
2810 struct snd_xferi __user *_xferi = arg;
2811 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 snd_pcm_sframes_t result;
2813 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2814 return -EBADFD;
2815 if (put_user(0, &_xferi->result))
2816 return -EFAULT;
2817 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2818 return -EFAULT;
2819 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2820 __put_user(result, &_xferi->result);
2821 return result < 0 ? result : 0;
2822 }
2823 case SNDRV_PCM_IOCTL_READN_FRAMES:
2824 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002825 struct snd_xfern xfern;
2826 struct snd_xfern __user *_xfern = arg;
2827 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 void *bufs;
2829 snd_pcm_sframes_t result;
2830 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2831 return -EBADFD;
2832 if (runtime->channels > 128)
2833 return -EINVAL;
2834 if (put_user(0, &_xfern->result))
2835 return -EFAULT;
2836 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2837 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002838
2839 bufs = memdup_user(xfern.bufs,
2840 sizeof(void *) * runtime->channels);
2841 if (IS_ERR(bufs))
2842 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2844 kfree(bufs);
2845 __put_user(result, &_xfern->result);
2846 return result < 0 ? result : 0;
2847 }
2848 case SNDRV_PCM_IOCTL_REWIND:
2849 {
2850 snd_pcm_uframes_t frames;
2851 snd_pcm_uframes_t __user *_frames = arg;
2852 snd_pcm_sframes_t result;
2853 if (get_user(frames, _frames))
2854 return -EFAULT;
2855 if (put_user(0, _frames))
2856 return -EFAULT;
2857 result = snd_pcm_capture_rewind(substream, frames);
2858 __put_user(result, _frames);
2859 return result < 0 ? result : 0;
2860 }
2861 case SNDRV_PCM_IOCTL_FORWARD:
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_capture_forward(substream, frames);
2871 __put_user(result, _frames);
2872 return result < 0 ? result : 0;
2873 }
2874 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002875 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
2877
Takashi Iwai877211f2005-11-17 13:59:38 +01002878static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2879 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
Takashi Iwai877211f2005-11-17 13:59:38 +01002881 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 pcm_file = file->private_data;
2884
2885 if (((cmd >> 8) & 0xff) != 'A')
2886 return -ENOTTY;
2887
Takashi Iwai0df63e42006-04-28 15:13:41 +02002888 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2889 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
Takashi Iwai877211f2005-11-17 13:59:38 +01002892static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2893 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Takashi Iwai877211f2005-11-17 13:59:38 +01002895 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 pcm_file = file->private_data;
2898
2899 if (((cmd >> 8) & 0xff) != 'A')
2900 return -ENOTTY;
2901
Takashi Iwai0df63e42006-04-28 15:13:41 +02002902 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2903 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
2905
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002906/**
2907 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
2908 * @substream: PCM substream
2909 * @cmd: IOCTL cmd
2910 * @arg: IOCTL argument
2911 *
2912 * The function is provided primarily for OSS layer and USB gadget drivers,
2913 * and it allows only the limited set of ioctls (hw_params, sw_params,
2914 * prepare, start, drain, drop, forward).
2915 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002916int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 unsigned int cmd, void *arg)
2918{
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002919 snd_pcm_uframes_t *frames = arg;
2920 snd_pcm_sframes_t result;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002921
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002922 switch (cmd) {
2923 case SNDRV_PCM_IOCTL_FORWARD:
2924 {
2925 /* provided only for OSS; capture-only and no value returned */
2926 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
2927 return -EINVAL;
2928 result = snd_pcm_capture_forward(substream, *frames);
2929 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002931 case SNDRV_PCM_IOCTL_HW_PARAMS:
2932 return snd_pcm_hw_params(substream, arg);
2933 case SNDRV_PCM_IOCTL_SW_PARAMS:
2934 return snd_pcm_sw_params(substream, arg);
2935 case SNDRV_PCM_IOCTL_PREPARE:
2936 return snd_pcm_prepare(substream, NULL);
2937 case SNDRV_PCM_IOCTL_START:
2938 return snd_pcm_start_lock_irq(substream);
2939 case SNDRV_PCM_IOCTL_DRAIN:
2940 return snd_pcm_drain(substream, NULL);
2941 case SNDRV_PCM_IOCTL_DROP:
2942 return snd_pcm_drop(substream);
2943 case SNDRV_PCM_IOCTL_DELAY:
2944 {
2945 result = snd_pcm_delay(substream);
2946 if (result < 0)
2947 return result;
2948 *frames = result;
2949 return 0;
2950 }
2951 default:
2952 return -EINVAL;
2953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002955EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2956
Takashi Iwai877211f2005-11-17 13:59:38 +01002957static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2958 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Takashi Iwai877211f2005-11-17 13:59:38 +01002960 struct snd_pcm_file *pcm_file;
2961 struct snd_pcm_substream *substream;
2962 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 snd_pcm_sframes_t result;
2964
2965 pcm_file = file->private_data;
2966 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002967 if (PCM_RUNTIME_CHECK(substream))
2968 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 runtime = substream->runtime;
2970 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2971 return -EBADFD;
2972 if (!frame_aligned(runtime, count))
2973 return -EINVAL;
2974 count = bytes_to_frames(runtime, count);
2975 result = snd_pcm_lib_read(substream, buf, count);
2976 if (result > 0)
2977 result = frames_to_bytes(runtime, result);
2978 return result;
2979}
2980
Takashi Iwai877211f2005-11-17 13:59:38 +01002981static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2982 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
Takashi Iwai877211f2005-11-17 13:59:38 +01002984 struct snd_pcm_file *pcm_file;
2985 struct snd_pcm_substream *substream;
2986 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 snd_pcm_sframes_t result;
2988
2989 pcm_file = file->private_data;
2990 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002991 if (PCM_RUNTIME_CHECK(substream))
2992 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002994 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2995 return -EBADFD;
2996 if (!frame_aligned(runtime, count))
2997 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 count = bytes_to_frames(runtime, count);
2999 result = snd_pcm_lib_write(substream, buf, count);
3000 if (result > 0)
3001 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return result;
3003}
3004
Al Viro1c65d982015-04-04 00:19:32 -04003005static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
Takashi Iwai877211f2005-11-17 13:59:38 +01003007 struct snd_pcm_file *pcm_file;
3008 struct snd_pcm_substream *substream;
3009 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 snd_pcm_sframes_t result;
3011 unsigned long i;
3012 void __user **bufs;
3013 snd_pcm_uframes_t frames;
3014
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003015 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003017 if (PCM_RUNTIME_CHECK(substream))
3018 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 runtime = substream->runtime;
3020 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3021 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003022 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003024 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003026 if (!frame_aligned(runtime, to->iov->iov_len))
3027 return -EINVAL;
3028 frames = bytes_to_samples(runtime, to->iov->iov_len);
3029 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 if (bufs == NULL)
3031 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003032 for (i = 0; i < to->nr_segs; ++i)
3033 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 result = snd_pcm_lib_readv(substream, bufs, frames);
3035 if (result > 0)
3036 result = frames_to_bytes(runtime, result);
3037 kfree(bufs);
3038 return result;
3039}
3040
Al Viro1c65d982015-04-04 00:19:32 -04003041static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Takashi Iwai877211f2005-11-17 13:59:38 +01003043 struct snd_pcm_file *pcm_file;
3044 struct snd_pcm_substream *substream;
3045 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 snd_pcm_sframes_t result;
3047 unsigned long i;
3048 void __user **bufs;
3049 snd_pcm_uframes_t frames;
3050
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003051 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003053 if (PCM_RUNTIME_CHECK(substream))
3054 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003056 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3057 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003058 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003059 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003060 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3061 !frame_aligned(runtime, from->iov->iov_len))
3062 return -EINVAL;
3063 frames = bytes_to_samples(runtime, from->iov->iov_len);
3064 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (bufs == NULL)
3066 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003067 for (i = 0; i < from->nr_segs; ++i)
3068 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 result = snd_pcm_lib_writev(substream, bufs, frames);
3070 if (result > 0)
3071 result = frames_to_bytes(runtime, result);
3072 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return result;
3074}
3075
3076static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3077{
Takashi Iwai877211f2005-11-17 13:59:38 +01003078 struct snd_pcm_file *pcm_file;
3079 struct snd_pcm_substream *substream;
3080 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 unsigned int mask;
3082 snd_pcm_uframes_t avail;
3083
3084 pcm_file = file->private_data;
3085
3086 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003087 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003088 return POLLOUT | POLLWRNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 runtime = substream->runtime;
3090
3091 poll_wait(file, &runtime->sleep, wait);
3092
3093 snd_pcm_stream_lock_irq(substream);
3094 avail = snd_pcm_playback_avail(runtime);
3095 switch (runtime->status->state) {
3096 case SNDRV_PCM_STATE_RUNNING:
3097 case SNDRV_PCM_STATE_PREPARED:
3098 case SNDRV_PCM_STATE_PAUSED:
3099 if (avail >= runtime->control->avail_min) {
3100 mask = POLLOUT | POLLWRNORM;
3101 break;
3102 }
3103 /* Fall through */
3104 case SNDRV_PCM_STATE_DRAINING:
3105 mask = 0;
3106 break;
3107 default:
3108 mask = POLLOUT | POLLWRNORM | POLLERR;
3109 break;
3110 }
3111 snd_pcm_stream_unlock_irq(substream);
3112 return mask;
3113}
3114
3115static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3116{
Takashi Iwai877211f2005-11-17 13:59:38 +01003117 struct snd_pcm_file *pcm_file;
3118 struct snd_pcm_substream *substream;
3119 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 unsigned int mask;
3121 snd_pcm_uframes_t avail;
3122
3123 pcm_file = file->private_data;
3124
3125 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003126 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003127 return POLLIN | POLLRDNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 runtime = substream->runtime;
3129
3130 poll_wait(file, &runtime->sleep, wait);
3131
3132 snd_pcm_stream_lock_irq(substream);
3133 avail = snd_pcm_capture_avail(runtime);
3134 switch (runtime->status->state) {
3135 case SNDRV_PCM_STATE_RUNNING:
3136 case SNDRV_PCM_STATE_PREPARED:
3137 case SNDRV_PCM_STATE_PAUSED:
3138 if (avail >= runtime->control->avail_min) {
3139 mask = POLLIN | POLLRDNORM;
3140 break;
3141 }
3142 mask = 0;
3143 break;
3144 case SNDRV_PCM_STATE_DRAINING:
3145 if (avail > 0) {
3146 mask = POLLIN | POLLRDNORM;
3147 break;
3148 }
3149 /* Fall through */
3150 default:
3151 mask = POLLIN | POLLRDNORM | POLLERR;
3152 break;
3153 }
3154 snd_pcm_stream_unlock_irq(substream);
3155 return mask;
3156}
3157
3158/*
3159 * mmap support
3160 */
3161
3162/*
3163 * Only on coherent architectures, we can mmap the status and the control records
3164 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3165 */
3166#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3167/*
3168 * mmap status record
3169 */
Dave Jiang11bac802017-02-24 14:56:41 -08003170static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
Dave Jiang11bac802017-02-24 14:56:41 -08003172 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003173 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
3175 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003176 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003178 vmf->page = virt_to_page(runtime->status);
3179 get_page(vmf->page);
3180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003183static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003185 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186};
3187
Takashi Iwai877211f2005-11-17 13:59:38 +01003188static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 struct vm_area_struct *area)
3190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 long size;
3192 if (!(area->vm_flags & VM_READ))
3193 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003195 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return -EINVAL;
3197 area->vm_ops = &snd_pcm_vm_ops_status;
3198 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003199 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 return 0;
3201}
3202
3203/*
3204 * mmap control record
3205 */
Dave Jiang11bac802017-02-24 14:56:41 -08003206static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
Dave Jiang11bac802017-02-24 14:56:41 -08003208 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003209 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003212 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003214 vmf->page = virt_to_page(runtime->control);
3215 get_page(vmf->page);
3216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003219static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003221 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222};
3223
Takashi Iwai877211f2005-11-17 13:59:38 +01003224static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 struct vm_area_struct *area)
3226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 long size;
3228 if (!(area->vm_flags & VM_READ))
3229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003231 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return -EINVAL;
3233 area->vm_ops = &snd_pcm_vm_ops_control;
3234 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003235 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return 0;
3237}
3238#else /* ! coherent mmap */
3239/*
3240 * don't support mmap for status and control records.
3241 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003242static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 struct vm_area_struct *area)
3244{
3245 return -ENXIO;
3246}
Takashi Iwai877211f2005-11-17 13:59:38 +01003247static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 struct vm_area_struct *area)
3249{
3250 return -ENXIO;
3251}
3252#endif /* coherent mmap */
3253
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003254static inline struct page *
3255snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3256{
3257 void *vaddr = substream->runtime->dma_area + ofs;
3258 return virt_to_page(vaddr);
3259}
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003262 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 */
Dave Jiang11bac802017-02-24 14:56:41 -08003264static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Dave Jiang11bac802017-02-24 14:56:41 -08003266 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003267 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 unsigned long offset;
3269 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 size_t dma_bytes;
3271
3272 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003273 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003275 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3277 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003278 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003279 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003281 else
3282 page = snd_pcm_default_page_ops(substream, offset);
3283 if (!page)
3284 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003285 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003286 vmf->page = page;
3287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288}
3289
Takashi Iwai657b1982009-11-26 12:40:21 +01003290static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3291 .open = snd_pcm_mmap_data_open,
3292 .close = snd_pcm_mmap_data_close,
3293};
3294
3295static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 .open = snd_pcm_mmap_data_open,
3297 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003298 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299};
3300
3301/*
3302 * mmap the DMA buffer on RAM
3303 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003304
3305/**
3306 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3307 * @substream: PCM substream
3308 * @area: VMA
3309 *
3310 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3311 * this function is invoked implicitly.
3312 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003313int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3314 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003316 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003317#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003318 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3319 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3320 return remap_pfn_range(area, area->vm_start,
3321 substream->dma_buffer.addr >> PAGE_SHIFT,
3322 area->vm_end - area->vm_start, area->vm_page_prot);
3323 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003324#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003325#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003326 if (!substream->ops->page &&
3327 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3328 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3329 area,
3330 substream->runtime->dma_area,
3331 substream->runtime->dma_addr,
3332 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003333#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003334 /* mmap with fault handler */
3335 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return 0;
3337}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003338EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
3340/*
3341 * mmap the DMA buffer on I/O memory area
3342 */
3343#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003344/**
3345 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3346 * @substream: PCM substream
3347 * @area: VMA
3348 *
3349 * When your hardware uses the iomapped pages as the hardware buffer and
3350 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3351 * is supposed to work only on limited architectures.
3352 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003353int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3354 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003356 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003359 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003361
3362EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363#endif /* SNDRV_PCM_INFO_MMAP */
3364
3365/*
3366 * mmap DMA buffer
3367 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003368int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 struct vm_area_struct *area)
3370{
Takashi Iwai877211f2005-11-17 13:59:38 +01003371 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 long size;
3373 unsigned long offset;
3374 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003375 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
3377 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3378 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3379 return -EINVAL;
3380 } else {
3381 if (!(area->vm_flags & VM_READ))
3382 return -EINVAL;
3383 }
3384 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3386 return -EBADFD;
3387 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3388 return -ENXIO;
3389 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3390 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3391 return -EINVAL;
3392 size = area->vm_end - area->vm_start;
3393 offset = area->vm_pgoff << PAGE_SHIFT;
3394 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3395 if ((size_t)size > dma_bytes)
3396 return -EINVAL;
3397 if (offset > dma_bytes - size)
3398 return -EINVAL;
3399
Takashi Iwai657b1982009-11-26 12:40:21 +01003400 area->vm_ops = &snd_pcm_vm_ops_data;
3401 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003403 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003405 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003406 if (!err)
3407 atomic_inc(&substream->mmap_count);
3408 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003411EXPORT_SYMBOL(snd_pcm_mmap_data);
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3414{
Takashi Iwai877211f2005-11-17 13:59:38 +01003415 struct snd_pcm_file * pcm_file;
3416 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 unsigned long offset;
3418
3419 pcm_file = file->private_data;
3420 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003421 if (PCM_RUNTIME_CHECK(substream))
3422 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 offset = area->vm_pgoff << PAGE_SHIFT;
3425 switch (offset) {
3426 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003427 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return -ENXIO;
3429 return snd_pcm_mmap_status(substream, file, area);
3430 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003431 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 return -ENXIO;
3433 return snd_pcm_mmap_control(substream, file, area);
3434 default:
3435 return snd_pcm_mmap_data(substream, file, area);
3436 }
3437 return 0;
3438}
3439
3440static int snd_pcm_fasync(int fd, struct file * file, int on)
3441{
Takashi Iwai877211f2005-11-17 13:59:38 +01003442 struct snd_pcm_file * pcm_file;
3443 struct snd_pcm_substream *substream;
3444 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
3446 pcm_file = file->private_data;
3447 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003448 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003449 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003451 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
3454/*
3455 * ioctl32 compat
3456 */
3457#ifdef CONFIG_COMPAT
3458#include "pcm_compat.c"
3459#else
3460#define snd_pcm_ioctl_compat NULL
3461#endif
3462
3463/*
3464 * To be removed helpers to keep binary compatibility
3465 */
3466
Takashi Iwai59d48582005-12-01 10:51:58 +01003467#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3469#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3470
Takashi Iwai877211f2005-11-17 13:59:38 +01003471static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3472 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
3474 unsigned int i;
3475
3476 memset(params, 0, sizeof(*params));
3477 params->flags = oparams->flags;
3478 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3479 params->masks[i].bits[0] = oparams->masks[i];
3480 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3481 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3482 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3483 params->info = oparams->info;
3484 params->msbits = oparams->msbits;
3485 params->rate_num = oparams->rate_num;
3486 params->rate_den = oparams->rate_den;
3487 params->fifo_size = oparams->fifo_size;
3488}
3489
Takashi Iwai877211f2005-11-17 13:59:38 +01003490static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3491 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
3493 unsigned int i;
3494
3495 memset(oparams, 0, sizeof(*oparams));
3496 oparams->flags = params->flags;
3497 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3498 oparams->masks[i] = params->masks[i].bits[0];
3499 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3500 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3501 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3502 oparams->info = params->info;
3503 oparams->msbits = params->msbits;
3504 oparams->rate_num = params->rate_num;
3505 oparams->rate_den = params->rate_den;
3506 oparams->fifo_size = params->fifo_size;
3507}
3508
Takashi Iwai877211f2005-11-17 13:59:38 +01003509static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3510 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
Takashi Iwai877211f2005-11-17 13:59:38 +01003512 struct snd_pcm_hw_params *params;
3513 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 int err;
3515
3516 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003517 if (!params)
3518 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Li Zefanef44a1e2009-04-10 09:43:08 +08003520 oparams = memdup_user(_oparams, sizeof(*oparams));
3521 if (IS_ERR(oparams)) {
3522 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 goto out;
3524 }
3525 snd_pcm_hw_convert_from_old_params(params, oparams);
3526 err = snd_pcm_hw_refine(substream, params);
3527 snd_pcm_hw_convert_to_old_params(oparams, params);
3528 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3529 if (!err)
3530 err = -EFAULT;
3531 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003532
3533 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534out:
3535 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return err;
3537}
3538
Takashi Iwai877211f2005-11-17 13:59:38 +01003539static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3540 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
Takashi Iwai877211f2005-11-17 13:59:38 +01003542 struct snd_pcm_hw_params *params;
3543 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 int err;
3545
3546 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003547 if (!params)
3548 return -ENOMEM;
3549
3550 oparams = memdup_user(_oparams, sizeof(*oparams));
3551 if (IS_ERR(oparams)) {
3552 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 goto out;
3554 }
3555 snd_pcm_hw_convert_from_old_params(params, oparams);
3556 err = snd_pcm_hw_params(substream, params);
3557 snd_pcm_hw_convert_to_old_params(oparams, params);
3558 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3559 if (!err)
3560 err = -EFAULT;
3561 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003562
3563 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564out:
3565 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 return err;
3567}
Takashi Iwai59d48582005-12-01 10:51:58 +01003568#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Cliff Cai70036092008-09-03 10:54:36 +02003570#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003571static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3572 unsigned long addr,
3573 unsigned long len,
3574 unsigned long pgoff,
3575 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003576{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003577 struct snd_pcm_file *pcm_file = file->private_data;
3578 struct snd_pcm_substream *substream = pcm_file->substream;
3579 struct snd_pcm_runtime *runtime = substream->runtime;
3580 unsigned long offset = pgoff << PAGE_SHIFT;
3581
3582 switch (offset) {
3583 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3584 return (unsigned long)runtime->status;
3585 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3586 return (unsigned long)runtime->control;
3587 default:
3588 return (unsigned long)runtime->dma_area + offset;
3589 }
Cliff Cai70036092008-09-03 10:54:36 +02003590}
3591#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003592# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003593#endif
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595/*
3596 * Register section
3597 */
3598
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003599const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003601 .owner = THIS_MODULE,
3602 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003603 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003604 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003605 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003606 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003607 .poll = snd_pcm_playback_poll,
3608 .unlocked_ioctl = snd_pcm_playback_ioctl,
3609 .compat_ioctl = snd_pcm_ioctl_compat,
3610 .mmap = snd_pcm_mmap,
3611 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003612 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 },
3614 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003615 .owner = THIS_MODULE,
3616 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003617 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003618 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003619 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003620 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003621 .poll = snd_pcm_capture_poll,
3622 .unlocked_ioctl = snd_pcm_capture_ioctl,
3623 .compat_ioctl = snd_pcm_ioctl_compat,
3624 .mmap = snd_pcm_mmap,
3625 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003626 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
3628};