blob: 1dee5f960fbe410f8a0b47b5473b29d4f019cd4b [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 Sakamoto561e1ca2017-06-09 06:36:59 +0900256static int constrain_mask_params(struct snd_pcm_substream *substream,
257 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900259 struct snd_pcm_hw_constraints *constrs =
260 &substream->runtime->hw_constraints;
261 struct snd_mask *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int k;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900263 struct snd_mask old_mask;
264 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
267 m = hw_param_mask(params, k);
268 if (snd_mask_empty(m))
269 return -EINVAL;
270 if (!(params->rmask & (1 << k)))
271 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900272
273 if (trace_hw_mask_param_enabled())
274 old_mask = *m;
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 changed = snd_mask_refine(m, constrs_mask(constrs, k));
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900277
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900278 trace_hw_mask_param(substream, k, 0, &old_mask, m);
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (changed)
281 params->cmask |= 1 << k;
282 if (changed < 0)
283 return changed;
284 }
285
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900286 return 0;
287}
288
289int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
290 struct snd_pcm_hw_params *params)
291{
292 unsigned int k;
293 struct snd_pcm_hardware *hw;
294 struct snd_interval *i = NULL;
295 struct snd_mask *m = NULL;
296 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
297 unsigned int rstamps[constrs->rules_num];
298 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
299 unsigned int stamp = 2;
300 int changed, again;
301 int err;
302
303 struct snd_mask __maybe_unused old_mask;
304 struct snd_interval __maybe_unused old_interval;
305
306 params->info = 0;
307 params->fifo_size = 0;
308 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
309 params->msbits = 0;
310 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
311 params->rate_num = 0;
312 params->rate_den = 0;
313 }
314
315 err = constrain_mask_params(substream, params);
316 if (err < 0)
317 return err;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
320 i = hw_param_interval(params, k);
321 if (snd_interval_empty(i))
322 return -EINVAL;
323 if (!(params->rmask & (1 << k)))
324 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900325
326 if (trace_hw_interval_param_enabled())
327 old_interval = *i;
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 changed = snd_interval_refine(i, constrs_interval(constrs, k));
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900330
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900331 trace_hw_interval_param(substream, k, 0, &old_interval, i);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (changed)
334 params->cmask |= 1 << k;
335 if (changed < 0)
336 return changed;
337 }
338
339 for (k = 0; k < constrs->rules_num; k++)
340 rstamps[k] = 0;
341 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
342 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
343 do {
344 again = 0;
345 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100346 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 unsigned int d;
348 int doit = 0;
349 if (r->cond && !(r->cond & params->flags))
350 continue;
351 for (d = 0; r->deps[d] >= 0; d++) {
352 if (vstamps[r->deps[d]] > rstamps[k]) {
353 doit = 1;
354 break;
355 }
356 }
357 if (!doit)
358 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900359
360 if (trace_hw_mask_param_enabled()) {
361 if (hw_is_mask(r->var))
362 old_mask = *hw_param_mask(params, r->var);
363 }
364 if (trace_hw_interval_param_enabled()) {
365 if (hw_is_interval(r->var))
366 old_interval = *hw_param_interval(params, r->var);
367 }
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 changed = r->func(params, r);
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900370
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900371 if (hw_is_mask(r->var)) {
372 trace_hw_mask_param(substream, r->var, k + 1,
373 &old_mask, hw_param_mask(params, r->var));
374 }
375 if (hw_is_interval(r->var)) {
376 trace_hw_interval_param(substream, r->var, k + 1,
377 &old_interval, hw_param_interval(params, r->var));
378 }
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 rstamps[k] = stamp;
381 if (changed && r->var >= 0) {
382 params->cmask |= (1 << r->var);
383 vstamps[r->var] = stamp;
384 again = 1;
385 }
386 if (changed < 0)
387 return changed;
388 stamp++;
389 }
390 } while (again);
391 if (!params->msbits) {
392 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
393 if (snd_interval_single(i))
394 params->msbits = snd_interval_value(i);
395 }
396
397 if (!params->rate_den) {
398 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
399 if (snd_interval_single(i)) {
400 params->rate_num = snd_interval_value(i);
401 params->rate_den = 1;
402 }
403 }
404
405 hw = &substream->runtime->hw;
Takashi Iwai63825f32014-10-22 12:04:46 +0200406 if (!params->info) {
Libin Yang48d88292014-12-31 22:09:54 +0800407 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
408 SNDRV_PCM_INFO_DRAIN_TRIGGER);
Takashi Iwai63825f32014-10-22 12:04:46 +0200409 if (!hw_support_mmap(substream))
410 params->info &= ~(SNDRV_PCM_INFO_MMAP |
411 SNDRV_PCM_INFO_MMAP_VALID);
412 }
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200413 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100414 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
415 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
416 if (snd_mask_min(m) == snd_mask_max(m) &&
417 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200418 changed = substream->ops->ioctl(substream,
419 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200420 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200421 return changed;
422 }
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 params->rmask = 0;
425 return 0;
426}
427
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200428EXPORT_SYMBOL(snd_pcm_hw_refine);
429
Takashi Iwai877211f2005-11-17 13:59:38 +0100430static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
431 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Takashi Iwai877211f2005-11-17 13:59:38 +0100433 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int err;
435
Li Zefanef44a1e2009-04-10 09:43:08 +0800436 params = memdup_user(_params, sizeof(*params));
437 if (IS_ERR(params))
438 return PTR_ERR(params);
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 err = snd_pcm_hw_refine(substream, params);
441 if (copy_to_user(_params, params, sizeof(*params))) {
442 if (!err)
443 err = -EFAULT;
444 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 kfree(params);
447 return err;
448}
449
Takashi Iwai9442e692006-09-30 23:27:19 -0700450static int period_to_usecs(struct snd_pcm_runtime *runtime)
451{
452 int usecs;
453
454 if (! runtime->rate)
455 return -1; /* invalid */
456
457 /* take 75% of period time as the deadline */
458 usecs = (750000 / runtime->rate) * runtime->period_size;
459 usecs += ((750000 % runtime->rate) * runtime->period_size) /
460 runtime->rate;
461
462 return usecs;
463}
464
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200465static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
466{
467 snd_pcm_stream_lock_irq(substream);
468 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
469 substream->runtime->status->state = state;
470 snd_pcm_stream_unlock_irq(substream);
471}
472
Jie Yang90bbaf62015-10-16 17:57:46 +0800473static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
474 int event)
475{
476#ifdef CONFIG_SND_PCM_TIMER
477 if (substream->timer)
478 snd_timer_notify(substream->timer, event,
479 &substream->runtime->trigger_tstamp);
480#endif
481}
482
Takashi Iwai877211f2005-11-17 13:59:38 +0100483static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
484 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Takashi Iwai877211f2005-11-17 13:59:38 +0100486 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700487 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 unsigned int bits;
489 snd_pcm_uframes_t frames;
490
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200491 if (PCM_RUNTIME_CHECK(substream))
492 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 snd_pcm_stream_lock_irq(substream);
495 switch (runtime->status->state) {
496 case SNDRV_PCM_STATE_OPEN:
497 case SNDRV_PCM_STATE_SETUP:
498 case SNDRV_PCM_STATE_PREPARED:
499 break;
500 default:
501 snd_pcm_stream_unlock_irq(substream);
502 return -EBADFD;
503 }
504 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100505#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!substream->oss.oss)
507#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200508 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return -EBADFD;
510
511 params->rmask = ~0U;
512 err = snd_pcm_hw_refine(substream, params);
513 if (err < 0)
514 goto _error;
515
516 err = snd_pcm_hw_params_choose(substream, params);
517 if (err < 0)
518 goto _error;
519
520 if (substream->ops->hw_params != NULL) {
521 err = substream->ops->hw_params(substream, params);
522 if (err < 0)
523 goto _error;
524 }
525
526 runtime->access = params_access(params);
527 runtime->format = params_format(params);
528 runtime->subformat = params_subformat(params);
529 runtime->channels = params_channels(params);
530 runtime->rate = params_rate(params);
531 runtime->period_size = params_period_size(params);
532 runtime->periods = params_periods(params);
533 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 runtime->info = params->info;
535 runtime->rate_num = params->rate_num;
536 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100537 runtime->no_period_wakeup =
538 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
539 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 bits = snd_pcm_format_physical_width(runtime->format);
542 runtime->sample_bits = bits;
543 bits *= runtime->channels;
544 runtime->frame_bits = bits;
545 frames = 1;
546 while (bits % 8 != 0) {
547 bits *= 2;
548 frames *= 2;
549 }
550 runtime->byte_align = bits / 8;
551 runtime->min_align = frames;
552
553 /* Default sw params */
554 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
555 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 runtime->start_threshold = 1;
558 runtime->stop_threshold = runtime->buffer_size;
559 runtime->silence_threshold = 0;
560 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200561 runtime->boundary = runtime->buffer_size;
562 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
563 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200566 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700567
James Bottomley82f68252010-07-05 22:53:06 +0200568 if (pm_qos_request_active(&substream->latency_pm_qos_req))
569 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700570 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200571 pm_qos_add_request(&substream->latency_pm_qos_req,
572 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return 0;
574 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300575 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 so we force application to retry to set
577 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200578 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (substream->ops->hw_free != NULL)
580 substream->ops->hw_free(substream);
581 return err;
582}
583
Takashi Iwai877211f2005-11-17 13:59:38 +0100584static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
585 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Takashi Iwai877211f2005-11-17 13:59:38 +0100587 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int err;
589
Li Zefanef44a1e2009-04-10 09:43:08 +0800590 params = memdup_user(_params, sizeof(*params));
591 if (IS_ERR(params))
592 return PTR_ERR(params);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 err = snd_pcm_hw_params(substream, params);
595 if (copy_to_user(_params, params, sizeof(*params))) {
596 if (!err)
597 err = -EFAULT;
598 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 kfree(params);
601 return err;
602}
603
Takashi Iwai877211f2005-11-17 13:59:38 +0100604static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Takashi Iwai877211f2005-11-17 13:59:38 +0100606 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int result = 0;
608
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200609 if (PCM_RUNTIME_CHECK(substream))
610 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 snd_pcm_stream_lock_irq(substream);
613 switch (runtime->status->state) {
614 case SNDRV_PCM_STATE_SETUP:
615 case SNDRV_PCM_STATE_PREPARED:
616 break;
617 default:
618 snd_pcm_stream_unlock_irq(substream);
619 return -EBADFD;
620 }
621 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200622 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EBADFD;
624 if (substream->ops->hw_free)
625 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200626 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200627 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return result;
629}
630
Takashi Iwai877211f2005-11-17 13:59:38 +0100631static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
632 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Takashi Iwai877211f2005-11-17 13:59:38 +0100634 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100635 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200637 if (PCM_RUNTIME_CHECK(substream))
638 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 snd_pcm_stream_lock_irq(substream);
641 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
642 snd_pcm_stream_unlock_irq(substream);
643 return -EBADFD;
644 }
645 snd_pcm_stream_unlock_irq(substream);
646
Dan Carpenter145d92e2015-09-23 12:42:28 +0300647 if (params->tstamp_mode < 0 ||
648 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -EINVAL;
Takashi Iwai589008102014-07-16 17:45:27 +0200650 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
651 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200652 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (params->avail_min == 0)
654 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (params->silence_size >= runtime->boundary) {
656 if (params->silence_threshold != 0)
657 return -EINVAL;
658 } else {
659 if (params->silence_size > params->silence_threshold)
660 return -EINVAL;
661 if (params->silence_threshold > runtime->buffer_size)
662 return -EINVAL;
663 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100664 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 snd_pcm_stream_lock_irq(substream);
666 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai589008102014-07-16 17:45:27 +0200667 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
668 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 runtime->period_step = params->period_step;
670 runtime->control->avail_min = params->avail_min;
671 runtime->start_threshold = params->start_threshold;
672 runtime->stop_threshold = params->stop_threshold;
673 runtime->silence_threshold = params->silence_threshold;
674 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 params->boundary = runtime->boundary;
676 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
678 runtime->silence_size > 0)
679 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100680 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Takashi Iwai877211f2005-11-17 13:59:38 +0100686static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
687 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Takashi Iwai877211f2005-11-17 13:59:38 +0100689 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int err;
691 if (copy_from_user(&params, _params, sizeof(params)))
692 return -EFAULT;
693 err = snd_pcm_sw_params(substream, &params);
694 if (copy_to_user(_params, &params, sizeof(params)))
695 return -EFAULT;
696 return err;
697}
698
Takashi Iwai877211f2005-11-17 13:59:38 +0100699int snd_pcm_status(struct snd_pcm_substream *substream,
700 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Takashi Iwai877211f2005-11-17 13:59:38 +0100702 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600705
706 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
707 &runtime->audio_tstamp_config);
708
709 /* backwards compatible behavior */
710 if (runtime->audio_tstamp_config.type_requested ==
711 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
712 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
713 runtime->audio_tstamp_config.type_requested =
714 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
715 else
716 runtime->audio_tstamp_config.type_requested =
717 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
718 runtime->audio_tstamp_report.valid = 0;
719 } else
720 runtime->audio_tstamp_report.valid = 1;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 status->state = runtime->status->state;
723 status->suspended_state = runtime->status->suspended_state;
724 if (status->state == SNDRV_PCM_STATE_OPEN)
725 goto _end;
726 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100727 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100729 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
730 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600731 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500732 status->audio_tstamp =
733 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600734 if (runtime->audio_tstamp_report.valid == 1)
735 /* backwards compatibility, no report provided in COMPAT mode */
736 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
737 &status->audio_tstamp_accuracy,
738 &runtime->audio_tstamp_report);
739
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100740 goto _tstamp_end;
741 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600742 } else {
743 /* get tstamp only in fallback mode and only if enabled */
744 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
745 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100746 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100747 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 status->appl_ptr = runtime->control->appl_ptr;
749 status->hw_ptr = runtime->status->hw_ptr;
750 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
751 status->avail = snd_pcm_playback_avail(runtime);
752 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200753 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200755 status->delay += runtime->delay;
756 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 status->delay = 0;
758 } else {
759 status->avail = snd_pcm_capture_avail(runtime);
760 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200761 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 else
763 status->delay = 0;
764 }
765 status->avail_max = runtime->avail_max;
766 status->overrange = runtime->overrange;
767 runtime->avail_max = 0;
768 runtime->overrange = 0;
769 _end:
770 snd_pcm_stream_unlock_irq(substream);
771 return 0;
772}
773
Takashi Iwai877211f2005-11-17 13:59:38 +0100774static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600775 struct snd_pcm_status __user * _status,
776 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Takashi Iwai877211f2005-11-17 13:59:38 +0100778 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600782 /*
783 * with extension, parameters are read/write,
784 * get audio_tstamp_data from user,
785 * ignore rest of status structure
786 */
787 if (ext && get_user(status.audio_tstamp_data,
788 (u32 __user *)(&_status->audio_tstamp_data)))
789 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 res = snd_pcm_status(substream, &status);
791 if (res < 0)
792 return res;
793 if (copy_to_user(_status, &status, sizeof(status)))
794 return -EFAULT;
795 return 0;
796}
797
Takashi Iwai877211f2005-11-17 13:59:38 +0100798static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
799 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Takashi Iwai877211f2005-11-17 13:59:38 +0100801 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 unsigned int channel;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 channel = info->channel;
805 runtime = substream->runtime;
806 snd_pcm_stream_lock_irq(substream);
807 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
808 snd_pcm_stream_unlock_irq(substream);
809 return -EBADFD;
810 }
811 snd_pcm_stream_unlock_irq(substream);
812 if (channel >= runtime->channels)
813 return -EINVAL;
814 memset(info, 0, sizeof(*info));
815 info->channel = channel;
816 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
817}
818
Takashi Iwai877211f2005-11-17 13:59:38 +0100819static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
820 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Takashi Iwai877211f2005-11-17 13:59:38 +0100822 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int res;
824
825 if (copy_from_user(&info, _info, sizeof(info)))
826 return -EFAULT;
827 res = snd_pcm_channel_info(substream, &info);
828 if (res < 0)
829 return res;
830 if (copy_to_user(_info, &info, sizeof(info)))
831 return -EFAULT;
832 return 0;
833}
834
Takashi Iwai877211f2005-11-17 13:59:38 +0100835static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Takashi Iwai877211f2005-11-17 13:59:38 +0100837 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (runtime->trigger_master == NULL)
839 return;
840 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600841 if (!runtime->trigger_tstamp_latched)
842 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 } else {
844 snd_pcm_trigger_tstamp(runtime->trigger_master);
845 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
846 }
847 runtime->trigger_master = NULL;
848}
849
850struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100851 int (*pre_action)(struct snd_pcm_substream *substream, int state);
852 int (*do_action)(struct snd_pcm_substream *substream, int state);
853 void (*undo_action)(struct snd_pcm_substream *substream, int state);
854 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855};
856
857/*
858 * this functions is core for handling of linked stream
859 * Note: the stream state might be changed also on failure
860 * Note2: call with calling stream lock + link lock
861 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100862static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100863 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 int state, int do_lock)
865{
Takashi Iwai877211f2005-11-17 13:59:38 +0100866 struct snd_pcm_substream *s = NULL;
867 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200868 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Takashi Iwaief991b92007-02-22 12:52:53 +0100870 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200871 if (do_lock && s != substream) {
872 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200873 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200874 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200875 spin_lock_nested(&s->self_group.lock, depth);
876 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 res = ops->pre_action(s, state);
879 if (res < 0)
880 goto _unlock;
881 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100882 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 res = ops->do_action(s, state);
884 if (res < 0) {
885 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100886 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (s1 == s) /* failed stream */
888 break;
889 ops->undo_action(s1, state);
890 }
891 }
892 s = NULL; /* unlock all */
893 goto _unlock;
894 }
895 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100896 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 ops->post_action(s, state);
898 }
899 _unlock:
900 if (do_lock) {
901 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100902 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200903 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200904 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200905 mutex_unlock(&s1->self_group.mutex);
906 else
907 spin_unlock(&s1->self_group.lock);
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (s1 == s) /* end */
910 break;
911 }
912 }
913 return res;
914}
915
916/*
917 * Note: call with stream lock
918 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100919static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100920 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int state)
922{
923 int res;
924
925 res = ops->pre_action(substream, state);
926 if (res < 0)
927 return res;
928 res = ops->do_action(substream, state);
929 if (res == 0)
930 ops->post_action(substream, state);
931 else if (ops->undo_action)
932 ops->undo_action(substream, state);
933 return res;
934}
935
936/*
937 * Note: call with stream lock
938 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100939static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100940 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int state)
942{
943 int res;
944
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100945 if (!snd_pcm_stream_linked(substream))
946 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200947
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100948 if (substream->pcm->nonatomic) {
949 if (!mutex_trylock(&substream->group->mutex)) {
950 mutex_unlock(&substream->self_group.mutex);
951 mutex_lock(&substream->group->mutex);
952 mutex_lock(&substream->self_group.mutex);
953 }
954 res = snd_pcm_action_group(ops, substream, state, 1);
955 mutex_unlock(&substream->group->mutex);
956 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (!spin_trylock(&substream->group->lock)) {
958 spin_unlock(&substream->self_group.lock);
959 spin_lock(&substream->group->lock);
960 spin_lock(&substream->self_group.lock);
961 }
962 res = snd_pcm_action_group(ops, substream, state, 1);
963 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 return res;
966}
967
968/*
969 * Note: don't use any locks before
970 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100971static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100972 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int state)
974{
975 int res;
976
Takashi Iwaie3a4bd52014-10-31 14:45:04 +0100977 snd_pcm_stream_lock_irq(substream);
978 res = snd_pcm_action(ops, substream, state);
979 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return res;
981}
982
983/*
984 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100985static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100986 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int state)
988{
989 int res;
990
991 down_read(&snd_pcm_link_rwsem);
992 if (snd_pcm_stream_linked(substream))
993 res = snd_pcm_action_group(ops, substream, state, 0);
994 else
995 res = snd_pcm_action_single(ops, substream, state);
996 up_read(&snd_pcm_link_rwsem);
997 return res;
998}
999
1000/*
1001 * start callbacks
1002 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001003static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Takashi Iwai877211f2005-11-17 13:59:38 +01001005 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1007 return -EBADFD;
1008 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1009 !snd_pcm_playback_data(substream))
1010 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001011 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 runtime->trigger_master = substream;
1013 return 0;
1014}
1015
Takashi Iwai877211f2005-11-17 13:59:38 +01001016static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 if (substream->runtime->trigger_master != substream)
1019 return 0;
1020 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1021}
1022
Takashi Iwai877211f2005-11-17 13:59:38 +01001023static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 if (substream->runtime->trigger_master == substream)
1026 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1027}
1028
Takashi Iwai877211f2005-11-17 13:59:38 +01001029static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Takashi Iwai877211f2005-11-17 13:59:38 +01001031 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001033 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001034 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1035 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 runtime->status->state = state;
1037 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1038 runtime->silence_size > 0)
1039 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001040 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Julia Lawallb17154c2015-11-29 16:36:40 +01001043static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 .pre_action = snd_pcm_pre_start,
1045 .do_action = snd_pcm_do_start,
1046 .undo_action = snd_pcm_undo_start,
1047 .post_action = snd_pcm_post_start
1048};
1049
1050/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001051 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001052 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001053 *
1054 * Return: Zero if successful, or a negative error code.
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001055 * The stream lock must be acquired before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001057int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Takashi Iwai877211f2005-11-17 13:59:38 +01001059 return snd_pcm_action(&snd_pcm_action_start, substream,
1060 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001063/* take the stream lock and start the streams */
1064static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1065{
1066 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1067 SNDRV_PCM_STATE_RUNNING);
1068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070/*
1071 * stop callbacks
1072 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001073static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Takashi Iwai877211f2005-11-17 13:59:38 +01001075 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1077 return -EBADFD;
1078 runtime->trigger_master = substream;
1079 return 0;
1080}
1081
Takashi Iwai877211f2005-11-17 13:59:38 +01001082static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 if (substream->runtime->trigger_master == substream &&
1085 snd_pcm_running(substream))
1086 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1087 return 0; /* unconditonally stop all substreams */
1088}
1089
Takashi Iwai877211f2005-11-17 13:59:38 +01001090static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Takashi Iwai877211f2005-11-17 13:59:38 +01001092 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (runtime->status->state != state) {
1094 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001095 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001096 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001099 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Julia Lawallb17154c2015-11-29 16:36:40 +01001102static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 .pre_action = snd_pcm_pre_stop,
1104 .do_action = snd_pcm_do_stop,
1105 .post_action = snd_pcm_post_stop
1106};
1107
1108/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001109 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001110 * @substream: the PCM substream instance
1111 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001113 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001114 *
Masanari Iida0a114582014-02-09 00:47:36 +09001115 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001117int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1120}
1121
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001122EXPORT_SYMBOL(snd_pcm_stop);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001125 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001126 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001128 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001130 *
1131 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001133int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Takashi Iwai877211f2005-11-17 13:59:38 +01001135 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1136 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Takashi Iwai1fb85102014-11-07 17:08:28 +01001139/**
1140 * snd_pcm_stop_xrun - stop the running streams as XRUN
1141 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001142 *
1143 * This stops the given running substream (and all linked substreams) as XRUN.
1144 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1145 *
1146 * Return: Zero if successful, or a negative error code.
1147 */
1148int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1149{
1150 unsigned long flags;
1151 int ret = 0;
1152
1153 snd_pcm_stream_lock_irqsave(substream, flags);
1154 if (snd_pcm_running(substream))
1155 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1156 snd_pcm_stream_unlock_irqrestore(substream, flags);
1157 return ret;
1158}
1159EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161/*
1162 * pause callbacks
1163 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001164static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Takashi Iwai877211f2005-11-17 13:59:38 +01001166 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1168 return -ENOSYS;
1169 if (push) {
1170 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1171 return -EBADFD;
1172 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1173 return -EBADFD;
1174 runtime->trigger_master = substream;
1175 return 0;
1176}
1177
Takashi Iwai877211f2005-11-17 13:59:38 +01001178static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 if (substream->runtime->trigger_master != substream)
1181 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001182 /* some drivers might use hw_ptr to recover from the pause -
1183 update the hw_ptr now */
1184 if (push)
1185 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001186 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001187 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001188 * delta, effectively to skip the check once.
1189 */
1190 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return substream->ops->trigger(substream,
1192 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1193 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1194}
1195
Takashi Iwai877211f2005-11-17 13:59:38 +01001196static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 if (substream->runtime->trigger_master == substream)
1199 substream->ops->trigger(substream,
1200 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1201 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1202}
1203
Takashi Iwai877211f2005-11-17 13:59:38 +01001204static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Takashi Iwai877211f2005-11-17 13:59:38 +01001206 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 snd_pcm_trigger_tstamp(substream);
1208 if (push) {
1209 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001210 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001212 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 } else {
1214 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001215 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217}
1218
Julia Lawallb17154c2015-11-29 16:36:40 +01001219static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 .pre_action = snd_pcm_pre_pause,
1221 .do_action = snd_pcm_do_pause,
1222 .undo_action = snd_pcm_undo_pause,
1223 .post_action = snd_pcm_post_pause
1224};
1225
1226/*
1227 * Push/release the pause for all linked streams.
1228 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001229static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1232}
1233
1234#ifdef CONFIG_PM
1235/* suspend */
1236
Takashi Iwai877211f2005-11-17 13:59:38 +01001237static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Takashi Iwai877211f2005-11-17 13:59:38 +01001239 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1241 return -EBUSY;
1242 runtime->trigger_master = substream;
1243 return 0;
1244}
1245
Takashi Iwai877211f2005-11-17 13:59:38 +01001246static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Takashi Iwai877211f2005-11-17 13:59:38 +01001248 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (runtime->trigger_master != substream)
1250 return 0;
1251 if (! snd_pcm_running(substream))
1252 return 0;
1253 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1254 return 0; /* suspend unconditionally */
1255}
1256
Takashi Iwai877211f2005-11-17 13:59:38 +01001257static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Takashi Iwai877211f2005-11-17 13:59:38 +01001259 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001261 runtime->status->suspended_state = runtime->status->state;
1262 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001263 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001265 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Julia Lawallb17154c2015-11-29 16:36:40 +01001268static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 .pre_action = snd_pcm_pre_suspend,
1270 .do_action = snd_pcm_do_suspend,
1271 .post_action = snd_pcm_post_suspend
1272};
1273
1274/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001275 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001276 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001279 *
1280 * Return: Zero if successful (or @substream is %NULL), or a negative error
1281 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001283int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 int err;
1286 unsigned long flags;
1287
Takashi Iwai603bf522005-11-17 15:59:14 +01001288 if (! substream)
1289 return 0;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 snd_pcm_stream_lock_irqsave(substream, flags);
1292 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1293 snd_pcm_stream_unlock_irqrestore(substream, flags);
1294 return err;
1295}
1296
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001297EXPORT_SYMBOL(snd_pcm_suspend);
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001300 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001301 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001304 *
1305 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001307int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Takashi Iwai877211f2005-11-17 13:59:38 +01001309 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int stream, err = 0;
1311
Takashi Iwai603bf522005-11-17 15:59:14 +01001312 if (! pcm)
1313 return 0;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001316 for (substream = pcm->streams[stream].substream;
1317 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /* FIXME: the open/close code should lock this as well */
1319 if (substream->runtime == NULL)
1320 continue;
1321 err = snd_pcm_suspend(substream);
1322 if (err < 0 && err != -EBUSY)
1323 return err;
1324 }
1325 }
1326 return 0;
1327}
1328
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001329EXPORT_SYMBOL(snd_pcm_suspend_all);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331/* resume */
1332
Takashi Iwai877211f2005-11-17 13:59:38 +01001333static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Takashi Iwai877211f2005-11-17 13:59:38 +01001335 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1337 return -ENOSYS;
1338 runtime->trigger_master = substream;
1339 return 0;
1340}
1341
Takashi Iwai877211f2005-11-17 13:59:38 +01001342static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Takashi Iwai877211f2005-11-17 13:59:38 +01001344 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (runtime->trigger_master != substream)
1346 return 0;
1347 /* DMA not running previously? */
1348 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1349 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1350 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1351 return 0;
1352 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1353}
1354
Takashi Iwai877211f2005-11-17 13:59:38 +01001355static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 if (substream->runtime->trigger_master == substream &&
1358 snd_pcm_running(substream))
1359 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1360}
1361
Takashi Iwai877211f2005-11-17 13:59:38 +01001362static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Takashi Iwai877211f2005-11-17 13:59:38 +01001364 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001366 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001367 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Julia Lawallb17154c2015-11-29 16:36:40 +01001370static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 .pre_action = snd_pcm_pre_resume,
1372 .do_action = snd_pcm_do_resume,
1373 .undo_action = snd_pcm_undo_resume,
1374 .post_action = snd_pcm_post_resume
1375};
1376
Takashi Iwai877211f2005-11-17 13:59:38 +01001377static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Takashi Iwai877211f2005-11-17 13:59:38 +01001379 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int res;
1381
1382 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001383 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1385 snd_power_unlock(card);
1386 return res;
1387}
1388
1389#else
1390
Takashi Iwai877211f2005-11-17 13:59:38 +01001391static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 return -ENOSYS;
1394}
1395
1396#endif /* CONFIG_PM */
1397
1398/*
1399 * xrun ioctl
1400 *
1401 * Change the RUNNING stream(s) to XRUN state.
1402 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001403static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Takashi Iwai877211f2005-11-17 13:59:38 +01001405 struct snd_card *card = substream->pcm->card;
1406 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int result;
1408
1409 snd_power_lock(card);
1410 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001411 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (result < 0)
1413 goto _unlock;
1414 }
1415
1416 snd_pcm_stream_lock_irq(substream);
1417 switch (runtime->status->state) {
1418 case SNDRV_PCM_STATE_XRUN:
1419 result = 0; /* already there */
1420 break;
1421 case SNDRV_PCM_STATE_RUNNING:
1422 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1423 break;
1424 default:
1425 result = -EBADFD;
1426 }
1427 snd_pcm_stream_unlock_irq(substream);
1428 _unlock:
1429 snd_power_unlock(card);
1430 return result;
1431}
1432
1433/*
1434 * reset ioctl
1435 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001436static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Takashi Iwai877211f2005-11-17 13:59:38 +01001438 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 switch (runtime->status->state) {
1440 case SNDRV_PCM_STATE_RUNNING:
1441 case SNDRV_PCM_STATE_PREPARED:
1442 case SNDRV_PCM_STATE_PAUSED:
1443 case SNDRV_PCM_STATE_SUSPENDED:
1444 return 0;
1445 default:
1446 return -EBADFD;
1447 }
1448}
1449
Takashi Iwai877211f2005-11-17 13:59:38 +01001450static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Takashi Iwai877211f2005-11-17 13:59:38 +01001452 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1454 if (err < 0)
1455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001457 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1458 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 runtime->silence_start = runtime->status->hw_ptr;
1460 runtime->silence_filled = 0;
1461 return 0;
1462}
1463
Takashi Iwai877211f2005-11-17 13:59:38 +01001464static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Takashi Iwai877211f2005-11-17 13:59:38 +01001466 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 runtime->control->appl_ptr = runtime->status->hw_ptr;
1468 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1469 runtime->silence_size > 0)
1470 snd_pcm_playback_silence(substream, ULONG_MAX);
1471}
1472
Julia Lawallb17154c2015-11-29 16:36:40 +01001473static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 .pre_action = snd_pcm_pre_reset,
1475 .do_action = snd_pcm_do_reset,
1476 .post_action = snd_pcm_post_reset
1477};
1478
Takashi Iwai877211f2005-11-17 13:59:38 +01001479static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1482}
1483
1484/*
1485 * prepare ioctl
1486 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001487/* we use the second argument for updating f_flags */
1488static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1489 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Takashi Iwai877211f2005-11-17 13:59:38 +01001491 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001492 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1493 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EBADFD;
1495 if (snd_pcm_running(substream))
1496 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001497 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return 0;
1499}
1500
Takashi Iwai877211f2005-11-17 13:59:38 +01001501static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 int err;
1504 err = substream->ops->prepare(substream);
1505 if (err < 0)
1506 return err;
1507 return snd_pcm_do_reset(substream, 0);
1508}
1509
Takashi Iwai877211f2005-11-17 13:59:38 +01001510static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Takashi Iwai877211f2005-11-17 13:59:38 +01001512 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001514 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Julia Lawallb17154c2015-11-29 16:36:40 +01001517static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 .pre_action = snd_pcm_pre_prepare,
1519 .do_action = snd_pcm_do_prepare,
1520 .post_action = snd_pcm_post_prepare
1521};
1522
1523/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001524 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001525 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001526 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001527 *
1528 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001530static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1531 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001534 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001535 int f_flags;
1536
1537 if (file)
1538 f_flags = file->f_flags;
1539 else
1540 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001543 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001544 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1545 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 snd_power_unlock(card);
1547 return res;
1548}
1549
1550/*
1551 * drain ioctl
1552 */
1553
Takashi Iwai877211f2005-11-17 13:59:38 +01001554static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001556 struct snd_pcm_runtime *runtime = substream->runtime;
1557 switch (runtime->status->state) {
1558 case SNDRV_PCM_STATE_OPEN:
1559 case SNDRV_PCM_STATE_DISCONNECTED:
1560 case SNDRV_PCM_STATE_SUSPENDED:
1561 return -EBADFD;
1562 }
1563 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return 0;
1565}
1566
Takashi Iwai877211f2005-11-17 13:59:38 +01001567static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Takashi Iwai877211f2005-11-17 13:59:38 +01001569 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1571 switch (runtime->status->state) {
1572 case SNDRV_PCM_STATE_PREPARED:
1573 /* start playback stream if possible */
1574 if (! snd_pcm_playback_empty(substream)) {
1575 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1576 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001577 } else {
1578 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 break;
1581 case SNDRV_PCM_STATE_RUNNING:
1582 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1583 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001584 case SNDRV_PCM_STATE_XRUN:
1585 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 default:
1588 break;
1589 }
1590 } else {
1591 /* stop running stream */
1592 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001593 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001595 snd_pcm_do_stop(substream, new_state);
1596 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598 }
Libin Yang48d88292014-12-31 22:09:54 +08001599
1600 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1601 runtime->trigger_master == substream &&
1602 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1603 return substream->ops->trigger(substream,
1604 SNDRV_PCM_TRIGGER_DRAIN);
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return 0;
1607}
1608
Takashi Iwai877211f2005-11-17 13:59:38 +01001609static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611}
1612
Julia Lawallb17154c2015-11-29 16:36:40 +01001613static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 .pre_action = snd_pcm_pre_drain_init,
1615 .do_action = snd_pcm_do_drain_init,
1616 .post_action = snd_pcm_post_drain_init
1617};
1618
Takashi Iwai877211f2005-11-17 13:59:38 +01001619static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621/*
1622 * Drain the stream(s).
1623 * When the substream is linked, sync until the draining of all playback streams
1624 * is finished.
1625 * After this call, all streams are supposed to be either SETUP or DRAINING
1626 * (capture only) state.
1627 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001628static int snd_pcm_drain(struct snd_pcm_substream *substream,
1629 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Takashi Iwai877211f2005-11-17 13:59:38 +01001631 struct snd_card *card;
1632 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001633 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001634 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001636 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 card = substream->pcm->card;
1639 runtime = substream->runtime;
1640
1641 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1642 return -EBADFD;
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 snd_power_lock(card);
1645 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001646 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001647 if (result < 0) {
1648 snd_power_unlock(card);
1649 return result;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001653 if (file) {
1654 if (file->f_flags & O_NONBLOCK)
1655 nonblock = 1;
1656 } else if (substream->f_flags & O_NONBLOCK)
1657 nonblock = 1;
1658
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001659 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001660 snd_pcm_stream_lock_irq(substream);
1661 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001662 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001663 snd_pcm_pause(substream, 0);
1664
1665 /* pre-start/stop - all running streams are changed to DRAINING state */
1666 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001667 if (result < 0)
1668 goto unlock;
1669 /* in non-blocking, we don't wait in ioctl but let caller poll */
1670 if (nonblock) {
1671 result = -EAGAIN;
1672 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 for (;;) {
1676 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001677 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (signal_pending(current)) {
1679 result = -ERESTARTSYS;
1680 break;
1681 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001682 /* find a substream to drain */
1683 to_check = NULL;
1684 snd_pcm_group_for_each_entry(s, substream) {
1685 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1686 continue;
1687 runtime = s->runtime;
1688 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1689 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001690 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001691 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001692 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001693 if (!to_check)
1694 break; /* all drained */
1695 init_waitqueue_entry(&wait, current);
1696 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001698 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001700 if (runtime->no_period_wakeup)
1701 tout = MAX_SCHEDULE_TIMEOUT;
1702 else {
1703 tout = 10;
1704 if (runtime->rate) {
1705 long t = runtime->period_size * 2 / runtime->rate;
1706 tout = max(t, tout);
1707 }
1708 tout = msecs_to_jiffies(tout * 1000);
1709 }
1710 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001712 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001714 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001715 if (card->shutdown) {
1716 result = -ENODEV;
1717 break;
1718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (tout == 0) {
1720 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1721 result = -ESTRPIPE;
1722 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001723 dev_dbg(substream->pcm->card->dev,
1724 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1726 result = -EIO;
1727 }
1728 break;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001731
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001732 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001733 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001734 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 return result;
1738}
1739
1740/*
1741 * drop ioctl
1742 *
1743 * Immediately put all linked substreams into SETUP state.
1744 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001745static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Takashi Iwai877211f2005-11-17 13:59:38 +01001747 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int result = 0;
1749
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001750 if (PCM_RUNTIME_CHECK(substream))
1751 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001754 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001755 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1756 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return -EBADFD;
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 snd_pcm_stream_lock_irq(substream);
1760 /* resume pause */
1761 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1762 snd_pcm_pause(substream, 0);
1763
1764 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1765 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1766 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return result;
1769}
1770
1771
Al Viro0888c322013-06-05 14:09:55 -04001772static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Al Viro0888c322013-06-05 14:09:55 -04001774 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001775 unsigned int minor;
1776
Al Viro0888c322013-06-05 14:09:55 -04001777 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1778 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001780 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1781 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
1784/*
1785 * PCM link handling
1786 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001787static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001790 struct snd_pcm_file *pcm_file;
1791 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001792 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001793 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Al Viro0888c322013-06-05 14:09:55 -04001795 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001797 if (!is_pcm_file(f.file)) {
1798 res = -EBADFD;
1799 goto _badf;
1800 }
1801 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001803 group = kmalloc(sizeof(*group), GFP_KERNEL);
1804 if (!group) {
1805 res = -ENOMEM;
1806 goto _nolock;
1807 }
Takashi Iwai67ec10722016-02-17 14:30:26 +01001808 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 write_lock_irq(&snd_pcm_link_rwlock);
1810 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001811 substream->runtime->status->state != substream1->runtime->status->state ||
1812 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 res = -EBADFD;
1814 goto _end;
1815 }
1816 if (snd_pcm_stream_linked(substream1)) {
1817 res = -EALREADY;
1818 goto _end;
1819 }
1820 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001821 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001822 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001824 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 INIT_LIST_HEAD(&substream->group->substreams);
1826 list_add_tail(&substream->link_list, &substream->group->substreams);
1827 substream->group->count = 1;
1828 }
1829 list_add_tail(&substream1->link_list, &substream->group->substreams);
1830 substream->group->count++;
1831 substream1->group = substream->group;
1832 _end:
1833 write_unlock_irq(&snd_pcm_link_rwlock);
1834 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001835 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001836 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001837 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001838 _badf:
1839 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return res;
1841}
1842
Takashi Iwai877211f2005-11-17 13:59:38 +01001843static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 substream->group = &substream->self_group;
1846 INIT_LIST_HEAD(&substream->self_group.substreams);
1847 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1848}
1849
Takashi Iwai877211f2005-11-17 13:59:38 +01001850static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Takashi Iwaief991b92007-02-22 12:52:53 +01001852 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 int res = 0;
1854
Takashi Iwai67ec10722016-02-17 14:30:26 +01001855 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 write_lock_irq(&snd_pcm_link_rwlock);
1857 if (!snd_pcm_stream_linked(substream)) {
1858 res = -EALREADY;
1859 goto _end;
1860 }
1861 list_del(&substream->link_list);
1862 substream->group->count--;
1863 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001864 snd_pcm_group_for_each_entry(s, substream) {
1865 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 break;
1867 }
1868 kfree(substream->group);
1869 }
1870 relink_to_local(substream);
1871 _end:
1872 write_unlock_irq(&snd_pcm_link_rwlock);
1873 up_write(&snd_pcm_link_rwsem);
1874 return res;
1875}
1876
1877/*
1878 * hw configurator
1879 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001880static int snd_pcm_hw_rule_mul(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_mul(hw_param_interval_c(params, rule->deps[0]),
1885 hw_param_interval_c(params, rule->deps[1]), &t);
1886 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1887}
1888
Takashi Iwai877211f2005-11-17 13:59:38 +01001889static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1890 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Takashi Iwai877211f2005-11-17 13:59:38 +01001892 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1894 hw_param_interval_c(params, rule->deps[1]), &t);
1895 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1896}
1897
Takashi Iwai877211f2005-11-17 13:59:38 +01001898static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1899 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Takashi Iwai877211f2005-11-17 13:59:38 +01001901 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1903 hw_param_interval_c(params, rule->deps[1]),
1904 (unsigned long) rule->private, &t);
1905 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1906}
1907
Takashi Iwai877211f2005-11-17 13:59:38 +01001908static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1909 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Takashi Iwai877211f2005-11-17 13:59:38 +01001911 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1913 (unsigned long) rule->private,
1914 hw_param_interval_c(params, rule->deps[1]), &t);
1915 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1916}
1917
Takashi Iwai877211f2005-11-17 13:59:38 +01001918static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1919 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 unsigned int k;
Takashi Sakamotob55f9fd2017-05-17 08:48:20 +09001922 const struct snd_interval *i =
1923 hw_param_interval_c(params, rule->deps[0]);
Takashi Iwai877211f2005-11-17 13:59:38 +01001924 struct snd_mask m;
1925 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 snd_mask_any(&m);
1927 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1928 int bits;
1929 if (! snd_mask_test(mask, k))
1930 continue;
1931 bits = snd_pcm_format_physical_width(k);
1932 if (bits <= 0)
1933 continue; /* ignore invalid formats */
1934 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1935 snd_mask_reset(&m, k);
1936 }
1937 return snd_mask_refine(mask, &m);
1938}
1939
Takashi Iwai877211f2005-11-17 13:59:38 +01001940static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1941 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Takashi Iwai877211f2005-11-17 13:59:38 +01001943 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 unsigned int k;
1945 t.min = UINT_MAX;
1946 t.max = 0;
1947 t.openmin = 0;
1948 t.openmax = 0;
1949 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1950 int bits;
1951 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1952 continue;
1953 bits = snd_pcm_format_physical_width(k);
1954 if (bits <= 0)
1955 continue; /* ignore invalid formats */
1956 if (t.min > (unsigned)bits)
1957 t.min = bits;
1958 if (t.max < (unsigned)bits)
1959 t.max = bits;
1960 }
1961 t.integer = 1;
1962 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1963}
1964
1965#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1966#error "Change this table"
1967#endif
1968
Takashi Sakamoto8b674302017-05-17 08:48:17 +09001969static const unsigned int rates[] = {
1970 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1971 48000, 64000, 88200, 96000, 176400, 192000
1972};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Clemens Ladisch7653d552007-08-13 17:38:54 +02001974const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1975 .count = ARRAY_SIZE(rates),
1976 .list = rates,
1977};
1978
Takashi Iwai877211f2005-11-17 13:59:38 +01001979static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1980 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Takashi Iwai877211f2005-11-17 13:59:38 +01001982 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001984 snd_pcm_known_rates.count,
1985 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Takashi Iwai877211f2005-11-17 13:59:38 +01001988static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1989 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Takashi Iwai877211f2005-11-17 13:59:38 +01001991 struct snd_interval t;
1992 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 t.min = 0;
1994 t.max = substream->buffer_bytes_max;
1995 t.openmin = 0;
1996 t.openmax = 0;
1997 t.integer = 1;
1998 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1999}
2000
Takashi Iwai877211f2005-11-17 13:59:38 +01002001int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Takashi Iwai877211f2005-11-17 13:59:38 +01002003 struct snd_pcm_runtime *runtime = substream->runtime;
2004 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 int k, err;
2006
2007 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2008 snd_mask_any(constrs_mask(constrs, k));
2009 }
2010
2011 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2012 snd_interval_any(constrs_interval(constrs, k));
2013 }
2014
2015 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2016 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2017 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2018 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2019 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2020
2021 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2022 snd_pcm_hw_rule_format, NULL,
2023 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2024 if (err < 0)
2025 return err;
2026 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2027 snd_pcm_hw_rule_sample_bits, NULL,
2028 SNDRV_PCM_HW_PARAM_FORMAT,
2029 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2030 if (err < 0)
2031 return err;
2032 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2033 snd_pcm_hw_rule_div, NULL,
2034 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2035 if (err < 0)
2036 return err;
2037 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2038 snd_pcm_hw_rule_mul, NULL,
2039 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2040 if (err < 0)
2041 return err;
2042 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2043 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2044 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2045 if (err < 0)
2046 return err;
2047 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2048 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2049 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2050 if (err < 0)
2051 return err;
2052 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2053 snd_pcm_hw_rule_div, NULL,
2054 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2055 if (err < 0)
2056 return err;
2057 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2058 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2059 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2060 if (err < 0)
2061 return err;
2062 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2063 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2064 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2065 if (err < 0)
2066 return err;
2067 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2068 snd_pcm_hw_rule_div, NULL,
2069 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2070 if (err < 0)
2071 return err;
2072 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2073 snd_pcm_hw_rule_div, NULL,
2074 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2075 if (err < 0)
2076 return err;
2077 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2078 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2079 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2080 if (err < 0)
2081 return err;
2082 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2083 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2084 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2085 if (err < 0)
2086 return err;
2087 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2088 snd_pcm_hw_rule_mul, NULL,
2089 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2090 if (err < 0)
2091 return err;
2092 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2093 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2094 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2095 if (err < 0)
2096 return err;
2097 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2098 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2099 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2100 if (err < 0)
2101 return err;
2102 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2103 snd_pcm_hw_rule_muldivk, (void*) 8,
2104 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2105 if (err < 0)
2106 return err;
2107 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2108 snd_pcm_hw_rule_muldivk, (void*) 8,
2109 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2110 if (err < 0)
2111 return err;
2112 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2113 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2114 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2115 if (err < 0)
2116 return err;
2117 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2118 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2119 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2120 if (err < 0)
2121 return err;
2122 return 0;
2123}
2124
Takashi Iwai877211f2005-11-17 13:59:38 +01002125int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Takashi Iwai877211f2005-11-17 13:59:38 +01002127 struct snd_pcm_runtime *runtime = substream->runtime;
2128 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 int err;
2130 unsigned int mask = 0;
2131
2132 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2133 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2134 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2135 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002136 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2138 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2139 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2140 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2141 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2142 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2143 }
2144 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002145 if (err < 0)
2146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002149 if (err < 0)
2150 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002153 if (err < 0)
2154 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2157 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002158 if (err < 0)
2159 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2162 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002163 if (err < 0)
2164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2167 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002168 if (err < 0)
2169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2172 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002173 if (err < 0)
2174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2177 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002178 if (err < 0)
2179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2182 snd_pcm_hw_rule_buffer_bytes_max, substream,
2183 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2184 if (err < 0)
2185 return err;
2186
2187 /* FIXME: remove */
2188 if (runtime->dma_bytes) {
2189 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002190 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302191 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193
2194 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2195 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2196 snd_pcm_hw_rule_rate, hw,
2197 SNDRV_PCM_HW_PARAM_RATE, -1);
2198 if (err < 0)
2199 return err;
2200 }
2201
2202 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2204
2205 return 0;
2206}
2207
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002208static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002211}
2212
2213void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2214{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002215 substream->ref_count--;
2216 if (substream->ref_count > 0)
2217 return;
2218
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002219 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002220 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002221 if (substream->ops->hw_free &&
2222 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 substream->ops->hw_free(substream);
2224 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002225 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002227 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2228 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002229 if (substream->pcm_release) {
2230 substream->pcm_release(substream);
2231 substream->pcm_release = NULL;
2232 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002233 snd_pcm_detach_substream(substream);
2234}
2235
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002236EXPORT_SYMBOL(snd_pcm_release_substream);
2237
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002238int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2239 struct file *file,
2240 struct snd_pcm_substream **rsubstream)
2241{
2242 struct snd_pcm_substream *substream;
2243 int err;
2244
2245 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2246 if (err < 0)
2247 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002248 if (substream->ref_count > 1) {
2249 *rsubstream = substream;
2250 return 0;
2251 }
2252
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002253 err = snd_pcm_hw_constraints_init(substream);
2254 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002255 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002256 goto error;
2257 }
2258
2259 if ((err = substream->ops->open(substream)) < 0)
2260 goto error;
2261
2262 substream->hw_opened = 1;
2263
2264 err = snd_pcm_hw_constraints_complete(substream);
2265 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002266 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002267 goto error;
2268 }
2269
2270 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002272
2273 error:
2274 snd_pcm_release_substream(substream);
2275 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002278EXPORT_SYMBOL(snd_pcm_open_substream);
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002281 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002282 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Takashi Iwai877211f2005-11-17 13:59:38 +01002284 struct snd_pcm_file *pcm_file;
2285 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002286 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002288 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2289 if (err < 0)
2290 return err;
2291
Takashi Iwai548a6482006-07-31 16:51:51 +02002292 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2293 if (pcm_file == NULL) {
2294 snd_pcm_release_substream(substream);
2295 return -ENOMEM;
2296 }
2297 pcm_file->substream = substream;
2298 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002299 substream->file = pcm_file;
2300 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return 0;
2305}
2306
Clemens Ladischf87135f2005-11-20 14:06:59 +01002307static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Takashi Iwai877211f2005-11-17 13:59:38 +01002309 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002310 int err = nonseekable_open(inode, file);
2311 if (err < 0)
2312 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002313 pcm = snd_lookup_minor_data(iminor(inode),
2314 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002315 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002316 if (pcm)
2317 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002318 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002319}
2320
2321static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2322{
2323 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002324 int err = nonseekable_open(inode, file);
2325 if (err < 0)
2326 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002327 pcm = snd_lookup_minor_data(iminor(inode),
2328 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002329 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002330 if (pcm)
2331 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002332 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002333}
2334
2335static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2336{
2337 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 wait_queue_t wait;
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (pcm == NULL) {
2341 err = -ENODEV;
2342 goto __error1;
2343 }
2344 err = snd_card_file_add(pcm->card, file);
2345 if (err < 0)
2346 goto __error1;
2347 if (!try_module_get(pcm->card->module)) {
2348 err = -EFAULT;
2349 goto __error2;
2350 }
2351 init_waitqueue_entry(&wait, current);
2352 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002353 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002355 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (err >= 0)
2357 break;
2358 if (err == -EAGAIN) {
2359 if (file->f_flags & O_NONBLOCK) {
2360 err = -EBUSY;
2361 break;
2362 }
2363 } else
2364 break;
2365 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002366 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002368 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002369 if (pcm->card->shutdown) {
2370 err = -ENODEV;
2371 break;
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (signal_pending(current)) {
2374 err = -ERESTARTSYS;
2375 break;
2376 }
2377 }
2378 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002379 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (err < 0)
2381 goto __error;
2382 return err;
2383
2384 __error:
2385 module_put(pcm->card->module);
2386 __error2:
2387 snd_card_file_remove(pcm->card, file);
2388 __error1:
2389 return err;
2390}
2391
2392static int snd_pcm_release(struct inode *inode, struct file *file)
2393{
Takashi Iwai877211f2005-11-17 13:59:38 +01002394 struct snd_pcm *pcm;
2395 struct snd_pcm_substream *substream;
2396 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 pcm_file = file->private_data;
2399 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002400 if (snd_BUG_ON(!substream))
2401 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002403 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002404 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002405 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002406 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 wake_up(&pcm->open_wait);
2408 module_put(pcm->card->module);
2409 snd_card_file_remove(pcm->card, file);
2410 return 0;
2411}
2412
Takashi Iwaif839cc12017-05-19 20:04:23 +02002413/* check and update PCM state; return 0 or a negative error
2414 * call this inside PCM lock
2415 */
2416static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2417{
2418 switch (substream->runtime->status->state) {
2419 case SNDRV_PCM_STATE_DRAINING:
2420 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2421 return -EBADFD;
2422 /* Fall through */
2423 case SNDRV_PCM_STATE_RUNNING:
2424 return snd_pcm_update_hw_ptr(substream);
2425 case SNDRV_PCM_STATE_PREPARED:
2426 case SNDRV_PCM_STATE_PAUSED:
2427 return 0;
2428 case SNDRV_PCM_STATE_SUSPENDED:
2429 return -ESTRPIPE;
2430 case SNDRV_PCM_STATE_XRUN:
2431 return -EPIPE;
2432 default:
2433 return -EBADFD;
2434 }
2435}
2436
Takashi Iwai9027c462017-05-19 20:22:33 +02002437/* update to the given appl_ptr and call ack callback if needed;
2438 * when an error is returned, take back to the original value
2439 */
2440static int apply_appl_ptr(struct snd_pcm_substream *substream,
2441 snd_pcm_uframes_t appl_ptr)
2442{
2443 struct snd_pcm_runtime *runtime = substream->runtime;
2444 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2445 int ret;
2446
2447 runtime->control->appl_ptr = appl_ptr;
2448 if (substream->ops->ack) {
2449 ret = substream->ops->ack(substream);
2450 if (ret < 0) {
2451 runtime->control->appl_ptr = old_appl_ptr;
2452 return ret;
2453 }
2454 }
2455 return 0;
2456}
2457
2458/* increase the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002459static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2460 snd_pcm_uframes_t frames,
2461 snd_pcm_sframes_t avail)
2462{
2463 struct snd_pcm_runtime *runtime = substream->runtime;
2464 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002465 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002466
2467 if (avail <= 0)
2468 return 0;
2469 if (frames > (snd_pcm_uframes_t)avail)
2470 frames = avail;
2471 appl_ptr = runtime->control->appl_ptr + frames;
2472 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2473 appl_ptr -= runtime->boundary;
Takashi Iwai9027c462017-05-19 20:22:33 +02002474 ret = apply_appl_ptr(substream, appl_ptr);
2475 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002476}
2477
Takashi Iwai9027c462017-05-19 20:22:33 +02002478/* decrease the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002479static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2480 snd_pcm_uframes_t frames,
2481 snd_pcm_sframes_t avail)
2482{
2483 struct snd_pcm_runtime *runtime = substream->runtime;
2484 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002485 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002486
2487 if (avail <= 0)
2488 return 0;
2489 if (frames > (snd_pcm_uframes_t)avail)
2490 frames = avail;
2491 appl_ptr = runtime->control->appl_ptr - frames;
2492 if (appl_ptr < 0)
2493 appl_ptr += runtime->boundary;
Takashi Iwai9027c462017-05-19 20:22:33 +02002494 ret = apply_appl_ptr(substream, appl_ptr);
2495 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002496}
2497
Takashi Iwai877211f2005-11-17 13:59:38 +01002498static snd_pcm_sframes_t snd_pcm_playback_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_playback_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_capture_rewind(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 = rewind_appl_ptr(substream, frames,
2529 snd_pcm_capture_hw_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_playback_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_playback_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 snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2553 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Takashi Iwai877211f2005-11-17 13:59:38 +01002555 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
2558 if (frames == 0)
2559 return 0;
2560
2561 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002562 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002563 if (!ret)
2564 ret = forward_appl_ptr(substream, frames,
2565 snd_pcm_capture_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 snd_pcm_stream_unlock_irq(substream);
2567 return ret;
2568}
2569
Takashi Iwai877211f2005-11-17 13:59:38 +01002570static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 int err;
2573
2574 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002575 err = do_pcm_hwsync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 snd_pcm_stream_unlock_irq(substream);
2577 return err;
2578}
2579
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002580static snd_pcm_sframes_t snd_pcm_delay(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Takashi Iwai877211f2005-11-17 13:59:38 +01002582 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 int err;
2584 snd_pcm_sframes_t n = 0;
2585
2586 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002587 err = do_pcm_hwsync(substream);
2588 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2590 n = snd_pcm_playback_hw_avail(runtime);
2591 else
2592 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002593 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002596 return err < 0 ? err : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
Takashi Iwai877211f2005-11-17 13:59:38 +01002599static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2600 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Takashi Iwai877211f2005-11-17 13:59:38 +01002602 struct snd_pcm_runtime *runtime = substream->runtime;
2603 struct snd_pcm_sync_ptr sync_ptr;
2604 volatile struct snd_pcm_mmap_status *status;
2605 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 int err;
2607
2608 memset(&sync_ptr, 0, sizeof(sync_ptr));
2609 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2610 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002611 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 -07002612 return -EFAULT;
2613 status = runtime->status;
2614 control = runtime->control;
2615 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2616 err = snd_pcm_hwsync(substream);
2617 if (err < 0)
2618 return err;
2619 }
2620 snd_pcm_stream_lock_irq(substream);
Takashi Iwai9027c462017-05-19 20:22:33 +02002621 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2622 err = apply_appl_ptr(substream, sync_ptr.c.control.appl_ptr);
2623 if (err < 0) {
2624 snd_pcm_stream_unlock_irq(substream);
2625 return err;
2626 }
2627 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 sync_ptr.c.control.appl_ptr = control->appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2631 control->avail_min = sync_ptr.c.control.avail_min;
2632 else
2633 sync_ptr.c.control.avail_min = control->avail_min;
2634 sync_ptr.s.status.state = status->state;
2635 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2636 sync_ptr.s.status.tstamp = status->tstamp;
2637 sync_ptr.s.status.suspended_state = status->suspended_state;
2638 snd_pcm_stream_unlock_irq(substream);
2639 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2640 return -EFAULT;
2641 return 0;
2642}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002643
2644static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2645{
2646 struct snd_pcm_runtime *runtime = substream->runtime;
2647 int arg;
2648
2649 if (get_user(arg, _arg))
2650 return -EFAULT;
2651 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2652 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002653 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002654 return 0;
2655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Takashi Iwai0df63e42006-04-28 15:13:41 +02002657static int snd_pcm_common_ioctl1(struct file *file,
2658 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 unsigned int cmd, void __user *arg)
2660{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 switch (cmd) {
2662 case SNDRV_PCM_IOCTL_PVERSION:
2663 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2664 case SNDRV_PCM_IOCTL_INFO:
2665 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002666 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2667 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002668 case SNDRV_PCM_IOCTL_TTSTAMP:
2669 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 case SNDRV_PCM_IOCTL_HW_REFINE:
2671 return snd_pcm_hw_refine_user(substream, arg);
2672 case SNDRV_PCM_IOCTL_HW_PARAMS:
2673 return snd_pcm_hw_params_user(substream, arg);
2674 case SNDRV_PCM_IOCTL_HW_FREE:
2675 return snd_pcm_hw_free(substream);
2676 case SNDRV_PCM_IOCTL_SW_PARAMS:
2677 return snd_pcm_sw_params_user(substream, arg);
2678 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002679 return snd_pcm_status_user(substream, arg, false);
2680 case SNDRV_PCM_IOCTL_STATUS_EXT:
2681 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2683 return snd_pcm_channel_info_user(substream, arg);
2684 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002685 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 case SNDRV_PCM_IOCTL_RESET:
2687 return snd_pcm_reset(substream);
2688 case SNDRV_PCM_IOCTL_START:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002689 return snd_pcm_start_lock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 case SNDRV_PCM_IOCTL_LINK:
2691 return snd_pcm_link(substream, (int)(unsigned long) arg);
2692 case SNDRV_PCM_IOCTL_UNLINK:
2693 return snd_pcm_unlink(substream);
2694 case SNDRV_PCM_IOCTL_RESUME:
2695 return snd_pcm_resume(substream);
2696 case SNDRV_PCM_IOCTL_XRUN:
2697 return snd_pcm_xrun(substream);
2698 case SNDRV_PCM_IOCTL_HWSYNC:
2699 return snd_pcm_hwsync(substream);
2700 case SNDRV_PCM_IOCTL_DELAY:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002701 {
2702 snd_pcm_sframes_t delay = snd_pcm_delay(substream);
2703 snd_pcm_sframes_t __user *res = arg;
2704
2705 if (delay < 0)
2706 return delay;
2707 if (put_user(delay, res))
2708 return -EFAULT;
2709 return 0;
2710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 case SNDRV_PCM_IOCTL_SYNC_PTR:
2712 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002713#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2715 return snd_pcm_hw_refine_old_user(substream, arg);
2716 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2717 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002720 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 case SNDRV_PCM_IOCTL_DROP:
2722 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002723 case SNDRV_PCM_IOCTL_PAUSE:
2724 {
2725 int res;
2726 snd_pcm_stream_lock_irq(substream);
2727 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2728 snd_pcm_stream_unlock_irq(substream);
2729 return res;
2730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002732 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return -ENOTTY;
2734}
2735
Takashi Iwai0df63e42006-04-28 15:13:41 +02002736static int snd_pcm_playback_ioctl1(struct file *file,
2737 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 unsigned int cmd, void __user *arg)
2739{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002740 if (snd_BUG_ON(!substream))
2741 return -ENXIO;
2742 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2743 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 switch (cmd) {
2745 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2746 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002747 struct snd_xferi xferi;
2748 struct snd_xferi __user *_xferi = arg;
2749 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 snd_pcm_sframes_t result;
2751 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2752 return -EBADFD;
2753 if (put_user(0, &_xferi->result))
2754 return -EFAULT;
2755 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2756 return -EFAULT;
2757 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2758 __put_user(result, &_xferi->result);
2759 return result < 0 ? result : 0;
2760 }
2761 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2762 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002763 struct snd_xfern xfern;
2764 struct snd_xfern __user *_xfern = arg;
2765 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 void __user **bufs;
2767 snd_pcm_sframes_t result;
2768 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2769 return -EBADFD;
2770 if (runtime->channels > 128)
2771 return -EINVAL;
2772 if (put_user(0, &_xfern->result))
2773 return -EFAULT;
2774 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2775 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002776
2777 bufs = memdup_user(xfern.bufs,
2778 sizeof(void *) * runtime->channels);
2779 if (IS_ERR(bufs))
2780 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2782 kfree(bufs);
2783 __put_user(result, &_xfern->result);
2784 return result < 0 ? result : 0;
2785 }
2786 case SNDRV_PCM_IOCTL_REWIND:
2787 {
2788 snd_pcm_uframes_t frames;
2789 snd_pcm_uframes_t __user *_frames = arg;
2790 snd_pcm_sframes_t result;
2791 if (get_user(frames, _frames))
2792 return -EFAULT;
2793 if (put_user(0, _frames))
2794 return -EFAULT;
2795 result = snd_pcm_playback_rewind(substream, frames);
2796 __put_user(result, _frames);
2797 return result < 0 ? result : 0;
2798 }
2799 case SNDRV_PCM_IOCTL_FORWARD:
2800 {
2801 snd_pcm_uframes_t frames;
2802 snd_pcm_uframes_t __user *_frames = arg;
2803 snd_pcm_sframes_t result;
2804 if (get_user(frames, _frames))
2805 return -EFAULT;
2806 if (put_user(0, _frames))
2807 return -EFAULT;
2808 result = snd_pcm_playback_forward(substream, frames);
2809 __put_user(result, _frames);
2810 return result < 0 ? result : 0;
2811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002813 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
Takashi Iwai0df63e42006-04-28 15:13:41 +02002816static int snd_pcm_capture_ioctl1(struct file *file,
2817 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 unsigned int cmd, void __user *arg)
2819{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002820 if (snd_BUG_ON(!substream))
2821 return -ENXIO;
2822 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2823 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 switch (cmd) {
2825 case SNDRV_PCM_IOCTL_READI_FRAMES:
2826 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002827 struct snd_xferi xferi;
2828 struct snd_xferi __user *_xferi = arg;
2829 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 snd_pcm_sframes_t result;
2831 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2832 return -EBADFD;
2833 if (put_user(0, &_xferi->result))
2834 return -EFAULT;
2835 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2836 return -EFAULT;
2837 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2838 __put_user(result, &_xferi->result);
2839 return result < 0 ? result : 0;
2840 }
2841 case SNDRV_PCM_IOCTL_READN_FRAMES:
2842 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002843 struct snd_xfern xfern;
2844 struct snd_xfern __user *_xfern = arg;
2845 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 void *bufs;
2847 snd_pcm_sframes_t result;
2848 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2849 return -EBADFD;
2850 if (runtime->channels > 128)
2851 return -EINVAL;
2852 if (put_user(0, &_xfern->result))
2853 return -EFAULT;
2854 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2855 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002856
2857 bufs = memdup_user(xfern.bufs,
2858 sizeof(void *) * runtime->channels);
2859 if (IS_ERR(bufs))
2860 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2862 kfree(bufs);
2863 __put_user(result, &_xfern->result);
2864 return result < 0 ? result : 0;
2865 }
2866 case SNDRV_PCM_IOCTL_REWIND:
2867 {
2868 snd_pcm_uframes_t frames;
2869 snd_pcm_uframes_t __user *_frames = arg;
2870 snd_pcm_sframes_t result;
2871 if (get_user(frames, _frames))
2872 return -EFAULT;
2873 if (put_user(0, _frames))
2874 return -EFAULT;
2875 result = snd_pcm_capture_rewind(substream, frames);
2876 __put_user(result, _frames);
2877 return result < 0 ? result : 0;
2878 }
2879 case SNDRV_PCM_IOCTL_FORWARD:
2880 {
2881 snd_pcm_uframes_t frames;
2882 snd_pcm_uframes_t __user *_frames = arg;
2883 snd_pcm_sframes_t result;
2884 if (get_user(frames, _frames))
2885 return -EFAULT;
2886 if (put_user(0, _frames))
2887 return -EFAULT;
2888 result = snd_pcm_capture_forward(substream, frames);
2889 __put_user(result, _frames);
2890 return result < 0 ? result : 0;
2891 }
2892 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002893 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
Takashi Iwai877211f2005-11-17 13:59:38 +01002896static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2897 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Takashi Iwai877211f2005-11-17 13:59:38 +01002899 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 pcm_file = file->private_data;
2902
2903 if (((cmd >> 8) & 0xff) != 'A')
2904 return -ENOTTY;
2905
Takashi Iwai0df63e42006-04-28 15:13:41 +02002906 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2907 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}
2909
Takashi Iwai877211f2005-11-17 13:59:38 +01002910static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2911 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
Takashi Iwai877211f2005-11-17 13:59:38 +01002913 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 pcm_file = file->private_data;
2916
2917 if (((cmd >> 8) & 0xff) != 'A')
2918 return -ENOTTY;
2919
Takashi Iwai0df63e42006-04-28 15:13:41 +02002920 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2921 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002924/**
2925 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
2926 * @substream: PCM substream
2927 * @cmd: IOCTL cmd
2928 * @arg: IOCTL argument
2929 *
2930 * The function is provided primarily for OSS layer and USB gadget drivers,
2931 * and it allows only the limited set of ioctls (hw_params, sw_params,
2932 * prepare, start, drain, drop, forward).
2933 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002934int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 unsigned int cmd, void *arg)
2936{
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002937 snd_pcm_uframes_t *frames = arg;
2938 snd_pcm_sframes_t result;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002939
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002940 switch (cmd) {
2941 case SNDRV_PCM_IOCTL_FORWARD:
2942 {
2943 /* provided only for OSS; capture-only and no value returned */
2944 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
2945 return -EINVAL;
2946 result = snd_pcm_capture_forward(substream, *frames);
2947 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 }
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002949 case SNDRV_PCM_IOCTL_HW_PARAMS:
2950 return snd_pcm_hw_params(substream, arg);
2951 case SNDRV_PCM_IOCTL_SW_PARAMS:
2952 return snd_pcm_sw_params(substream, arg);
2953 case SNDRV_PCM_IOCTL_PREPARE:
2954 return snd_pcm_prepare(substream, NULL);
2955 case SNDRV_PCM_IOCTL_START:
2956 return snd_pcm_start_lock_irq(substream);
2957 case SNDRV_PCM_IOCTL_DRAIN:
2958 return snd_pcm_drain(substream, NULL);
2959 case SNDRV_PCM_IOCTL_DROP:
2960 return snd_pcm_drop(substream);
2961 case SNDRV_PCM_IOCTL_DELAY:
2962 {
2963 result = snd_pcm_delay(substream);
2964 if (result < 0)
2965 return result;
2966 *frames = result;
2967 return 0;
2968 }
2969 default:
2970 return -EINVAL;
2971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002973EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2974
Takashi Iwai877211f2005-11-17 13:59:38 +01002975static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2976 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Takashi Iwai877211f2005-11-17 13:59:38 +01002978 struct snd_pcm_file *pcm_file;
2979 struct snd_pcm_substream *substream;
2980 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 snd_pcm_sframes_t result;
2982
2983 pcm_file = file->private_data;
2984 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002985 if (PCM_RUNTIME_CHECK(substream))
2986 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 runtime = substream->runtime;
2988 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2989 return -EBADFD;
2990 if (!frame_aligned(runtime, count))
2991 return -EINVAL;
2992 count = bytes_to_frames(runtime, count);
2993 result = snd_pcm_lib_read(substream, buf, count);
2994 if (result > 0)
2995 result = frames_to_bytes(runtime, result);
2996 return result;
2997}
2998
Takashi Iwai877211f2005-11-17 13:59:38 +01002999static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3000 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Takashi Iwai877211f2005-11-17 13:59:38 +01003002 struct snd_pcm_file *pcm_file;
3003 struct snd_pcm_substream *substream;
3004 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 snd_pcm_sframes_t result;
3006
3007 pcm_file = file->private_data;
3008 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003009 if (PCM_RUNTIME_CHECK(substream))
3010 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003012 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3013 return -EBADFD;
3014 if (!frame_aligned(runtime, count))
3015 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 count = bytes_to_frames(runtime, count);
3017 result = snd_pcm_lib_write(substream, buf, count);
3018 if (result > 0)
3019 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return result;
3021}
3022
Al Viro1c65d982015-04-04 00:19:32 -04003023static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Takashi Iwai877211f2005-11-17 13:59:38 +01003025 struct snd_pcm_file *pcm_file;
3026 struct snd_pcm_substream *substream;
3027 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 snd_pcm_sframes_t result;
3029 unsigned long i;
3030 void __user **bufs;
3031 snd_pcm_uframes_t frames;
3032
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003033 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003035 if (PCM_RUNTIME_CHECK(substream))
3036 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 runtime = substream->runtime;
3038 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3039 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003040 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003042 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003044 if (!frame_aligned(runtime, to->iov->iov_len))
3045 return -EINVAL;
3046 frames = bytes_to_samples(runtime, to->iov->iov_len);
3047 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (bufs == NULL)
3049 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003050 for (i = 0; i < to->nr_segs; ++i)
3051 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 result = snd_pcm_lib_readv(substream, bufs, frames);
3053 if (result > 0)
3054 result = frames_to_bytes(runtime, result);
3055 kfree(bufs);
3056 return result;
3057}
3058
Al Viro1c65d982015-04-04 00:19:32 -04003059static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Takashi Iwai877211f2005-11-17 13:59:38 +01003061 struct snd_pcm_file *pcm_file;
3062 struct snd_pcm_substream *substream;
3063 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 snd_pcm_sframes_t result;
3065 unsigned long i;
3066 void __user **bufs;
3067 snd_pcm_uframes_t frames;
3068
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003069 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003071 if (PCM_RUNTIME_CHECK(substream))
3072 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003074 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3075 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003076 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003077 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003078 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3079 !frame_aligned(runtime, from->iov->iov_len))
3080 return -EINVAL;
3081 frames = bytes_to_samples(runtime, from->iov->iov_len);
3082 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (bufs == NULL)
3084 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003085 for (i = 0; i < from->nr_segs; ++i)
3086 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 result = snd_pcm_lib_writev(substream, bufs, frames);
3088 if (result > 0)
3089 result = frames_to_bytes(runtime, result);
3090 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return result;
3092}
3093
3094static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3095{
Takashi Iwai877211f2005-11-17 13:59:38 +01003096 struct snd_pcm_file *pcm_file;
3097 struct snd_pcm_substream *substream;
3098 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 unsigned int mask;
3100 snd_pcm_uframes_t avail;
3101
3102 pcm_file = file->private_data;
3103
3104 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003105 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003106 return POLLOUT | POLLWRNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 runtime = substream->runtime;
3108
3109 poll_wait(file, &runtime->sleep, wait);
3110
3111 snd_pcm_stream_lock_irq(substream);
3112 avail = snd_pcm_playback_avail(runtime);
3113 switch (runtime->status->state) {
3114 case SNDRV_PCM_STATE_RUNNING:
3115 case SNDRV_PCM_STATE_PREPARED:
3116 case SNDRV_PCM_STATE_PAUSED:
3117 if (avail >= runtime->control->avail_min) {
3118 mask = POLLOUT | POLLWRNORM;
3119 break;
3120 }
3121 /* Fall through */
3122 case SNDRV_PCM_STATE_DRAINING:
3123 mask = 0;
3124 break;
3125 default:
3126 mask = POLLOUT | POLLWRNORM | POLLERR;
3127 break;
3128 }
3129 snd_pcm_stream_unlock_irq(substream);
3130 return mask;
3131}
3132
3133static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3134{
Takashi Iwai877211f2005-11-17 13:59:38 +01003135 struct snd_pcm_file *pcm_file;
3136 struct snd_pcm_substream *substream;
3137 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 unsigned int mask;
3139 snd_pcm_uframes_t avail;
3140
3141 pcm_file = file->private_data;
3142
3143 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003144 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003145 return POLLIN | POLLRDNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 runtime = substream->runtime;
3147
3148 poll_wait(file, &runtime->sleep, wait);
3149
3150 snd_pcm_stream_lock_irq(substream);
3151 avail = snd_pcm_capture_avail(runtime);
3152 switch (runtime->status->state) {
3153 case SNDRV_PCM_STATE_RUNNING:
3154 case SNDRV_PCM_STATE_PREPARED:
3155 case SNDRV_PCM_STATE_PAUSED:
3156 if (avail >= runtime->control->avail_min) {
3157 mask = POLLIN | POLLRDNORM;
3158 break;
3159 }
3160 mask = 0;
3161 break;
3162 case SNDRV_PCM_STATE_DRAINING:
3163 if (avail > 0) {
3164 mask = POLLIN | POLLRDNORM;
3165 break;
3166 }
3167 /* Fall through */
3168 default:
3169 mask = POLLIN | POLLRDNORM | POLLERR;
3170 break;
3171 }
3172 snd_pcm_stream_unlock_irq(substream);
3173 return mask;
3174}
3175
3176/*
3177 * mmap support
3178 */
3179
3180/*
3181 * Only on coherent architectures, we can mmap the status and the control records
3182 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3183 */
3184#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3185/*
3186 * mmap status record
3187 */
Dave Jiang11bac802017-02-24 14:56:41 -08003188static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
Dave Jiang11bac802017-02-24 14:56:41 -08003190 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003191 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003194 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003196 vmf->page = virt_to_page(runtime->status);
3197 get_page(vmf->page);
3198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199}
3200
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003201static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003203 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204};
3205
Takashi Iwai877211f2005-11-17 13:59:38 +01003206static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 struct vm_area_struct *area)
3208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 long size;
3210 if (!(area->vm_flags & VM_READ))
3211 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003213 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return -EINVAL;
3215 area->vm_ops = &snd_pcm_vm_ops_status;
3216 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003217 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return 0;
3219}
3220
3221/*
3222 * mmap control record
3223 */
Dave Jiang11bac802017-02-24 14:56:41 -08003224static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
Dave Jiang11bac802017-02-24 14:56:41 -08003226 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003227 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003230 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003232 vmf->page = virt_to_page(runtime->control);
3233 get_page(vmf->page);
3234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003237static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003239 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240};
3241
Takashi Iwai877211f2005-11-17 13:59:38 +01003242static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 struct vm_area_struct *area)
3244{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 long size;
3246 if (!(area->vm_flags & VM_READ))
3247 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003249 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return -EINVAL;
3251 area->vm_ops = &snd_pcm_vm_ops_control;
3252 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003253 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return 0;
3255}
3256#else /* ! coherent mmap */
3257/*
3258 * don't support mmap for status and control records.
3259 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003260static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 struct vm_area_struct *area)
3262{
3263 return -ENXIO;
3264}
Takashi Iwai877211f2005-11-17 13:59:38 +01003265static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct vm_area_struct *area)
3267{
3268 return -ENXIO;
3269}
3270#endif /* coherent mmap */
3271
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003272static inline struct page *
3273snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3274{
3275 void *vaddr = substream->runtime->dma_area + ofs;
3276 return virt_to_page(vaddr);
3277}
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003280 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 */
Dave Jiang11bac802017-02-24 14:56:41 -08003282static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Dave Jiang11bac802017-02-24 14:56:41 -08003284 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003285 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 unsigned long offset;
3287 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 size_t dma_bytes;
3289
3290 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003291 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003293 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3295 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003296 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003297 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003299 else
3300 page = snd_pcm_default_page_ops(substream, offset);
3301 if (!page)
3302 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003303 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003304 vmf->page = page;
3305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
Takashi Iwai657b1982009-11-26 12:40:21 +01003308static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3309 .open = snd_pcm_mmap_data_open,
3310 .close = snd_pcm_mmap_data_close,
3311};
3312
3313static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 .open = snd_pcm_mmap_data_open,
3315 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003316 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317};
3318
3319/*
3320 * mmap the DMA buffer on RAM
3321 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003322
3323/**
3324 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3325 * @substream: PCM substream
3326 * @area: VMA
3327 *
3328 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3329 * this function is invoked implicitly.
3330 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003331int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3332 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003334 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003335#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003336 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3337 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3338 return remap_pfn_range(area, area->vm_start,
3339 substream->dma_buffer.addr >> PAGE_SHIFT,
3340 area->vm_end - area->vm_start, area->vm_page_prot);
3341 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003342#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003343#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003344 if (!substream->ops->page &&
3345 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3346 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3347 area,
3348 substream->runtime->dma_area,
3349 substream->runtime->dma_addr,
3350 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003351#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003352 /* mmap with fault handler */
3353 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 return 0;
3355}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003356EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358/*
3359 * mmap the DMA buffer on I/O memory area
3360 */
3361#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003362/**
3363 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3364 * @substream: PCM substream
3365 * @area: VMA
3366 *
3367 * When your hardware uses the iomapped pages as the hardware buffer and
3368 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3369 * is supposed to work only on limited architectures.
3370 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003371int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3372 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003374 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003377 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003379
3380EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381#endif /* SNDRV_PCM_INFO_MMAP */
3382
3383/*
3384 * mmap DMA buffer
3385 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003386int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 struct vm_area_struct *area)
3388{
Takashi Iwai877211f2005-11-17 13:59:38 +01003389 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 long size;
3391 unsigned long offset;
3392 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003393 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3396 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3397 return -EINVAL;
3398 } else {
3399 if (!(area->vm_flags & VM_READ))
3400 return -EINVAL;
3401 }
3402 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3404 return -EBADFD;
3405 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3406 return -ENXIO;
3407 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3408 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3409 return -EINVAL;
3410 size = area->vm_end - area->vm_start;
3411 offset = area->vm_pgoff << PAGE_SHIFT;
3412 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3413 if ((size_t)size > dma_bytes)
3414 return -EINVAL;
3415 if (offset > dma_bytes - size)
3416 return -EINVAL;
3417
Takashi Iwai657b1982009-11-26 12:40:21 +01003418 area->vm_ops = &snd_pcm_vm_ops_data;
3419 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003421 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003423 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003424 if (!err)
3425 atomic_inc(&substream->mmap_count);
3426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003429EXPORT_SYMBOL(snd_pcm_mmap_data);
3430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3432{
Takashi Iwai877211f2005-11-17 13:59:38 +01003433 struct snd_pcm_file * pcm_file;
3434 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 unsigned long offset;
3436
3437 pcm_file = file->private_data;
3438 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003439 if (PCM_RUNTIME_CHECK(substream))
3440 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 offset = area->vm_pgoff << PAGE_SHIFT;
3443 switch (offset) {
3444 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003445 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 return -ENXIO;
3447 return snd_pcm_mmap_status(substream, file, area);
3448 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003449 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return -ENXIO;
3451 return snd_pcm_mmap_control(substream, file, area);
3452 default:
3453 return snd_pcm_mmap_data(substream, file, area);
3454 }
3455 return 0;
3456}
3457
3458static int snd_pcm_fasync(int fd, struct file * file, int on)
3459{
Takashi Iwai877211f2005-11-17 13:59:38 +01003460 struct snd_pcm_file * pcm_file;
3461 struct snd_pcm_substream *substream;
3462 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464 pcm_file = file->private_data;
3465 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003466 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003467 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003469 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
3472/*
3473 * ioctl32 compat
3474 */
3475#ifdef CONFIG_COMPAT
3476#include "pcm_compat.c"
3477#else
3478#define snd_pcm_ioctl_compat NULL
3479#endif
3480
3481/*
3482 * To be removed helpers to keep binary compatibility
3483 */
3484
Takashi Iwai59d48582005-12-01 10:51:58 +01003485#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3487#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3488
Takashi Iwai877211f2005-11-17 13:59:38 +01003489static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3490 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 unsigned int i;
3493
3494 memset(params, 0, sizeof(*params));
3495 params->flags = oparams->flags;
3496 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3497 params->masks[i].bits[0] = oparams->masks[i];
3498 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3499 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3500 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3501 params->info = oparams->info;
3502 params->msbits = oparams->msbits;
3503 params->rate_num = oparams->rate_num;
3504 params->rate_den = oparams->rate_den;
3505 params->fifo_size = oparams->fifo_size;
3506}
3507
Takashi Iwai877211f2005-11-17 13:59:38 +01003508static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3509 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510{
3511 unsigned int i;
3512
3513 memset(oparams, 0, sizeof(*oparams));
3514 oparams->flags = params->flags;
3515 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3516 oparams->masks[i] = params->masks[i].bits[0];
3517 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3518 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3519 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3520 oparams->info = params->info;
3521 oparams->msbits = params->msbits;
3522 oparams->rate_num = params->rate_num;
3523 oparams->rate_den = params->rate_den;
3524 oparams->fifo_size = params->fifo_size;
3525}
3526
Takashi Iwai877211f2005-11-17 13:59:38 +01003527static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3528 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Takashi Iwai877211f2005-11-17 13:59:38 +01003530 struct snd_pcm_hw_params *params;
3531 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 int err;
3533
3534 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003535 if (!params)
3536 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Li Zefanef44a1e2009-04-10 09:43:08 +08003538 oparams = memdup_user(_oparams, sizeof(*oparams));
3539 if (IS_ERR(oparams)) {
3540 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 goto out;
3542 }
3543 snd_pcm_hw_convert_from_old_params(params, oparams);
3544 err = snd_pcm_hw_refine(substream, params);
3545 snd_pcm_hw_convert_to_old_params(oparams, params);
3546 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3547 if (!err)
3548 err = -EFAULT;
3549 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003550
3551 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552out:
3553 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 return err;
3555}
3556
Takashi Iwai877211f2005-11-17 13:59:38 +01003557static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3558 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
Takashi Iwai877211f2005-11-17 13:59:38 +01003560 struct snd_pcm_hw_params *params;
3561 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 int err;
3563
3564 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003565 if (!params)
3566 return -ENOMEM;
3567
3568 oparams = memdup_user(_oparams, sizeof(*oparams));
3569 if (IS_ERR(oparams)) {
3570 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 goto out;
3572 }
3573 snd_pcm_hw_convert_from_old_params(params, oparams);
3574 err = snd_pcm_hw_params(substream, params);
3575 snd_pcm_hw_convert_to_old_params(oparams, params);
3576 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3577 if (!err)
3578 err = -EFAULT;
3579 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003580
3581 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582out:
3583 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return err;
3585}
Takashi Iwai59d48582005-12-01 10:51:58 +01003586#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Cliff Cai70036092008-09-03 10:54:36 +02003588#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003589static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3590 unsigned long addr,
3591 unsigned long len,
3592 unsigned long pgoff,
3593 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003594{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003595 struct snd_pcm_file *pcm_file = file->private_data;
3596 struct snd_pcm_substream *substream = pcm_file->substream;
3597 struct snd_pcm_runtime *runtime = substream->runtime;
3598 unsigned long offset = pgoff << PAGE_SHIFT;
3599
3600 switch (offset) {
3601 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3602 return (unsigned long)runtime->status;
3603 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3604 return (unsigned long)runtime->control;
3605 default:
3606 return (unsigned long)runtime->dma_area + offset;
3607 }
Cliff Cai70036092008-09-03 10:54:36 +02003608}
3609#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003610# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003611#endif
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613/*
3614 * Register section
3615 */
3616
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003617const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003619 .owner = THIS_MODULE,
3620 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003621 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003622 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003623 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003624 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003625 .poll = snd_pcm_playback_poll,
3626 .unlocked_ioctl = snd_pcm_playback_ioctl,
3627 .compat_ioctl = snd_pcm_ioctl_compat,
3628 .mmap = snd_pcm_mmap,
3629 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003630 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 },
3632 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003633 .owner = THIS_MODULE,
3634 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003635 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003636 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003637 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003638 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003639 .poll = snd_pcm_capture_poll,
3640 .unlocked_ioctl = snd_pcm_capture_ioctl,
3641 .compat_ioctl = snd_pcm_ioctl_compat,
3642 .mmap = snd_pcm_mmap,
3643 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003644 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 }
3646};