blob: aef86025627837621d9fc94ba3b3d9170e2515c4 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Arnd Bergmann09d94172018-04-24 20:06:14 +08007#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -04009#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/file.h>
11#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010012#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/time.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020014#include <linux/pm_qos.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010015#include <linux/io.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010016#include <linux/dma-mapping.h>
Takashi Iwai7e8edae2019-11-05 09:01:37 +010017#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <sound/core.h>
19#include <sound/control.h>
20#include <sound/info.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23#include <sound/timer.h>
24#include <sound/minors.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080025#include <linux/uio.h>
Chanho Minb888a5f72018-11-26 14:36:37 +090026#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Takashi Sakamoto2c4842d2017-05-26 09:30:46 +090028#include "pcm_local.h"
29
Takashi Sakamoto37567c52017-06-07 08:46:44 +090030#ifdef CONFIG_SND_DEBUG
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090031#define CREATE_TRACE_POINTS
32#include "pcm_param_trace.h"
Takashi Sakamoto37567c52017-06-07 08:46:44 +090033#else
34#define trace_hw_mask_param_enabled() 0
35#define trace_hw_interval_param_enabled() 0
36#define trace_hw_mask_param(substream, type, index, prev, curr)
37#define trace_hw_interval_param(substream, type, index, prev, curr)
38#endif
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
41 * Compatibility
42 */
43
Takashi Iwai877211f2005-11-17 13:59:38 +010044struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned int flags;
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010048 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
50 unsigned int rmask;
51 unsigned int cmask;
52 unsigned int info;
53 unsigned int msbits;
54 unsigned int rate_num;
55 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010056 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned char reserved[64];
58};
59
Takashi Iwai59d48582005-12-01 10:51:58 +010060#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010061#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Takashi Iwai877211f2005-11-17 13:59:38 +010064static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010068#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010069static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 *
73 */
74
Takashi Iwai7af142f2014-09-01 11:19:37 +020075static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Takashi Iwai73365cb2019-01-13 09:35:17 +010077void snd_pcm_group_init(struct snd_pcm_group *group)
Takashi Iwai67ec10722016-02-17 14:30:26 +010078{
Takashi Iwai73365cb2019-01-13 09:35:17 +010079 spin_lock_init(&group->lock);
80 mutex_init(&group->mutex);
81 INIT_LIST_HEAD(&group->substreams);
Takashi Iwai0e279dc2019-07-19 10:55:05 +020082 refcount_set(&group->refs, 1);
Takashi Iwai67ec10722016-02-17 14:30:26 +010083}
84
Takashi Iwaif57f3df2019-01-13 09:50:33 +010085/* define group lock helpers */
86#define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
87static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
88{ \
89 if (nonatomic) \
90 mutex_ ## mutex_action(&group->mutex); \
91 else \
92 spin_ ## action(&group->lock); \
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +020093}
94
Takashi Iwaif57f3df2019-01-13 09:50:33 +010095DEFINE_PCM_GROUP_LOCK(lock, lock);
96DEFINE_PCM_GROUP_LOCK(unlock, unlock);
97DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
98DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
Anna-Maria Gleixner10aa7ca2018-05-04 17:28:10 +020099
Takashi Iwai30b771c2014-10-30 15:02:50 +0100100/**
101 * snd_pcm_stream_lock - Lock the PCM stream
102 * @substream: PCM substream
103 *
104 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
105 * flag of the given substream. This also takes the global link rw lock
106 * (or rw sem), too, for avoiding the race with linked streams.
107 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200108void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
109{
Takashi Iwaief2056b2019-01-13 10:15:03 +0100110 snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200111}
112EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
113
Takashi Iwai30b771c2014-10-30 15:02:50 +0100114/**
115 * snd_pcm_stream_lock - Unlock the PCM stream
116 * @substream: PCM substream
117 *
118 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
119 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200120void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
121{
Takashi Iwaief2056b2019-01-13 10:15:03 +0100122 snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200123}
124EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
125
Takashi Iwai30b771c2014-10-30 15:02:50 +0100126/**
127 * snd_pcm_stream_lock_irq - Lock the PCM stream
128 * @substream: PCM substream
129 *
130 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
131 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
132 * as snd_pcm_stream_lock().
133 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200134void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
135{
Takashi Iwaief2056b2019-01-13 10:15:03 +0100136 snd_pcm_group_lock_irq(&substream->self_group,
137 substream->pcm->nonatomic);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200138}
139EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
140
Takashi Iwai30b771c2014-10-30 15:02:50 +0100141/**
142 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
143 * @substream: PCM substream
144 *
145 * This is a counter-part of snd_pcm_stream_lock_irq().
146 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200147void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
148{
Takashi Iwaief2056b2019-01-13 10:15:03 +0100149 snd_pcm_group_unlock_irq(&substream->self_group,
150 substream->pcm->nonatomic);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200151}
152EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
153
154unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
155{
Takashi Iwaief2056b2019-01-13 10:15:03 +0100156 unsigned long flags = 0;
157 if (substream->pcm->nonatomic)
158 mutex_lock(&substream->self_group.mutex);
159 else
160 spin_lock_irqsave(&substream->self_group.lock, flags);
161 return flags;
Takashi Iwai7af142f2014-09-01 11:19:37 +0200162}
163EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
164
Takashi Iwai30b771c2014-10-30 15:02:50 +0100165/**
166 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
167 * @substream: PCM substream
168 * @flags: irq flags
169 *
170 * This is a counter-part of snd_pcm_stream_lock_irqsave().
171 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200172void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
173 unsigned long flags)
174{
Takashi Iwaief2056b2019-01-13 10:15:03 +0100175 if (substream->pcm->nonatomic)
176 mutex_unlock(&substream->self_group.mutex);
177 else
178 spin_unlock_irqrestore(&substream->self_group.lock, flags);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200179}
180EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Takashi Iwaifc033cb2019-11-17 09:53:03 +0100182/* Run PCM ioctl ops */
183static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
184 unsigned cmd, void *arg)
185{
186 if (substream->ops->ioctl)
187 return substream->ops->ioctl(substream, cmd, arg);
188 else
189 return snd_pcm_lib_ioctl(substream, cmd, arg);
190}
191
Takashi Iwai877211f2005-11-17 13:59:38 +0100192int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Takashi Iwai877211f2005-11-17 13:59:38 +0100194 struct snd_pcm *pcm = substream->pcm;
195 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 memset(info, 0, sizeof(*info));
198 info->card = pcm->card->number;
199 info->device = pcm->device;
200 info->stream = substream->stream;
201 info->subdevice = substream->number;
202 strlcpy(info->id, pcm->id, sizeof(info->id));
203 strlcpy(info->name, pcm->name, sizeof(info->name));
204 info->dev_class = pcm->dev_class;
205 info->dev_subclass = pcm->dev_subclass;
206 info->subdevices_count = pstr->substream_count;
207 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
208 strlcpy(info->subname, substream->name, sizeof(info->subname));
Takashi Sakamotoe11f0f92017-06-14 19:30:03 +0900209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 0;
211}
212
Takashi Iwai877211f2005-11-17 13:59:38 +0100213int snd_pcm_info_user(struct snd_pcm_substream *substream,
214 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Takashi Iwai877211f2005-11-17 13:59:38 +0100216 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int err;
218
219 info = kmalloc(sizeof(*info), GFP_KERNEL);
220 if (! info)
221 return -ENOMEM;
222 err = snd_pcm_info(substream, info);
223 if (err >= 0) {
224 if (copy_to_user(_info, info, sizeof(*info)))
225 err = -EFAULT;
226 }
227 kfree(info);
228 return err;
229}
230
Takashi Iwaif9b0c052020-02-06 17:39:43 +0100231/* macro for simplified cast */
232#define PARAM_MASK_BIT(b) (1U << (__force int)(b))
233
Takashi Iwai63825f32014-10-22 12:04:46 +0200234static bool hw_support_mmap(struct snd_pcm_substream *substream)
235{
236 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
237 return false;
Christoph Hellwig425da152019-08-03 13:31:59 +0300238
239 if (substream->ops->mmap ||
Takashi Iwai6111fd22019-11-04 11:11:15 +0100240 (substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV &&
241 substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV_UC))
Christoph Hellwig425da152019-08-03 13:31:59 +0300242 return true;
243
244 return dma_can_mmap(substream->dma_buffer.dev.dev);
Takashi Iwai63825f32014-10-22 12:04:46 +0200245}
246
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900247static int constrain_mask_params(struct snd_pcm_substream *substream,
248 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900250 struct snd_pcm_hw_constraints *constrs =
251 &substream->runtime->hw_constraints;
252 struct snd_mask *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned int k;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900254 struct snd_mask old_mask;
255 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
258 m = hw_param_mask(params, k);
259 if (snd_mask_empty(m))
260 return -EINVAL;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900261
262 /* This parameter is not requested to change by a caller. */
Takashi Iwaif9b0c052020-02-06 17:39:43 +0100263 if (!(params->rmask & PARAM_MASK_BIT(k)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900265
266 if (trace_hw_mask_param_enabled())
267 old_mask = *m;
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 changed = snd_mask_refine(m, constrs_mask(constrs, k));
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900270 if (changed < 0)
271 return changed;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900272 if (changed == 0)
273 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900274
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900275 /* Set corresponding flag so that the caller gets it. */
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900276 trace_hw_mask_param(substream, k, 0, &old_mask, m);
Takashi Iwaif9b0c052020-02-06 17:39:43 +0100277 params->cmask |= PARAM_MASK_BIT(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900280 return 0;
281}
282
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900283static int constrain_interval_params(struct snd_pcm_substream *substream,
284 struct snd_pcm_hw_params *params)
285{
286 struct snd_pcm_hw_constraints *constrs =
287 &substream->runtime->hw_constraints;
288 struct snd_interval *i;
289 unsigned int k;
290 struct snd_interval old_interval;
291 int changed;
292
293 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
294 i = hw_param_interval(params, k);
295 if (snd_interval_empty(i))
296 return -EINVAL;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900297
298 /* This parameter is not requested to change by a caller. */
Takashi Iwaif9b0c052020-02-06 17:39:43 +0100299 if (!(params->rmask & PARAM_MASK_BIT(k)))
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900300 continue;
301
302 if (trace_hw_interval_param_enabled())
303 old_interval = *i;
304
305 changed = snd_interval_refine(i, constrs_interval(constrs, k));
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900306 if (changed < 0)
307 return changed;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900308 if (changed == 0)
309 continue;
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900310
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900311 /* Set corresponding flag so that the caller gets it. */
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900312 trace_hw_interval_param(substream, k, 0, &old_interval, i);
Takashi Iwaif9b0c052020-02-06 17:39:43 +0100313 params->cmask |= PARAM_MASK_BIT(k);
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900314 }
315
316 return 0;
317}
318
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900319static int constrain_params_by_rules(struct snd_pcm_substream *substream,
320 struct snd_pcm_hw_params *params)
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900321{
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900322 struct snd_pcm_hw_constraints *constrs =
323 &substream->runtime->hw_constraints;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900324 unsigned int k;
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100325 unsigned int *rstamps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900327 unsigned int stamp;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900328 struct snd_pcm_hw_rule *r;
329 unsigned int d;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900330 struct snd_mask old_mask;
331 struct snd_interval old_interval;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900332 bool again;
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100333 int changed, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900335 /*
336 * Each application of rule has own sequence number.
337 *
338 * Each member of 'rstamps' array represents the sequence number of
339 * recent application of corresponding rule.
340 */
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100341 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
342 if (!rstamps)
343 return -ENOMEM;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900344
345 /*
346 * Each member of 'vstamps' array represents the sequence number of
347 * recent application of rule in which corresponding parameters were
348 * changed.
349 *
350 * In initial state, elements corresponding to parameters requested by
351 * a caller is 1. For unrequested parameters, corresponding members
352 * have 0 so that the parameters are never changed anymore.
353 */
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900354 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
Takashi Iwaif9b0c052020-02-06 17:39:43 +0100355 vstamps[k] = (params->rmask & PARAM_MASK_BIT(k)) ? 1 : 0;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900356
357 /* Due to the above design, actual sequence number starts at 2. */
358 stamp = 2;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900359retry:
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900360 /* Apply all rules in order. */
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900361 again = false;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900362 for (k = 0; k < constrs->rules_num; k++) {
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900363 r = &constrs->rules[k];
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900364
365 /*
366 * Check condition bits of this rule. When the rule has
367 * some condition bits, parameter without the bits is
368 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
369 * is an example of the condition bits.
370 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900371 if (r->cond && !(r->cond & params->flags))
372 continue;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900373
374 /*
375 * The 'deps' array includes maximum three dependencies
376 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
377 * member of this array is a sentinel and should be
378 * negative value.
379 *
380 * This rule should be processed in this time when dependent
381 * parameters were changed at former applications of the other
382 * rules.
383 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900384 for (d = 0; r->deps[d] >= 0; d++) {
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900385 if (vstamps[r->deps[d]] > rstamps[k])
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900388 if (r->deps[d] < 0)
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900389 continue;
390
391 if (trace_hw_mask_param_enabled()) {
392 if (hw_is_mask(r->var))
393 old_mask = *hw_param_mask(params, r->var);
394 }
395 if (trace_hw_interval_param_enabled()) {
396 if (hw_is_interval(r->var))
397 old_interval = *hw_param_interval(params, r->var);
398 }
399
400 changed = r->func(params, r);
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100401 if (changed < 0) {
402 err = changed;
403 goto out;
404 }
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900405
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900406 /*
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900407 * When the parameter is changed, notify it to the caller
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900408 * by corresponding returned bit, then preparing for next
409 * iteration.
410 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900411 if (changed && r->var >= 0) {
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900412 if (hw_is_mask(r->var)) {
413 trace_hw_mask_param(substream, r->var,
414 k + 1, &old_mask,
415 hw_param_mask(params, r->var));
416 }
417 if (hw_is_interval(r->var)) {
418 trace_hw_interval_param(substream, r->var,
419 k + 1, &old_interval,
420 hw_param_interval(params, r->var));
421 }
422
Takashi Iwaif9b0c052020-02-06 17:39:43 +0100423 params->cmask |= PARAM_MASK_BIT(r->var);
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900424 vstamps[r->var] = stamp;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900425 again = true;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900426 }
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900427
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900428 rstamps[k] = stamp++;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900429 }
430
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900431 /* Iterate to evaluate all rules till no parameters are changed. */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900432 if (again)
433 goto retry;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900434
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100435 out:
436 kfree(rstamps);
437 return err;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900438}
439
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900440static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
441 struct snd_pcm_hw_params *params)
442{
443 const struct snd_interval *i;
444 const struct snd_mask *m;
445 int err;
446
447 if (!params->msbits) {
448 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
449 if (snd_interval_single(i))
450 params->msbits = snd_interval_value(i);
451 }
452
453 if (!params->rate_den) {
454 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
455 if (snd_interval_single(i)) {
456 params->rate_num = snd_interval_value(i);
457 params->rate_den = 1;
458 }
459 }
460
461 if (!params->fifo_size) {
462 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
463 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
464 if (snd_mask_single(m) && snd_interval_single(i)) {
Takashi Iwaifc033cb2019-11-17 09:53:03 +0100465 err = snd_pcm_ops_ioctl(substream,
466 SNDRV_PCM_IOCTL1_FIFO_SIZE,
467 params);
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900468 if (err < 0)
469 return err;
470 }
471 }
472
Takashi Sakamoto7802fb52017-06-09 09:34:40 +0900473 if (!params->info) {
474 params->info = substream->runtime->hw.info;
475 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
476 SNDRV_PCM_INFO_DRAIN_TRIGGER);
477 if (!hw_support_mmap(substream))
478 params->info &= ~(SNDRV_PCM_INFO_MMAP |
479 SNDRV_PCM_INFO_MMAP_VALID);
480 }
481
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900482 return 0;
483}
484
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900485int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
486 struct snd_pcm_hw_params *params)
487{
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900488 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 params->info = 0;
491 params->fifo_size = 0;
Takashi Iwaif9b0c052020-02-06 17:39:43 +0100492 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 params->msbits = 0;
Takashi Iwaif9b0c052020-02-06 17:39:43 +0100494 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_RATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 params->rate_num = 0;
496 params->rate_den = 0;
497 }
498
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900499 err = constrain_mask_params(substream, params);
500 if (err < 0)
501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900503 err = constrain_interval_params(substream, params);
504 if (err < 0)
505 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900507 err = constrain_params_by_rules(substream, params);
508 if (err < 0)
509 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 params->rmask = 0;
Takashi Sakamoto7802fb52017-06-09 09:34:40 +0900512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
514}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200515EXPORT_SYMBOL(snd_pcm_hw_refine);
516
Takashi Iwai877211f2005-11-17 13:59:38 +0100517static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
518 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Takashi Iwai877211f2005-11-17 13:59:38 +0100520 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int err;
522
Li Zefanef44a1e2009-04-10 09:43:08 +0800523 params = memdup_user(_params, sizeof(*params));
524 if (IS_ERR(params))
525 return PTR_ERR(params);
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 err = snd_pcm_hw_refine(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900528 if (err < 0)
529 goto end;
Li Zefanef44a1e2009-04-10 09:43:08 +0800530
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900531 err = fixup_unreferenced_params(substream, params);
532 if (err < 0)
533 goto end;
534
535 if (copy_to_user(_params, params, sizeof(*params)))
536 err = -EFAULT;
537end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 kfree(params);
539 return err;
540}
541
Takashi Iwai9442e692006-09-30 23:27:19 -0700542static int period_to_usecs(struct snd_pcm_runtime *runtime)
543{
544 int usecs;
545
546 if (! runtime->rate)
547 return -1; /* invalid */
548
549 /* take 75% of period time as the deadline */
550 usecs = (750000 / runtime->rate) * runtime->period_size;
551 usecs += ((750000 % runtime->rate) * runtime->period_size) /
552 runtime->rate;
553
554 return usecs;
555}
556
Takashi Iwaicb639a42020-01-31 16:22:14 +0100557static void snd_pcm_set_state(struct snd_pcm_substream *substream,
558 snd_pcm_state_t state)
Takashi Iwai9b0573c02012-10-12 15:07:34 +0200559{
560 snd_pcm_stream_lock_irq(substream);
561 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
562 substream->runtime->status->state = state;
563 snd_pcm_stream_unlock_irq(substream);
564}
565
Jie Yang90bbaf62015-10-16 17:57:46 +0800566static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
567 int event)
568{
569#ifdef CONFIG_SND_PCM_TIMER
570 if (substream->timer)
571 snd_timer_notify(substream->timer, event,
572 &substream->runtime->trigger_tstamp);
573#endif
574}
575
Takashi Iwai1e850be2019-11-17 09:53:06 +0100576static void snd_pcm_sync_stop(struct snd_pcm_substream *substream)
577{
578 if (substream->runtime->stop_operating) {
579 substream->runtime->stop_operating = false;
580 if (substream->ops->sync_stop)
581 substream->ops->sync_stop(substream);
Takashi Iwaifabb26d2019-11-17 09:53:07 +0100582 else if (substream->pcm->card->sync_irq > 0)
583 synchronize_irq(substream->pcm->card->sync_irq);
Takashi Iwai1e850be2019-11-17 09:53:06 +0100584 }
585}
586
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900587/**
588 * snd_pcm_hw_param_choose - choose a configuration defined by @params
589 * @pcm: PCM instance
590 * @params: the hw_params instance
591 *
592 * Choose one configuration from configuration space defined by @params.
593 * The configuration chosen is that obtained fixing in this order:
594 * first access, first format, first subformat, min channels,
595 * min rate, min period time, max buffer size, min tick time
596 *
597 * Return: Zero if successful, or a negative error code on failure.
598 */
599static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
600 struct snd_pcm_hw_params *params)
601{
602 static const int vars[] = {
603 SNDRV_PCM_HW_PARAM_ACCESS,
604 SNDRV_PCM_HW_PARAM_FORMAT,
605 SNDRV_PCM_HW_PARAM_SUBFORMAT,
606 SNDRV_PCM_HW_PARAM_CHANNELS,
607 SNDRV_PCM_HW_PARAM_RATE,
608 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
609 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
610 SNDRV_PCM_HW_PARAM_TICK_TIME,
611 -1
612 };
613 const int *v;
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900614 struct snd_mask old_mask;
615 struct snd_interval old_interval;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900616 int changed;
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900617
618 for (v = vars; *v != -1; v++) {
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900619 /* Keep old parameter to trace. */
620 if (trace_hw_mask_param_enabled()) {
621 if (hw_is_mask(*v))
622 old_mask = *hw_param_mask(params, *v);
623 }
624 if (trace_hw_interval_param_enabled()) {
625 if (hw_is_interval(*v))
626 old_interval = *hw_param_interval(params, *v);
627 }
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900628 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900629 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900630 else
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900631 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwaie1a3a982018-04-09 17:12:16 +0200632 if (changed < 0)
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900633 return changed;
634 if (changed == 0)
635 continue;
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900636
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900637 /* Trace the changed parameter. */
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900638 if (hw_is_mask(*v)) {
639 trace_hw_mask_param(pcm, *v, 0, &old_mask,
640 hw_param_mask(params, *v));
641 }
642 if (hw_is_interval(*v)) {
643 trace_hw_interval_param(pcm, *v, 0, &old_interval,
644 hw_param_interval(params, *v));
645 }
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900646 }
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900647
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900648 return 0;
649}
650
Takashi Iwai877211f2005-11-17 13:59:38 +0100651static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
652 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Takashi Iwai877211f2005-11-17 13:59:38 +0100654 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700655 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 unsigned int bits;
657 snd_pcm_uframes_t frames;
658
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200659 if (PCM_RUNTIME_CHECK(substream))
660 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 snd_pcm_stream_lock_irq(substream);
663 switch (runtime->status->state) {
664 case SNDRV_PCM_STATE_OPEN:
665 case SNDRV_PCM_STATE_SETUP:
666 case SNDRV_PCM_STATE_PREPARED:
667 break;
668 default:
669 snd_pcm_stream_unlock_irq(substream);
670 return -EBADFD;
671 }
672 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100673#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!substream->oss.oss)
675#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200676 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -EBADFD;
678
Takashi Iwai1e850be2019-11-17 09:53:06 +0100679 snd_pcm_sync_stop(substream);
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 params->rmask = ~0U;
682 err = snd_pcm_hw_refine(substream, params);
683 if (err < 0)
684 goto _error;
685
686 err = snd_pcm_hw_params_choose(substream, params);
687 if (err < 0)
688 goto _error;
689
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900690 err = fixup_unreferenced_params(substream, params);
691 if (err < 0)
692 goto _error;
693
Takashi Iwai0dba8082019-11-17 09:53:01 +0100694 if (substream->managed_buffer_alloc) {
695 err = snd_pcm_lib_malloc_pages(substream,
696 params_buffer_bytes(params));
697 if (err < 0)
698 goto _error;
699 runtime->buffer_changed = err > 0;
700 }
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (substream->ops->hw_params != NULL) {
703 err = substream->ops->hw_params(substream, params);
704 if (err < 0)
705 goto _error;
706 }
707
708 runtime->access = params_access(params);
709 runtime->format = params_format(params);
710 runtime->subformat = params_subformat(params);
711 runtime->channels = params_channels(params);
712 runtime->rate = params_rate(params);
713 runtime->period_size = params_period_size(params);
714 runtime->periods = params_periods(params);
715 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 runtime->info = params->info;
717 runtime->rate_num = params->rate_num;
718 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100719 runtime->no_period_wakeup =
720 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
721 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 bits = snd_pcm_format_physical_width(runtime->format);
724 runtime->sample_bits = bits;
725 bits *= runtime->channels;
726 runtime->frame_bits = bits;
727 frames = 1;
728 while (bits % 8 != 0) {
729 bits *= 2;
730 frames *= 2;
731 }
732 runtime->byte_align = bits / 8;
733 runtime->min_align = frames;
734
735 /* Default sw params */
736 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
737 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 runtime->start_threshold = 1;
740 runtime->stop_threshold = runtime->buffer_size;
741 runtime->silence_threshold = 0;
742 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200743 runtime->boundary = runtime->buffer_size;
744 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
745 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Takashi Iwaiadd9d562019-12-11 16:57:42 +0100747 /* clear the buffer for avoiding possible kernel info leaks */
748 if (runtime->dma_area && !substream->ops->copy_user)
749 memset(runtime->dma_area, 0, runtime->dma_bytes);
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c02012-10-12 15:07:34 +0200752 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700753
Rafael J. Wysocki5371a792020-02-12 00:34:15 +0100754 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
755 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700756 if ((usecs = period_to_usecs(runtime)) >= 0)
Rafael J. Wysocki5371a792020-02-12 00:34:15 +0100757 cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
758 usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 0;
760 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300761 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 so we force application to retry to set
763 the correct hardware parameter settings */
Takashi Iwai9b0573c02012-10-12 15:07:34 +0200764 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (substream->ops->hw_free != NULL)
766 substream->ops->hw_free(substream);
Takashi Iwai0dba8082019-11-17 09:53:01 +0100767 if (substream->managed_buffer_alloc)
768 snd_pcm_lib_free_pages(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return err;
770}
771
Takashi Iwai877211f2005-11-17 13:59:38 +0100772static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
773 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Takashi Iwai877211f2005-11-17 13:59:38 +0100775 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int err;
777
Li Zefanef44a1e2009-04-10 09:43:08 +0800778 params = memdup_user(_params, sizeof(*params));
779 if (IS_ERR(params))
780 return PTR_ERR(params);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 err = snd_pcm_hw_params(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900783 if (err < 0)
784 goto end;
Li Zefanef44a1e2009-04-10 09:43:08 +0800785
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900786 if (copy_to_user(_params, params, sizeof(*params)))
787 err = -EFAULT;
788end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 kfree(params);
790 return err;
791}
792
Takashi Iwai66f2d192020-01-29 20:59:07 +0100793static int do_hw_free(struct snd_pcm_substream *substream)
794{
795 int result = 0;
796
797 snd_pcm_sync_stop(substream);
798 if (substream->ops->hw_free)
799 result = substream->ops->hw_free(substream);
800 if (substream->managed_buffer_alloc)
801 snd_pcm_lib_free_pages(substream);
802 return result;
803}
804
Takashi Iwai877211f2005-11-17 13:59:38 +0100805static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Takashi Iwai877211f2005-11-17 13:59:38 +0100807 struct snd_pcm_runtime *runtime;
Takashi Iwai66f2d192020-01-29 20:59:07 +0100808 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200810 if (PCM_RUNTIME_CHECK(substream))
811 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 snd_pcm_stream_lock_irq(substream);
814 switch (runtime->status->state) {
815 case SNDRV_PCM_STATE_SETUP:
816 case SNDRV_PCM_STATE_PREPARED:
817 break;
818 default:
819 snd_pcm_stream_unlock_irq(substream);
820 return -EBADFD;
821 }
822 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200823 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -EBADFD;
Takashi Iwai66f2d192020-01-29 20:59:07 +0100825 result = do_hw_free(substream);
Takashi Iwai9b0573c02012-10-12 15:07:34 +0200826 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Rafael J. Wysocki5371a792020-02-12 00:34:15 +0100827 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return result;
829}
830
Takashi Iwai877211f2005-11-17 13:59:38 +0100831static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
832 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Takashi Iwai877211f2005-11-17 13:59:38 +0100834 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100835 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200837 if (PCM_RUNTIME_CHECK(substream))
838 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 snd_pcm_stream_lock_irq(substream);
841 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
842 snd_pcm_stream_unlock_irq(substream);
843 return -EBADFD;
844 }
845 snd_pcm_stream_unlock_irq(substream);
846
Dan Carpenter145d92e2015-09-23 12:42:28 +0300847 if (params->tstamp_mode < 0 ||
848 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return -EINVAL;
Takashi Iwai589008102014-07-16 17:45:27 +0200850 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
851 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200852 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (params->avail_min == 0)
854 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (params->silence_size >= runtime->boundary) {
856 if (params->silence_threshold != 0)
857 return -EINVAL;
858 } else {
859 if (params->silence_size > params->silence_threshold)
860 return -EINVAL;
861 if (params->silence_threshold > runtime->buffer_size)
862 return -EINVAL;
863 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100864 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 snd_pcm_stream_lock_irq(substream);
866 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai589008102014-07-16 17:45:27 +0200867 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
868 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 runtime->period_step = params->period_step;
870 runtime->control->avail_min = params->avail_min;
871 runtime->start_threshold = params->start_threshold;
872 runtime->stop_threshold = params->stop_threshold;
873 runtime->silence_threshold = params->silence_threshold;
874 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 params->boundary = runtime->boundary;
876 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
878 runtime->silence_size > 0)
879 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100880 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100883 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Takashi Iwai877211f2005-11-17 13:59:38 +0100886static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
887 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Takashi Iwai877211f2005-11-17 13:59:38 +0100889 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int err;
891 if (copy_from_user(&params, _params, sizeof(params)))
892 return -EFAULT;
893 err = snd_pcm_sw_params(substream, &params);
894 if (copy_to_user(_params, &params, sizeof(params)))
895 return -EFAULT;
896 return err;
897}
898
Takashi Iwaic99c5a32018-04-11 18:07:27 +0200899static inline snd_pcm_uframes_t
900snd_pcm_calc_delay(struct snd_pcm_substream *substream)
901{
902 snd_pcm_uframes_t delay;
903
904 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
905 delay = snd_pcm_playback_hw_avail(substream->runtime);
906 else
907 delay = snd_pcm_capture_avail(substream->runtime);
908 return delay + substream->runtime->delay;
909}
910
Baolin Wang3ddee7f2018-04-24 20:06:11 +0800911int snd_pcm_status64(struct snd_pcm_substream *substream,
912 struct snd_pcm_status64 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Takashi Iwai877211f2005-11-17 13:59:38 +0100914 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600917
918 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
919 &runtime->audio_tstamp_config);
920
921 /* backwards compatible behavior */
922 if (runtime->audio_tstamp_config.type_requested ==
923 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
924 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
925 runtime->audio_tstamp_config.type_requested =
926 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
927 else
928 runtime->audio_tstamp_config.type_requested =
929 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
930 runtime->audio_tstamp_report.valid = 0;
931 } else
932 runtime->audio_tstamp_report.valid = 1;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 status->state = runtime->status->state;
935 status->suspended_state = runtime->status->suspended_state;
936 if (status->state == SNDRV_PCM_STATE_OPEN)
937 goto _end;
Baolin Wang3ddee7f2018-04-24 20:06:11 +0800938 status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
939 status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100940 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100942 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
Baolin Wang3ddee7f2018-04-24 20:06:11 +0800943 status->tstamp_sec = runtime->status->tstamp.tv_sec;
944 status->tstamp_nsec =
945 runtime->status->tstamp.tv_nsec;
946 status->driver_tstamp_sec =
947 runtime->driver_tstamp.tv_sec;
948 status->driver_tstamp_nsec =
949 runtime->driver_tstamp.tv_nsec;
950 status->audio_tstamp_sec =
951 runtime->status->audio_tstamp.tv_sec;
952 status->audio_tstamp_nsec =
953 runtime->status->audio_tstamp.tv_nsec;
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 */
Baolin Wangfcae40c2018-04-24 20:06:08 +0800964 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
965 struct timespec64 tstamp;
966
967 snd_pcm_gettime(runtime, &tstamp);
Baolin Wang3ddee7f2018-04-24 20:06:11 +0800968 status->tstamp_sec = tstamp.tv_sec;
969 status->tstamp_nsec = tstamp.tv_nsec;
Baolin Wangfcae40c2018-04-24 20:06:08 +0800970 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100971 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100972 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 status->appl_ptr = runtime->control->appl_ptr;
974 status->hw_ptr = runtime->status->hw_ptr;
Takashi Iwai763e5062018-04-11 17:56:52 +0200975 status->avail = snd_pcm_avail(substream);
Takashi Iwaic99c5a32018-04-11 18:07:27 +0200976 status->delay = snd_pcm_running(substream) ?
977 snd_pcm_calc_delay(substream) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 status->avail_max = runtime->avail_max;
979 status->overrange = runtime->overrange;
980 runtime->avail_max = 0;
981 runtime->overrange = 0;
982 _end:
983 snd_pcm_stream_unlock_irq(substream);
984 return 0;
985}
986
Baolin Wang3ddee7f2018-04-24 20:06:11 +0800987static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
988 struct snd_pcm_status64 __user * _status,
989 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Baolin Wang3ddee7f2018-04-24 20:06:11 +0800991 struct snd_pcm_status64 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600995 /*
996 * with extension, parameters are read/write,
997 * get audio_tstamp_data from user,
998 * ignore rest of status structure
999 */
1000 if (ext && get_user(status.audio_tstamp_data,
1001 (u32 __user *)(&_status->audio_tstamp_data)))
1002 return -EFAULT;
Baolin Wang3ddee7f2018-04-24 20:06:11 +08001003 res = snd_pcm_status64(substream, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (res < 0)
1005 return res;
1006 if (copy_to_user(_status, &status, sizeof(status)))
1007 return -EFAULT;
1008 return 0;
1009}
1010
Baolin Wang3ddee7f2018-04-24 20:06:11 +08001011static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
1012 struct snd_pcm_status32 __user * _status,
1013 bool ext)
1014{
1015 struct snd_pcm_status64 status64;
1016 struct snd_pcm_status32 status32;
1017 int res;
1018
1019 memset(&status64, 0, sizeof(status64));
1020 memset(&status32, 0, sizeof(status32));
1021 /*
1022 * with extension, parameters are read/write,
1023 * get audio_tstamp_data from user,
1024 * ignore rest of status structure
1025 */
1026 if (ext && get_user(status64.audio_tstamp_data,
1027 (u32 __user *)(&_status->audio_tstamp_data)))
1028 return -EFAULT;
1029 res = snd_pcm_status64(substream, &status64);
1030 if (res < 0)
1031 return res;
1032
1033 status32 = (struct snd_pcm_status32) {
1034 .state = status64.state,
1035 .trigger_tstamp_sec = status64.trigger_tstamp_sec,
1036 .trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1037 .tstamp_sec = status64.tstamp_sec,
1038 .tstamp_nsec = status64.tstamp_nsec,
1039 .appl_ptr = status64.appl_ptr,
1040 .hw_ptr = status64.hw_ptr,
1041 .delay = status64.delay,
1042 .avail = status64.avail,
1043 .avail_max = status64.avail_max,
1044 .overrange = status64.overrange,
1045 .suspended_state = status64.suspended_state,
1046 .audio_tstamp_data = status64.audio_tstamp_data,
1047 .audio_tstamp_sec = status64.audio_tstamp_sec,
1048 .audio_tstamp_nsec = status64.audio_tstamp_nsec,
1049 .driver_tstamp_sec = status64.audio_tstamp_sec,
1050 .driver_tstamp_nsec = status64.audio_tstamp_nsec,
1051 .audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1052 };
1053
1054 if (copy_to_user(_status, &status32, sizeof(status32)))
1055 return -EFAULT;
1056
1057 return 0;
1058}
1059
Takashi Iwai877211f2005-11-17 13:59:38 +01001060static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1061 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Takashi Iwai877211f2005-11-17 13:59:38 +01001063 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 unsigned int channel;
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 channel = info->channel;
1067 runtime = substream->runtime;
1068 snd_pcm_stream_lock_irq(substream);
1069 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1070 snd_pcm_stream_unlock_irq(substream);
1071 return -EBADFD;
1072 }
1073 snd_pcm_stream_unlock_irq(substream);
1074 if (channel >= runtime->channels)
1075 return -EINVAL;
1076 memset(info, 0, sizeof(*info));
1077 info->channel = channel;
Takashi Iwaifc033cb2019-11-17 09:53:03 +01001078 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Takashi Iwai877211f2005-11-17 13:59:38 +01001081static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1082 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Takashi Iwai877211f2005-11-17 13:59:38 +01001084 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int res;
1086
1087 if (copy_from_user(&info, _info, sizeof(info)))
1088 return -EFAULT;
1089 res = snd_pcm_channel_info(substream, &info);
1090 if (res < 0)
1091 return res;
1092 if (copy_to_user(_info, &info, sizeof(info)))
1093 return -EFAULT;
1094 return 0;
1095}
1096
Takashi Iwai877211f2005-11-17 13:59:38 +01001097static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Takashi Iwai877211f2005-11-17 13:59:38 +01001099 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (runtime->trigger_master == NULL)
1101 return;
1102 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001103 if (!runtime->trigger_tstamp_latched)
1104 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 } else {
1106 snd_pcm_trigger_tstamp(runtime->trigger_master);
1107 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1108 }
1109 runtime->trigger_master = NULL;
1110}
1111
Takashi Iwaicb639a42020-01-31 16:22:14 +01001112#define ACTION_ARG_IGNORE (__force snd_pcm_state_t)0
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114struct action_ops {
Takashi Iwaicb639a42020-01-31 16:22:14 +01001115 int (*pre_action)(struct snd_pcm_substream *substream,
1116 snd_pcm_state_t state);
1117 int (*do_action)(struct snd_pcm_substream *substream,
1118 snd_pcm_state_t state);
1119 void (*undo_action)(struct snd_pcm_substream *substream,
1120 snd_pcm_state_t state);
1121 void (*post_action)(struct snd_pcm_substream *substream,
1122 snd_pcm_state_t state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123};
1124
1125/*
1126 * this functions is core for handling of linked stream
1127 * Note: the stream state might be changed also on failure
1128 * Note2: call with calling stream lock + link lock
1129 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001130static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001131 struct snd_pcm_substream *substream,
Takashi Iwaicb639a42020-01-31 16:22:14 +01001132 snd_pcm_state_t state,
1133 bool do_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Takashi Iwai877211f2005-11-17 13:59:38 +01001135 struct snd_pcm_substream *s = NULL;
1136 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +02001137 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Takashi Iwaief991b92007-02-22 12:52:53 +01001139 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001140 if (do_lock && s != substream) {
1141 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +02001142 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001143 else
Takashi Iwaidde1c652014-10-21 15:32:13 +02001144 spin_lock_nested(&s->self_group.lock, depth);
1145 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 res = ops->pre_action(s, state);
1148 if (res < 0)
1149 goto _unlock;
1150 }
Takashi Iwaief991b92007-02-22 12:52:53 +01001151 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 res = ops->do_action(s, state);
1153 if (res < 0) {
1154 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +01001155 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (s1 == s) /* failed stream */
1157 break;
1158 ops->undo_action(s1, state);
1159 }
1160 }
1161 s = NULL; /* unlock all */
1162 goto _unlock;
1163 }
1164 }
Takashi Iwaief991b92007-02-22 12:52:53 +01001165 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ops->post_action(s, state);
1167 }
1168 _unlock:
1169 if (do_lock) {
1170 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +01001171 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001172 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +02001173 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001174 mutex_unlock(&s1->self_group.mutex);
1175 else
1176 spin_unlock(&s1->self_group.lock);
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (s1 == s) /* end */
1179 break;
1180 }
1181 }
1182 return res;
1183}
1184
1185/*
1186 * Note: call with stream lock
1187 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001188static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001189 struct snd_pcm_substream *substream,
Takashi Iwaicb639a42020-01-31 16:22:14 +01001190 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 int res;
1193
1194 res = ops->pre_action(substream, state);
1195 if (res < 0)
1196 return res;
1197 res = ops->do_action(substream, state);
1198 if (res == 0)
1199 ops->post_action(substream, state);
1200 else if (ops->undo_action)
1201 ops->undo_action(substream, state);
1202 return res;
1203}
1204
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01001205static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1206 struct snd_pcm_group *new_group)
1207{
1208 substream->group = new_group;
1209 list_move(&substream->link_list, &new_group->substreams);
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/*
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001213 * Unref and unlock the group, but keep the stream lock;
1214 * when the group becomes empty and no longer referred, destroy itself
1215 */
1216static void snd_pcm_group_unref(struct snd_pcm_group *group,
1217 struct snd_pcm_substream *substream)
1218{
1219 bool do_free;
1220
1221 if (!group)
1222 return;
Takashi Iwai0e279dc2019-07-19 10:55:05 +02001223 do_free = refcount_dec_and_test(&group->refs);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001224 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1225 if (do_free)
1226 kfree(group);
1227}
1228
1229/*
1230 * Lock the group inside a stream lock and reference it;
1231 * return the locked group object, or NULL if not linked
1232 */
1233static struct snd_pcm_group *
1234snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1235{
1236 bool nonatomic = substream->pcm->nonatomic;
1237 struct snd_pcm_group *group;
1238 bool trylock;
1239
1240 for (;;) {
1241 if (!snd_pcm_stream_linked(substream))
1242 return NULL;
1243 group = substream->group;
1244 /* block freeing the group object */
1245 refcount_inc(&group->refs);
1246
1247 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1248 spin_trylock(&group->lock);
1249 if (trylock)
1250 break; /* OK */
1251
1252 /* re-lock for avoiding ABBA deadlock */
1253 snd_pcm_stream_unlock(substream);
1254 snd_pcm_group_lock(group, nonatomic);
1255 snd_pcm_stream_lock(substream);
1256
1257 /* check the group again; the above opens a small race window */
1258 if (substream->group == group)
1259 break; /* OK */
1260 /* group changed, try again */
1261 snd_pcm_group_unref(group, substream);
1262 }
1263 return group;
1264}
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266/*
1267 * Note: call with stream lock
1268 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001269static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001270 struct snd_pcm_substream *substream,
Takashi Iwaicb639a42020-01-31 16:22:14 +01001271 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001273 struct snd_pcm_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int res;
1275
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001276 group = snd_pcm_stream_group_ref(substream);
1277 if (group)
Takashi Iwaicb639a42020-01-31 16:22:14 +01001278 res = snd_pcm_action_group(ops, substream, state, true);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001279 else
1280 res = snd_pcm_action_single(ops, substream, state);
1281 snd_pcm_group_unref(group, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return res;
1283}
1284
1285/*
1286 * Note: don't use any locks before
1287 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001288static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001289 struct snd_pcm_substream *substream,
Takashi Iwaicb639a42020-01-31 16:22:14 +01001290 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 int res;
1293
Takashi Iwaie3a4bd52014-10-31 14:45:04 +01001294 snd_pcm_stream_lock_irq(substream);
1295 res = snd_pcm_action(ops, substream, state);
1296 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return res;
1298}
1299
1300/*
1301 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001302static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001303 struct snd_pcm_substream *substream,
Takashi Iwaicb639a42020-01-31 16:22:14 +01001304 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 int res;
1307
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001308 /* Guarantee the group members won't change during non-atomic action */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 down_read(&snd_pcm_link_rwsem);
1310 if (snd_pcm_stream_linked(substream))
Takashi Iwaicb639a42020-01-31 16:22:14 +01001311 res = snd_pcm_action_group(ops, substream, state, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 else
1313 res = snd_pcm_action_single(ops, substream, state);
1314 up_read(&snd_pcm_link_rwsem);
1315 return res;
1316}
1317
1318/*
1319 * start callbacks
1320 */
Takashi Iwaicb639a42020-01-31 16:22:14 +01001321static int snd_pcm_pre_start(struct snd_pcm_substream *substream,
1322 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Takashi Iwai877211f2005-11-17 13:59:38 +01001324 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1326 return -EBADFD;
1327 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1328 !snd_pcm_playback_data(substream))
1329 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001330 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 runtime->trigger_master = substream;
1332 return 0;
1333}
1334
Takashi Iwaicb639a42020-01-31 16:22:14 +01001335static int snd_pcm_do_start(struct snd_pcm_substream *substream,
1336 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 if (substream->runtime->trigger_master != substream)
1339 return 0;
1340 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1341}
1342
Takashi Iwaicb639a42020-01-31 16:22:14 +01001343static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
1344 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 if (substream->runtime->trigger_master == substream)
1347 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1348}
1349
Takashi Iwaicb639a42020-01-31 16:22:14 +01001350static void snd_pcm_post_start(struct snd_pcm_substream *substream,
1351 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Takashi Iwai877211f2005-11-17 13:59:38 +01001353 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001355 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001356 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1357 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 runtime->status->state = state;
1359 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1360 runtime->silence_size > 0)
1361 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001362 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Julia Lawallb17154c2015-11-29 16:36:40 +01001365static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 .pre_action = snd_pcm_pre_start,
1367 .do_action = snd_pcm_do_start,
1368 .undo_action = snd_pcm_undo_start,
1369 .post_action = snd_pcm_post_start
1370};
1371
1372/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001373 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001374 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001375 *
1376 * Return: Zero if successful, or a negative error code.
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001377 * The stream lock must be acquired before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001379int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Takashi Iwai877211f2005-11-17 13:59:38 +01001381 return snd_pcm_action(&snd_pcm_action_start, substream,
1382 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001385/* take the stream lock and start the streams */
1386static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1387{
1388 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1389 SNDRV_PCM_STATE_RUNNING);
1390}
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392/*
1393 * stop callbacks
1394 */
Takashi Iwaicb639a42020-01-31 16:22:14 +01001395static int snd_pcm_pre_stop(struct snd_pcm_substream *substream,
1396 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Takashi Iwai877211f2005-11-17 13:59:38 +01001398 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1400 return -EBADFD;
1401 runtime->trigger_master = substream;
1402 return 0;
1403}
1404
Takashi Iwaicb639a42020-01-31 16:22:14 +01001405static int snd_pcm_do_stop(struct snd_pcm_substream *substream,
1406 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 if (substream->runtime->trigger_master == substream &&
1409 snd_pcm_running(substream))
1410 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1411 return 0; /* unconditonally stop all substreams */
1412}
1413
Takashi Iwaicb639a42020-01-31 16:22:14 +01001414static void snd_pcm_post_stop(struct snd_pcm_substream *substream,
1415 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Takashi Iwai877211f2005-11-17 13:59:38 +01001417 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (runtime->status->state != state) {
1419 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001420 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001421 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Takashi Iwai1e850be2019-11-17 09:53:06 +01001423 runtime->stop_operating = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001425 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Julia Lawallb17154c2015-11-29 16:36:40 +01001428static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 .pre_action = snd_pcm_pre_stop,
1430 .do_action = snd_pcm_do_stop,
1431 .post_action = snd_pcm_post_stop
1432};
1433
1434/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001435 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001436 * @substream: the PCM substream instance
1437 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001439 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001440 *
Masanari Iida0a114582014-02-09 00:47:36 +09001441 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001443int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1446}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001447EXPORT_SYMBOL(snd_pcm_stop);
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001450 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001451 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001453 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001455 *
1456 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001458int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Takashi Iwai877211f2005-11-17 13:59:38 +01001460 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1461 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Takashi Iwai1fb85102014-11-07 17:08:28 +01001464/**
1465 * snd_pcm_stop_xrun - stop the running streams as XRUN
1466 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001467 *
1468 * This stops the given running substream (and all linked substreams) as XRUN.
1469 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1470 *
1471 * Return: Zero if successful, or a negative error code.
1472 */
1473int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1474{
1475 unsigned long flags;
Takashi Iwai1fb85102014-11-07 17:08:28 +01001476
1477 snd_pcm_stream_lock_irqsave(substream, flags);
Takashi Iwaie647f5a52018-07-04 15:08:05 +02001478 if (substream->runtime && snd_pcm_running(substream))
Takashi Iwai9cd641e2018-07-04 14:46:27 +02001479 __snd_pcm_xrun(substream);
Takashi Iwai1fb85102014-11-07 17:08:28 +01001480 snd_pcm_stream_unlock_irqrestore(substream, flags);
Takashi Iwai9cd641e2018-07-04 14:46:27 +02001481 return 0;
Takashi Iwai1fb85102014-11-07 17:08:28 +01001482}
1483EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/*
Takashi Iwaicb639a42020-01-31 16:22:14 +01001486 * pause callbacks: pass boolean (to start pause or resume) as state argument
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 */
Takashi Iwaicb639a42020-01-31 16:22:14 +01001488#define pause_pushed(state) (__force bool)(state)
1489
1490static int snd_pcm_pre_pause(struct snd_pcm_substream *substream,
1491 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Takashi Iwai877211f2005-11-17 13:59:38 +01001493 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1495 return -ENOSYS;
Takashi Iwaicb639a42020-01-31 16:22:14 +01001496 if (pause_pushed(state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1498 return -EBADFD;
1499 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1500 return -EBADFD;
1501 runtime->trigger_master = substream;
1502 return 0;
1503}
1504
Takashi Iwaicb639a42020-01-31 16:22:14 +01001505static int snd_pcm_do_pause(struct snd_pcm_substream *substream,
1506 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 if (substream->runtime->trigger_master != substream)
1509 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001510 /* some drivers might use hw_ptr to recover from the pause -
1511 update the hw_ptr now */
Takashi Iwaicb639a42020-01-31 16:22:14 +01001512 if (pause_pushed(state))
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001513 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001514 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001515 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001516 * delta, effectively to skip the check once.
1517 */
1518 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return substream->ops->trigger(substream,
Takashi Iwaicb639a42020-01-31 16:22:14 +01001520 pause_pushed(state) ?
1521 SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1522 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Takashi Iwaicb639a42020-01-31 16:22:14 +01001525static void snd_pcm_undo_pause(struct snd_pcm_substream *substream,
1526 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 if (substream->runtime->trigger_master == substream)
1529 substream->ops->trigger(substream,
Takashi Iwaicb639a42020-01-31 16:22:14 +01001530 pause_pushed(state) ?
1531 SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1533}
1534
Takashi Iwaicb639a42020-01-31 16:22:14 +01001535static void snd_pcm_post_pause(struct snd_pcm_substream *substream,
1536 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Takashi Iwai877211f2005-11-17 13:59:38 +01001538 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 snd_pcm_trigger_tstamp(substream);
Takashi Iwaicb639a42020-01-31 16:22:14 +01001540 if (pause_pushed(state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001542 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001544 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 } else {
1546 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001547 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549}
1550
Julia Lawallb17154c2015-11-29 16:36:40 +01001551static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 .pre_action = snd_pcm_pre_pause,
1553 .do_action = snd_pcm_do_pause,
1554 .undo_action = snd_pcm_undo_pause,
1555 .post_action = snd_pcm_post_pause
1556};
1557
1558/*
1559 * Push/release the pause for all linked streams.
1560 */
Takashi Iwaicb639a42020-01-31 16:22:14 +01001561static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Takashi Iwaicb639a42020-01-31 16:22:14 +01001563 return snd_pcm_action(&snd_pcm_action_pause, substream,
1564 (__force snd_pcm_state_t)push);
1565}
1566
1567static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream,
1568 bool push)
1569{
1570 return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream,
1571 (__force snd_pcm_state_t)push);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
1574#ifdef CONFIG_PM
Takashi Iwaicb639a42020-01-31 16:22:14 +01001575/* suspend callback: state argument ignored */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Takashi Iwaicb639a42020-01-31 16:22:14 +01001577static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream,
1578 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Takashi Iwai877211f2005-11-17 13:59:38 +01001580 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai113ce082019-03-25 10:38:58 +01001581 switch (runtime->status->state) {
1582 case SNDRV_PCM_STATE_SUSPENDED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return -EBUSY;
Takashi Iwai113ce082019-03-25 10:38:58 +01001584 /* unresumable PCM state; return -EBUSY for skipping suspend */
1585 case SNDRV_PCM_STATE_OPEN:
1586 case SNDRV_PCM_STATE_SETUP:
1587 case SNDRV_PCM_STATE_DISCONNECTED:
1588 return -EBUSY;
1589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 runtime->trigger_master = substream;
1591 return 0;
1592}
1593
Takashi Iwaicb639a42020-01-31 16:22:14 +01001594static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
1595 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Takashi Iwai877211f2005-11-17 13:59:38 +01001597 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (runtime->trigger_master != substream)
1599 return 0;
1600 if (! snd_pcm_running(substream))
1601 return 0;
1602 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1603 return 0; /* suspend unconditionally */
1604}
1605
Takashi Iwaicb639a42020-01-31 16:22:14 +01001606static void snd_pcm_post_suspend(struct snd_pcm_substream *substream,
1607 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Takashi Iwai877211f2005-11-17 13:59:38 +01001609 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001611 runtime->status->suspended_state = runtime->status->state;
1612 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001613 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001615 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Julia Lawallb17154c2015-11-29 16:36:40 +01001618static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 .pre_action = snd_pcm_pre_suspend,
1620 .do_action = snd_pcm_do_suspend,
1621 .post_action = snd_pcm_post_suspend
1622};
1623
Takashi Iwaice7f93e22019-01-15 10:54:02 +01001624/*
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001625 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001626 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001629 *
Takashi Iwaice7f93e22019-01-15 10:54:02 +01001630 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
Takashi Iwaice7f93e22019-01-15 10:54:02 +01001632static int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 int err;
1635 unsigned long flags;
1636
1637 snd_pcm_stream_lock_irqsave(substream, flags);
Takashi Iwaicb639a42020-01-31 16:22:14 +01001638 err = snd_pcm_action(&snd_pcm_action_suspend, substream,
1639 ACTION_ARG_IGNORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 snd_pcm_stream_unlock_irqrestore(substream, flags);
1641 return err;
1642}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001645 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001646 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001649 *
1650 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001652int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Takashi Iwai877211f2005-11-17 13:59:38 +01001654 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 int stream, err = 0;
1656
Takashi Iwai603bf522005-11-17 15:59:14 +01001657 if (! pcm)
1658 return 0;
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001661 for (substream = pcm->streams[stream].substream;
1662 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /* FIXME: the open/close code should lock this as well */
1664 if (substream->runtime == NULL)
1665 continue;
Ranjani Sridharand9c0b2a2019-02-08 17:29:53 -06001666
1667 /*
1668 * Skip BE dai link PCM's that are internal and may
1669 * not have their substream ops set.
1670 */
1671 if (!substream->ops)
1672 continue;
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 err = snd_pcm_suspend(substream);
1675 if (err < 0 && err != -EBUSY)
1676 return err;
1677 }
1678 }
1679 return 0;
1680}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001681EXPORT_SYMBOL(snd_pcm_suspend_all);
1682
Takashi Iwaicb639a42020-01-31 16:22:14 +01001683/* resume callbacks: state argument ignored */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Takashi Iwaicb639a42020-01-31 16:22:14 +01001685static int snd_pcm_pre_resume(struct snd_pcm_substream *substream,
1686 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Takashi Iwai877211f2005-11-17 13:59:38 +01001688 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1690 return -ENOSYS;
1691 runtime->trigger_master = substream;
1692 return 0;
1693}
1694
Takashi Iwaicb639a42020-01-31 16:22:14 +01001695static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
1696 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Takashi Iwai877211f2005-11-17 13:59:38 +01001698 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (runtime->trigger_master != substream)
1700 return 0;
1701 /* DMA not running previously? */
1702 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1703 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1704 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1705 return 0;
1706 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1707}
1708
Takashi Iwaicb639a42020-01-31 16:22:14 +01001709static void snd_pcm_undo_resume(struct snd_pcm_substream *substream,
1710 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 if (substream->runtime->trigger_master == substream &&
1713 snd_pcm_running(substream))
1714 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1715}
1716
Takashi Iwaicb639a42020-01-31 16:22:14 +01001717static void snd_pcm_post_resume(struct snd_pcm_substream *substream,
1718 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Takashi Iwai877211f2005-11-17 13:59:38 +01001720 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001722 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001723 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Takashi Iwai1e850be2019-11-17 09:53:06 +01001724 snd_pcm_sync_stop(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Julia Lawallb17154c2015-11-29 16:36:40 +01001727static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 .pre_action = snd_pcm_pre_resume,
1729 .do_action = snd_pcm_do_resume,
1730 .undo_action = snd_pcm_undo_resume,
1731 .post_action = snd_pcm_post_resume
1732};
1733
Takashi Iwai877211f2005-11-17 13:59:38 +01001734static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Takashi Iwaicb639a42020-01-31 16:22:14 +01001736 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream,
1737 ACTION_ARG_IGNORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
1740#else
1741
Takashi Iwai877211f2005-11-17 13:59:38 +01001742static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 return -ENOSYS;
1745}
1746
1747#endif /* CONFIG_PM */
1748
1749/*
1750 * xrun ioctl
1751 *
1752 * Change the RUNNING stream(s) to XRUN state.
1753 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001754static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Takashi Iwai877211f2005-11-17 13:59:38 +01001756 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 int result;
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 snd_pcm_stream_lock_irq(substream);
1760 switch (runtime->status->state) {
1761 case SNDRV_PCM_STATE_XRUN:
1762 result = 0; /* already there */
1763 break;
1764 case SNDRV_PCM_STATE_RUNNING:
Takashi Iwai9cd641e2018-07-04 14:46:27 +02001765 __snd_pcm_xrun(substream);
1766 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 break;
1768 default:
1769 result = -EBADFD;
1770 }
1771 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return result;
1773}
1774
1775/*
1776 * reset ioctl
1777 */
Takashi Iwaicb639a42020-01-31 16:22:14 +01001778/* reset callbacks: state argument ignored */
1779static int snd_pcm_pre_reset(struct snd_pcm_substream *substream,
1780 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwai877211f2005-11-17 13:59:38 +01001782 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 switch (runtime->status->state) {
1784 case SNDRV_PCM_STATE_RUNNING:
1785 case SNDRV_PCM_STATE_PREPARED:
1786 case SNDRV_PCM_STATE_PAUSED:
1787 case SNDRV_PCM_STATE_SUSPENDED:
1788 return 0;
1789 default:
1790 return -EBADFD;
1791 }
1792}
1793
Takashi Iwaicb639a42020-01-31 16:22:14 +01001794static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
1795 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Takashi Iwai877211f2005-11-17 13:59:38 +01001797 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaifc033cb2019-11-17 09:53:03 +01001798 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (err < 0)
1800 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001802 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1803 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 runtime->silence_start = runtime->status->hw_ptr;
1805 runtime->silence_filled = 0;
1806 return 0;
1807}
1808
Takashi Iwaicb639a42020-01-31 16:22:14 +01001809static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
1810 snd_pcm_state_t 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 runtime->control->appl_ptr = runtime->status->hw_ptr;
1814 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1815 runtime->silence_size > 0)
1816 snd_pcm_playback_silence(substream, ULONG_MAX);
1817}
1818
Julia Lawallb17154c2015-11-29 16:36:40 +01001819static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 .pre_action = snd_pcm_pre_reset,
1821 .do_action = snd_pcm_do_reset,
1822 .post_action = snd_pcm_post_reset
1823};
1824
Takashi Iwai877211f2005-11-17 13:59:38 +01001825static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Takashi Iwaicb639a42020-01-31 16:22:14 +01001827 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream,
1828 ACTION_ARG_IGNORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
1831/*
1832 * prepare ioctl
1833 */
Takashi Iwaicb639a42020-01-31 16:22:14 +01001834/* pass f_flags as state argument */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001835static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
Takashi Iwaicb639a42020-01-31 16:22:14 +01001836 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Takashi Iwai877211f2005-11-17 13:59:38 +01001838 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaicb639a42020-01-31 16:22:14 +01001839 int f_flags = (__force int)state;
1840
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001841 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1842 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return -EBADFD;
1844 if (snd_pcm_running(substream))
1845 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001846 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return 0;
1848}
1849
Takashi Iwaicb639a42020-01-31 16:22:14 +01001850static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
1851 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 int err;
Takashi Iwai1e850be2019-11-17 09:53:06 +01001854 snd_pcm_sync_stop(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 err = substream->ops->prepare(substream);
1856 if (err < 0)
1857 return err;
Takashi Iwaicb639a42020-01-31 16:22:14 +01001858 return snd_pcm_do_reset(substream, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Takashi Iwaicb639a42020-01-31 16:22:14 +01001861static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
1862 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Takashi Iwai877211f2005-11-17 13:59:38 +01001864 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c02012-10-12 15:07:34 +02001866 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Julia Lawallb17154c2015-11-29 16:36:40 +01001869static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 .pre_action = snd_pcm_pre_prepare,
1871 .do_action = snd_pcm_do_prepare,
1872 .post_action = snd_pcm_post_prepare
1873};
1874
1875/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001876 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001877 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001878 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001879 *
1880 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001882static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1883 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001885 int f_flags;
1886
1887 if (file)
1888 f_flags = file->f_flags;
1889 else
1890 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Takashi Iwai1b745cd2016-05-24 15:40:03 +02001892 snd_pcm_stream_lock_irq(substream);
1893 switch (substream->runtime->status->state) {
1894 case SNDRV_PCM_STATE_PAUSED:
Takashi Iwaicb639a42020-01-31 16:22:14 +01001895 snd_pcm_pause(substream, false);
Takashi Iwai1b745cd2016-05-24 15:40:03 +02001896 /* fallthru */
1897 case SNDRV_PCM_STATE_SUSPENDED:
1898 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1899 break;
1900 }
1901 snd_pcm_stream_unlock_irq(substream);
1902
Takashi Iwai68b4acd2016-05-24 15:07:39 +02001903 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
Takashi Iwaicb639a42020-01-31 16:22:14 +01001904 substream,
1905 (__force snd_pcm_state_t)f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
1908/*
1909 * drain ioctl
1910 */
1911
Takashi Iwaicb639a42020-01-31 16:22:14 +01001912/* drain init callbacks: state argument ignored */
1913static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream,
1914 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001916 struct snd_pcm_runtime *runtime = substream->runtime;
1917 switch (runtime->status->state) {
1918 case SNDRV_PCM_STATE_OPEN:
1919 case SNDRV_PCM_STATE_DISCONNECTED:
1920 case SNDRV_PCM_STATE_SUSPENDED:
1921 return -EBADFD;
1922 }
1923 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926
Takashi Iwaicb639a42020-01-31 16:22:14 +01001927static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,
1928 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Takashi Iwai877211f2005-11-17 13:59:38 +01001930 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1932 switch (runtime->status->state) {
1933 case SNDRV_PCM_STATE_PREPARED:
1934 /* start playback stream if possible */
1935 if (! snd_pcm_playback_empty(substream)) {
1936 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1937 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001938 } else {
1939 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 break;
1942 case SNDRV_PCM_STATE_RUNNING:
1943 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1944 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001945 case SNDRV_PCM_STATE_XRUN:
1946 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 default:
1949 break;
1950 }
1951 } else {
1952 /* stop running stream */
1953 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Takashi Iwaicb639a42020-01-31 16:22:14 +01001954 snd_pcm_state_t new_state;
1955
1956 new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001958 snd_pcm_do_stop(substream, new_state);
1959 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961 }
Libin Yang48d88292014-12-31 22:09:54 +08001962
1963 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1964 runtime->trigger_master == substream &&
1965 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1966 return substream->ops->trigger(substream,
1967 SNDRV_PCM_TRIGGER_DRAIN);
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return 0;
1970}
1971
Takashi Iwaicb639a42020-01-31 16:22:14 +01001972static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream,
1973 snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975}
1976
Julia Lawallb17154c2015-11-29 16:36:40 +01001977static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 .pre_action = snd_pcm_pre_drain_init,
1979 .do_action = snd_pcm_do_drain_init,
1980 .post_action = snd_pcm_post_drain_init
1981};
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983/*
1984 * Drain the stream(s).
1985 * When the substream is linked, sync until the draining of all playback streams
1986 * is finished.
1987 * After this call, all streams are supposed to be either SETUP or DRAINING
1988 * (capture only) state.
1989 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001990static int snd_pcm_drain(struct snd_pcm_substream *substream,
1991 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Takashi Iwai877211f2005-11-17 13:59:38 +01001993 struct snd_card *card;
1994 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001995 struct snd_pcm_substream *s;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01001996 struct snd_pcm_group *group;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001997 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001999 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 card = substream->pcm->card;
2002 runtime = substream->runtime;
2003
2004 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2005 return -EBADFD;
2006
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002007 if (file) {
2008 if (file->f_flags & O_NONBLOCK)
2009 nonblock = 1;
2010 } else if (substream->f_flags & O_NONBLOCK)
2011 nonblock = 1;
2012
Takashi Iwai21cb2a22005-05-31 14:35:31 +02002013 snd_pcm_stream_lock_irq(substream);
2014 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02002015 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwaicb639a42020-01-31 16:22:14 +01002016 snd_pcm_pause(substream, false);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02002017
2018 /* pre-start/stop - all running streams are changed to DRAINING state */
Takashi Iwaicb639a42020-01-31 16:22:14 +01002019 result = snd_pcm_action(&snd_pcm_action_drain_init, substream,
2020 ACTION_ARG_IGNORE);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002021 if (result < 0)
2022 goto unlock;
2023 /* in non-blocking, we don't wait in ioctl but let caller poll */
2024 if (nonblock) {
2025 result = -EAGAIN;
2026 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02002027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 for (;;) {
2030 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02002031 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (signal_pending(current)) {
2033 result = -ERESTARTSYS;
2034 break;
2035 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02002036 /* find a substream to drain */
2037 to_check = NULL;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002038 group = snd_pcm_stream_group_ref(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02002039 snd_pcm_group_for_each_entry(s, substream) {
2040 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
2041 continue;
2042 runtime = s->runtime;
2043 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2044 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02002045 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02002046 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02002047 }
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002048 snd_pcm_group_unref(group, substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02002049 if (!to_check)
2050 break; /* all drained */
2051 init_waitqueue_entry(&wait, current);
Yuki Tsunashima37151a42019-07-29 17:10:36 +02002052 set_current_state(TASK_INTERRUPTIBLE);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02002053 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaif2b36142011-05-26 08:09:38 +02002055 if (runtime->no_period_wakeup)
2056 tout = MAX_SCHEDULE_TIMEOUT;
2057 else {
2058 tout = 10;
2059 if (runtime->rate) {
2060 long t = runtime->period_size * 2 / runtime->rate;
2061 tout = max(t, tout);
2062 }
2063 tout = msecs_to_jiffies(tout * 1000);
2064 }
Yuki Tsunashima37151a42019-07-29 17:10:36 +02002065 tout = schedule_timeout(tout);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002068 group = snd_pcm_stream_group_ref(substream);
2069 snd_pcm_group_for_each_entry(s, substream) {
2070 if (s->runtime == to_check) {
2071 remove_wait_queue(&to_check->sleep, &wait);
2072 break;
2073 }
2074 }
2075 snd_pcm_group_unref(group, substream);
2076
Takashi Iwai0914f792012-10-16 16:43:39 +02002077 if (card->shutdown) {
2078 result = -ENODEV;
2079 break;
2080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (tout == 0) {
2082 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
2083 result = -ESTRPIPE;
2084 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002085 dev_dbg(substream->pcm->card->dev,
2086 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2088 result = -EIO;
2089 }
2090 break;
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02002093
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002094 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02002095 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 return result;
2098}
2099
2100/*
2101 * drop ioctl
2102 *
2103 * Immediately put all linked substreams into SETUP state.
2104 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002105static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Takashi Iwai877211f2005-11-17 13:59:38 +01002107 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 int result = 0;
2109
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002110 if (PCM_RUNTIME_CHECK(substream))
2111 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Takashi Iwaide1b8b92006-11-08 15:41:29 +01002114 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai4b95ff72016-05-24 15:08:31 +02002115 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return -EBADFD;
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 snd_pcm_stream_lock_irq(substream);
2119 /* resume pause */
2120 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwaicb639a42020-01-31 16:22:14 +01002121 snd_pcm_pause(substream, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2124 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2125 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return result;
2128}
2129
2130
Al Viro0888c322013-06-05 14:09:55 -04002131static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Al Viro0888c322013-06-05 14:09:55 -04002133 struct inode *inode = file_inode(file);
Takashi Iwaid819fb212019-01-13 09:25:42 +01002134 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002135 unsigned int minor;
2136
Al Viro0888c322013-06-05 14:09:55 -04002137 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2138 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 minor = iminor(inode);
Takashi Iwaid819fb212019-01-13 09:25:42 +01002140 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2141 if (!pcm)
2142 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2143 if (!pcm)
2144 return false;
2145 snd_card_unref(pcm->card);
2146 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149/*
2150 * PCM link handling
2151 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002152static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01002155 struct snd_pcm_file *pcm_file;
2156 struct snd_pcm_substream *substream1;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002157 struct snd_pcm_group *group, *target_group;
2158 bool nonatomic = substream->pcm->nonatomic;
Al Viro0888c322013-06-05 14:09:55 -04002159 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Al Viro0888c322013-06-05 14:09:55 -04002161 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04002163 if (!is_pcm_file(f.file)) {
2164 res = -EBADFD;
2165 goto _badf;
2166 }
2167 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 substream1 = pcm_file->substream;
Takashi Iwai73365cb2019-01-13 09:35:17 +01002169 group = kzalloc(sizeof(*group), GFP_KERNEL);
Takashi Iwai16625912012-03-13 15:55:43 +01002170 if (!group) {
2171 res = -ENOMEM;
2172 goto _nolock;
2173 }
Takashi Iwai73365cb2019-01-13 09:35:17 +01002174 snd_pcm_group_init(group);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002175
Takashi Iwaiecb41f02019-01-22 14:29:51 +01002176 down_write(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02002178 substream->runtime->status->state != substream1->runtime->status->state ||
2179 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 res = -EBADFD;
2181 goto _end;
2182 }
2183 if (snd_pcm_stream_linked(substream1)) {
2184 res = -EALREADY;
2185 goto _end;
2186 }
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002187
2188 snd_pcm_stream_lock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002190 snd_pcm_group_assign(substream, group);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002191 group = NULL; /* assigned, don't free this one below */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002193 target_group = substream->group;
2194 snd_pcm_stream_unlock_irq(substream);
2195
2196 snd_pcm_group_lock_irq(target_group, nonatomic);
2197 snd_pcm_stream_lock(substream1);
2198 snd_pcm_group_assign(substream1, target_group);
Takashi Iwai0e279dc2019-07-19 10:55:05 +02002199 refcount_inc(&target_group->refs);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002200 snd_pcm_stream_unlock(substream1);
2201 snd_pcm_group_unlock_irq(target_group, nonatomic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01002204 _nolock:
Al Virodd6c5cd2013-06-05 14:07:08 -04002205 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04002206 _badf:
2207 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return res;
2209}
2210
Takashi Iwai877211f2005-11-17 13:59:38 +01002211static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002213 snd_pcm_stream_lock(substream);
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002214 snd_pcm_group_assign(substream, &substream->self_group);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002215 snd_pcm_stream_unlock(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Takashi Iwai877211f2005-11-17 13:59:38 +01002218static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002220 struct snd_pcm_group *group;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002221 bool nonatomic = substream->pcm->nonatomic;
2222 bool do_free = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 int res = 0;
2224
Takashi Iwaiecb41f02019-01-22 14:29:51 +01002225 down_write(&snd_pcm_link_rwsem);
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (!snd_pcm_stream_linked(substream)) {
2228 res = -EALREADY;
2229 goto _end;
2230 }
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002231
2232 group = substream->group;
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002233 snd_pcm_group_lock_irq(group, nonatomic);
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 relink_to_local(substream);
Takashi Iwai0e279dc2019-07-19 10:55:05 +02002236 refcount_dec(&group->refs);
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002237
2238 /* detach the last stream, too */
2239 if (list_is_singular(&group->substreams)) {
Takashi Iwai7df5a5f2019-01-13 10:19:32 +01002240 relink_to_local(list_first_entry(&group->substreams,
2241 struct snd_pcm_substream,
2242 link_list));
Takashi Iwai0e279dc2019-07-19 10:55:05 +02002243 do_free = refcount_dec_and_test(&group->refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
Takashi Iwaia41c4cb2019-01-13 09:40:21 +01002245
Takashi Iwaif57f3df2019-01-13 09:50:33 +01002246 snd_pcm_group_unlock_irq(group, nonatomic);
2247 if (do_free)
2248 kfree(group);
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 _end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 up_write(&snd_pcm_link_rwsem);
2252 return res;
2253}
2254
2255/*
2256 * hw configurator
2257 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002258static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2259 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Takashi Iwai877211f2005-11-17 13:59:38 +01002261 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2263 hw_param_interval_c(params, rule->deps[1]), &t);
2264 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2265}
2266
Takashi Iwai877211f2005-11-17 13:59:38 +01002267static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2268 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
Takashi Iwai877211f2005-11-17 13:59:38 +01002270 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2272 hw_param_interval_c(params, rule->deps[1]), &t);
2273 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2274}
2275
Takashi Iwai877211f2005-11-17 13:59:38 +01002276static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2277 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Takashi Iwai877211f2005-11-17 13:59:38 +01002279 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2281 hw_param_interval_c(params, rule->deps[1]),
2282 (unsigned long) rule->private, &t);
2283 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2284}
2285
Takashi Iwai877211f2005-11-17 13:59:38 +01002286static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2287 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Takashi Iwai877211f2005-11-17 13:59:38 +01002289 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2291 (unsigned long) rule->private,
2292 hw_param_interval_c(params, rule->deps[1]), &t);
2293 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2294}
2295
Takashi Iwai877211f2005-11-17 13:59:38 +01002296static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2297 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Takashi Iwaiba71d222020-02-06 17:39:42 +01002299 snd_pcm_format_t k;
Takashi Sakamotob55f9fd2017-05-17 08:48:20 +09002300 const struct snd_interval *i =
2301 hw_param_interval_c(params, rule->deps[0]);
Takashi Iwai877211f2005-11-17 13:59:38 +01002302 struct snd_mask m;
2303 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 snd_mask_any(&m);
Takashi Iwaiba71d222020-02-06 17:39:42 +01002305 pcm_for_each_format(k) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 int bits;
Takashi Iwaiba71d222020-02-06 17:39:42 +01002307 if (!snd_mask_test_format(mask, k))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 continue;
2309 bits = snd_pcm_format_physical_width(k);
2310 if (bits <= 0)
2311 continue; /* ignore invalid formats */
2312 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
Takashi Iwaiba71d222020-02-06 17:39:42 +01002313 snd_mask_reset(&m, (__force unsigned)k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315 return snd_mask_refine(mask, &m);
2316}
2317
Takashi Iwai877211f2005-11-17 13:59:38 +01002318static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2319 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Takashi Iwai877211f2005-11-17 13:59:38 +01002321 struct snd_interval t;
Takashi Iwaiba71d222020-02-06 17:39:42 +01002322 snd_pcm_format_t k;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 t.min = UINT_MAX;
2325 t.max = 0;
2326 t.openmin = 0;
2327 t.openmax = 0;
Takashi Iwaiba71d222020-02-06 17:39:42 +01002328 pcm_for_each_format(k) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 int bits;
Takashi Iwaiba71d222020-02-06 17:39:42 +01002330 if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 continue;
2332 bits = snd_pcm_format_physical_width(k);
2333 if (bits <= 0)
2334 continue; /* ignore invalid formats */
2335 if (t.min > (unsigned)bits)
2336 t.min = bits;
2337 if (t.max < (unsigned)bits)
2338 t.max = bits;
2339 }
2340 t.integer = 1;
2341 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2342}
2343
2344#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2345#error "Change this table"
2346#endif
2347
Takashi Sakamoto8b674302017-05-17 08:48:17 +09002348static const unsigned int rates[] = {
2349 5512, 8000, 11025, 16000, 22050, 32000, 44100,
Vidyakumar Athota4cc45312019-08-22 10:56:50 +01002350 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
Takashi Sakamoto8b674302017-05-17 08:48:17 +09002351};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Clemens Ladisch7653d552007-08-13 17:38:54 +02002353const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2354 .count = ARRAY_SIZE(rates),
2355 .list = rates,
2356};
2357
Takashi Iwai877211f2005-11-17 13:59:38 +01002358static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2359 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Takashi Iwai877211f2005-11-17 13:59:38 +01002361 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002363 snd_pcm_known_rates.count,
2364 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Takashi Iwai877211f2005-11-17 13:59:38 +01002367static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2368 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Takashi Iwai877211f2005-11-17 13:59:38 +01002370 struct snd_interval t;
2371 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 t.min = 0;
2373 t.max = substream->buffer_bytes_max;
2374 t.openmin = 0;
2375 t.openmax = 0;
2376 t.integer = 1;
2377 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2378}
2379
Takashi Iwaid6621172020-01-16 17:28:25 +01002380static int snd_pcm_hw_constraints_init(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_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 int k, err;
2385
2386 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2387 snd_mask_any(constrs_mask(constrs, k));
2388 }
2389
2390 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2391 snd_interval_any(constrs_interval(constrs, k));
2392 }
2393
2394 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2395 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2396 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2397 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2398 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2399
2400 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2401 snd_pcm_hw_rule_format, NULL,
2402 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2403 if (err < 0)
2404 return err;
2405 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2406 snd_pcm_hw_rule_sample_bits, NULL,
2407 SNDRV_PCM_HW_PARAM_FORMAT,
2408 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2409 if (err < 0)
2410 return err;
2411 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2412 snd_pcm_hw_rule_div, NULL,
2413 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2414 if (err < 0)
2415 return err;
2416 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2417 snd_pcm_hw_rule_mul, NULL,
2418 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2419 if (err < 0)
2420 return err;
2421 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2422 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2423 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2424 if (err < 0)
2425 return err;
2426 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2427 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2428 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2429 if (err < 0)
2430 return err;
2431 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2432 snd_pcm_hw_rule_div, NULL,
2433 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2434 if (err < 0)
2435 return err;
2436 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2437 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2438 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2439 if (err < 0)
2440 return err;
2441 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2442 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2443 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2444 if (err < 0)
2445 return err;
2446 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2447 snd_pcm_hw_rule_div, NULL,
2448 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2449 if (err < 0)
2450 return err;
2451 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2452 snd_pcm_hw_rule_div, NULL,
2453 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2454 if (err < 0)
2455 return err;
2456 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2457 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2458 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2459 if (err < 0)
2460 return err;
2461 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2462 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2463 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2464 if (err < 0)
2465 return err;
2466 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2467 snd_pcm_hw_rule_mul, NULL,
2468 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2469 if (err < 0)
2470 return err;
2471 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2472 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2473 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2474 if (err < 0)
2475 return err;
2476 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2477 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2478 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2479 if (err < 0)
2480 return err;
2481 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2482 snd_pcm_hw_rule_muldivk, (void*) 8,
2483 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2484 if (err < 0)
2485 return err;
2486 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2487 snd_pcm_hw_rule_muldivk, (void*) 8,
2488 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2489 if (err < 0)
2490 return err;
2491 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2492 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2493 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2494 if (err < 0)
2495 return err;
2496 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2497 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2498 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2499 if (err < 0)
2500 return err;
2501 return 0;
2502}
2503
Takashi Iwaid6621172020-01-16 17:28:25 +01002504static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Takashi Iwai877211f2005-11-17 13:59:38 +01002506 struct snd_pcm_runtime *runtime = substream->runtime;
2507 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 int err;
2509 unsigned int mask = 0;
2510
2511 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
Takashi Iwaif9b0c052020-02-06 17:39:43 +01002512 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
Takashi Iwaif9b0c052020-02-06 17:39:43 +01002514 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
Takashi Iwai63825f32014-10-22 12:04:46 +02002515 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
Takashi Iwaif9b0c052020-02-06 17:39:43 +01002517 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
Takashi Iwaif9b0c052020-02-06 17:39:43 +01002519 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
Takashi Iwaif9b0c052020-02-06 17:39:43 +01002521 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
2523 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002524 if (err < 0)
2525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002528 if (err < 0)
2529 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Takashi Iwaif9b0c052020-02-06 17:39:43 +01002531 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT,
2532 PARAM_MASK_BIT(SNDRV_PCM_SUBFORMAT_STD));
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002533 if (err < 0)
2534 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2537 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002538 if (err < 0)
2539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2542 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002543 if (err < 0)
2544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
2546 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2547 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002548 if (err < 0)
2549 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2552 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002553 if (err < 0)
2554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2557 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002558 if (err < 0)
2559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2562 snd_pcm_hw_rule_buffer_bytes_max, substream,
2563 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2564 if (err < 0)
2565 return err;
2566
2567 /* FIXME: remove */
2568 if (runtime->dma_bytes) {
2569 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002570 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302571 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
2573
2574 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2575 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2576 snd_pcm_hw_rule_rate, hw,
2577 SNDRV_PCM_HW_PARAM_RATE, -1);
2578 if (err < 0)
2579 return err;
2580 }
2581
2582 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2584
2585 return 0;
2586}
2587
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002588static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Takashi Iwaib51abed2018-11-29 08:02:49 +01002590 if (snd_pcm_stream_linked(substream))
2591 snd_pcm_unlink(substream);
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002592}
2593
2594void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2595{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002596 substream->ref_count--;
2597 if (substream->ref_count > 0)
2598 return;
2599
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002600 snd_pcm_drop(substream);
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002601 if (substream->hw_opened) {
Takashi Iwai0fbb0272020-02-13 07:03:49 +01002602 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2603 do_hw_free(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 substream->ops->close(substream);
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002605 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
Rafael J. Wysocki5371a792020-02-12 00:34:15 +01002607 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
2608 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002609 if (substream->pcm_release) {
2610 substream->pcm_release(substream);
2611 substream->pcm_release = NULL;
2612 }
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002613 snd_pcm_detach_substream(substream);
2614}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002615EXPORT_SYMBOL(snd_pcm_release_substream);
2616
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002617int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2618 struct file *file,
2619 struct snd_pcm_substream **rsubstream)
2620{
2621 struct snd_pcm_substream *substream;
2622 int err;
2623
2624 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2625 if (err < 0)
2626 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002627 if (substream->ref_count > 1) {
2628 *rsubstream = substream;
2629 return 0;
2630 }
2631
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002632 err = snd_pcm_hw_constraints_init(substream);
2633 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002634 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002635 goto error;
2636 }
2637
2638 if ((err = substream->ops->open(substream)) < 0)
2639 goto error;
2640
2641 substream->hw_opened = 1;
2642
2643 err = snd_pcm_hw_constraints_complete(substream);
2644 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002645 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002646 goto error;
2647 }
2648
2649 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 return 0;
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002651
2652 error:
2653 snd_pcm_release_substream(substream);
2654 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002656EXPORT_SYMBOL(snd_pcm_open_substream);
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002659 struct snd_pcm *pcm,
Feng Tangffd3d5c62011-10-10 10:31:48 +08002660 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Takashi Iwai877211f2005-11-17 13:59:38 +01002662 struct snd_pcm_file *pcm_file;
2663 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002664 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002666 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2667 if (err < 0)
2668 return err;
2669
Takashi Iwai548a6482006-07-31 16:51:51 +02002670 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2671 if (pcm_file == NULL) {
2672 snd_pcm_release_substream(substream);
2673 return -ENOMEM;
2674 }
2675 pcm_file->substream = substream;
Takashi Iwaide897502019-01-23 12:47:34 +01002676 if (substream->ref_count == 1)
Takashi Iwai0df63e42006-04-28 15:13:41 +02002677 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 file->private_data = pcm_file;
Feng Tangffd3d5c62011-10-10 10:31:48 +08002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return 0;
2681}
2682
Clemens Ladischf87135f2005-11-20 14:06:59 +01002683static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Takashi Iwai877211f2005-11-17 13:59:38 +01002685 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002686 int err = nonseekable_open(inode, file);
2687 if (err < 0)
2688 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002689 pcm = snd_lookup_minor_data(iminor(inode),
2690 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002691 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002692 if (pcm)
2693 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002694 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002695}
2696
2697static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2698{
2699 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002700 int err = nonseekable_open(inode, file);
2701 if (err < 0)
2702 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002703 pcm = snd_lookup_minor_data(iminor(inode),
2704 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002705 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002706 if (pcm)
2707 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002708 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002709}
2710
2711static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2712{
2713 int err;
Ingo Molnarac6424b2017-06-20 12:06:13 +02002714 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if (pcm == NULL) {
2717 err = -ENODEV;
2718 goto __error1;
2719 }
2720 err = snd_card_file_add(pcm->card, file);
2721 if (err < 0)
2722 goto __error1;
2723 if (!try_module_get(pcm->card->module)) {
2724 err = -EFAULT;
2725 goto __error2;
2726 }
2727 init_waitqueue_entry(&wait, current);
2728 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002729 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 while (1) {
Feng Tangffd3d5c62011-10-10 10:31:48 +08002731 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 if (err >= 0)
2733 break;
2734 if (err == -EAGAIN) {
2735 if (file->f_flags & O_NONBLOCK) {
2736 err = -EBUSY;
2737 break;
2738 }
2739 } else
2740 break;
2741 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002742 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002744 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002745 if (pcm->card->shutdown) {
2746 err = -ENODEV;
2747 break;
2748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (signal_pending(current)) {
2750 err = -ERESTARTSYS;
2751 break;
2752 }
2753 }
2754 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002755 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (err < 0)
2757 goto __error;
2758 return err;
2759
2760 __error:
2761 module_put(pcm->card->module);
2762 __error2:
2763 snd_card_file_remove(pcm->card, file);
2764 __error1:
2765 return err;
2766}
2767
2768static int snd_pcm_release(struct inode *inode, struct file *file)
2769{
Takashi Iwai877211f2005-11-17 13:59:38 +01002770 struct snd_pcm *pcm;
2771 struct snd_pcm_substream *substream;
2772 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 pcm_file = file->private_data;
2775 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002776 if (snd_BUG_ON(!substream))
2777 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002779 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002780 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002781 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002782 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 wake_up(&pcm->open_wait);
2784 module_put(pcm->card->module);
2785 snd_card_file_remove(pcm->card, file);
2786 return 0;
2787}
2788
Takashi Iwaif839cc12017-05-19 20:04:23 +02002789/* check and update PCM state; return 0 or a negative error
2790 * call this inside PCM lock
2791 */
2792static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2793{
2794 switch (substream->runtime->status->state) {
2795 case SNDRV_PCM_STATE_DRAINING:
2796 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2797 return -EBADFD;
2798 /* Fall through */
2799 case SNDRV_PCM_STATE_RUNNING:
2800 return snd_pcm_update_hw_ptr(substream);
2801 case SNDRV_PCM_STATE_PREPARED:
2802 case SNDRV_PCM_STATE_PAUSED:
2803 return 0;
2804 case SNDRV_PCM_STATE_SUSPENDED:
2805 return -ESTRPIPE;
2806 case SNDRV_PCM_STATE_XRUN:
2807 return -EPIPE;
2808 default:
2809 return -EBADFD;
2810 }
2811}
2812
Takashi Iwai9027c462017-05-19 20:22:33 +02002813/* increase the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002814static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2815 snd_pcm_uframes_t frames,
2816 snd_pcm_sframes_t avail)
2817{
2818 struct snd_pcm_runtime *runtime = substream->runtime;
2819 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002820 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002821
2822 if (avail <= 0)
2823 return 0;
2824 if (frames > (snd_pcm_uframes_t)avail)
2825 frames = avail;
2826 appl_ptr = runtime->control->appl_ptr + frames;
2827 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2828 appl_ptr -= runtime->boundary;
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002829 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
Takashi Iwai9027c462017-05-19 20:22:33 +02002830 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002831}
2832
Takashi Iwaifb51f1c2018-01-03 15:16:30 +01002833/* decrease the appl_ptr; returns the processed frames or zero for error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002834static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2835 snd_pcm_uframes_t frames,
2836 snd_pcm_sframes_t avail)
2837{
2838 struct snd_pcm_runtime *runtime = substream->runtime;
2839 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002840 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002841
2842 if (avail <= 0)
2843 return 0;
2844 if (frames > (snd_pcm_uframes_t)avail)
2845 frames = avail;
2846 appl_ptr = runtime->control->appl_ptr - frames;
2847 if (appl_ptr < 0)
2848 appl_ptr += runtime->boundary;
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002849 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
Takashi Iwaifb51f1c2018-01-03 15:16:30 +01002850 /* NOTE: we return zero for errors because PulseAudio gets depressed
2851 * upon receiving an error from rewind ioctl and stops processing
2852 * any longer. Returning zero means that no rewind is done, so
2853 * it's not absolutely wrong to answer like that.
2854 */
2855 return ret < 0 ? 0 : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002856}
2857
Takashi Iwai763e5062018-04-11 17:56:52 +02002858static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2859 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 if (frames == 0)
2864 return 0;
2865
2866 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002867 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002868 if (!ret)
2869 ret = rewind_appl_ptr(substream, frames,
Takashi Iwai763e5062018-04-11 17:56:52 +02002870 snd_pcm_hw_avail(substream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 snd_pcm_stream_unlock_irq(substream);
2872 return ret;
2873}
2874
Takashi Iwai763e5062018-04-11 17:56:52 +02002875static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2876 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 if (frames == 0)
2881 return 0;
2882
2883 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002884 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002885 if (!ret)
2886 ret = forward_appl_ptr(substream, frames,
Takashi Iwai763e5062018-04-11 17:56:52 +02002887 snd_pcm_avail(substream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 snd_pcm_stream_unlock_irq(substream);
2889 return ret;
2890}
2891
Takashi Iwai877211f2005-11-17 13:59:38 +01002892static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 int err;
2895
2896 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002897 err = do_pcm_hwsync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 snd_pcm_stream_unlock_irq(substream);
2899 return err;
2900}
2901
Jeffery Miller912e4c32018-04-20 23:20:46 -05002902static int snd_pcm_delay(struct snd_pcm_substream *substream,
2903 snd_pcm_sframes_t *delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 int err;
2906 snd_pcm_sframes_t n = 0;
2907
2908 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002909 err = do_pcm_hwsync(substream);
Takashi Iwaic99c5a32018-04-11 18:07:27 +02002910 if (!err)
2911 n = snd_pcm_calc_delay(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 snd_pcm_stream_unlock_irq(substream);
Jeffery Miller912e4c32018-04-20 23:20:46 -05002913 if (!err)
2914 *delay = n;
2915 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
Takashi Iwai877211f2005-11-17 13:59:38 +01002918static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2919 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
Takashi Iwai877211f2005-11-17 13:59:38 +01002921 struct snd_pcm_runtime *runtime = substream->runtime;
2922 struct snd_pcm_sync_ptr sync_ptr;
2923 volatile struct snd_pcm_mmap_status *status;
2924 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 int err;
2926
2927 memset(&sync_ptr, 0, sizeof(sync_ptr));
2928 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2929 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002930 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 -07002931 return -EFAULT;
2932 status = runtime->status;
2933 control = runtime->control;
2934 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2935 err = snd_pcm_hwsync(substream);
2936 if (err < 0)
2937 return err;
2938 }
2939 snd_pcm_stream_lock_irq(substream);
Takashi Iwai9027c462017-05-19 20:22:33 +02002940 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002941 err = pcm_lib_apply_appl_ptr(substream,
2942 sync_ptr.c.control.appl_ptr);
Takashi Iwai9027c462017-05-19 20:22:33 +02002943 if (err < 0) {
2944 snd_pcm_stream_unlock_irq(substream);
2945 return err;
2946 }
2947 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 sync_ptr.c.control.appl_ptr = control->appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2951 control->avail_min = sync_ptr.c.control.avail_min;
2952 else
2953 sync_ptr.c.control.avail_min = control->avail_min;
2954 sync_ptr.s.status.state = status->state;
2955 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2956 sync_ptr.s.status.tstamp = status->tstamp;
2957 sync_ptr.s.status.suspended_state = status->suspended_state;
David Henningssonf853dca2018-04-21 14:57:40 +02002958 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 snd_pcm_stream_unlock_irq(substream);
2960 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2961 return -EFAULT;
2962 return 0;
2963}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002964
Arnd Bergmann09d94172018-04-24 20:06:14 +08002965struct snd_pcm_mmap_status32 {
Takashi Iwaicb639a42020-01-31 16:22:14 +01002966 snd_pcm_state_t state;
Arnd Bergmann09d94172018-04-24 20:06:14 +08002967 s32 pad1;
2968 u32 hw_ptr;
Arnd Bergmann80fe7432018-04-24 20:06:15 +08002969 s32 tstamp_sec;
2970 s32 tstamp_nsec;
Takashi Iwaicb639a42020-01-31 16:22:14 +01002971 snd_pcm_state_t suspended_state;
Arnd Bergmann80fe7432018-04-24 20:06:15 +08002972 s32 audio_tstamp_sec;
2973 s32 audio_tstamp_nsec;
Arnd Bergmann09d94172018-04-24 20:06:14 +08002974} __attribute__((packed));
2975
2976struct snd_pcm_mmap_control32 {
2977 u32 appl_ptr;
2978 u32 avail_min;
2979};
2980
2981struct snd_pcm_sync_ptr32 {
2982 u32 flags;
2983 union {
2984 struct snd_pcm_mmap_status32 status;
2985 unsigned char reserved[64];
2986 } s;
2987 union {
2988 struct snd_pcm_mmap_control32 control;
2989 unsigned char reserved[64];
2990 } c;
2991} __attribute__((packed));
2992
2993/* recalcuate the boundary within 32bit */
2994static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
2995{
2996 snd_pcm_uframes_t boundary;
2997
2998 if (! runtime->buffer_size)
2999 return 0;
3000 boundary = runtime->buffer_size;
3001 while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
3002 boundary *= 2;
3003 return boundary;
3004}
3005
3006static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
3007 struct snd_pcm_sync_ptr32 __user *src)
3008{
3009 struct snd_pcm_runtime *runtime = substream->runtime;
3010 volatile struct snd_pcm_mmap_status *status;
3011 volatile struct snd_pcm_mmap_control *control;
3012 u32 sflags;
3013 struct snd_pcm_mmap_control scontrol;
3014 struct snd_pcm_mmap_status sstatus;
3015 snd_pcm_uframes_t boundary;
3016 int err;
3017
3018 if (snd_BUG_ON(!runtime))
3019 return -EINVAL;
3020
3021 if (get_user(sflags, &src->flags) ||
3022 get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3023 get_user(scontrol.avail_min, &src->c.control.avail_min))
3024 return -EFAULT;
3025 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3026 err = snd_pcm_hwsync(substream);
3027 if (err < 0)
3028 return err;
3029 }
3030 status = runtime->status;
3031 control = runtime->control;
3032 boundary = recalculate_boundary(runtime);
3033 if (! boundary)
3034 boundary = 0x7fffffff;
3035 snd_pcm_stream_lock_irq(substream);
3036 /* FIXME: we should consider the boundary for the sync from app */
3037 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
3038 control->appl_ptr = scontrol.appl_ptr;
3039 else
3040 scontrol.appl_ptr = control->appl_ptr % boundary;
3041 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3042 control->avail_min = scontrol.avail_min;
3043 else
3044 scontrol.avail_min = control->avail_min;
3045 sstatus.state = status->state;
3046 sstatus.hw_ptr = status->hw_ptr % boundary;
3047 sstatus.tstamp = status->tstamp;
3048 sstatus.suspended_state = status->suspended_state;
3049 sstatus.audio_tstamp = status->audio_tstamp;
3050 snd_pcm_stream_unlock_irq(substream);
3051 if (put_user(sstatus.state, &src->s.status.state) ||
3052 put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
Arnd Bergmann80fe7432018-04-24 20:06:15 +08003053 put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
3054 put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) ||
Arnd Bergmann09d94172018-04-24 20:06:14 +08003055 put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
Arnd Bergmann80fe7432018-04-24 20:06:15 +08003056 put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) ||
3057 put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) ||
Arnd Bergmann09d94172018-04-24 20:06:14 +08003058 put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3059 put_user(scontrol.avail_min, &src->c.control.avail_min))
3060 return -EFAULT;
3061
3062 return 0;
3063}
Arnd Bergmann80fe7432018-04-24 20:06:15 +08003064#define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32)
Arnd Bergmann09d94172018-04-24 20:06:14 +08003065
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01003066static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
3067{
3068 struct snd_pcm_runtime *runtime = substream->runtime;
3069 int arg;
3070
3071 if (get_user(arg, _arg))
3072 return -EFAULT;
3073 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3074 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02003075 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01003076 return 0;
3077}
Takashi Iwai67616fe2017-08-30 15:39:32 +02003078
3079static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3080 struct snd_xferi __user *_xferi)
3081{
3082 struct snd_xferi xferi;
3083 struct snd_pcm_runtime *runtime = substream->runtime;
3084 snd_pcm_sframes_t result;
3085
3086 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3087 return -EBADFD;
3088 if (put_user(0, &_xferi->result))
3089 return -EFAULT;
3090 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3091 return -EFAULT;
3092 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3093 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3094 else
3095 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3096 __put_user(result, &_xferi->result);
3097 return result < 0 ? result : 0;
3098}
3099
3100static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3101 struct snd_xfern __user *_xfern)
3102{
3103 struct snd_xfern xfern;
3104 struct snd_pcm_runtime *runtime = substream->runtime;
3105 void *bufs;
3106 snd_pcm_sframes_t result;
3107
3108 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3109 return -EBADFD;
3110 if (runtime->channels > 128)
3111 return -EINVAL;
3112 if (put_user(0, &_xfern->result))
3113 return -EFAULT;
3114 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3115 return -EFAULT;
3116
3117 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
3118 if (IS_ERR(bufs))
3119 return PTR_ERR(bufs);
3120 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3121 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3122 else
3123 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3124 kfree(bufs);
3125 __put_user(result, &_xfern->result);
3126 return result < 0 ? result : 0;
3127}
3128
3129static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3130 snd_pcm_uframes_t __user *_frames)
3131{
3132 snd_pcm_uframes_t frames;
3133 snd_pcm_sframes_t result;
3134
3135 if (get_user(frames, _frames))
3136 return -EFAULT;
3137 if (put_user(0, _frames))
3138 return -EFAULT;
Takashi Iwai763e5062018-04-11 17:56:52 +02003139 result = snd_pcm_rewind(substream, frames);
Takashi Iwai67616fe2017-08-30 15:39:32 +02003140 __put_user(result, _frames);
3141 return result < 0 ? result : 0;
3142}
3143
3144static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3145 snd_pcm_uframes_t __user *_frames)
3146{
3147 snd_pcm_uframes_t frames;
3148 snd_pcm_sframes_t result;
3149
3150 if (get_user(frames, _frames))
3151 return -EFAULT;
3152 if (put_user(0, _frames))
3153 return -EFAULT;
Takashi Iwai763e5062018-04-11 17:56:52 +02003154 result = snd_pcm_forward(substream, frames);
Takashi Iwai67616fe2017-08-30 15:39:32 +02003155 __put_user(result, _frames);
3156 return result < 0 ? result : 0;
3157}
3158
3159static int snd_pcm_common_ioctl(struct file *file,
Takashi Iwai0df63e42006-04-28 15:13:41 +02003160 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 unsigned int cmd, void __user *arg)
3162{
Takashi Iwai4b671f52017-06-19 23:11:54 +02003163 struct snd_pcm_file *pcm_file = file->private_data;
Takashi Iwai7d8e8292017-08-30 16:13:25 +02003164 int res;
3165
Takashi Iwai67616fe2017-08-30 15:39:32 +02003166 if (PCM_RUNTIME_CHECK(substream))
3167 return -ENXIO;
3168
Takashi Iwai7d8e8292017-08-30 16:13:25 +02003169 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
3170 if (res < 0)
3171 return res;
Takashi Iwai4b671f52017-06-19 23:11:54 +02003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 switch (cmd) {
3174 case SNDRV_PCM_IOCTL_PVERSION:
3175 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3176 case SNDRV_PCM_IOCTL_INFO:
3177 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01003178 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
3179 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01003180 case SNDRV_PCM_IOCTL_TTSTAMP:
3181 return snd_pcm_tstamp(substream, arg);
Takashi Iwai4b671f52017-06-19 23:11:54 +02003182 case SNDRV_PCM_IOCTL_USER_PVERSION:
3183 if (get_user(pcm_file->user_pversion,
3184 (unsigned int __user *)arg))
3185 return -EFAULT;
3186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 case SNDRV_PCM_IOCTL_HW_REFINE:
3188 return snd_pcm_hw_refine_user(substream, arg);
3189 case SNDRV_PCM_IOCTL_HW_PARAMS:
3190 return snd_pcm_hw_params_user(substream, arg);
3191 case SNDRV_PCM_IOCTL_HW_FREE:
3192 return snd_pcm_hw_free(substream);
3193 case SNDRV_PCM_IOCTL_SW_PARAMS:
3194 return snd_pcm_sw_params_user(substream, arg);
Baolin Wang3ddee7f2018-04-24 20:06:11 +08003195 case SNDRV_PCM_IOCTL_STATUS32:
3196 return snd_pcm_status_user32(substream, arg, false);
3197 case SNDRV_PCM_IOCTL_STATUS_EXT32:
3198 return snd_pcm_status_user32(substream, arg, true);
3199 case SNDRV_PCM_IOCTL_STATUS64:
3200 return snd_pcm_status_user64(substream, arg, false);
3201 case SNDRV_PCM_IOCTL_STATUS_EXT64:
3202 return snd_pcm_status_user64(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3204 return snd_pcm_channel_info_user(substream, arg);
3205 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003206 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 case SNDRV_PCM_IOCTL_RESET:
3208 return snd_pcm_reset(substream);
3209 case SNDRV_PCM_IOCTL_START:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003210 return snd_pcm_start_lock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 case SNDRV_PCM_IOCTL_LINK:
3212 return snd_pcm_link(substream, (int)(unsigned long) arg);
3213 case SNDRV_PCM_IOCTL_UNLINK:
3214 return snd_pcm_unlink(substream);
3215 case SNDRV_PCM_IOCTL_RESUME:
3216 return snd_pcm_resume(substream);
3217 case SNDRV_PCM_IOCTL_XRUN:
3218 return snd_pcm_xrun(substream);
3219 case SNDRV_PCM_IOCTL_HWSYNC:
3220 return snd_pcm_hwsync(substream);
3221 case SNDRV_PCM_IOCTL_DELAY:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003222 {
Jeffery Miller912e4c32018-04-20 23:20:46 -05003223 snd_pcm_sframes_t delay;
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003224 snd_pcm_sframes_t __user *res = arg;
Jeffery Miller912e4c32018-04-20 23:20:46 -05003225 int err;
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003226
Jeffery Miller912e4c32018-04-20 23:20:46 -05003227 err = snd_pcm_delay(substream, &delay);
3228 if (err)
3229 return err;
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003230 if (put_user(delay, res))
3231 return -EFAULT;
3232 return 0;
3233 }
Arnd Bergmann80fe7432018-04-24 20:06:15 +08003234 case __SNDRV_PCM_IOCTL_SYNC_PTR32:
3235 return snd_pcm_ioctl_sync_ptr_compat(substream, arg);
3236 case __SNDRV_PCM_IOCTL_SYNC_PTR64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01003238#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3240 return snd_pcm_hw_refine_old_user(substream, arg);
3241 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3242 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01003243#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02003245 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 case SNDRV_PCM_IOCTL_DROP:
3247 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01003248 case SNDRV_PCM_IOCTL_PAUSE:
Takashi Iwaicb639a42020-01-31 16:22:14 +01003249 return snd_pcm_pause_lock_irq(substream, (unsigned long)arg);
Takashi Iwai67616fe2017-08-30 15:39:32 +02003250 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3251 case SNDRV_PCM_IOCTL_READI_FRAMES:
3252 return snd_pcm_xferi_frames_ioctl(substream, arg);
3253 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3254 case SNDRV_PCM_IOCTL_READN_FRAMES:
3255 return snd_pcm_xfern_frames_ioctl(substream, arg);
3256 case SNDRV_PCM_IOCTL_REWIND:
3257 return snd_pcm_rewind_ioctl(substream, arg);
3258 case SNDRV_PCM_IOCTL_FORWARD:
3259 return snd_pcm_forward_ioctl(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01003261 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 return -ENOTTY;
3263}
3264
Takashi Iwai67616fe2017-08-30 15:39:32 +02003265static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3266 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Takashi Iwai877211f2005-11-17 13:59:38 +01003268 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 pcm_file = file->private_data;
3271
3272 if (((cmd >> 8) & 0xff) != 'A')
3273 return -ENOTTY;
3274
Takashi Iwai67616fe2017-08-30 15:39:32 +02003275 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3276 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003279/**
3280 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3281 * @substream: PCM substream
3282 * @cmd: IOCTL cmd
3283 * @arg: IOCTL argument
3284 *
3285 * The function is provided primarily for OSS layer and USB gadget drivers,
3286 * and it allows only the limited set of ioctls (hw_params, sw_params,
3287 * prepare, start, drain, drop, forward).
3288 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003289int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 unsigned int cmd, void *arg)
3291{
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003292 snd_pcm_uframes_t *frames = arg;
3293 snd_pcm_sframes_t result;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003294
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003295 switch (cmd) {
3296 case SNDRV_PCM_IOCTL_FORWARD:
3297 {
3298 /* provided only for OSS; capture-only and no value returned */
3299 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3300 return -EINVAL;
Takashi Iwai763e5062018-04-11 17:56:52 +02003301 result = snd_pcm_forward(substream, *frames);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003302 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003304 case SNDRV_PCM_IOCTL_HW_PARAMS:
3305 return snd_pcm_hw_params(substream, arg);
3306 case SNDRV_PCM_IOCTL_SW_PARAMS:
3307 return snd_pcm_sw_params(substream, arg);
3308 case SNDRV_PCM_IOCTL_PREPARE:
3309 return snd_pcm_prepare(substream, NULL);
3310 case SNDRV_PCM_IOCTL_START:
3311 return snd_pcm_start_lock_irq(substream);
3312 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai7d8e8292017-08-30 16:13:25 +02003313 return snd_pcm_drain(substream, NULL);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003314 case SNDRV_PCM_IOCTL_DROP:
3315 return snd_pcm_drop(substream);
3316 case SNDRV_PCM_IOCTL_DELAY:
Jeffery Miller912e4c32018-04-20 23:20:46 -05003317 return snd_pcm_delay(substream, frames);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003318 default:
3319 return -EINVAL;
3320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003322EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3323
Takashi Iwai877211f2005-11-17 13:59:38 +01003324static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3325 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
Takashi Iwai877211f2005-11-17 13:59:38 +01003327 struct snd_pcm_file *pcm_file;
3328 struct snd_pcm_substream *substream;
3329 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 snd_pcm_sframes_t result;
3331
3332 pcm_file = file->private_data;
3333 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003334 if (PCM_RUNTIME_CHECK(substream))
3335 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 runtime = substream->runtime;
3337 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3338 return -EBADFD;
3339 if (!frame_aligned(runtime, count))
3340 return -EINVAL;
3341 count = bytes_to_frames(runtime, count);
3342 result = snd_pcm_lib_read(substream, buf, count);
3343 if (result > 0)
3344 result = frames_to_bytes(runtime, result);
3345 return result;
3346}
3347
Takashi Iwai877211f2005-11-17 13:59:38 +01003348static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3349 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
Takashi Iwai877211f2005-11-17 13:59:38 +01003351 struct snd_pcm_file *pcm_file;
3352 struct snd_pcm_substream *substream;
3353 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 snd_pcm_sframes_t result;
3355
3356 pcm_file = file->private_data;
3357 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003358 if (PCM_RUNTIME_CHECK(substream))
3359 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003361 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3362 return -EBADFD;
3363 if (!frame_aligned(runtime, count))
3364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 count = bytes_to_frames(runtime, count);
3366 result = snd_pcm_lib_write(substream, buf, count);
3367 if (result > 0)
3368 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return result;
3370}
3371
Al Viro1c65d982015-04-04 00:19:32 -04003372static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
Takashi Iwai877211f2005-11-17 13:59:38 +01003374 struct snd_pcm_file *pcm_file;
3375 struct snd_pcm_substream *substream;
3376 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 snd_pcm_sframes_t result;
3378 unsigned long i;
3379 void __user **bufs;
3380 snd_pcm_uframes_t frames;
3381
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003382 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003384 if (PCM_RUNTIME_CHECK(substream))
3385 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 runtime = substream->runtime;
3387 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3388 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003389 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003391 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003393 if (!frame_aligned(runtime, to->iov->iov_len))
3394 return -EINVAL;
3395 frames = bytes_to_samples(runtime, to->iov->iov_len);
Kees Cook6da2ec52018-06-12 13:55:00 -07003396 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 if (bufs == NULL)
3398 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003399 for (i = 0; i < to->nr_segs; ++i)
3400 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 result = snd_pcm_lib_readv(substream, bufs, frames);
3402 if (result > 0)
3403 result = frames_to_bytes(runtime, result);
3404 kfree(bufs);
3405 return result;
3406}
3407
Al Viro1c65d982015-04-04 00:19:32 -04003408static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Takashi Iwai877211f2005-11-17 13:59:38 +01003410 struct snd_pcm_file *pcm_file;
3411 struct snd_pcm_substream *substream;
3412 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 snd_pcm_sframes_t result;
3414 unsigned long i;
3415 void __user **bufs;
3416 snd_pcm_uframes_t frames;
3417
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003418 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003420 if (PCM_RUNTIME_CHECK(substream))
3421 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003423 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3424 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003425 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003426 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003427 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3428 !frame_aligned(runtime, from->iov->iov_len))
3429 return -EINVAL;
3430 frames = bytes_to_samples(runtime, from->iov->iov_len);
Kees Cook6da2ec52018-06-12 13:55:00 -07003431 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 if (bufs == NULL)
3433 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003434 for (i = 0; i < from->nr_segs; ++i)
3435 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 result = snd_pcm_lib_writev(substream, bufs, frames);
3437 if (result > 0)
3438 result = frames_to_bytes(runtime, result);
3439 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return result;
3441}
3442
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003443static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
Takashi Iwai877211f2005-11-17 13:59:38 +01003445 struct snd_pcm_file *pcm_file;
3446 struct snd_pcm_substream *substream;
3447 struct snd_pcm_runtime *runtime;
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003448 __poll_t mask, ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 snd_pcm_uframes_t avail;
3450
3451 pcm_file = file->private_data;
3452
3453 substream = pcm_file->substream;
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003454 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3455 ok = EPOLLOUT | EPOLLWRNORM;
3456 else
3457 ok = EPOLLIN | EPOLLRDNORM;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003458 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003459 return ok | EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003461 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 poll_wait(file, &runtime->sleep, wait);
3463
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003464 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 snd_pcm_stream_lock_irq(substream);
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003466 avail = snd_pcm_avail(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 switch (runtime->status->state) {
3468 case SNDRV_PCM_STATE_RUNNING:
3469 case SNDRV_PCM_STATE_PREPARED:
3470 case SNDRV_PCM_STATE_PAUSED:
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003471 if (avail >= runtime->control->avail_min)
3472 mask = ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 break;
3474 case SNDRV_PCM_STATE_DRAINING:
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003475 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3476 mask = ok;
3477 if (!avail)
3478 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 }
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 default:
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003482 mask = ok | EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 break;
3484 }
3485 snd_pcm_stream_unlock_irq(substream);
3486 return mask;
3487}
3488
3489/*
3490 * mmap support
3491 */
3492
3493/*
3494 * Only on coherent architectures, we can mmap the status and the control records
3495 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3496 */
3497#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3498/*
3499 * mmap status record
3500 */
Souptick Joarder41412fe2018-04-25 09:50:29 +05303501static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Dave Jiang11bac802017-02-24 14:56:41 -08003503 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003504 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
3506 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003507 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003509 vmf->page = virt_to_page(runtime->status);
3510 get_page(vmf->page);
3511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003514static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003516 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517};
3518
Takashi Iwai877211f2005-11-17 13:59:38 +01003519static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 struct vm_area_struct *area)
3521{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 long size;
3523 if (!(area->vm_flags & VM_READ))
3524 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003526 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return -EINVAL;
3528 area->vm_ops = &snd_pcm_vm_ops_status;
3529 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003530 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return 0;
3532}
3533
3534/*
3535 * mmap control record
3536 */
Souptick Joarder41412fe2018-04-25 09:50:29 +05303537static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Dave Jiang11bac802017-02-24 14:56:41 -08003539 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003540 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003543 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003545 vmf->page = virt_to_page(runtime->control);
3546 get_page(vmf->page);
3547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003550static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003552 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553};
3554
Takashi Iwai877211f2005-11-17 13:59:38 +01003555static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 struct vm_area_struct *area)
3557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 long size;
3559 if (!(area->vm_flags & VM_READ))
3560 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003562 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 return -EINVAL;
3564 area->vm_ops = &snd_pcm_vm_ops_control;
3565 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003566 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 return 0;
3568}
Takashi Iwai42f94592017-06-19 22:39:18 +02003569
3570static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3571{
Takashi Iwaib602aa82017-06-27 11:54:37 +02003572 /* See pcm_control_mmap_allowed() below.
3573 * Since older alsa-lib requires both status and control mmaps to be
3574 * coupled, we have to disable the status mmap for old alsa-lib, too.
3575 */
3576 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3577 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3578 return false;
3579 return true;
3580}
3581
3582static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3583{
3584 if (pcm_file->no_compat_mmap)
3585 return false;
3586 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
Takashi Iwai42f94592017-06-19 22:39:18 +02003587 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3588 * thus it effectively assures the manual update of appl_ptr.
Takashi Iwai42f94592017-06-19 22:39:18 +02003589 */
3590 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3591 return false;
3592 return true;
3593}
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595#else /* ! coherent mmap */
3596/*
3597 * don't support mmap for status and control records.
3598 */
Takashi Iwai42f94592017-06-19 22:39:18 +02003599#define pcm_status_mmap_allowed(pcm_file) false
Takashi Iwaib602aa82017-06-27 11:54:37 +02003600#define pcm_control_mmap_allowed(pcm_file) false
Takashi Iwai42f94592017-06-19 22:39:18 +02003601
Takashi Iwai877211f2005-11-17 13:59:38 +01003602static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 struct vm_area_struct *area)
3604{
3605 return -ENXIO;
3606}
Takashi Iwai877211f2005-11-17 13:59:38 +01003607static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 struct vm_area_struct *area)
3609{
3610 return -ENXIO;
3611}
3612#endif /* coherent mmap */
3613
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003614static inline struct page *
3615snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3616{
3617 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai7e8edae2019-11-05 09:01:37 +01003618
3619 switch (substream->dma_buffer.dev.type) {
3620#ifdef CONFIG_SND_DMA_SGBUF
3621 case SNDRV_DMA_TYPE_DEV_SG:
3622 case SNDRV_DMA_TYPE_DEV_UC_SG:
3623 return snd_pcm_sgbuf_ops_page(substream, ofs);
3624#endif /* CONFIG_SND_DMA_SGBUF */
3625 case SNDRV_DMA_TYPE_VMALLOC:
3626 return vmalloc_to_page(vaddr);
3627 default:
3628 return virt_to_page(vaddr);
3629 }
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003630}
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003633 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 */
Souptick Joarder41412fe2018-04-25 09:50:29 +05303635static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
Dave Jiang11bac802017-02-24 14:56:41 -08003637 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003638 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 unsigned long offset;
3640 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 size_t dma_bytes;
3642
3643 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003644 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003646 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3648 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003649 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003650 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003652 else
3653 page = snd_pcm_default_page_ops(substream, offset);
3654 if (!page)
3655 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003656 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003657 vmf->page = page;
3658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
Takashi Iwai657b1982009-11-26 12:40:21 +01003661static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3662 .open = snd_pcm_mmap_data_open,
3663 .close = snd_pcm_mmap_data_close,
3664};
3665
3666static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 .open = snd_pcm_mmap_data_open,
3668 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003669 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670};
3671
3672/*
3673 * mmap the DMA buffer on RAM
3674 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003675
3676/**
3677 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3678 * @substream: PCM substream
3679 * @area: VMA
3680 *
3681 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3682 * this function is invoked implicitly.
3683 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003684int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3685 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003687 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003688#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003689 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3690 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3691 return remap_pfn_range(area, area->vm_start,
3692 substream->dma_buffer.addr >> PAGE_SHIFT,
3693 area->vm_end - area->vm_start, area->vm_page_prot);
3694 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003695#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003696#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Geert Uytterhoevenabe594c2017-07-09 21:31:02 +02003697 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
Takashi Iwai2406ff92019-11-08 17:56:26 +01003698 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3699 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
Takashi Iwai657b1982009-11-26 12:40:21 +01003700 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3701 area,
3702 substream->runtime->dma_area,
3703 substream->runtime->dma_addr,
Stefan Roese9066ae72018-03-26 16:10:21 +02003704 substream->runtime->dma_bytes);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003705#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003706 /* mmap with fault handler */
3707 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 return 0;
3709}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003710EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712/*
3713 * mmap the DMA buffer on I/O memory area
3714 */
3715#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003716/**
3717 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3718 * @substream: PCM substream
3719 * @area: VMA
3720 *
3721 * When your hardware uses the iomapped pages as the hardware buffer and
3722 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3723 * is supposed to work only on limited architectures.
3724 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003725int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3726 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
Luis de Bethencourt3c7f6912018-01-16 13:26:26 +00003728 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003731 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003733EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734#endif /* SNDRV_PCM_INFO_MMAP */
3735
3736/*
3737 * mmap DMA buffer
3738 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003739int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 struct vm_area_struct *area)
3741{
Takashi Iwai877211f2005-11-17 13:59:38 +01003742 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 long size;
3744 unsigned long offset;
3745 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003746 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
3748 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3749 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3750 return -EINVAL;
3751 } else {
3752 if (!(area->vm_flags & VM_READ))
3753 return -EINVAL;
3754 }
3755 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3757 return -EBADFD;
3758 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3759 return -ENXIO;
3760 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3761 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3762 return -EINVAL;
3763 size = area->vm_end - area->vm_start;
3764 offset = area->vm_pgoff << PAGE_SHIFT;
3765 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3766 if ((size_t)size > dma_bytes)
3767 return -EINVAL;
3768 if (offset > dma_bytes - size)
3769 return -EINVAL;
3770
Takashi Iwai657b1982009-11-26 12:40:21 +01003771 area->vm_ops = &snd_pcm_vm_ops_data;
3772 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003774 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003776 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003777 if (!err)
3778 atomic_inc(&substream->mmap_count);
3779 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003781EXPORT_SYMBOL(snd_pcm_mmap_data);
3782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3784{
Takashi Iwai877211f2005-11-17 13:59:38 +01003785 struct snd_pcm_file * pcm_file;
3786 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 unsigned long offset;
3788
3789 pcm_file = file->private_data;
3790 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003791 if (PCM_RUNTIME_CHECK(substream))
3792 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
3794 offset = area->vm_pgoff << PAGE_SHIFT;
3795 switch (offset) {
Arnd Bergmann80fe7432018-04-24 20:06:15 +08003796 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
3797 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3798 return -ENXIO;
3799 /* fallthrough */
3800 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
Takashi Iwai42f94592017-06-19 22:39:18 +02003801 if (!pcm_status_mmap_allowed(pcm_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return -ENXIO;
3803 return snd_pcm_mmap_status(substream, file, area);
Arnd Bergmann80fe7432018-04-24 20:06:15 +08003804 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
3805 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3806 return -ENXIO;
3807 /* fallthrough */
3808 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
Takashi Iwaib602aa82017-06-27 11:54:37 +02003809 if (!pcm_control_mmap_allowed(pcm_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 return -ENXIO;
3811 return snd_pcm_mmap_control(substream, file, area);
3812 default:
3813 return snd_pcm_mmap_data(substream, file, area);
3814 }
3815 return 0;
3816}
3817
3818static int snd_pcm_fasync(int fd, struct file * file, int on)
3819{
Takashi Iwai877211f2005-11-17 13:59:38 +01003820 struct snd_pcm_file * pcm_file;
3821 struct snd_pcm_substream *substream;
3822 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 pcm_file = file->private_data;
3825 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003826 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003827 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003829 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830}
3831
3832/*
3833 * ioctl32 compat
3834 */
3835#ifdef CONFIG_COMPAT
3836#include "pcm_compat.c"
3837#else
3838#define snd_pcm_ioctl_compat NULL
3839#endif
3840
3841/*
3842 * To be removed helpers to keep binary compatibility
3843 */
3844
Takashi Iwai59d48582005-12-01 10:51:58 +01003845#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3847#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3848
Takashi Iwai877211f2005-11-17 13:59:38 +01003849static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3850 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
3852 unsigned int i;
3853
3854 memset(params, 0, sizeof(*params));
3855 params->flags = oparams->flags;
3856 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3857 params->masks[i].bits[0] = oparams->masks[i];
3858 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3859 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3860 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3861 params->info = oparams->info;
3862 params->msbits = oparams->msbits;
3863 params->rate_num = oparams->rate_num;
3864 params->rate_den = oparams->rate_den;
3865 params->fifo_size = oparams->fifo_size;
3866}
3867
Takashi Iwai877211f2005-11-17 13:59:38 +01003868static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3869 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
3871 unsigned int i;
3872
3873 memset(oparams, 0, sizeof(*oparams));
3874 oparams->flags = params->flags;
3875 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3876 oparams->masks[i] = params->masks[i].bits[0];
3877 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3878 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3879 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3880 oparams->info = params->info;
3881 oparams->msbits = params->msbits;
3882 oparams->rate_num = params->rate_num;
3883 oparams->rate_den = params->rate_den;
3884 oparams->fifo_size = params->fifo_size;
3885}
3886
Takashi Iwai877211f2005-11-17 13:59:38 +01003887static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3888 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
Takashi Iwai877211f2005-11-17 13:59:38 +01003890 struct snd_pcm_hw_params *params;
3891 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 int err;
3893
3894 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003895 if (!params)
3896 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Li Zefanef44a1e2009-04-10 09:43:08 +08003898 oparams = memdup_user(_oparams, sizeof(*oparams));
3899 if (IS_ERR(oparams)) {
3900 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 goto out;
3902 }
3903 snd_pcm_hw_convert_from_old_params(params, oparams);
3904 err = snd_pcm_hw_refine(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003905 if (err < 0)
3906 goto out_old;
Li Zefanef44a1e2009-04-10 09:43:08 +08003907
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003908 err = fixup_unreferenced_params(substream, params);
3909 if (err < 0)
3910 goto out_old;
3911
3912 snd_pcm_hw_convert_to_old_params(oparams, params);
3913 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3914 err = -EFAULT;
3915out_old:
Li Zefanef44a1e2009-04-10 09:43:08 +08003916 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917out:
3918 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return err;
3920}
3921
Takashi Iwai877211f2005-11-17 13:59:38 +01003922static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3923 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Takashi Iwai877211f2005-11-17 13:59:38 +01003925 struct snd_pcm_hw_params *params;
3926 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 int err;
3928
3929 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003930 if (!params)
3931 return -ENOMEM;
3932
3933 oparams = memdup_user(_oparams, sizeof(*oparams));
3934 if (IS_ERR(oparams)) {
3935 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 goto out;
3937 }
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 snd_pcm_hw_convert_from_old_params(params, oparams);
3940 err = snd_pcm_hw_params(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003941 if (err < 0)
3942 goto out_old;
Li Zefanef44a1e2009-04-10 09:43:08 +08003943
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003944 snd_pcm_hw_convert_to_old_params(oparams, params);
3945 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3946 err = -EFAULT;
3947out_old:
Li Zefanef44a1e2009-04-10 09:43:08 +08003948 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949out:
3950 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 return err;
3952}
Takashi Iwai59d48582005-12-01 10:51:58 +01003953#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Cliff Cai70036092008-09-03 10:54:36 +02003955#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003956static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3957 unsigned long addr,
3958 unsigned long len,
3959 unsigned long pgoff,
3960 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003961{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003962 struct snd_pcm_file *pcm_file = file->private_data;
3963 struct snd_pcm_substream *substream = pcm_file->substream;
3964 struct snd_pcm_runtime *runtime = substream->runtime;
3965 unsigned long offset = pgoff << PAGE_SHIFT;
3966
3967 switch (offset) {
Arnd Bergmann80fe7432018-04-24 20:06:15 +08003968 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003969 return (unsigned long)runtime->status;
Arnd Bergmann80fe7432018-04-24 20:06:15 +08003970 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003971 return (unsigned long)runtime->control;
3972 default:
3973 return (unsigned long)runtime->dma_area + offset;
3974 }
Cliff Cai70036092008-09-03 10:54:36 +02003975}
3976#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003977# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003978#endif
3979
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980/*
3981 * Register section
3982 */
3983
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003984const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003986 .owner = THIS_MODULE,
3987 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003988 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003989 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003990 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003991 .llseek = no_llseek,
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003992 .poll = snd_pcm_poll,
Takashi Iwai67616fe2017-08-30 15:39:32 +02003993 .unlocked_ioctl = snd_pcm_ioctl,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003994 .compat_ioctl = snd_pcm_ioctl_compat,
3995 .mmap = snd_pcm_mmap,
3996 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003997 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 },
3999 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01004000 .owner = THIS_MODULE,
4001 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04004002 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01004003 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01004004 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02004005 .llseek = no_llseek,
Takashi Iwai6448fcb2018-04-11 18:05:00 +02004006 .poll = snd_pcm_poll,
Takashi Iwai67616fe2017-08-30 15:39:32 +02004007 .unlocked_ioctl = snd_pcm_ioctl,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01004008 .compat_ioctl = snd_pcm_ioctl_compat,
4009 .mmap = snd_pcm_mmap,
4010 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05004011 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 }
4013};