blob: cbde23fc67a9b9930bc8249ee24d40b50eb79c09 [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>
Chanho Minb888a5f72018-11-26 14:36:37 +090039#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Sakamoto2c4842d2017-05-26 09:30:46 +090041#include "pcm_local.h"
42
Takashi Sakamoto37567c52017-06-07 08:46:44 +090043#ifdef CONFIG_SND_DEBUG
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090044#define CREATE_TRACE_POINTS
45#include "pcm_param_trace.h"
Takashi Sakamoto37567c52017-06-07 08:46:44 +090046#else
47#define trace_hw_mask_param_enabled() 0
48#define trace_hw_interval_param_enabled() 0
49#define trace_hw_mask_param(substream, type, index, prev, curr)
50#define trace_hw_interval_param(substream, type, index, prev, curr)
51#endif
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * Compatibility
55 */
56
Takashi Iwai877211f2005-11-17 13:59:38 +010057struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned int flags;
59 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
60 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010061 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
63 unsigned int rmask;
64 unsigned int cmask;
65 unsigned int info;
66 unsigned int msbits;
67 unsigned int rate_num;
68 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010069 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned char reserved[64];
71};
72
Takashi Iwai59d48582005-12-01 10:51:58 +010073#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010074#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
75#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Takashi Iwai877211f2005-11-17 13:59:38 +010077static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
78 struct snd_pcm_hw_params_old __user * _oparams);
79static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
80 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010081#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010082static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 *
86 */
87
Takashi Iwai7af142f2014-09-01 11:19:37 +020088static 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
Chanho Minb888a5f72018-11-26 14:36:37 +090094 * sleep until all the readers are completed without blocking by writer.
Takashi Iwai67ec10722016-02-17 14:30:26 +010095 */
Chanho Minb888a5f72018-11-26 14:36:37 +090096static inline void down_write_nonfifo(struct rw_semaphore *lock)
Takashi Iwai67ec10722016-02-17 14:30:26 +010097{
98 while (!down_write_trylock(lock))
Chanho Minb888a5f72018-11-26 14:36:37 +090099 msleep(1);
Takashi Iwai67ec10722016-02-17 14:30:26 +0100100}
101
Takashi Iwai73365cb2019-01-13 09:35:17 +0100102void snd_pcm_group_init(struct snd_pcm_group *group)
103{
104 spin_lock_init(&group->lock);
105 mutex_init(&group->mutex);
106 INIT_LIST_HEAD(&group->substreams);
Takashi Iwaif57f3df2019-01-13 09:50:33 +0100107 refcount_set(&group->refs, 0);
Takashi Iwai73365cb2019-01-13 09:35:17 +0100108}
109
Takashi Iwaif57f3df2019-01-13 09:50:33 +0100110/* define group lock helpers */
111#define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
112static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
113{ \
114 if (nonatomic) \
115 mutex_ ## mutex_action(&group->mutex); \
116 else \
117 spin_ ## action(&group->lock); \
118}
119
120DEFINE_PCM_GROUP_LOCK(lock, lock);
121DEFINE_PCM_GROUP_LOCK(unlock, unlock);
122DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
123DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
124
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200125#define PCM_LOCK_DEFAULT 0
126#define PCM_LOCK_IRQ 1
127#define PCM_LOCK_IRQSAVE 2
128
129static unsigned long __snd_pcm_stream_lock_mode(struct snd_pcm_substream *substream,
130 unsigned int mode)
131{
132 unsigned long flags = 0;
133 if (substream->pcm->nonatomic) {
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200134 mutex_lock(&substream->self_group.mutex);
135 } else {
136 switch (mode) {
137 case PCM_LOCK_DEFAULT:
Takashi Iwaif57f3df2019-01-13 09:50:33 +0100138 spin_lock(&substream->self_group.lock);
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200139 break;
140 case PCM_LOCK_IRQ:
Takashi Iwaif57f3df2019-01-13 09:50:33 +0100141 spin_lock_irq(&substream->self_group.lock);
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200142 break;
143 case PCM_LOCK_IRQSAVE:
Takashi Iwaif57f3df2019-01-13 09:50:33 +0100144 spin_lock_irqsave(&substream->self_group.lock, flags);
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200145 break;
146 }
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200147 }
148 return flags;
149}
150
151static void __snd_pcm_stream_unlock_mode(struct snd_pcm_substream *substream,
152 unsigned int mode, unsigned long flags)
153{
154 if (substream->pcm->nonatomic) {
155 mutex_unlock(&substream->self_group.mutex);
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200156 } else {
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200157 switch (mode) {
158 case PCM_LOCK_DEFAULT:
Takashi Iwaif57f3df2019-01-13 09:50:33 +0100159 spin_unlock(&substream->self_group.lock);
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200160 break;
161 case PCM_LOCK_IRQ:
Takashi Iwaif57f3df2019-01-13 09:50:33 +0100162 spin_unlock_irq(&substream->self_group.lock);
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200163 break;
164 case PCM_LOCK_IRQSAVE:
Takashi Iwaif57f3df2019-01-13 09:50:33 +0100165 spin_unlock_irqrestore(&substream->self_group.lock, flags);
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200166 break;
167 }
168 }
169}
170
Takashi Iwai30b771c2014-10-30 15:02:50 +0100171/**
172 * snd_pcm_stream_lock - Lock the PCM stream
173 * @substream: PCM substream
174 *
175 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
176 * flag of the given substream. This also takes the global link rw lock
177 * (or rw sem), too, for avoiding the race with linked streams.
178 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200179void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
180{
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200181 __snd_pcm_stream_lock_mode(substream, PCM_LOCK_DEFAULT);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200182}
183EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
184
Takashi Iwai30b771c2014-10-30 15:02:50 +0100185/**
186 * snd_pcm_stream_lock - Unlock the PCM stream
187 * @substream: PCM substream
188 *
189 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
190 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200191void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
192{
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200193 __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_DEFAULT, 0);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200194}
195EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
196
Takashi Iwai30b771c2014-10-30 15:02:50 +0100197/**
198 * snd_pcm_stream_lock_irq - Lock the PCM stream
199 * @substream: PCM substream
200 *
201 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
202 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
203 * as snd_pcm_stream_lock().
204 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200205void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
206{
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200207 __snd_pcm_stream_lock_mode(substream, PCM_LOCK_IRQ);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200208}
209EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
210
Takashi Iwai30b771c2014-10-30 15:02:50 +0100211/**
212 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
213 * @substream: PCM substream
214 *
215 * This is a counter-part of snd_pcm_stream_lock_irq().
216 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200217void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
218{
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200219 __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_IRQ, 0);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200220}
221EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
222
223unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
224{
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200225 return __snd_pcm_stream_lock_mode(substream, PCM_LOCK_IRQSAVE);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200226}
227EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
228
Takashi Iwai30b771c2014-10-30 15:02:50 +0100229/**
230 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
231 * @substream: PCM substream
232 * @flags: irq flags
233 *
234 * This is a counter-part of snd_pcm_stream_lock_irqsave().
235 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200236void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
237 unsigned long flags)
238{
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +0200239 __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_IRQSAVE, flags);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200240}
241EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Takashi Iwai877211f2005-11-17 13:59:38 +0100243int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Takashi Iwai877211f2005-11-17 13:59:38 +0100245 struct snd_pcm *pcm = substream->pcm;
246 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 memset(info, 0, sizeof(*info));
249 info->card = pcm->card->number;
250 info->device = pcm->device;
251 info->stream = substream->stream;
252 info->subdevice = substream->number;
253 strlcpy(info->id, pcm->id, sizeof(info->id));
254 strlcpy(info->name, pcm->name, sizeof(info->name));
255 info->dev_class = pcm->dev_class;
256 info->dev_subclass = pcm->dev_subclass;
257 info->subdevices_count = pstr->substream_count;
258 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
259 strlcpy(info->subname, substream->name, sizeof(info->subname));
Takashi Sakamotoe11f0f92017-06-14 19:30:03 +0900260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262}
263
Takashi Iwai877211f2005-11-17 13:59:38 +0100264int snd_pcm_info_user(struct snd_pcm_substream *substream,
265 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Takashi Iwai877211f2005-11-17 13:59:38 +0100267 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int err;
269
270 info = kmalloc(sizeof(*info), GFP_KERNEL);
271 if (! info)
272 return -ENOMEM;
273 err = snd_pcm_info(substream, info);
274 if (err >= 0) {
275 if (copy_to_user(_info, info, sizeof(*info)))
276 err = -EFAULT;
277 }
278 kfree(info);
279 return err;
280}
281
Takashi Iwai63825f32014-10-22 12:04:46 +0200282static bool hw_support_mmap(struct snd_pcm_substream *substream)
283{
284 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
285 return false;
Takashi Iwai85dc0f82017-07-10 16:05:58 +0200286 /* architecture supports dma_mmap_coherent()? */
287#if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
Takashi Iwai63825f32014-10-22 12:04:46 +0200288 if (!substream->ops->mmap &&
289 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
290 return false;
291#endif
292 return true;
293}
294
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900295static int constrain_mask_params(struct snd_pcm_substream *substream,
296 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900298 struct snd_pcm_hw_constraints *constrs =
299 &substream->runtime->hw_constraints;
300 struct snd_mask *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned int k;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900302 struct snd_mask old_mask;
303 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
306 m = hw_param_mask(params, k);
307 if (snd_mask_empty(m))
308 return -EINVAL;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900309
310 /* This parameter is not requested to change by a caller. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!(params->rmask & (1 << k)))
312 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900313
314 if (trace_hw_mask_param_enabled())
315 old_mask = *m;
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 changed = snd_mask_refine(m, constrs_mask(constrs, k));
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900318 if (changed < 0)
319 return changed;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900320 if (changed == 0)
321 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900322
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900323 /* Set corresponding flag so that the caller gets it. */
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900324 trace_hw_mask_param(substream, k, 0, &old_mask, m);
325 params->cmask |= 1 << k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900328 return 0;
329}
330
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900331static int constrain_interval_params(struct snd_pcm_substream *substream,
332 struct snd_pcm_hw_params *params)
333{
334 struct snd_pcm_hw_constraints *constrs =
335 &substream->runtime->hw_constraints;
336 struct snd_interval *i;
337 unsigned int k;
338 struct snd_interval old_interval;
339 int changed;
340
341 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
342 i = hw_param_interval(params, k);
343 if (snd_interval_empty(i))
344 return -EINVAL;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900345
346 /* This parameter is not requested to change by a caller. */
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900347 if (!(params->rmask & (1 << k)))
348 continue;
349
350 if (trace_hw_interval_param_enabled())
351 old_interval = *i;
352
353 changed = snd_interval_refine(i, constrs_interval(constrs, k));
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900354 if (changed < 0)
355 return changed;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900356 if (changed == 0)
357 continue;
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900358
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900359 /* Set corresponding flag so that the caller gets it. */
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900360 trace_hw_interval_param(substream, k, 0, &old_interval, i);
361 params->cmask |= 1 << k;
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900362 }
363
364 return 0;
365}
366
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900367static int constrain_params_by_rules(struct snd_pcm_substream *substream,
368 struct snd_pcm_hw_params *params)
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900369{
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900370 struct snd_pcm_hw_constraints *constrs =
371 &substream->runtime->hw_constraints;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900372 unsigned int k;
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100373 unsigned int *rstamps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900375 unsigned int stamp;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900376 struct snd_pcm_hw_rule *r;
377 unsigned int d;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900378 struct snd_mask old_mask;
379 struct snd_interval old_interval;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900380 bool again;
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100381 int changed, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900383 /*
384 * Each application of rule has own sequence number.
385 *
386 * Each member of 'rstamps' array represents the sequence number of
387 * recent application of corresponding rule.
388 */
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100389 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
390 if (!rstamps)
391 return -ENOMEM;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900392
393 /*
394 * Each member of 'vstamps' array represents the sequence number of
395 * recent application of rule in which corresponding parameters were
396 * changed.
397 *
398 * In initial state, elements corresponding to parameters requested by
399 * a caller is 1. For unrequested parameters, corresponding members
400 * have 0 so that the parameters are never changed anymore.
401 */
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900402 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900404
405 /* Due to the above design, actual sequence number starts at 2. */
406 stamp = 2;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900407retry:
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900408 /* Apply all rules in order. */
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900409 again = false;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900410 for (k = 0; k < constrs->rules_num; k++) {
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900411 r = &constrs->rules[k];
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900412
413 /*
414 * Check condition bits of this rule. When the rule has
415 * some condition bits, parameter without the bits is
416 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
417 * is an example of the condition bits.
418 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900419 if (r->cond && !(r->cond & params->flags))
420 continue;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900421
422 /*
423 * The 'deps' array includes maximum three dependencies
424 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
425 * member of this array is a sentinel and should be
426 * negative value.
427 *
428 * This rule should be processed in this time when dependent
429 * parameters were changed at former applications of the other
430 * rules.
431 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900432 for (d = 0; r->deps[d] >= 0; d++) {
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900433 if (vstamps[r->deps[d]] > rstamps[k])
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900436 if (r->deps[d] < 0)
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900437 continue;
438
439 if (trace_hw_mask_param_enabled()) {
440 if (hw_is_mask(r->var))
441 old_mask = *hw_param_mask(params, r->var);
442 }
443 if (trace_hw_interval_param_enabled()) {
444 if (hw_is_interval(r->var))
445 old_interval = *hw_param_interval(params, r->var);
446 }
447
448 changed = r->func(params, r);
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100449 if (changed < 0) {
450 err = changed;
451 goto out;
452 }
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900453
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900454 /*
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900455 * When the parameter is changed, notify it to the caller
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900456 * by corresponding returned bit, then preparing for next
457 * iteration.
458 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900459 if (changed && r->var >= 0) {
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900460 if (hw_is_mask(r->var)) {
461 trace_hw_mask_param(substream, r->var,
462 k + 1, &old_mask,
463 hw_param_mask(params, r->var));
464 }
465 if (hw_is_interval(r->var)) {
466 trace_hw_interval_param(substream, r->var,
467 k + 1, &old_interval,
468 hw_param_interval(params, r->var));
469 }
470
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900471 params->cmask |= (1 << r->var);
472 vstamps[r->var] = stamp;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900473 again = true;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900474 }
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900475
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900476 rstamps[k] = stamp++;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900477 }
478
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900479 /* Iterate to evaluate all rules till no parameters are changed. */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900480 if (again)
481 goto retry;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900482
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100483 out:
484 kfree(rstamps);
485 return err;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900486}
487
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900488static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
489 struct snd_pcm_hw_params *params)
490{
491 const struct snd_interval *i;
492 const struct snd_mask *m;
493 int err;
494
495 if (!params->msbits) {
496 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
497 if (snd_interval_single(i))
498 params->msbits = snd_interval_value(i);
499 }
500
501 if (!params->rate_den) {
502 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
503 if (snd_interval_single(i)) {
504 params->rate_num = snd_interval_value(i);
505 params->rate_den = 1;
506 }
507 }
508
509 if (!params->fifo_size) {
510 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
511 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
512 if (snd_mask_single(m) && snd_interval_single(i)) {
513 err = substream->ops->ioctl(substream,
514 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
515 if (err < 0)
516 return err;
517 }
518 }
519
Takashi Sakamoto7802fb52017-06-09 09:34:40 +0900520 if (!params->info) {
521 params->info = substream->runtime->hw.info;
522 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
523 SNDRV_PCM_INFO_DRAIN_TRIGGER);
524 if (!hw_support_mmap(substream))
525 params->info &= ~(SNDRV_PCM_INFO_MMAP |
526 SNDRV_PCM_INFO_MMAP_VALID);
527 }
528
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900529 return 0;
530}
531
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900532int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
533 struct snd_pcm_hw_params *params)
534{
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900535 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 params->info = 0;
538 params->fifo_size = 0;
539 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
540 params->msbits = 0;
541 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
542 params->rate_num = 0;
543 params->rate_den = 0;
544 }
545
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900546 err = constrain_mask_params(substream, params);
547 if (err < 0)
548 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900550 err = constrain_interval_params(substream, params);
551 if (err < 0)
552 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900554 err = constrain_params_by_rules(substream, params);
555 if (err < 0)
556 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 params->rmask = 0;
Takashi Sakamoto7802fb52017-06-09 09:34:40 +0900559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return 0;
561}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200562EXPORT_SYMBOL(snd_pcm_hw_refine);
563
Takashi Iwai877211f2005-11-17 13:59:38 +0100564static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
565 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Takashi Iwai877211f2005-11-17 13:59:38 +0100567 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int err;
569
Li Zefanef44a1e2009-04-10 09:43:08 +0800570 params = memdup_user(_params, sizeof(*params));
571 if (IS_ERR(params))
572 return PTR_ERR(params);
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 err = snd_pcm_hw_refine(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900575 if (err < 0)
576 goto end;
Li Zefanef44a1e2009-04-10 09:43:08 +0800577
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900578 err = fixup_unreferenced_params(substream, params);
579 if (err < 0)
580 goto end;
581
582 if (copy_to_user(_params, params, sizeof(*params)))
583 err = -EFAULT;
584end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 kfree(params);
586 return err;
587}
588
Takashi Iwai9442e692006-09-30 23:27:19 -0700589static int period_to_usecs(struct snd_pcm_runtime *runtime)
590{
591 int usecs;
592
593 if (! runtime->rate)
594 return -1; /* invalid */
595
596 /* take 75% of period time as the deadline */
597 usecs = (750000 / runtime->rate) * runtime->period_size;
598 usecs += ((750000 % runtime->rate) * runtime->period_size) /
599 runtime->rate;
600
601 return usecs;
602}
603
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200604static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
605{
606 snd_pcm_stream_lock_irq(substream);
607 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
608 substream->runtime->status->state = state;
609 snd_pcm_stream_unlock_irq(substream);
610}
611
Jie Yang90bbaf62015-10-16 17:57:46 +0800612static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
613 int event)
614{
615#ifdef CONFIG_SND_PCM_TIMER
616 if (substream->timer)
617 snd_timer_notify(substream->timer, event,
618 &substream->runtime->trigger_tstamp);
619#endif
620}
621
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900622/**
623 * snd_pcm_hw_param_choose - choose a configuration defined by @params
624 * @pcm: PCM instance
625 * @params: the hw_params instance
626 *
627 * Choose one configuration from configuration space defined by @params.
628 * The configuration chosen is that obtained fixing in this order:
629 * first access, first format, first subformat, min channels,
630 * min rate, min period time, max buffer size, min tick time
631 *
632 * Return: Zero if successful, or a negative error code on failure.
633 */
634static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
635 struct snd_pcm_hw_params *params)
636{
637 static const int vars[] = {
638 SNDRV_PCM_HW_PARAM_ACCESS,
639 SNDRV_PCM_HW_PARAM_FORMAT,
640 SNDRV_PCM_HW_PARAM_SUBFORMAT,
641 SNDRV_PCM_HW_PARAM_CHANNELS,
642 SNDRV_PCM_HW_PARAM_RATE,
643 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
644 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
645 SNDRV_PCM_HW_PARAM_TICK_TIME,
646 -1
647 };
648 const int *v;
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900649 struct snd_mask old_mask;
650 struct snd_interval old_interval;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900651 int changed;
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900652
653 for (v = vars; *v != -1; v++) {
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900654 /* Keep old parameter to trace. */
655 if (trace_hw_mask_param_enabled()) {
656 if (hw_is_mask(*v))
657 old_mask = *hw_param_mask(params, *v);
658 }
659 if (trace_hw_interval_param_enabled()) {
660 if (hw_is_interval(*v))
661 old_interval = *hw_param_interval(params, *v);
662 }
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900663 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900664 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900665 else
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900666 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwaie1a3a982018-04-09 17:12:16 +0200667 if (changed < 0)
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900668 return changed;
669 if (changed == 0)
670 continue;
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900671
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900672 /* Trace the changed parameter. */
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900673 if (hw_is_mask(*v)) {
674 trace_hw_mask_param(pcm, *v, 0, &old_mask,
675 hw_param_mask(params, *v));
676 }
677 if (hw_is_interval(*v)) {
678 trace_hw_interval_param(pcm, *v, 0, &old_interval,
679 hw_param_interval(params, *v));
680 }
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900681 }
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900682
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900683 return 0;
684}
685
Takashi Iwai877211f2005-11-17 13:59:38 +0100686static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
687 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Takashi Iwai877211f2005-11-17 13:59:38 +0100689 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700690 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned int bits;
692 snd_pcm_uframes_t frames;
693
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200694 if (PCM_RUNTIME_CHECK(substream))
695 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 snd_pcm_stream_lock_irq(substream);
698 switch (runtime->status->state) {
699 case SNDRV_PCM_STATE_OPEN:
700 case SNDRV_PCM_STATE_SETUP:
701 case SNDRV_PCM_STATE_PREPARED:
702 break;
703 default:
704 snd_pcm_stream_unlock_irq(substream);
705 return -EBADFD;
706 }
707 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100708#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (!substream->oss.oss)
710#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200711 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return -EBADFD;
713
714 params->rmask = ~0U;
715 err = snd_pcm_hw_refine(substream, params);
716 if (err < 0)
717 goto _error;
718
719 err = snd_pcm_hw_params_choose(substream, params);
720 if (err < 0)
721 goto _error;
722
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900723 err = fixup_unreferenced_params(substream, params);
724 if (err < 0)
725 goto _error;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (substream->ops->hw_params != NULL) {
728 err = substream->ops->hw_params(substream, params);
729 if (err < 0)
730 goto _error;
731 }
732
733 runtime->access = params_access(params);
734 runtime->format = params_format(params);
735 runtime->subformat = params_subformat(params);
736 runtime->channels = params_channels(params);
737 runtime->rate = params_rate(params);
738 runtime->period_size = params_period_size(params);
739 runtime->periods = params_periods(params);
740 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 runtime->info = params->info;
742 runtime->rate_num = params->rate_num;
743 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100744 runtime->no_period_wakeup =
745 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
746 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 bits = snd_pcm_format_physical_width(runtime->format);
749 runtime->sample_bits = bits;
750 bits *= runtime->channels;
751 runtime->frame_bits = bits;
752 frames = 1;
753 while (bits % 8 != 0) {
754 bits *= 2;
755 frames *= 2;
756 }
757 runtime->byte_align = bits / 8;
758 runtime->min_align = frames;
759
760 /* Default sw params */
761 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
762 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 runtime->start_threshold = 1;
765 runtime->stop_threshold = runtime->buffer_size;
766 runtime->silence_threshold = 0;
767 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200768 runtime->boundary = runtime->buffer_size;
769 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
770 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200773 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700774
James Bottomley82f68252010-07-05 22:53:06 +0200775 if (pm_qos_request_active(&substream->latency_pm_qos_req))
776 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700777 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200778 pm_qos_add_request(&substream->latency_pm_qos_req,
779 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
781 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300782 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 so we force application to retry to set
784 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200785 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (substream->ops->hw_free != NULL)
787 substream->ops->hw_free(substream);
788 return err;
789}
790
Takashi Iwai877211f2005-11-17 13:59:38 +0100791static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
792 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Takashi Iwai877211f2005-11-17 13:59:38 +0100794 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int err;
796
Li Zefanef44a1e2009-04-10 09:43:08 +0800797 params = memdup_user(_params, sizeof(*params));
798 if (IS_ERR(params))
799 return PTR_ERR(params);
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 err = snd_pcm_hw_params(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900802 if (err < 0)
803 goto end;
Li Zefanef44a1e2009-04-10 09:43:08 +0800804
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900805 if (copy_to_user(_params, params, sizeof(*params)))
806 err = -EFAULT;
807end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 kfree(params);
809 return err;
810}
811
Takashi Iwai877211f2005-11-17 13:59:38 +0100812static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Takashi Iwai877211f2005-11-17 13:59:38 +0100814 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 int result = 0;
816
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200817 if (PCM_RUNTIME_CHECK(substream))
818 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 snd_pcm_stream_lock_irq(substream);
821 switch (runtime->status->state) {
822 case SNDRV_PCM_STATE_SETUP:
823 case SNDRV_PCM_STATE_PREPARED:
824 break;
825 default:
826 snd_pcm_stream_unlock_irq(substream);
827 return -EBADFD;
828 }
829 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200830 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return -EBADFD;
832 if (substream->ops->hw_free)
833 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200834 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200835 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return result;
837}
838
Takashi Iwai877211f2005-11-17 13:59:38 +0100839static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
840 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Takashi Iwai877211f2005-11-17 13:59:38 +0100842 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100843 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200845 if (PCM_RUNTIME_CHECK(substream))
846 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 snd_pcm_stream_lock_irq(substream);
849 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
850 snd_pcm_stream_unlock_irq(substream);
851 return -EBADFD;
852 }
853 snd_pcm_stream_unlock_irq(substream);
854
Dan Carpenter145d92e2015-09-23 12:42:28 +0300855 if (params->tstamp_mode < 0 ||
856 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -EINVAL;
Takashi Iwai589008102014-07-16 17:45:27 +0200858 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
859 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200860 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (params->avail_min == 0)
862 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (params->silence_size >= runtime->boundary) {
864 if (params->silence_threshold != 0)
865 return -EINVAL;
866 } else {
867 if (params->silence_size > params->silence_threshold)
868 return -EINVAL;
869 if (params->silence_threshold > runtime->buffer_size)
870 return -EINVAL;
871 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100872 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 snd_pcm_stream_lock_irq(substream);
874 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai589008102014-07-16 17:45:27 +0200875 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
876 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 runtime->period_step = params->period_step;
878 runtime->control->avail_min = params->avail_min;
879 runtime->start_threshold = params->start_threshold;
880 runtime->stop_threshold = params->stop_threshold;
881 runtime->silence_threshold = params->silence_threshold;
882 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 params->boundary = runtime->boundary;
884 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
886 runtime->silence_size > 0)
887 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100888 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100891 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Takashi Iwai877211f2005-11-17 13:59:38 +0100894static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
895 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Takashi Iwai877211f2005-11-17 13:59:38 +0100897 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int err;
899 if (copy_from_user(&params, _params, sizeof(params)))
900 return -EFAULT;
901 err = snd_pcm_sw_params(substream, &params);
902 if (copy_to_user(_params, &params, sizeof(params)))
903 return -EFAULT;
904 return err;
905}
906
Takashi Iwaic99c5a32018-04-11 18:07:27 +0200907static inline snd_pcm_uframes_t
908snd_pcm_calc_delay(struct snd_pcm_substream *substream)
909{
910 snd_pcm_uframes_t delay;
911
912 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
913 delay = snd_pcm_playback_hw_avail(substream->runtime);
914 else
915 delay = snd_pcm_capture_avail(substream->runtime);
916 return delay + substream->runtime->delay;
917}
918
Takashi Iwai877211f2005-11-17 13:59:38 +0100919int snd_pcm_status(struct snd_pcm_substream *substream,
920 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Takashi Iwai877211f2005-11-17 13:59:38 +0100922 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600925
926 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
927 &runtime->audio_tstamp_config);
928
929 /* backwards compatible behavior */
930 if (runtime->audio_tstamp_config.type_requested ==
931 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
932 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
933 runtime->audio_tstamp_config.type_requested =
934 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
935 else
936 runtime->audio_tstamp_config.type_requested =
937 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
938 runtime->audio_tstamp_report.valid = 0;
939 } else
940 runtime->audio_tstamp_report.valid = 1;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 status->state = runtime->status->state;
943 status->suspended_state = runtime->status->suspended_state;
944 if (status->state == SNDRV_PCM_STATE_OPEN)
945 goto _end;
946 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100947 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100949 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
950 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600951 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500952 status->audio_tstamp =
953 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600954 if (runtime->audio_tstamp_report.valid == 1)
955 /* backwards compatibility, no report provided in COMPAT mode */
956 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
957 &status->audio_tstamp_accuracy,
958 &runtime->audio_tstamp_report);
959
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100960 goto _tstamp_end;
961 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600962 } else {
963 /* get tstamp only in fallback mode and only if enabled */
964 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
965 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100966 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100967 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 status->appl_ptr = runtime->control->appl_ptr;
969 status->hw_ptr = runtime->status->hw_ptr;
Takashi Iwai763e5062018-04-11 17:56:52 +0200970 status->avail = snd_pcm_avail(substream);
Takashi Iwaic99c5a32018-04-11 18:07:27 +0200971 status->delay = snd_pcm_running(substream) ?
972 snd_pcm_calc_delay(substream) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 status->avail_max = runtime->avail_max;
974 status->overrange = runtime->overrange;
975 runtime->avail_max = 0;
976 runtime->overrange = 0;
977 _end:
978 snd_pcm_stream_unlock_irq(substream);
979 return 0;
980}
981
Takashi Iwai877211f2005-11-17 13:59:38 +0100982static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600983 struct snd_pcm_status __user * _status,
984 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwai877211f2005-11-17 13:59:38 +0100986 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600990 /*
991 * with extension, parameters are read/write,
992 * get audio_tstamp_data from user,
993 * ignore rest of status structure
994 */
995 if (ext && get_user(status.audio_tstamp_data,
996 (u32 __user *)(&_status->audio_tstamp_data)))
997 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 res = snd_pcm_status(substream, &status);
999 if (res < 0)
1000 return res;
1001 if (copy_to_user(_status, &status, sizeof(status)))
1002 return -EFAULT;
1003 return 0;
1004}
1005
Takashi Iwai877211f2005-11-17 13:59:38 +01001006static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1007 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Takashi Iwai877211f2005-11-17 13:59:38 +01001009 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned int channel;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 channel = info->channel;
1013 runtime = substream->runtime;
1014 snd_pcm_stream_lock_irq(substream);
1015 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1016 snd_pcm_stream_unlock_irq(substream);
1017 return -EBADFD;
1018 }
1019 snd_pcm_stream_unlock_irq(substream);
1020 if (channel >= runtime->channels)
1021 return -EINVAL;
1022 memset(info, 0, sizeof(*info));
1023 info->channel = channel;
1024 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1025}
1026
Takashi Iwai877211f2005-11-17 13:59:38 +01001027static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1028 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Takashi Iwai877211f2005-11-17 13:59:38 +01001030 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 int res;
1032
1033 if (copy_from_user(&info, _info, sizeof(info)))
1034 return -EFAULT;
1035 res = snd_pcm_channel_info(substream, &info);
1036 if (res < 0)
1037 return res;
1038 if (copy_to_user(_info, &info, sizeof(info)))
1039 return -EFAULT;
1040 return 0;
1041}
1042
Takashi Iwai877211f2005-11-17 13:59:38 +01001043static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Takashi Iwai877211f2005-11-17 13:59:38 +01001045 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (runtime->trigger_master == NULL)
1047 return;
1048 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001049 if (!runtime->trigger_tstamp_latched)
1050 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 } else {
1052 snd_pcm_trigger_tstamp(runtime->trigger_master);
1053 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1054 }
1055 runtime->trigger_master = NULL;
1056}
1057
1058struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +01001059 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1060 int (*do_action)(struct snd_pcm_substream *substream, int state);
1061 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1062 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063};
1064
1065/*
1066 * this functions is core for handling of linked stream
1067 * Note: the stream state might be changed also on failure
1068 * Note2: call with calling stream lock + link lock
1069 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001070static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001071 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int state, int do_lock)
1073{
Takashi Iwai877211f2005-11-17 13:59:38 +01001074 struct snd_pcm_substream *s = NULL;
1075 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +02001076 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Takashi Iwaief991b92007-02-22 12:52:53 +01001078 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001079 if (do_lock && s != substream) {
1080 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +02001081 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001082 else
Takashi Iwaidde1c652014-10-21 15:32:13 +02001083 spin_lock_nested(&s->self_group.lock, depth);
1084 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 res = ops->pre_action(s, state);
1087 if (res < 0)
1088 goto _unlock;
1089 }
Takashi Iwaief991b92007-02-22 12:52:53 +01001090 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 res = ops->do_action(s, state);
1092 if (res < 0) {
1093 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +01001094 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (s1 == s) /* failed stream */
1096 break;
1097 ops->undo_action(s1, state);
1098 }
1099 }
1100 s = NULL; /* unlock all */
1101 goto _unlock;
1102 }
1103 }
Takashi Iwaief991b92007-02-22 12:52:53 +01001104 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 ops->post_action(s, state);
1106 }
1107 _unlock:
1108 if (do_lock) {
1109 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +01001110 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001111 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +02001112 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001113 mutex_unlock(&s1->self_group.mutex);
1114 else
1115 spin_unlock(&s1->self_group.lock);
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (s1 == s) /* end */
1118 break;
1119 }
1120 }
1121 return res;
1122}
1123
1124/*
1125 * Note: call with stream lock
1126 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001127static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001128 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int state)
1130{
1131 int res;
1132
1133 res = ops->pre_action(substream, state);
1134 if (res < 0)
1135 return res;
1136 res = ops->do_action(substream, state);
1137 if (res == 0)
1138 ops->post_action(substream, state);
1139 else if (ops->undo_action)
1140 ops->undo_action(substream, state);
1141 return res;
1142}
1143
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01001144static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1145 struct snd_pcm_group *new_group)
1146{
1147 substream->group = new_group;
1148 list_move(&substream->link_list, &new_group->substreams);
1149}
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151/*
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001152 * Unref and unlock the group, but keep the stream lock;
1153 * when the group becomes empty and no longer referred, destroy itself
1154 */
1155static void snd_pcm_group_unref(struct snd_pcm_group *group,
1156 struct snd_pcm_substream *substream)
1157{
1158 bool do_free;
1159
1160 if (!group)
1161 return;
1162 do_free = refcount_dec_and_test(&group->refs) &&
1163 list_empty(&group->substreams);
1164 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1165 if (do_free)
1166 kfree(group);
1167}
1168
1169/*
1170 * Lock the group inside a stream lock and reference it;
1171 * return the locked group object, or NULL if not linked
1172 */
1173static struct snd_pcm_group *
1174snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1175{
1176 bool nonatomic = substream->pcm->nonatomic;
1177 struct snd_pcm_group *group;
1178 bool trylock;
1179
1180 for (;;) {
1181 if (!snd_pcm_stream_linked(substream))
1182 return NULL;
1183 group = substream->group;
1184 /* block freeing the group object */
1185 refcount_inc(&group->refs);
1186
1187 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1188 spin_trylock(&group->lock);
1189 if (trylock)
1190 break; /* OK */
1191
1192 /* re-lock for avoiding ABBA deadlock */
1193 snd_pcm_stream_unlock(substream);
1194 snd_pcm_group_lock(group, nonatomic);
1195 snd_pcm_stream_lock(substream);
1196
1197 /* check the group again; the above opens a small race window */
1198 if (substream->group == group)
1199 break; /* OK */
1200 /* group changed, try again */
1201 snd_pcm_group_unref(group, substream);
1202 }
1203 return group;
1204}
1205
1206/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 * Note: call with stream lock
1208 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001209static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001210 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int state)
1212{
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001213 struct snd_pcm_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int res;
1215
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001216 group = snd_pcm_stream_group_ref(substream);
1217 if (group)
Takashi Iwaiaa8edd82014-10-31 15:19:36 +01001218 res = snd_pcm_action_group(ops, substream, state, 1);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001219 else
1220 res = snd_pcm_action_single(ops, substream, state);
1221 snd_pcm_group_unref(group, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return res;
1223}
1224
1225/*
1226 * Note: don't use any locks before
1227 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001228static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001229 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int state)
1231{
1232 int res;
1233
Takashi Iwaie3a4bd52014-10-31 14:45:04 +01001234 snd_pcm_stream_lock_irq(substream);
1235 res = snd_pcm_action(ops, substream, state);
1236 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return res;
1238}
1239
1240/*
1241 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001242static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001243 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int state)
1245{
1246 int res;
1247
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001248 /* Guarantee the group members won't change during non-atomic action */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 down_read(&snd_pcm_link_rwsem);
1250 if (snd_pcm_stream_linked(substream))
1251 res = snd_pcm_action_group(ops, substream, state, 0);
1252 else
1253 res = snd_pcm_action_single(ops, substream, state);
1254 up_read(&snd_pcm_link_rwsem);
1255 return res;
1256}
1257
1258/*
1259 * start callbacks
1260 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001261static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwai877211f2005-11-17 13:59:38 +01001263 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1265 return -EBADFD;
1266 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1267 !snd_pcm_playback_data(substream))
1268 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001269 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 runtime->trigger_master = substream;
1271 return 0;
1272}
1273
Takashi Iwai877211f2005-11-17 13:59:38 +01001274static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 if (substream->runtime->trigger_master != substream)
1277 return 0;
1278 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1279}
1280
Takashi Iwai877211f2005-11-17 13:59:38 +01001281static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 if (substream->runtime->trigger_master == substream)
1284 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1285}
1286
Takashi Iwai877211f2005-11-17 13:59:38 +01001287static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Takashi Iwai877211f2005-11-17 13:59:38 +01001289 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001291 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001292 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1293 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 runtime->status->state = state;
1295 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1296 runtime->silence_size > 0)
1297 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001298 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Julia Lawallb17154c2015-11-29 16:36:40 +01001301static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 .pre_action = snd_pcm_pre_start,
1303 .do_action = snd_pcm_do_start,
1304 .undo_action = snd_pcm_undo_start,
1305 .post_action = snd_pcm_post_start
1306};
1307
1308/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001309 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001310 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001311 *
1312 * Return: Zero if successful, or a negative error code.
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001313 * The stream lock must be acquired before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001315int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Takashi Iwai877211f2005-11-17 13:59:38 +01001317 return snd_pcm_action(&snd_pcm_action_start, substream,
1318 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001321/* take the stream lock and start the streams */
1322static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1323{
1324 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1325 SNDRV_PCM_STATE_RUNNING);
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/*
1329 * stop callbacks
1330 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001331static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Takashi Iwai877211f2005-11-17 13:59:38 +01001333 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1335 return -EBADFD;
1336 runtime->trigger_master = substream;
1337 return 0;
1338}
1339
Takashi Iwai877211f2005-11-17 13:59:38 +01001340static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 if (substream->runtime->trigger_master == substream &&
1343 snd_pcm_running(substream))
1344 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1345 return 0; /* unconditonally stop all substreams */
1346}
1347
Takashi Iwai877211f2005-11-17 13:59:38 +01001348static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Takashi Iwai877211f2005-11-17 13:59:38 +01001350 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (runtime->status->state != state) {
1352 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001353 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001354 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001357 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Julia Lawallb17154c2015-11-29 16:36:40 +01001360static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 .pre_action = snd_pcm_pre_stop,
1362 .do_action = snd_pcm_do_stop,
1363 .post_action = snd_pcm_post_stop
1364};
1365
1366/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001367 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001368 * @substream: the PCM substream instance
1369 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001371 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001372 *
Masanari Iida0a114582014-02-09 00:47:36 +09001373 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001375int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1378}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001379EXPORT_SYMBOL(snd_pcm_stop);
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001382 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001383 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001385 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001387 *
1388 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001390int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Takashi Iwai877211f2005-11-17 13:59:38 +01001392 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1393 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Takashi Iwai1fb85102014-11-07 17:08:28 +01001396/**
1397 * snd_pcm_stop_xrun - stop the running streams as XRUN
1398 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001399 *
1400 * This stops the given running substream (and all linked substreams) as XRUN.
1401 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1402 *
1403 * Return: Zero if successful, or a negative error code.
1404 */
1405int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1406{
1407 unsigned long flags;
Takashi Iwai1fb85102014-11-07 17:08:28 +01001408
1409 snd_pcm_stream_lock_irqsave(substream, flags);
Takashi Iwaie647f5a52018-07-04 15:08:05 +02001410 if (substream->runtime && snd_pcm_running(substream))
Takashi Iwai9cd641e2018-07-04 14:46:27 +02001411 __snd_pcm_xrun(substream);
Takashi Iwai1fb85102014-11-07 17:08:28 +01001412 snd_pcm_stream_unlock_irqrestore(substream, flags);
Takashi Iwai9cd641e2018-07-04 14:46:27 +02001413 return 0;
Takashi Iwai1fb85102014-11-07 17:08:28 +01001414}
1415EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417/*
1418 * pause callbacks
1419 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001420static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Takashi Iwai877211f2005-11-17 13:59:38 +01001422 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1424 return -ENOSYS;
1425 if (push) {
1426 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1427 return -EBADFD;
1428 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1429 return -EBADFD;
1430 runtime->trigger_master = substream;
1431 return 0;
1432}
1433
Takashi Iwai877211f2005-11-17 13:59:38 +01001434static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 if (substream->runtime->trigger_master != substream)
1437 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001438 /* some drivers might use hw_ptr to recover from the pause -
1439 update the hw_ptr now */
1440 if (push)
1441 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001442 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001443 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001444 * delta, effectively to skip the check once.
1445 */
1446 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return substream->ops->trigger(substream,
1448 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1449 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1450}
1451
Takashi Iwai877211f2005-11-17 13:59:38 +01001452static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 if (substream->runtime->trigger_master == substream)
1455 substream->ops->trigger(substream,
1456 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1457 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1458}
1459
Takashi Iwai877211f2005-11-17 13:59:38 +01001460static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Takashi Iwai877211f2005-11-17 13:59:38 +01001462 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 snd_pcm_trigger_tstamp(substream);
1464 if (push) {
1465 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001466 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001468 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 } else {
1470 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001471 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473}
1474
Julia Lawallb17154c2015-11-29 16:36:40 +01001475static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 .pre_action = snd_pcm_pre_pause,
1477 .do_action = snd_pcm_do_pause,
1478 .undo_action = snd_pcm_undo_pause,
1479 .post_action = snd_pcm_post_pause
1480};
1481
1482/*
1483 * Push/release the pause for all linked streams.
1484 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001485static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1488}
1489
1490#ifdef CONFIG_PM
1491/* suspend */
1492
Takashi Iwai877211f2005-11-17 13:59:38 +01001493static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Takashi Iwai877211f2005-11-17 13:59:38 +01001495 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1497 return -EBUSY;
1498 runtime->trigger_master = substream;
1499 return 0;
1500}
1501
Takashi Iwai877211f2005-11-17 13:59:38 +01001502static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Takashi Iwai877211f2005-11-17 13:59:38 +01001504 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (runtime->trigger_master != substream)
1506 return 0;
1507 if (! snd_pcm_running(substream))
1508 return 0;
1509 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1510 return 0; /* suspend unconditionally */
1511}
1512
Takashi Iwai877211f2005-11-17 13:59:38 +01001513static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Takashi Iwai877211f2005-11-17 13:59:38 +01001515 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001517 runtime->status->suspended_state = runtime->status->state;
1518 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001519 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001521 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
Julia Lawallb17154c2015-11-29 16:36:40 +01001524static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 .pre_action = snd_pcm_pre_suspend,
1526 .do_action = snd_pcm_do_suspend,
1527 .post_action = snd_pcm_post_suspend
1528};
1529
1530/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001531 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001532 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001535 *
1536 * Return: Zero if successful (or @substream is %NULL), or a negative error
1537 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001539int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 int err;
1542 unsigned long flags;
1543
Takashi Iwai603bf522005-11-17 15:59:14 +01001544 if (! substream)
1545 return 0;
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 snd_pcm_stream_lock_irqsave(substream, flags);
1548 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1549 snd_pcm_stream_unlock_irqrestore(substream, flags);
1550 return err;
1551}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001552EXPORT_SYMBOL(snd_pcm_suspend);
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001555 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001556 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001559 *
1560 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001562int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Takashi Iwai877211f2005-11-17 13:59:38 +01001564 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int stream, err = 0;
1566
Takashi Iwai603bf522005-11-17 15:59:14 +01001567 if (! pcm)
1568 return 0;
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001571 for (substream = pcm->streams[stream].substream;
1572 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /* FIXME: the open/close code should lock this as well */
1574 if (substream->runtime == NULL)
1575 continue;
1576 err = snd_pcm_suspend(substream);
1577 if (err < 0 && err != -EBUSY)
1578 return err;
1579 }
1580 }
1581 return 0;
1582}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001583EXPORT_SYMBOL(snd_pcm_suspend_all);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585/* resume */
1586
Takashi Iwai877211f2005-11-17 13:59:38 +01001587static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Takashi Iwai877211f2005-11-17 13:59:38 +01001589 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1591 return -ENOSYS;
1592 runtime->trigger_master = substream;
1593 return 0;
1594}
1595
Takashi Iwai877211f2005-11-17 13:59:38 +01001596static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Takashi Iwai877211f2005-11-17 13:59:38 +01001598 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (runtime->trigger_master != substream)
1600 return 0;
1601 /* DMA not running previously? */
1602 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1603 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1604 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1605 return 0;
1606 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1607}
1608
Takashi Iwai877211f2005-11-17 13:59:38 +01001609static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 if (substream->runtime->trigger_master == substream &&
1612 snd_pcm_running(substream))
1613 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1614}
1615
Takashi Iwai877211f2005-11-17 13:59:38 +01001616static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Takashi Iwai877211f2005-11-17 13:59:38 +01001618 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001620 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001621 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
1623
Julia Lawallb17154c2015-11-29 16:36:40 +01001624static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 .pre_action = snd_pcm_pre_resume,
1626 .do_action = snd_pcm_do_resume,
1627 .undo_action = snd_pcm_undo_resume,
1628 .post_action = snd_pcm_post_resume
1629};
1630
Takashi Iwai877211f2005-11-17 13:59:38 +01001631static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwai68b4acd2016-05-24 15:07:39 +02001633 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636#else
1637
Takashi Iwai877211f2005-11-17 13:59:38 +01001638static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 return -ENOSYS;
1641}
1642
1643#endif /* CONFIG_PM */
1644
1645/*
1646 * xrun ioctl
1647 *
1648 * Change the RUNNING stream(s) to XRUN state.
1649 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001650static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Takashi Iwai877211f2005-11-17 13:59:38 +01001652 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 int result;
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 snd_pcm_stream_lock_irq(substream);
1656 switch (runtime->status->state) {
1657 case SNDRV_PCM_STATE_XRUN:
1658 result = 0; /* already there */
1659 break;
1660 case SNDRV_PCM_STATE_RUNNING:
Takashi Iwai9cd641e2018-07-04 14:46:27 +02001661 __snd_pcm_xrun(substream);
1662 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 break;
1664 default:
1665 result = -EBADFD;
1666 }
1667 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return result;
1669}
1670
1671/*
1672 * reset ioctl
1673 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001674static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Takashi Iwai877211f2005-11-17 13:59:38 +01001676 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 switch (runtime->status->state) {
1678 case SNDRV_PCM_STATE_RUNNING:
1679 case SNDRV_PCM_STATE_PREPARED:
1680 case SNDRV_PCM_STATE_PAUSED:
1681 case SNDRV_PCM_STATE_SUSPENDED:
1682 return 0;
1683 default:
1684 return -EBADFD;
1685 }
1686}
1687
Takashi Iwai877211f2005-11-17 13:59:38 +01001688static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Takashi Iwai877211f2005-11-17 13:59:38 +01001690 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1692 if (err < 0)
1693 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001695 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1696 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 runtime->silence_start = runtime->status->hw_ptr;
1698 runtime->silence_filled = 0;
1699 return 0;
1700}
1701
Takashi Iwai877211f2005-11-17 13:59:38 +01001702static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Takashi Iwai877211f2005-11-17 13:59:38 +01001704 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 runtime->control->appl_ptr = runtime->status->hw_ptr;
1706 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1707 runtime->silence_size > 0)
1708 snd_pcm_playback_silence(substream, ULONG_MAX);
1709}
1710
Julia Lawallb17154c2015-11-29 16:36:40 +01001711static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 .pre_action = snd_pcm_pre_reset,
1713 .do_action = snd_pcm_do_reset,
1714 .post_action = snd_pcm_post_reset
1715};
1716
Takashi Iwai877211f2005-11-17 13:59:38 +01001717static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1720}
1721
1722/*
1723 * prepare ioctl
1724 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001725/* we use the second argument for updating f_flags */
1726static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1727 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Takashi Iwai877211f2005-11-17 13:59:38 +01001729 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001730 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1731 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return -EBADFD;
1733 if (snd_pcm_running(substream))
1734 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001735 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return 0;
1737}
1738
Takashi Iwai877211f2005-11-17 13:59:38 +01001739static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 int err;
1742 err = substream->ops->prepare(substream);
1743 if (err < 0)
1744 return err;
1745 return snd_pcm_do_reset(substream, 0);
1746}
1747
Takashi Iwai877211f2005-11-17 13:59:38 +01001748static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Takashi Iwai877211f2005-11-17 13:59:38 +01001750 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001752 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Julia Lawallb17154c2015-11-29 16:36:40 +01001755static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 .pre_action = snd_pcm_pre_prepare,
1757 .do_action = snd_pcm_do_prepare,
1758 .post_action = snd_pcm_post_prepare
1759};
1760
1761/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001762 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001763 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001764 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001765 *
1766 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001768static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1769 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001771 int f_flags;
1772
1773 if (file)
1774 f_flags = file->f_flags;
1775 else
1776 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Takashi Iwai1b745cd2016-05-24 15:40:03 +02001778 snd_pcm_stream_lock_irq(substream);
1779 switch (substream->runtime->status->state) {
1780 case SNDRV_PCM_STATE_PAUSED:
1781 snd_pcm_pause(substream, 0);
1782 /* fallthru */
1783 case SNDRV_PCM_STATE_SUSPENDED:
1784 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1785 break;
1786 }
1787 snd_pcm_stream_unlock_irq(substream);
1788
Takashi Iwai68b4acd2016-05-24 15:07:39 +02001789 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1790 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
1793/*
1794 * drain ioctl
1795 */
1796
Takashi Iwai877211f2005-11-17 13:59:38 +01001797static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001799 struct snd_pcm_runtime *runtime = substream->runtime;
1800 switch (runtime->status->state) {
1801 case SNDRV_PCM_STATE_OPEN:
1802 case SNDRV_PCM_STATE_DISCONNECTED:
1803 case SNDRV_PCM_STATE_SUSPENDED:
1804 return -EBADFD;
1805 }
1806 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return 0;
1808}
1809
Takashi Iwai877211f2005-11-17 13:59:38 +01001810static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Takashi Iwai877211f2005-11-17 13:59:38 +01001812 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1814 switch (runtime->status->state) {
1815 case SNDRV_PCM_STATE_PREPARED:
1816 /* start playback stream if possible */
1817 if (! snd_pcm_playback_empty(substream)) {
1818 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1819 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001820 } else {
1821 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 break;
1824 case SNDRV_PCM_STATE_RUNNING:
1825 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1826 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001827 case SNDRV_PCM_STATE_XRUN:
1828 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 default:
1831 break;
1832 }
1833 } else {
1834 /* stop running stream */
1835 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001836 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001838 snd_pcm_do_stop(substream, new_state);
1839 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841 }
Libin Yang48d88292014-12-31 22:09:54 +08001842
1843 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1844 runtime->trigger_master == substream &&
1845 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1846 return substream->ops->trigger(substream,
1847 SNDRV_PCM_TRIGGER_DRAIN);
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return 0;
1850}
1851
Takashi Iwai877211f2005-11-17 13:59:38 +01001852static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854}
1855
Julia Lawallb17154c2015-11-29 16:36:40 +01001856static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 .pre_action = snd_pcm_pre_drain_init,
1858 .do_action = snd_pcm_do_drain_init,
1859 .post_action = snd_pcm_post_drain_init
1860};
1861
Takashi Iwai877211f2005-11-17 13:59:38 +01001862static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864/*
1865 * Drain the stream(s).
1866 * When the substream is linked, sync until the draining of all playback streams
1867 * is finished.
1868 * After this call, all streams are supposed to be either SETUP or DRAINING
1869 * (capture only) state.
1870 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001871static int snd_pcm_drain(struct snd_pcm_substream *substream,
1872 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Takashi Iwai877211f2005-11-17 13:59:38 +01001874 struct snd_card *card;
1875 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001876 struct snd_pcm_substream *s;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001877 struct snd_pcm_group *group;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001878 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001880 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 card = substream->pcm->card;
1883 runtime = substream->runtime;
1884
1885 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1886 return -EBADFD;
1887
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001888 if (file) {
1889 if (file->f_flags & O_NONBLOCK)
1890 nonblock = 1;
1891 } else if (substream->f_flags & O_NONBLOCK)
1892 nonblock = 1;
1893
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001894 snd_pcm_stream_lock_irq(substream);
1895 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001896 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001897 snd_pcm_pause(substream, 0);
1898
1899 /* pre-start/stop - all running streams are changed to DRAINING state */
1900 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001901 if (result < 0)
1902 goto unlock;
1903 /* in non-blocking, we don't wait in ioctl but let caller poll */
1904 if (nonblock) {
1905 result = -EAGAIN;
1906 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 for (;;) {
1910 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001911 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (signal_pending(current)) {
1913 result = -ERESTARTSYS;
1914 break;
1915 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001916 /* find a substream to drain */
1917 to_check = NULL;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001918 group = snd_pcm_stream_group_ref(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001919 snd_pcm_group_for_each_entry(s, substream) {
1920 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1921 continue;
1922 runtime = s->runtime;
1923 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1924 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001925 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001926 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001927 }
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001928 snd_pcm_group_unref(group, substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001929 if (!to_check)
1930 break; /* all drained */
1931 init_waitqueue_entry(&wait, current);
1932 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001934 if (runtime->no_period_wakeup)
1935 tout = MAX_SCHEDULE_TIMEOUT;
1936 else {
1937 tout = 10;
1938 if (runtime->rate) {
1939 long t = runtime->period_size * 2 / runtime->rate;
1940 tout = max(t, tout);
1941 }
1942 tout = msecs_to_jiffies(tout * 1000);
1943 }
1944 tout = schedule_timeout_interruptible(tout);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001947 group = snd_pcm_stream_group_ref(substream);
1948 snd_pcm_group_for_each_entry(s, substream) {
1949 if (s->runtime == to_check) {
1950 remove_wait_queue(&to_check->sleep, &wait);
1951 break;
1952 }
1953 }
1954 snd_pcm_group_unref(group, substream);
1955
Takashi Iwai0914f792012-10-16 16:43:39 +02001956 if (card->shutdown) {
1957 result = -ENODEV;
1958 break;
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (tout == 0) {
1961 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1962 result = -ESTRPIPE;
1963 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001964 dev_dbg(substream->pcm->card->dev,
1965 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1967 result = -EIO;
1968 }
1969 break;
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001972
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001973 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001974 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 return result;
1977}
1978
1979/*
1980 * drop ioctl
1981 *
1982 * Immediately put all linked substreams into SETUP state.
1983 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001984static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Takashi Iwai877211f2005-11-17 13:59:38 +01001986 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 int result = 0;
1988
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001989 if (PCM_RUNTIME_CHECK(substream))
1990 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001993 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai4b95ff72016-05-24 15:08:31 +02001994 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return -EBADFD;
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 snd_pcm_stream_lock_irq(substream);
1998 /* resume pause */
1999 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2000 snd_pcm_pause(substream, 0);
2001
2002 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2003 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2004 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return result;
2007}
2008
2009
Al Viro0888c322013-06-05 14:09:55 -04002010static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Al Viro0888c322013-06-05 14:09:55 -04002012 struct inode *inode = file_inode(file);
Takashi Iwaid819fb22019-01-13 09:25:42 +01002013 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002014 unsigned int minor;
2015
Al Viro0888c322013-06-05 14:09:55 -04002016 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2017 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 minor = iminor(inode);
Takashi Iwaid819fb22019-01-13 09:25:42 +01002019 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2020 if (!pcm)
2021 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2022 if (!pcm)
2023 return false;
2024 snd_card_unref(pcm->card);
2025 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
2028/*
2029 * PCM link handling
2030 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002031static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01002034 struct snd_pcm_file *pcm_file;
2035 struct snd_pcm_substream *substream1;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002036 struct snd_pcm_group *group, *target_group;
2037 bool nonatomic = substream->pcm->nonatomic;
Al Viro0888c322013-06-05 14:09:55 -04002038 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Al Viro0888c322013-06-05 14:09:55 -04002040 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04002042 if (!is_pcm_file(f.file)) {
2043 res = -EBADFD;
2044 goto _badf;
2045 }
2046 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 substream1 = pcm_file->substream;
Takashi Iwai73365cb2019-01-13 09:35:17 +01002048 group = kzalloc(sizeof(*group), GFP_KERNEL);
Takashi Iwai16625912012-03-13 15:55:43 +01002049 if (!group) {
2050 res = -ENOMEM;
2051 goto _nolock;
2052 }
Takashi Iwai73365cb2019-01-13 09:35:17 +01002053 snd_pcm_group_init(group);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002054
Chanho Minb888a5f72018-11-26 14:36:37 +09002055 down_write_nonfifo(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02002057 substream->runtime->status->state != substream1->runtime->status->state ||
2058 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 res = -EBADFD;
2060 goto _end;
2061 }
2062 if (snd_pcm_stream_linked(substream1)) {
2063 res = -EALREADY;
2064 goto _end;
2065 }
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002066
2067 snd_pcm_stream_lock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002069 snd_pcm_group_assign(substream, group);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002070 group = NULL; /* assigned, don't free this one below */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002072 target_group = substream->group;
2073 snd_pcm_stream_unlock_irq(substream);
2074
2075 snd_pcm_group_lock_irq(target_group, nonatomic);
2076 snd_pcm_stream_lock(substream1);
2077 snd_pcm_group_assign(substream1, target_group);
2078 snd_pcm_stream_unlock(substream1);
2079 snd_pcm_group_unlock_irq(target_group, nonatomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01002082 _nolock:
Al Virodd6c5cd2013-06-05 14:07:08 -04002083 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04002084 _badf:
2085 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return res;
2087}
2088
Takashi Iwai877211f2005-11-17 13:59:38 +01002089static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002091 snd_pcm_stream_lock(substream);
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002092 snd_pcm_group_assign(substream, &substream->self_group);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002093 snd_pcm_stream_unlock(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Takashi Iwai877211f2005-11-17 13:59:38 +01002096static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002098 struct snd_pcm_group *group;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002099 bool nonatomic = substream->pcm->nonatomic;
2100 bool do_free = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 int res = 0;
2102
Chanho Minb888a5f72018-11-26 14:36:37 +09002103 down_write_nonfifo(&snd_pcm_link_rwsem);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (!snd_pcm_stream_linked(substream)) {
2106 res = -EALREADY;
2107 goto _end;
2108 }
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002109
2110 group = substream->group;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002111 snd_pcm_group_lock_irq(group, nonatomic);
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002112
2113 relink_to_local(substream);
2114
2115 /* detach the last stream, too */
2116 if (list_is_singular(&group->substreams)) {
Takashi Iwai7df5a5f2019-01-13 10:19:32 +01002117 relink_to_local(list_first_entry(&group->substreams,
2118 struct snd_pcm_substream,
2119 link_list));
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002120 do_free = !refcount_read(&group->refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002122
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002123 snd_pcm_group_unlock_irq(group, nonatomic);
2124 if (do_free)
2125 kfree(group);
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 up_write(&snd_pcm_link_rwsem);
2129 return res;
2130}
2131
2132/*
2133 * hw configurator
2134 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002135static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2136 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Takashi Iwai877211f2005-11-17 13:59:38 +01002138 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2140 hw_param_interval_c(params, rule->deps[1]), &t);
2141 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2142}
2143
Takashi Iwai877211f2005-11-17 13:59:38 +01002144static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2145 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Takashi Iwai877211f2005-11-17 13:59:38 +01002147 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2149 hw_param_interval_c(params, rule->deps[1]), &t);
2150 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2151}
2152
Takashi Iwai877211f2005-11-17 13:59:38 +01002153static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2154 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Takashi Iwai877211f2005-11-17 13:59:38 +01002156 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2158 hw_param_interval_c(params, rule->deps[1]),
2159 (unsigned long) rule->private, &t);
2160 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2161}
2162
Takashi Iwai877211f2005-11-17 13:59:38 +01002163static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2164 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Takashi Iwai877211f2005-11-17 13:59:38 +01002166 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2168 (unsigned long) rule->private,
2169 hw_param_interval_c(params, rule->deps[1]), &t);
2170 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2171}
2172
Takashi Iwai877211f2005-11-17 13:59:38 +01002173static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2174 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 unsigned int k;
Takashi Sakamotob55f9fd2017-05-17 08:48:20 +09002177 const struct snd_interval *i =
2178 hw_param_interval_c(params, rule->deps[0]);
Takashi Iwai877211f2005-11-17 13:59:38 +01002179 struct snd_mask m;
2180 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 snd_mask_any(&m);
2182 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2183 int bits;
2184 if (! snd_mask_test(mask, k))
2185 continue;
2186 bits = snd_pcm_format_physical_width(k);
2187 if (bits <= 0)
2188 continue; /* ignore invalid formats */
2189 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2190 snd_mask_reset(&m, k);
2191 }
2192 return snd_mask_refine(mask, &m);
2193}
2194
Takashi Iwai877211f2005-11-17 13:59:38 +01002195static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2196 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Takashi Iwai877211f2005-11-17 13:59:38 +01002198 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 unsigned int k;
2200 t.min = UINT_MAX;
2201 t.max = 0;
2202 t.openmin = 0;
2203 t.openmax = 0;
2204 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2205 int bits;
2206 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2207 continue;
2208 bits = snd_pcm_format_physical_width(k);
2209 if (bits <= 0)
2210 continue; /* ignore invalid formats */
2211 if (t.min > (unsigned)bits)
2212 t.min = bits;
2213 if (t.max < (unsigned)bits)
2214 t.max = bits;
2215 }
2216 t.integer = 1;
2217 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2218}
2219
2220#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2221#error "Change this table"
2222#endif
2223
Takashi Sakamoto8b674302017-05-17 08:48:17 +09002224static const unsigned int rates[] = {
2225 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2226 48000, 64000, 88200, 96000, 176400, 192000
2227};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Clemens Ladisch7653d552007-08-13 17:38:54 +02002229const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2230 .count = ARRAY_SIZE(rates),
2231 .list = rates,
2232};
2233
Takashi Iwai877211f2005-11-17 13:59:38 +01002234static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2235 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Takashi Iwai877211f2005-11-17 13:59:38 +01002237 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002239 snd_pcm_known_rates.count,
2240 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
Takashi Iwai877211f2005-11-17 13:59:38 +01002243static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2244 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Takashi Iwai877211f2005-11-17 13:59:38 +01002246 struct snd_interval t;
2247 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 t.min = 0;
2249 t.max = substream->buffer_bytes_max;
2250 t.openmin = 0;
2251 t.openmax = 0;
2252 t.integer = 1;
2253 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2254}
2255
Takashi Iwai877211f2005-11-17 13:59:38 +01002256int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Takashi Iwai877211f2005-11-17 13:59:38 +01002258 struct snd_pcm_runtime *runtime = substream->runtime;
2259 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 int k, err;
2261
2262 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2263 snd_mask_any(constrs_mask(constrs, k));
2264 }
2265
2266 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2267 snd_interval_any(constrs_interval(constrs, k));
2268 }
2269
2270 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2271 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2272 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2273 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2274 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2275
2276 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2277 snd_pcm_hw_rule_format, NULL,
2278 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2279 if (err < 0)
2280 return err;
2281 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2282 snd_pcm_hw_rule_sample_bits, NULL,
2283 SNDRV_PCM_HW_PARAM_FORMAT,
2284 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2285 if (err < 0)
2286 return err;
2287 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2288 snd_pcm_hw_rule_div, NULL,
2289 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2290 if (err < 0)
2291 return err;
2292 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2293 snd_pcm_hw_rule_mul, NULL,
2294 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2295 if (err < 0)
2296 return err;
2297 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2298 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2299 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2300 if (err < 0)
2301 return err;
2302 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2303 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2304 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2305 if (err < 0)
2306 return err;
2307 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2308 snd_pcm_hw_rule_div, NULL,
2309 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2310 if (err < 0)
2311 return err;
2312 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2313 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2314 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2315 if (err < 0)
2316 return err;
2317 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2318 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2319 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2320 if (err < 0)
2321 return err;
2322 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2323 snd_pcm_hw_rule_div, NULL,
2324 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2325 if (err < 0)
2326 return err;
2327 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2328 snd_pcm_hw_rule_div, NULL,
2329 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2330 if (err < 0)
2331 return err;
2332 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2333 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2334 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2335 if (err < 0)
2336 return err;
2337 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2338 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2339 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2340 if (err < 0)
2341 return err;
2342 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2343 snd_pcm_hw_rule_mul, NULL,
2344 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2345 if (err < 0)
2346 return err;
2347 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2348 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2349 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2350 if (err < 0)
2351 return err;
2352 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2353 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2354 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2355 if (err < 0)
2356 return err;
2357 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2358 snd_pcm_hw_rule_muldivk, (void*) 8,
2359 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2360 if (err < 0)
2361 return err;
2362 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2363 snd_pcm_hw_rule_muldivk, (void*) 8,
2364 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2365 if (err < 0)
2366 return err;
2367 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2368 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2369 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2370 if (err < 0)
2371 return err;
2372 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2373 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2374 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2375 if (err < 0)
2376 return err;
2377 return 0;
2378}
2379
Takashi Iwai877211f2005-11-17 13:59:38 +01002380int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Takashi Iwai877211f2005-11-17 13:59:38 +01002382 struct snd_pcm_runtime *runtime = substream->runtime;
2383 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 int err;
2385 unsigned int mask = 0;
2386
2387 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2388 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2389 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2390 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002391 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2393 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2394 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2395 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2396 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2397 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2398 }
2399 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002400 if (err < 0)
2401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002404 if (err < 0)
2405 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002408 if (err < 0)
2409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2412 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002413 if (err < 0)
2414 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2417 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002418 if (err < 0)
2419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2422 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002423 if (err < 0)
2424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2427 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002428 if (err < 0)
2429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2432 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002433 if (err < 0)
2434 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2437 snd_pcm_hw_rule_buffer_bytes_max, substream,
2438 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2439 if (err < 0)
2440 return err;
2441
2442 /* FIXME: remove */
2443 if (runtime->dma_bytes) {
2444 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002445 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302446 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
2448
2449 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2450 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2451 snd_pcm_hw_rule_rate, hw,
2452 SNDRV_PCM_HW_PARAM_RATE, -1);
2453 if (err < 0)
2454 return err;
2455 }
2456
2457 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2459
2460 return 0;
2461}
2462
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002463static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Takashi Iwaib51abed2018-11-29 08:02:49 +01002465 if (snd_pcm_stream_linked(substream))
2466 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002467}
2468
2469void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2470{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002471 substream->ref_count--;
2472 if (substream->ref_count > 0)
2473 return;
2474
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002475 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002476 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002477 if (substream->ops->hw_free &&
2478 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 substream->ops->hw_free(substream);
2480 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002481 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002483 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2484 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002485 if (substream->pcm_release) {
2486 substream->pcm_release(substream);
2487 substream->pcm_release = NULL;
2488 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002489 snd_pcm_detach_substream(substream);
2490}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002491EXPORT_SYMBOL(snd_pcm_release_substream);
2492
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002493int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2494 struct file *file,
2495 struct snd_pcm_substream **rsubstream)
2496{
2497 struct snd_pcm_substream *substream;
2498 int err;
2499
2500 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2501 if (err < 0)
2502 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002503 if (substream->ref_count > 1) {
2504 *rsubstream = substream;
2505 return 0;
2506 }
2507
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002508 err = snd_pcm_hw_constraints_init(substream);
2509 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002510 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002511 goto error;
2512 }
2513
2514 if ((err = substream->ops->open(substream)) < 0)
2515 goto error;
2516
2517 substream->hw_opened = 1;
2518
2519 err = snd_pcm_hw_constraints_complete(substream);
2520 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002521 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002522 goto error;
2523 }
2524
2525 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002527
2528 error:
2529 snd_pcm_release_substream(substream);
2530 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002532EXPORT_SYMBOL(snd_pcm_open_substream);
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002535 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002536 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Takashi Iwai877211f2005-11-17 13:59:38 +01002538 struct snd_pcm_file *pcm_file;
2539 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002540 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002542 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2543 if (err < 0)
2544 return err;
2545
Takashi Iwai548a6482006-07-31 16:51:51 +02002546 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2547 if (pcm_file == NULL) {
2548 snd_pcm_release_substream(substream);
2549 return -ENOMEM;
2550 }
2551 pcm_file->substream = substream;
2552 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002553 substream->file = pcm_file;
2554 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 return 0;
2559}
2560
Clemens Ladischf87135f2005-11-20 14:06:59 +01002561static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
Takashi Iwai877211f2005-11-17 13:59:38 +01002563 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002564 int err = nonseekable_open(inode, file);
2565 if (err < 0)
2566 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002567 pcm = snd_lookup_minor_data(iminor(inode),
2568 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002569 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002570 if (pcm)
2571 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002572 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002573}
2574
2575static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2576{
2577 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002578 int err = nonseekable_open(inode, file);
2579 if (err < 0)
2580 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002581 pcm = snd_lookup_minor_data(iminor(inode),
2582 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002583 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002584 if (pcm)
2585 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002586 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002587}
2588
2589static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2590{
2591 int err;
Ingo Molnarac6424b2017-06-20 12:06:13 +02002592 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (pcm == NULL) {
2595 err = -ENODEV;
2596 goto __error1;
2597 }
2598 err = snd_card_file_add(pcm->card, file);
2599 if (err < 0)
2600 goto __error1;
2601 if (!try_module_get(pcm->card->module)) {
2602 err = -EFAULT;
2603 goto __error2;
2604 }
2605 init_waitqueue_entry(&wait, current);
2606 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002607 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002609 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (err >= 0)
2611 break;
2612 if (err == -EAGAIN) {
2613 if (file->f_flags & O_NONBLOCK) {
2614 err = -EBUSY;
2615 break;
2616 }
2617 } else
2618 break;
2619 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002620 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002622 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002623 if (pcm->card->shutdown) {
2624 err = -ENODEV;
2625 break;
2626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (signal_pending(current)) {
2628 err = -ERESTARTSYS;
2629 break;
2630 }
2631 }
2632 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002633 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (err < 0)
2635 goto __error;
2636 return err;
2637
2638 __error:
2639 module_put(pcm->card->module);
2640 __error2:
2641 snd_card_file_remove(pcm->card, file);
2642 __error1:
2643 return err;
2644}
2645
2646static int snd_pcm_release(struct inode *inode, struct file *file)
2647{
Takashi Iwai877211f2005-11-17 13:59:38 +01002648 struct snd_pcm *pcm;
2649 struct snd_pcm_substream *substream;
2650 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 pcm_file = file->private_data;
2653 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002654 if (snd_BUG_ON(!substream))
2655 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002657 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002658 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002659 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002660 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 wake_up(&pcm->open_wait);
2662 module_put(pcm->card->module);
2663 snd_card_file_remove(pcm->card, file);
2664 return 0;
2665}
2666
Takashi Iwaif839cc12017-05-19 20:04:23 +02002667/* check and update PCM state; return 0 or a negative error
2668 * call this inside PCM lock
2669 */
2670static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2671{
2672 switch (substream->runtime->status->state) {
2673 case SNDRV_PCM_STATE_DRAINING:
2674 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2675 return -EBADFD;
2676 /* Fall through */
2677 case SNDRV_PCM_STATE_RUNNING:
2678 return snd_pcm_update_hw_ptr(substream);
2679 case SNDRV_PCM_STATE_PREPARED:
2680 case SNDRV_PCM_STATE_PAUSED:
2681 return 0;
2682 case SNDRV_PCM_STATE_SUSPENDED:
2683 return -ESTRPIPE;
2684 case SNDRV_PCM_STATE_XRUN:
2685 return -EPIPE;
2686 default:
2687 return -EBADFD;
2688 }
2689}
2690
Takashi Iwai9027c462017-05-19 20:22:33 +02002691/* increase the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002692static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2693 snd_pcm_uframes_t frames,
2694 snd_pcm_sframes_t avail)
2695{
2696 struct snd_pcm_runtime *runtime = substream->runtime;
2697 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002698 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002699
2700 if (avail <= 0)
2701 return 0;
2702 if (frames > (snd_pcm_uframes_t)avail)
2703 frames = avail;
2704 appl_ptr = runtime->control->appl_ptr + frames;
2705 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2706 appl_ptr -= runtime->boundary;
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002707 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
Takashi Iwai9027c462017-05-19 20:22:33 +02002708 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002709}
2710
Takashi Iwaifb51f1c2018-01-03 15:16:30 +01002711/* decrease the appl_ptr; returns the processed frames or zero for error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002712static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2713 snd_pcm_uframes_t frames,
2714 snd_pcm_sframes_t avail)
2715{
2716 struct snd_pcm_runtime *runtime = substream->runtime;
2717 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002718 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002719
2720 if (avail <= 0)
2721 return 0;
2722 if (frames > (snd_pcm_uframes_t)avail)
2723 frames = avail;
2724 appl_ptr = runtime->control->appl_ptr - frames;
2725 if (appl_ptr < 0)
2726 appl_ptr += runtime->boundary;
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002727 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
Takashi Iwaifb51f1c2018-01-03 15:16:30 +01002728 /* NOTE: we return zero for errors because PulseAudio gets depressed
2729 * upon receiving an error from rewind ioctl and stops processing
2730 * any longer. Returning zero means that no rewind is done, so
2731 * it's not absolutely wrong to answer like that.
2732 */
2733 return ret < 0 ? 0 : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002734}
2735
Takashi Iwai763e5062018-04-11 17:56:52 +02002736static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2737 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 if (frames == 0)
2742 return 0;
2743
2744 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002745 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002746 if (!ret)
2747 ret = rewind_appl_ptr(substream, frames,
Takashi Iwai763e5062018-04-11 17:56:52 +02002748 snd_pcm_hw_avail(substream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 snd_pcm_stream_unlock_irq(substream);
2750 return ret;
2751}
2752
Takashi Iwai763e5062018-04-11 17:56:52 +02002753static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2754 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 if (frames == 0)
2759 return 0;
2760
2761 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002762 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002763 if (!ret)
2764 ret = forward_appl_ptr(substream, frames,
Takashi Iwai763e5062018-04-11 17:56:52 +02002765 snd_pcm_avail(substream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 snd_pcm_stream_unlock_irq(substream);
2767 return ret;
2768}
2769
Takashi Iwai877211f2005-11-17 13:59:38 +01002770static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 int err;
2773
2774 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002775 err = do_pcm_hwsync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 snd_pcm_stream_unlock_irq(substream);
2777 return err;
2778}
2779
Jeffery Miller912e4c32018-04-20 23:20:46 -05002780static int snd_pcm_delay(struct snd_pcm_substream *substream,
2781 snd_pcm_sframes_t *delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 int err;
2784 snd_pcm_sframes_t n = 0;
2785
2786 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002787 err = do_pcm_hwsync(substream);
Takashi Iwaic99c5a32018-04-11 18:07:27 +02002788 if (!err)
2789 n = snd_pcm_calc_delay(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 snd_pcm_stream_unlock_irq(substream);
Jeffery Miller912e4c32018-04-20 23:20:46 -05002791 if (!err)
2792 *delay = n;
2793 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
Takashi Iwai877211f2005-11-17 13:59:38 +01002796static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2797 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Takashi Iwai877211f2005-11-17 13:59:38 +01002799 struct snd_pcm_runtime *runtime = substream->runtime;
2800 struct snd_pcm_sync_ptr sync_ptr;
2801 volatile struct snd_pcm_mmap_status *status;
2802 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 int err;
2804
2805 memset(&sync_ptr, 0, sizeof(sync_ptr));
2806 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2807 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002808 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 -07002809 return -EFAULT;
2810 status = runtime->status;
2811 control = runtime->control;
2812 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2813 err = snd_pcm_hwsync(substream);
2814 if (err < 0)
2815 return err;
2816 }
2817 snd_pcm_stream_lock_irq(substream);
Takashi Iwai9027c462017-05-19 20:22:33 +02002818 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002819 err = pcm_lib_apply_appl_ptr(substream,
2820 sync_ptr.c.control.appl_ptr);
Takashi Iwai9027c462017-05-19 20:22:33 +02002821 if (err < 0) {
2822 snd_pcm_stream_unlock_irq(substream);
2823 return err;
2824 }
2825 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 sync_ptr.c.control.appl_ptr = control->appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2829 control->avail_min = sync_ptr.c.control.avail_min;
2830 else
2831 sync_ptr.c.control.avail_min = control->avail_min;
2832 sync_ptr.s.status.state = status->state;
2833 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2834 sync_ptr.s.status.tstamp = status->tstamp;
2835 sync_ptr.s.status.suspended_state = status->suspended_state;
David Henningssonf853dca2018-04-21 14:57:40 +02002836 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 snd_pcm_stream_unlock_irq(substream);
2838 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2839 return -EFAULT;
2840 return 0;
2841}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002842
2843static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2844{
2845 struct snd_pcm_runtime *runtime = substream->runtime;
2846 int arg;
2847
2848 if (get_user(arg, _arg))
2849 return -EFAULT;
2850 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2851 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002852 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002853 return 0;
2854}
Takashi Iwai67616fe2017-08-30 15:39:32 +02002855
2856static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2857 struct snd_xferi __user *_xferi)
2858{
2859 struct snd_xferi xferi;
2860 struct snd_pcm_runtime *runtime = substream->runtime;
2861 snd_pcm_sframes_t result;
2862
2863 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2864 return -EBADFD;
2865 if (put_user(0, &_xferi->result))
2866 return -EFAULT;
2867 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2868 return -EFAULT;
2869 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2870 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2871 else
2872 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2873 __put_user(result, &_xferi->result);
2874 return result < 0 ? result : 0;
2875}
2876
2877static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2878 struct snd_xfern __user *_xfern)
2879{
2880 struct snd_xfern xfern;
2881 struct snd_pcm_runtime *runtime = substream->runtime;
2882 void *bufs;
2883 snd_pcm_sframes_t result;
2884
2885 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2886 return -EBADFD;
2887 if (runtime->channels > 128)
2888 return -EINVAL;
2889 if (put_user(0, &_xfern->result))
2890 return -EFAULT;
2891 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2892 return -EFAULT;
2893
2894 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2895 if (IS_ERR(bufs))
2896 return PTR_ERR(bufs);
2897 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2898 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2899 else
2900 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2901 kfree(bufs);
2902 __put_user(result, &_xfern->result);
2903 return result < 0 ? result : 0;
2904}
2905
2906static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2907 snd_pcm_uframes_t __user *_frames)
2908{
2909 snd_pcm_uframes_t frames;
2910 snd_pcm_sframes_t result;
2911
2912 if (get_user(frames, _frames))
2913 return -EFAULT;
2914 if (put_user(0, _frames))
2915 return -EFAULT;
Takashi Iwai763e5062018-04-11 17:56:52 +02002916 result = snd_pcm_rewind(substream, frames);
Takashi Iwai67616fe2017-08-30 15:39:32 +02002917 __put_user(result, _frames);
2918 return result < 0 ? result : 0;
2919}
2920
2921static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2922 snd_pcm_uframes_t __user *_frames)
2923{
2924 snd_pcm_uframes_t frames;
2925 snd_pcm_sframes_t result;
2926
2927 if (get_user(frames, _frames))
2928 return -EFAULT;
2929 if (put_user(0, _frames))
2930 return -EFAULT;
Takashi Iwai763e5062018-04-11 17:56:52 +02002931 result = snd_pcm_forward(substream, frames);
Takashi Iwai67616fe2017-08-30 15:39:32 +02002932 __put_user(result, _frames);
2933 return result < 0 ? result : 0;
2934}
2935
2936static int snd_pcm_common_ioctl(struct file *file,
Takashi Iwai0df63e42006-04-28 15:13:41 +02002937 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 unsigned int cmd, void __user *arg)
2939{
Takashi Iwai4b671f52017-06-19 23:11:54 +02002940 struct snd_pcm_file *pcm_file = file->private_data;
Takashi Iwai7d8e8292017-08-30 16:13:25 +02002941 int res;
2942
Takashi Iwai67616fe2017-08-30 15:39:32 +02002943 if (PCM_RUNTIME_CHECK(substream))
2944 return -ENXIO;
2945
Takashi Iwai7d8e8292017-08-30 16:13:25 +02002946 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2947 if (res < 0)
2948 return res;
Takashi Iwai4b671f52017-06-19 23:11:54 +02002949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 switch (cmd) {
2951 case SNDRV_PCM_IOCTL_PVERSION:
2952 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2953 case SNDRV_PCM_IOCTL_INFO:
2954 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002955 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2956 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002957 case SNDRV_PCM_IOCTL_TTSTAMP:
2958 return snd_pcm_tstamp(substream, arg);
Takashi Iwai4b671f52017-06-19 23:11:54 +02002959 case SNDRV_PCM_IOCTL_USER_PVERSION:
2960 if (get_user(pcm_file->user_pversion,
2961 (unsigned int __user *)arg))
2962 return -EFAULT;
2963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 case SNDRV_PCM_IOCTL_HW_REFINE:
2965 return snd_pcm_hw_refine_user(substream, arg);
2966 case SNDRV_PCM_IOCTL_HW_PARAMS:
2967 return snd_pcm_hw_params_user(substream, arg);
2968 case SNDRV_PCM_IOCTL_HW_FREE:
2969 return snd_pcm_hw_free(substream);
2970 case SNDRV_PCM_IOCTL_SW_PARAMS:
2971 return snd_pcm_sw_params_user(substream, arg);
2972 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002973 return snd_pcm_status_user(substream, arg, false);
2974 case SNDRV_PCM_IOCTL_STATUS_EXT:
2975 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2977 return snd_pcm_channel_info_user(substream, arg);
2978 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002979 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 case SNDRV_PCM_IOCTL_RESET:
2981 return snd_pcm_reset(substream);
2982 case SNDRV_PCM_IOCTL_START:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002983 return snd_pcm_start_lock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 case SNDRV_PCM_IOCTL_LINK:
2985 return snd_pcm_link(substream, (int)(unsigned long) arg);
2986 case SNDRV_PCM_IOCTL_UNLINK:
2987 return snd_pcm_unlink(substream);
2988 case SNDRV_PCM_IOCTL_RESUME:
2989 return snd_pcm_resume(substream);
2990 case SNDRV_PCM_IOCTL_XRUN:
2991 return snd_pcm_xrun(substream);
2992 case SNDRV_PCM_IOCTL_HWSYNC:
2993 return snd_pcm_hwsync(substream);
2994 case SNDRV_PCM_IOCTL_DELAY:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002995 {
Jeffery Miller912e4c32018-04-20 23:20:46 -05002996 snd_pcm_sframes_t delay;
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002997 snd_pcm_sframes_t __user *res = arg;
Jeffery Miller912e4c32018-04-20 23:20:46 -05002998 int err;
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002999
Jeffery Miller912e4c32018-04-20 23:20:46 -05003000 err = snd_pcm_delay(substream, &delay);
3001 if (err)
3002 return err;
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003003 if (put_user(delay, res))
3004 return -EFAULT;
3005 return 0;
3006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 case SNDRV_PCM_IOCTL_SYNC_PTR:
3008 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01003009#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3011 return snd_pcm_hw_refine_old_user(substream, arg);
3012 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3013 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01003014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02003016 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 case SNDRV_PCM_IOCTL_DROP:
3018 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01003019 case SNDRV_PCM_IOCTL_PAUSE:
Takashi Iwai34bcc442016-05-24 14:58:04 +02003020 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
3021 substream,
3022 (int)(unsigned long)arg);
Takashi Iwai67616fe2017-08-30 15:39:32 +02003023 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3024 case SNDRV_PCM_IOCTL_READI_FRAMES:
3025 return snd_pcm_xferi_frames_ioctl(substream, arg);
3026 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3027 case SNDRV_PCM_IOCTL_READN_FRAMES:
3028 return snd_pcm_xfern_frames_ioctl(substream, arg);
3029 case SNDRV_PCM_IOCTL_REWIND:
3030 return snd_pcm_rewind_ioctl(substream, arg);
3031 case SNDRV_PCM_IOCTL_FORWARD:
3032 return snd_pcm_forward_ioctl(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01003034 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return -ENOTTY;
3036}
3037
Takashi Iwai67616fe2017-08-30 15:39:32 +02003038static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3039 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Takashi Iwai877211f2005-11-17 13:59:38 +01003041 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043 pcm_file = file->private_data;
3044
3045 if (((cmd >> 8) & 0xff) != 'A')
3046 return -ENOTTY;
3047
Takashi Iwai67616fe2017-08-30 15:39:32 +02003048 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3049 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003052/**
3053 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3054 * @substream: PCM substream
3055 * @cmd: IOCTL cmd
3056 * @arg: IOCTL argument
3057 *
3058 * The function is provided primarily for OSS layer and USB gadget drivers,
3059 * and it allows only the limited set of ioctls (hw_params, sw_params,
3060 * prepare, start, drain, drop, forward).
3061 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003062int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 unsigned int cmd, void *arg)
3064{
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003065 snd_pcm_uframes_t *frames = arg;
3066 snd_pcm_sframes_t result;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003067
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003068 switch (cmd) {
3069 case SNDRV_PCM_IOCTL_FORWARD:
3070 {
3071 /* provided only for OSS; capture-only and no value returned */
3072 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3073 return -EINVAL;
Takashi Iwai763e5062018-04-11 17:56:52 +02003074 result = snd_pcm_forward(substream, *frames);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003075 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003077 case SNDRV_PCM_IOCTL_HW_PARAMS:
3078 return snd_pcm_hw_params(substream, arg);
3079 case SNDRV_PCM_IOCTL_SW_PARAMS:
3080 return snd_pcm_sw_params(substream, arg);
3081 case SNDRV_PCM_IOCTL_PREPARE:
3082 return snd_pcm_prepare(substream, NULL);
3083 case SNDRV_PCM_IOCTL_START:
3084 return snd_pcm_start_lock_irq(substream);
3085 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai7d8e8292017-08-30 16:13:25 +02003086 return snd_pcm_drain(substream, NULL);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003087 case SNDRV_PCM_IOCTL_DROP:
3088 return snd_pcm_drop(substream);
3089 case SNDRV_PCM_IOCTL_DELAY:
Jeffery Miller912e4c32018-04-20 23:20:46 -05003090 return snd_pcm_delay(substream, frames);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003091 default:
3092 return -EINVAL;
3093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003095EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3096
Takashi Iwai877211f2005-11-17 13:59:38 +01003097static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3098 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Takashi Iwai877211f2005-11-17 13:59:38 +01003100 struct snd_pcm_file *pcm_file;
3101 struct snd_pcm_substream *substream;
3102 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 snd_pcm_sframes_t result;
3104
3105 pcm_file = file->private_data;
3106 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003107 if (PCM_RUNTIME_CHECK(substream))
3108 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 runtime = substream->runtime;
3110 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3111 return -EBADFD;
3112 if (!frame_aligned(runtime, count))
3113 return -EINVAL;
3114 count = bytes_to_frames(runtime, count);
3115 result = snd_pcm_lib_read(substream, buf, count);
3116 if (result > 0)
3117 result = frames_to_bytes(runtime, result);
3118 return result;
3119}
3120
Takashi Iwai877211f2005-11-17 13:59:38 +01003121static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3122 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Takashi Iwai877211f2005-11-17 13:59:38 +01003124 struct snd_pcm_file *pcm_file;
3125 struct snd_pcm_substream *substream;
3126 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 snd_pcm_sframes_t result;
3128
3129 pcm_file = file->private_data;
3130 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003131 if (PCM_RUNTIME_CHECK(substream))
3132 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003134 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3135 return -EBADFD;
3136 if (!frame_aligned(runtime, count))
3137 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 count = bytes_to_frames(runtime, count);
3139 result = snd_pcm_lib_write(substream, buf, count);
3140 if (result > 0)
3141 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return result;
3143}
3144
Al Viro1c65d982015-04-04 00:19:32 -04003145static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Takashi Iwai877211f2005-11-17 13:59:38 +01003147 struct snd_pcm_file *pcm_file;
3148 struct snd_pcm_substream *substream;
3149 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 snd_pcm_sframes_t result;
3151 unsigned long i;
3152 void __user **bufs;
3153 snd_pcm_uframes_t frames;
3154
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003155 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003157 if (PCM_RUNTIME_CHECK(substream))
3158 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 runtime = substream->runtime;
3160 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3161 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003162 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003164 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003166 if (!frame_aligned(runtime, to->iov->iov_len))
3167 return -EINVAL;
3168 frames = bytes_to_samples(runtime, to->iov->iov_len);
Kees Cook6da2ec52018-06-12 13:55:00 -07003169 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 if (bufs == NULL)
3171 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003172 for (i = 0; i < to->nr_segs; ++i)
3173 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 result = snd_pcm_lib_readv(substream, bufs, frames);
3175 if (result > 0)
3176 result = frames_to_bytes(runtime, result);
3177 kfree(bufs);
3178 return result;
3179}
3180
Al Viro1c65d982015-04-04 00:19:32 -04003181static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Takashi Iwai877211f2005-11-17 13:59:38 +01003183 struct snd_pcm_file *pcm_file;
3184 struct snd_pcm_substream *substream;
3185 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 snd_pcm_sframes_t result;
3187 unsigned long i;
3188 void __user **bufs;
3189 snd_pcm_uframes_t frames;
3190
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003191 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003193 if (PCM_RUNTIME_CHECK(substream))
3194 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003196 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3197 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003198 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003199 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003200 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3201 !frame_aligned(runtime, from->iov->iov_len))
3202 return -EINVAL;
3203 frames = bytes_to_samples(runtime, from->iov->iov_len);
Kees Cook6da2ec52018-06-12 13:55:00 -07003204 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (bufs == NULL)
3206 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003207 for (i = 0; i < from->nr_segs; ++i)
3208 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 result = snd_pcm_lib_writev(substream, bufs, frames);
3210 if (result > 0)
3211 result = frames_to_bytes(runtime, result);
3212 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return result;
3214}
3215
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003216static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
Takashi Iwai877211f2005-11-17 13:59:38 +01003218 struct snd_pcm_file *pcm_file;
3219 struct snd_pcm_substream *substream;
3220 struct snd_pcm_runtime *runtime;
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003221 __poll_t mask, ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 snd_pcm_uframes_t avail;
3223
3224 pcm_file = file->private_data;
3225
3226 substream = pcm_file->substream;
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003227 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3228 ok = EPOLLOUT | EPOLLWRNORM;
3229 else
3230 ok = EPOLLIN | EPOLLRDNORM;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003231 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003232 return ok | EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003234 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 poll_wait(file, &runtime->sleep, wait);
3236
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003237 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 snd_pcm_stream_lock_irq(substream);
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003239 avail = snd_pcm_avail(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 switch (runtime->status->state) {
3241 case SNDRV_PCM_STATE_RUNNING:
3242 case SNDRV_PCM_STATE_PREPARED:
3243 case SNDRV_PCM_STATE_PAUSED:
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003244 if (avail >= runtime->control->avail_min)
3245 mask = ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 break;
3247 case SNDRV_PCM_STATE_DRAINING:
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003248 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3249 mask = ok;
3250 if (!avail)
3251 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 }
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 default:
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003255 mask = ok | EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 break;
3257 }
3258 snd_pcm_stream_unlock_irq(substream);
3259 return mask;
3260}
3261
3262/*
3263 * mmap support
3264 */
3265
3266/*
3267 * Only on coherent architectures, we can mmap the status and the control records
3268 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3269 */
3270#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3271/*
3272 * mmap status record
3273 */
Souptick Joarder41412fe2018-04-25 09:50:29 +05303274static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
Dave Jiang11bac802017-02-24 14:56:41 -08003276 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003277 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003280 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003282 vmf->page = virt_to_page(runtime->status);
3283 get_page(vmf->page);
3284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285}
3286
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003287static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003289 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290};
3291
Takashi Iwai877211f2005-11-17 13:59:38 +01003292static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 struct vm_area_struct *area)
3294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 long size;
3296 if (!(area->vm_flags & VM_READ))
3297 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003299 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return -EINVAL;
3301 area->vm_ops = &snd_pcm_vm_ops_status;
3302 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003303 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return 0;
3305}
3306
3307/*
3308 * mmap control record
3309 */
Souptick Joarder41412fe2018-04-25 09:50:29 +05303310static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Dave Jiang11bac802017-02-24 14:56:41 -08003312 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003313 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003316 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003318 vmf->page = virt_to_page(runtime->control);
3319 get_page(vmf->page);
3320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003323static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003325 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326};
3327
Takashi Iwai877211f2005-11-17 13:59:38 +01003328static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 struct vm_area_struct *area)
3330{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 long size;
3332 if (!(area->vm_flags & VM_READ))
3333 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003335 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return -EINVAL;
3337 area->vm_ops = &snd_pcm_vm_ops_control;
3338 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003339 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return 0;
3341}
Takashi Iwai42f94592017-06-19 22:39:18 +02003342
3343static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3344{
3345 if (pcm_file->no_compat_mmap)
3346 return false;
Takashi Iwaib602aa82017-06-27 11:54:37 +02003347 /* See pcm_control_mmap_allowed() below.
3348 * Since older alsa-lib requires both status and control mmaps to be
3349 * coupled, we have to disable the status mmap for old alsa-lib, too.
3350 */
3351 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3352 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3353 return false;
3354 return true;
3355}
3356
3357static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3358{
3359 if (pcm_file->no_compat_mmap)
3360 return false;
3361 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
Takashi Iwai42f94592017-06-19 22:39:18 +02003362 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3363 * thus it effectively assures the manual update of appl_ptr.
Takashi Iwai42f94592017-06-19 22:39:18 +02003364 */
3365 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3366 return false;
3367 return true;
3368}
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370#else /* ! coherent mmap */
3371/*
3372 * don't support mmap for status and control records.
3373 */
Takashi Iwai42f94592017-06-19 22:39:18 +02003374#define pcm_status_mmap_allowed(pcm_file) false
Takashi Iwaib602aa82017-06-27 11:54:37 +02003375#define pcm_control_mmap_allowed(pcm_file) false
Takashi Iwai42f94592017-06-19 22:39:18 +02003376
Takashi Iwai877211f2005-11-17 13:59:38 +01003377static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 struct vm_area_struct *area)
3379{
3380 return -ENXIO;
3381}
Takashi Iwai877211f2005-11-17 13:59:38 +01003382static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct vm_area_struct *area)
3384{
3385 return -ENXIO;
3386}
3387#endif /* coherent mmap */
3388
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003389static inline struct page *
3390snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3391{
3392 void *vaddr = substream->runtime->dma_area + ofs;
3393 return virt_to_page(vaddr);
3394}
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003397 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 */
Souptick Joarder41412fe2018-04-25 09:50:29 +05303399static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Dave Jiang11bac802017-02-24 14:56:41 -08003401 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003402 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 unsigned long offset;
3404 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 size_t dma_bytes;
3406
3407 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003408 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003410 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3412 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003413 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003414 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003416 else
3417 page = snd_pcm_default_page_ops(substream, offset);
3418 if (!page)
3419 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003420 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003421 vmf->page = page;
3422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Takashi Iwai657b1982009-11-26 12:40:21 +01003425static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3426 .open = snd_pcm_mmap_data_open,
3427 .close = snd_pcm_mmap_data_close,
3428};
3429
3430static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 .open = snd_pcm_mmap_data_open,
3432 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003433 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434};
3435
3436/*
3437 * mmap the DMA buffer on RAM
3438 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003439
3440/**
3441 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3442 * @substream: PCM substream
3443 * @area: VMA
3444 *
3445 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3446 * this function is invoked implicitly.
3447 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003448int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3449 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003451 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003452#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003453 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3454 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3455 return remap_pfn_range(area, area->vm_start,
3456 substream->dma_buffer.addr >> PAGE_SHIFT,
3457 area->vm_end - area->vm_start, area->vm_page_prot);
3458 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003459#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003460#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Geert Uytterhoevenabe594c2017-07-09 21:31:02 +02003461 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
Takashi Iwai657b1982009-11-26 12:40:21 +01003462 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3463 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3464 area,
3465 substream->runtime->dma_area,
3466 substream->runtime->dma_addr,
Stefan Roese9066ae72018-03-26 16:10:21 +02003467 substream->runtime->dma_bytes);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003468#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003469 /* mmap with fault handler */
3470 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 return 0;
3472}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003473EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475/*
3476 * mmap the DMA buffer on I/O memory area
3477 */
3478#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003479/**
3480 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3481 * @substream: PCM substream
3482 * @area: VMA
3483 *
3484 * When your hardware uses the iomapped pages as the hardware buffer and
3485 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3486 * is supposed to work only on limited architectures.
3487 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003488int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3489 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
Luis de Bethencourt3c7f6912018-01-16 13:26:26 +00003491 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003494 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003496EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497#endif /* SNDRV_PCM_INFO_MMAP */
3498
3499/*
3500 * mmap DMA buffer
3501 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003502int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 struct vm_area_struct *area)
3504{
Takashi Iwai877211f2005-11-17 13:59:38 +01003505 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 long size;
3507 unsigned long offset;
3508 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003509 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
3511 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3512 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3513 return -EINVAL;
3514 } else {
3515 if (!(area->vm_flags & VM_READ))
3516 return -EINVAL;
3517 }
3518 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3520 return -EBADFD;
3521 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3522 return -ENXIO;
3523 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3524 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3525 return -EINVAL;
3526 size = area->vm_end - area->vm_start;
3527 offset = area->vm_pgoff << PAGE_SHIFT;
3528 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3529 if ((size_t)size > dma_bytes)
3530 return -EINVAL;
3531 if (offset > dma_bytes - size)
3532 return -EINVAL;
3533
Takashi Iwai657b1982009-11-26 12:40:21 +01003534 area->vm_ops = &snd_pcm_vm_ops_data;
3535 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003537 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003539 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003540 if (!err)
3541 atomic_inc(&substream->mmap_count);
3542 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003544EXPORT_SYMBOL(snd_pcm_mmap_data);
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3547{
Takashi Iwai877211f2005-11-17 13:59:38 +01003548 struct snd_pcm_file * pcm_file;
3549 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 unsigned long offset;
3551
3552 pcm_file = file->private_data;
3553 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003554 if (PCM_RUNTIME_CHECK(substream))
3555 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557 offset = area->vm_pgoff << PAGE_SHIFT;
3558 switch (offset) {
3559 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai42f94592017-06-19 22:39:18 +02003560 if (!pcm_status_mmap_allowed(pcm_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 return -ENXIO;
3562 return snd_pcm_mmap_status(substream, file, area);
3563 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwaib602aa82017-06-27 11:54:37 +02003564 if (!pcm_control_mmap_allowed(pcm_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return -ENXIO;
3566 return snd_pcm_mmap_control(substream, file, area);
3567 default:
3568 return snd_pcm_mmap_data(substream, file, area);
3569 }
3570 return 0;
3571}
3572
3573static int snd_pcm_fasync(int fd, struct file * file, int on)
3574{
Takashi Iwai877211f2005-11-17 13:59:38 +01003575 struct snd_pcm_file * pcm_file;
3576 struct snd_pcm_substream *substream;
3577 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579 pcm_file = file->private_data;
3580 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003581 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003582 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003584 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585}
3586
3587/*
3588 * ioctl32 compat
3589 */
3590#ifdef CONFIG_COMPAT
3591#include "pcm_compat.c"
3592#else
3593#define snd_pcm_ioctl_compat NULL
3594#endif
3595
3596/*
3597 * To be removed helpers to keep binary compatibility
3598 */
3599
Takashi Iwai59d48582005-12-01 10:51:58 +01003600#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3602#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3603
Takashi Iwai877211f2005-11-17 13:59:38 +01003604static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3605 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
3607 unsigned int i;
3608
3609 memset(params, 0, sizeof(*params));
3610 params->flags = oparams->flags;
3611 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3612 params->masks[i].bits[0] = oparams->masks[i];
3613 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3614 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3615 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3616 params->info = oparams->info;
3617 params->msbits = oparams->msbits;
3618 params->rate_num = oparams->rate_num;
3619 params->rate_den = oparams->rate_den;
3620 params->fifo_size = oparams->fifo_size;
3621}
3622
Takashi Iwai877211f2005-11-17 13:59:38 +01003623static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3624 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 unsigned int i;
3627
3628 memset(oparams, 0, sizeof(*oparams));
3629 oparams->flags = params->flags;
3630 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3631 oparams->masks[i] = params->masks[i].bits[0];
3632 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3633 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3634 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3635 oparams->info = params->info;
3636 oparams->msbits = params->msbits;
3637 oparams->rate_num = params->rate_num;
3638 oparams->rate_den = params->rate_den;
3639 oparams->fifo_size = params->fifo_size;
3640}
3641
Takashi Iwai877211f2005-11-17 13:59:38 +01003642static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3643 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
Takashi Iwai877211f2005-11-17 13:59:38 +01003645 struct snd_pcm_hw_params *params;
3646 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 int err;
3648
3649 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003650 if (!params)
3651 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Li Zefanef44a1e2009-04-10 09:43:08 +08003653 oparams = memdup_user(_oparams, sizeof(*oparams));
3654 if (IS_ERR(oparams)) {
3655 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 goto out;
3657 }
3658 snd_pcm_hw_convert_from_old_params(params, oparams);
3659 err = snd_pcm_hw_refine(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003660 if (err < 0)
3661 goto out_old;
Li Zefanef44a1e2009-04-10 09:43:08 +08003662
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003663 err = fixup_unreferenced_params(substream, params);
3664 if (err < 0)
3665 goto out_old;
3666
3667 snd_pcm_hw_convert_to_old_params(oparams, params);
3668 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3669 err = -EFAULT;
3670out_old:
Li Zefanef44a1e2009-04-10 09:43:08 +08003671 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672out:
3673 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return err;
3675}
3676
Takashi Iwai877211f2005-11-17 13:59:38 +01003677static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3678 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Takashi Iwai877211f2005-11-17 13:59:38 +01003680 struct snd_pcm_hw_params *params;
3681 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 int err;
3683
3684 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003685 if (!params)
3686 return -ENOMEM;
3687
3688 oparams = memdup_user(_oparams, sizeof(*oparams));
3689 if (IS_ERR(oparams)) {
3690 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 goto out;
3692 }
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 snd_pcm_hw_convert_from_old_params(params, oparams);
3695 err = snd_pcm_hw_params(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003696 if (err < 0)
3697 goto out_old;
Li Zefanef44a1e2009-04-10 09:43:08 +08003698
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003699 snd_pcm_hw_convert_to_old_params(oparams, params);
3700 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3701 err = -EFAULT;
3702out_old:
Li Zefanef44a1e2009-04-10 09:43:08 +08003703 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704out:
3705 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return err;
3707}
Takashi Iwai59d48582005-12-01 10:51:58 +01003708#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Cliff Cai70036092008-09-03 10:54:36 +02003710#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003711static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3712 unsigned long addr,
3713 unsigned long len,
3714 unsigned long pgoff,
3715 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003716{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003717 struct snd_pcm_file *pcm_file = file->private_data;
3718 struct snd_pcm_substream *substream = pcm_file->substream;
3719 struct snd_pcm_runtime *runtime = substream->runtime;
3720 unsigned long offset = pgoff << PAGE_SHIFT;
3721
3722 switch (offset) {
3723 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3724 return (unsigned long)runtime->status;
3725 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3726 return (unsigned long)runtime->control;
3727 default:
3728 return (unsigned long)runtime->dma_area + offset;
3729 }
Cliff Cai70036092008-09-03 10:54:36 +02003730}
3731#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003732# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003733#endif
3734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735/*
3736 * Register section
3737 */
3738
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003739const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003741 .owner = THIS_MODULE,
3742 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003743 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003744 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003745 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003746 .llseek = no_llseek,
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003747 .poll = snd_pcm_poll,
Takashi Iwai67616fe2017-08-30 15:39:32 +02003748 .unlocked_ioctl = snd_pcm_ioctl,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003749 .compat_ioctl = snd_pcm_ioctl_compat,
3750 .mmap = snd_pcm_mmap,
3751 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003752 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 },
3754 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003755 .owner = THIS_MODULE,
3756 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003757 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003758 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003759 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003760 .llseek = no_llseek,
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003761 .poll = snd_pcm_poll,
Takashi Iwai67616fe2017-08-30 15:39:32 +02003762 .unlocked_ioctl = snd_pcm_ioctl,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003763 .compat_ioctl = snd_pcm_ioctl_compat,
3764 .mmap = snd_pcm_mmap,
3765 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003766 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 }
3768};