blob: eddb0cd6d1eb6290ab42dc80b559801224e9b814 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040023#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/file.h>
25#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/time.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020028#include <linux/pm_qos.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010029#include <linux/io.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010030#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/timer.h>
37#include <sound/minors.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080038#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Sakamoto2c4842d2017-05-26 09:30:46 +090040#include "pcm_local.h"
41
Takashi Sakamoto37567c52017-06-07 08:46:44 +090042#ifdef CONFIG_SND_DEBUG
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090043#define CREATE_TRACE_POINTS
44#include "pcm_param_trace.h"
Takashi Sakamoto37567c52017-06-07 08:46:44 +090045#else
46#define trace_hw_mask_param_enabled() 0
47#define trace_hw_interval_param_enabled() 0
48#define trace_hw_mask_param(substream, type, index, prev, curr)
49#define trace_hw_interval_param(substream, type, index, prev, curr)
50#endif
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * Compatibility
54 */
55
Takashi Iwai877211f2005-11-17 13:59:38 +010056struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned int flags;
58 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
59 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010060 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
62 unsigned int rmask;
63 unsigned int cmask;
64 unsigned int info;
65 unsigned int msbits;
66 unsigned int rate_num;
67 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010068 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned char reserved[64];
70};
71
Takashi Iwai59d48582005-12-01 10:51:58 +010072#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010073#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
74#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Takashi Iwai877211f2005-11-17 13:59:38 +010076static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
77 struct snd_pcm_hw_params_old __user * _oparams);
78static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
79 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010080#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010081static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 *
85 */
86
Takashi Iwai7af142f2014-09-01 11:19:37 +020087static DEFINE_RWLOCK(snd_pcm_link_rwlock);
88static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwai67ec10722016-02-17 14:30:26 +010090/* Writer in rwsem may block readers even during its waiting in queue,
91 * and this may lead to a deadlock when the code path takes read sem
92 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
93 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
94 * spin until it gets the lock.
95 */
96static inline void down_write_nonblock(struct rw_semaphore *lock)
97{
98 while (!down_write_trylock(lock))
99 cond_resched();
100}
101
Takashi Iwai30b771c2014-10-30 15:02:50 +0100102/**
103 * snd_pcm_stream_lock - Lock the PCM stream
104 * @substream: PCM substream
105 *
106 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
107 * flag of the given substream. This also takes the global link rw lock
108 * (or rw sem), too, for avoiding the race with linked streams.
109 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200110void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
111{
112 if (substream->pcm->nonatomic) {
Takashi Iwai67756e32015-07-17 15:22:33 +0200113 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200114 mutex_lock(&substream->self_group.mutex);
115 } else {
116 read_lock(&snd_pcm_link_rwlock);
117 spin_lock(&substream->self_group.lock);
118 }
119}
120EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
121
Takashi Iwai30b771c2014-10-30 15:02:50 +0100122/**
123 * snd_pcm_stream_lock - Unlock the PCM stream
124 * @substream: PCM substream
125 *
126 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
127 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200128void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
129{
130 if (substream->pcm->nonatomic) {
131 mutex_unlock(&substream->self_group.mutex);
132 up_read(&snd_pcm_link_rwsem);
133 } else {
134 spin_unlock(&substream->self_group.lock);
135 read_unlock(&snd_pcm_link_rwlock);
136 }
137}
138EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
139
Takashi Iwai30b771c2014-10-30 15:02:50 +0100140/**
141 * snd_pcm_stream_lock_irq - Lock the PCM stream
142 * @substream: PCM substream
143 *
144 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
145 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
146 * as snd_pcm_stream_lock().
147 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200148void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
149{
150 if (!substream->pcm->nonatomic)
151 local_irq_disable();
152 snd_pcm_stream_lock(substream);
153}
154EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
155
Takashi Iwai30b771c2014-10-30 15:02:50 +0100156/**
157 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
158 * @substream: PCM substream
159 *
160 * This is a counter-part of snd_pcm_stream_lock_irq().
161 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200162void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
163{
164 snd_pcm_stream_unlock(substream);
165 if (!substream->pcm->nonatomic)
166 local_irq_enable();
167}
168EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
169
170unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
171{
172 unsigned long flags = 0;
173 if (!substream->pcm->nonatomic)
174 local_irq_save(flags);
175 snd_pcm_stream_lock(substream);
176 return flags;
177}
178EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
179
Takashi Iwai30b771c2014-10-30 15:02:50 +0100180/**
181 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
182 * @substream: PCM substream
183 * @flags: irq flags
184 *
185 * This is a counter-part of snd_pcm_stream_lock_irqsave().
186 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200187void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
188 unsigned long flags)
189{
190 snd_pcm_stream_unlock(substream);
191 if (!substream->pcm->nonatomic)
192 local_irq_restore(flags);
193}
194EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Takashi Iwai877211f2005-11-17 13:59:38 +0100196int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Takashi Iwai877211f2005-11-17 13:59:38 +0100198 struct snd_pcm *pcm = substream->pcm;
199 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 memset(info, 0, sizeof(*info));
202 info->card = pcm->card->number;
203 info->device = pcm->device;
204 info->stream = substream->stream;
205 info->subdevice = substream->number;
206 strlcpy(info->id, pcm->id, sizeof(info->id));
207 strlcpy(info->name, pcm->name, sizeof(info->name));
208 info->dev_class = pcm->dev_class;
209 info->dev_subclass = pcm->dev_subclass;
210 info->subdevices_count = pstr->substream_count;
211 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
212 strlcpy(info->subname, substream->name, sizeof(info->subname));
Takashi Sakamotoe11f0f92017-06-14 19:30:03 +0900213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215}
216
Takashi Iwai877211f2005-11-17 13:59:38 +0100217int snd_pcm_info_user(struct snd_pcm_substream *substream,
218 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Takashi Iwai877211f2005-11-17 13:59:38 +0100220 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int err;
222
223 info = kmalloc(sizeof(*info), GFP_KERNEL);
224 if (! info)
225 return -ENOMEM;
226 err = snd_pcm_info(substream, info);
227 if (err >= 0) {
228 if (copy_to_user(_info, info, sizeof(*info)))
229 err = -EFAULT;
230 }
231 kfree(info);
232 return err;
233}
234
Takashi Iwai63825f32014-10-22 12:04:46 +0200235static bool hw_support_mmap(struct snd_pcm_substream *substream)
236{
237 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
238 return false;
Takashi Iwai85dc0f82017-07-10 16:05:58 +0200239 /* architecture supports dma_mmap_coherent()? */
240#if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
Takashi Iwai63825f32014-10-22 12:04:46 +0200241 if (!substream->ops->mmap &&
242 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
243 return false;
244#endif
245 return true;
246}
247
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900248static int constrain_mask_params(struct snd_pcm_substream *substream,
249 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900251 struct snd_pcm_hw_constraints *constrs =
252 &substream->runtime->hw_constraints;
253 struct snd_mask *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned int k;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900255 struct snd_mask old_mask;
256 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
259 m = hw_param_mask(params, k);
260 if (snd_mask_empty(m))
261 return -EINVAL;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900262
263 /* This parameter is not requested to change by a caller. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!(params->rmask & (1 << k)))
265 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900266
267 if (trace_hw_mask_param_enabled())
268 old_mask = *m;
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 changed = snd_mask_refine(m, constrs_mask(constrs, k));
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900271 if (changed < 0)
272 return changed;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900273 if (changed == 0)
274 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900275
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900276 /* Set corresponding flag so that the caller gets it. */
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900277 trace_hw_mask_param(substream, k, 0, &old_mask, m);
278 params->cmask |= 1 << k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900281 return 0;
282}
283
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900284static int constrain_interval_params(struct snd_pcm_substream *substream,
285 struct snd_pcm_hw_params *params)
286{
287 struct snd_pcm_hw_constraints *constrs =
288 &substream->runtime->hw_constraints;
289 struct snd_interval *i;
290 unsigned int k;
291 struct snd_interval old_interval;
292 int changed;
293
294 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
295 i = hw_param_interval(params, k);
296 if (snd_interval_empty(i))
297 return -EINVAL;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900298
299 /* This parameter is not requested to change by a caller. */
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900300 if (!(params->rmask & (1 << k)))
301 continue;
302
303 if (trace_hw_interval_param_enabled())
304 old_interval = *i;
305
306 changed = snd_interval_refine(i, constrs_interval(constrs, k));
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900307 if (changed < 0)
308 return changed;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900309 if (changed == 0)
310 continue;
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900311
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900312 /* Set corresponding flag so that the caller gets it. */
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900313 trace_hw_interval_param(substream, k, 0, &old_interval, i);
314 params->cmask |= 1 << k;
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900315 }
316
317 return 0;
318}
319
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900320static int constrain_params_by_rules(struct snd_pcm_substream *substream,
321 struct snd_pcm_hw_params *params)
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900322{
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900323 struct snd_pcm_hw_constraints *constrs =
324 &substream->runtime->hw_constraints;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900325 unsigned int k;
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100326 unsigned int *rstamps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900328 unsigned int stamp;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900329 struct snd_pcm_hw_rule *r;
330 unsigned int d;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900331 struct snd_mask old_mask;
332 struct snd_interval old_interval;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900333 bool again;
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100334 int changed, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900336 /*
337 * Each application of rule has own sequence number.
338 *
339 * Each member of 'rstamps' array represents the sequence number of
340 * recent application of corresponding rule.
341 */
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100342 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
343 if (!rstamps)
344 return -ENOMEM;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900345
346 /*
347 * Each member of 'vstamps' array represents the sequence number of
348 * recent application of rule in which corresponding parameters were
349 * changed.
350 *
351 * In initial state, elements corresponding to parameters requested by
352 * a caller is 1. For unrequested parameters, corresponding members
353 * have 0 so that the parameters are never changed anymore.
354 */
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900355 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900357
358 /* Due to the above design, actual sequence number starts at 2. */
359 stamp = 2;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900360retry:
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900361 /* Apply all rules in order. */
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900362 again = false;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900363 for (k = 0; k < constrs->rules_num; k++) {
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900364 r = &constrs->rules[k];
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900365
366 /*
367 * Check condition bits of this rule. When the rule has
368 * some condition bits, parameter without the bits is
369 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
370 * is an example of the condition bits.
371 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900372 if (r->cond && !(r->cond & params->flags))
373 continue;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900374
375 /*
376 * The 'deps' array includes maximum three dependencies
377 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
378 * member of this array is a sentinel and should be
379 * negative value.
380 *
381 * This rule should be processed in this time when dependent
382 * parameters were changed at former applications of the other
383 * rules.
384 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900385 for (d = 0; r->deps[d] >= 0; d++) {
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900386 if (vstamps[r->deps[d]] > rstamps[k])
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900389 if (r->deps[d] < 0)
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900390 continue;
391
392 if (trace_hw_mask_param_enabled()) {
393 if (hw_is_mask(r->var))
394 old_mask = *hw_param_mask(params, r->var);
395 }
396 if (trace_hw_interval_param_enabled()) {
397 if (hw_is_interval(r->var))
398 old_interval = *hw_param_interval(params, r->var);
399 }
400
401 changed = r->func(params, r);
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100402 if (changed < 0) {
403 err = changed;
404 goto out;
405 }
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900406
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900407 /*
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900408 * When the parameter is changed, notify it to the caller
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900409 * by corresponding returned bit, then preparing for next
410 * iteration.
411 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900412 if (changed && r->var >= 0) {
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900413 if (hw_is_mask(r->var)) {
414 trace_hw_mask_param(substream, r->var,
415 k + 1, &old_mask,
416 hw_param_mask(params, r->var));
417 }
418 if (hw_is_interval(r->var)) {
419 trace_hw_interval_param(substream, r->var,
420 k + 1, &old_interval,
421 hw_param_interval(params, r->var));
422 }
423
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900424 params->cmask |= (1 << r->var);
425 vstamps[r->var] = stamp;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900426 again = true;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900427 }
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900428
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900429 rstamps[k] = stamp++;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900430 }
431
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900432 /* Iterate to evaluate all rules till no parameters are changed. */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900433 if (again)
434 goto retry;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900435
Takashi Iwai5730f9f2018-03-13 11:18:57 +0100436 out:
437 kfree(rstamps);
438 return err;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900439}
440
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900441static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
442 struct snd_pcm_hw_params *params)
443{
444 const struct snd_interval *i;
445 const struct snd_mask *m;
446 int err;
447
448 if (!params->msbits) {
449 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
450 if (snd_interval_single(i))
451 params->msbits = snd_interval_value(i);
452 }
453
454 if (!params->rate_den) {
455 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
456 if (snd_interval_single(i)) {
457 params->rate_num = snd_interval_value(i);
458 params->rate_den = 1;
459 }
460 }
461
462 if (!params->fifo_size) {
463 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
464 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
465 if (snd_mask_single(m) && snd_interval_single(i)) {
466 err = substream->ops->ioctl(substream,
467 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
468 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;
492 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
493 params->msbits = 0;
494 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
495 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 Iwai9b0573c2012-10-12 15:07:34 +0200557static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
558{
559 snd_pcm_stream_lock_irq(substream);
560 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
561 substream->runtime->status->state = state;
562 snd_pcm_stream_unlock_irq(substream);
563}
564
Jie Yang90bbaf62015-10-16 17:57:46 +0800565static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
566 int event)
567{
568#ifdef CONFIG_SND_PCM_TIMER
569 if (substream->timer)
570 snd_timer_notify(substream->timer, event,
571 &substream->runtime->trigger_tstamp);
572#endif
573}
574
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900575/**
576 * snd_pcm_hw_param_choose - choose a configuration defined by @params
577 * @pcm: PCM instance
578 * @params: the hw_params instance
579 *
580 * Choose one configuration from configuration space defined by @params.
581 * The configuration chosen is that obtained fixing in this order:
582 * first access, first format, first subformat, min channels,
583 * min rate, min period time, max buffer size, min tick time
584 *
585 * Return: Zero if successful, or a negative error code on failure.
586 */
587static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
588 struct snd_pcm_hw_params *params)
589{
590 static const int vars[] = {
591 SNDRV_PCM_HW_PARAM_ACCESS,
592 SNDRV_PCM_HW_PARAM_FORMAT,
593 SNDRV_PCM_HW_PARAM_SUBFORMAT,
594 SNDRV_PCM_HW_PARAM_CHANNELS,
595 SNDRV_PCM_HW_PARAM_RATE,
596 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
597 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
598 SNDRV_PCM_HW_PARAM_TICK_TIME,
599 -1
600 };
601 const int *v;
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900602 struct snd_mask old_mask;
603 struct snd_interval old_interval;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900604 int changed;
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900605
606 for (v = vars; *v != -1; v++) {
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900607 /* Keep old parameter to trace. */
608 if (trace_hw_mask_param_enabled()) {
609 if (hw_is_mask(*v))
610 old_mask = *hw_param_mask(params, *v);
611 }
612 if (trace_hw_interval_param_enabled()) {
613 if (hw_is_interval(*v))
614 old_interval = *hw_param_interval(params, *v);
615 }
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900616 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900617 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900618 else
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900619 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
Takashi Iwaie1a3a982018-04-09 17:12:16 +0200620 if (changed < 0)
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900621 return changed;
622 if (changed == 0)
623 continue;
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900624
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900625 /* Trace the changed parameter. */
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900626 if (hw_is_mask(*v)) {
627 trace_hw_mask_param(pcm, *v, 0, &old_mask,
628 hw_param_mask(params, *v));
629 }
630 if (hw_is_interval(*v)) {
631 trace_hw_interval_param(pcm, *v, 0, &old_interval,
632 hw_param_interval(params, *v));
633 }
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900634 }
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900635
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900636 return 0;
637}
638
Takashi Iwai877211f2005-11-17 13:59:38 +0100639static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
640 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Takashi Iwai877211f2005-11-17 13:59:38 +0100642 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700643 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 unsigned int bits;
645 snd_pcm_uframes_t frames;
646
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200647 if (PCM_RUNTIME_CHECK(substream))
648 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 snd_pcm_stream_lock_irq(substream);
651 switch (runtime->status->state) {
652 case SNDRV_PCM_STATE_OPEN:
653 case SNDRV_PCM_STATE_SETUP:
654 case SNDRV_PCM_STATE_PREPARED:
655 break;
656 default:
657 snd_pcm_stream_unlock_irq(substream);
658 return -EBADFD;
659 }
660 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100661#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (!substream->oss.oss)
663#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200664 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -EBADFD;
666
667 params->rmask = ~0U;
668 err = snd_pcm_hw_refine(substream, params);
669 if (err < 0)
670 goto _error;
671
672 err = snd_pcm_hw_params_choose(substream, params);
673 if (err < 0)
674 goto _error;
675
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900676 err = fixup_unreferenced_params(substream, params);
677 if (err < 0)
678 goto _error;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (substream->ops->hw_params != NULL) {
681 err = substream->ops->hw_params(substream, params);
682 if (err < 0)
683 goto _error;
684 }
685
686 runtime->access = params_access(params);
687 runtime->format = params_format(params);
688 runtime->subformat = params_subformat(params);
689 runtime->channels = params_channels(params);
690 runtime->rate = params_rate(params);
691 runtime->period_size = params_period_size(params);
692 runtime->periods = params_periods(params);
693 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 runtime->info = params->info;
695 runtime->rate_num = params->rate_num;
696 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100697 runtime->no_period_wakeup =
698 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
699 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 bits = snd_pcm_format_physical_width(runtime->format);
702 runtime->sample_bits = bits;
703 bits *= runtime->channels;
704 runtime->frame_bits = bits;
705 frames = 1;
706 while (bits % 8 != 0) {
707 bits *= 2;
708 frames *= 2;
709 }
710 runtime->byte_align = bits / 8;
711 runtime->min_align = frames;
712
713 /* Default sw params */
714 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
715 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 runtime->start_threshold = 1;
718 runtime->stop_threshold = runtime->buffer_size;
719 runtime->silence_threshold = 0;
720 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200721 runtime->boundary = runtime->buffer_size;
722 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
723 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200726 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700727
James Bottomley82f68252010-07-05 22:53:06 +0200728 if (pm_qos_request_active(&substream->latency_pm_qos_req))
729 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700730 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200731 pm_qos_add_request(&substream->latency_pm_qos_req,
732 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300735 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 so we force application to retry to set
737 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200738 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (substream->ops->hw_free != NULL)
740 substream->ops->hw_free(substream);
741 return err;
742}
743
Takashi Iwai877211f2005-11-17 13:59:38 +0100744static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
745 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Takashi Iwai877211f2005-11-17 13:59:38 +0100747 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int err;
749
Li Zefanef44a1e2009-04-10 09:43:08 +0800750 params = memdup_user(_params, sizeof(*params));
751 if (IS_ERR(params))
752 return PTR_ERR(params);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 err = snd_pcm_hw_params(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900755 if (err < 0)
756 goto end;
Li Zefanef44a1e2009-04-10 09:43:08 +0800757
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900758 if (copy_to_user(_params, params, sizeof(*params)))
759 err = -EFAULT;
760end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 kfree(params);
762 return err;
763}
764
Takashi Iwai877211f2005-11-17 13:59:38 +0100765static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Takashi Iwai877211f2005-11-17 13:59:38 +0100767 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int result = 0;
769
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200770 if (PCM_RUNTIME_CHECK(substream))
771 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 snd_pcm_stream_lock_irq(substream);
774 switch (runtime->status->state) {
775 case SNDRV_PCM_STATE_SETUP:
776 case SNDRV_PCM_STATE_PREPARED:
777 break;
778 default:
779 snd_pcm_stream_unlock_irq(substream);
780 return -EBADFD;
781 }
782 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200783 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -EBADFD;
785 if (substream->ops->hw_free)
786 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200787 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200788 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return result;
790}
791
Takashi Iwai877211f2005-11-17 13:59:38 +0100792static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
793 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Takashi Iwai877211f2005-11-17 13:59:38 +0100795 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100796 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200798 if (PCM_RUNTIME_CHECK(substream))
799 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 snd_pcm_stream_lock_irq(substream);
802 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
803 snd_pcm_stream_unlock_irq(substream);
804 return -EBADFD;
805 }
806 snd_pcm_stream_unlock_irq(substream);
807
Dan Carpenter145d92e2015-09-23 12:42:28 +0300808 if (params->tstamp_mode < 0 ||
809 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EINVAL;
Takashi Iwai589008102014-07-16 17:45:27 +0200811 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
812 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200813 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (params->avail_min == 0)
815 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (params->silence_size >= runtime->boundary) {
817 if (params->silence_threshold != 0)
818 return -EINVAL;
819 } else {
820 if (params->silence_size > params->silence_threshold)
821 return -EINVAL;
822 if (params->silence_threshold > runtime->buffer_size)
823 return -EINVAL;
824 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100825 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 snd_pcm_stream_lock_irq(substream);
827 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai589008102014-07-16 17:45:27 +0200828 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
829 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 runtime->period_step = params->period_step;
831 runtime->control->avail_min = params->avail_min;
832 runtime->start_threshold = params->start_threshold;
833 runtime->stop_threshold = params->stop_threshold;
834 runtime->silence_threshold = params->silence_threshold;
835 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 params->boundary = runtime->boundary;
837 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
839 runtime->silence_size > 0)
840 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100841 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100844 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Takashi Iwai877211f2005-11-17 13:59:38 +0100847static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
848 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Takashi Iwai877211f2005-11-17 13:59:38 +0100850 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int err;
852 if (copy_from_user(&params, _params, sizeof(params)))
853 return -EFAULT;
854 err = snd_pcm_sw_params(substream, &params);
855 if (copy_to_user(_params, &params, sizeof(params)))
856 return -EFAULT;
857 return err;
858}
859
Takashi Iwai877211f2005-11-17 13:59:38 +0100860int snd_pcm_status(struct snd_pcm_substream *substream,
861 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Takashi Iwai877211f2005-11-17 13:59:38 +0100863 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600866
867 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
868 &runtime->audio_tstamp_config);
869
870 /* backwards compatible behavior */
871 if (runtime->audio_tstamp_config.type_requested ==
872 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
873 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
874 runtime->audio_tstamp_config.type_requested =
875 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
876 else
877 runtime->audio_tstamp_config.type_requested =
878 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
879 runtime->audio_tstamp_report.valid = 0;
880 } else
881 runtime->audio_tstamp_report.valid = 1;
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 status->state = runtime->status->state;
884 status->suspended_state = runtime->status->suspended_state;
885 if (status->state == SNDRV_PCM_STATE_OPEN)
886 goto _end;
887 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100888 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100890 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
891 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600892 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500893 status->audio_tstamp =
894 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600895 if (runtime->audio_tstamp_report.valid == 1)
896 /* backwards compatibility, no report provided in COMPAT mode */
897 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
898 &status->audio_tstamp_accuracy,
899 &runtime->audio_tstamp_report);
900
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100901 goto _tstamp_end;
902 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600903 } else {
904 /* get tstamp only in fallback mode and only if enabled */
905 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
906 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100907 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100908 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 status->appl_ptr = runtime->control->appl_ptr;
910 status->hw_ptr = runtime->status->hw_ptr;
Takashi Iwai763e5062018-04-11 17:56:52 +0200911 status->avail = snd_pcm_avail(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200914 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200916 status->delay += runtime->delay;
917 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 status->delay = 0;
919 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200921 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 else
923 status->delay = 0;
924 }
925 status->avail_max = runtime->avail_max;
926 status->overrange = runtime->overrange;
927 runtime->avail_max = 0;
928 runtime->overrange = 0;
929 _end:
930 snd_pcm_stream_unlock_irq(substream);
931 return 0;
932}
933
Takashi Iwai877211f2005-11-17 13:59:38 +0100934static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600935 struct snd_pcm_status __user * _status,
936 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Takashi Iwai877211f2005-11-17 13:59:38 +0100938 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600942 /*
943 * with extension, parameters are read/write,
944 * get audio_tstamp_data from user,
945 * ignore rest of status structure
946 */
947 if (ext && get_user(status.audio_tstamp_data,
948 (u32 __user *)(&_status->audio_tstamp_data)))
949 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 res = snd_pcm_status(substream, &status);
951 if (res < 0)
952 return res;
953 if (copy_to_user(_status, &status, sizeof(status)))
954 return -EFAULT;
955 return 0;
956}
957
Takashi Iwai877211f2005-11-17 13:59:38 +0100958static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
959 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Takashi Iwai877211f2005-11-17 13:59:38 +0100961 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 unsigned int channel;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 channel = info->channel;
965 runtime = substream->runtime;
966 snd_pcm_stream_lock_irq(substream);
967 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
968 snd_pcm_stream_unlock_irq(substream);
969 return -EBADFD;
970 }
971 snd_pcm_stream_unlock_irq(substream);
972 if (channel >= runtime->channels)
973 return -EINVAL;
974 memset(info, 0, sizeof(*info));
975 info->channel = channel;
976 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
977}
978
Takashi Iwai877211f2005-11-17 13:59:38 +0100979static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
980 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Takashi Iwai877211f2005-11-17 13:59:38 +0100982 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int res;
984
985 if (copy_from_user(&info, _info, sizeof(info)))
986 return -EFAULT;
987 res = snd_pcm_channel_info(substream, &info);
988 if (res < 0)
989 return res;
990 if (copy_to_user(_info, &info, sizeof(info)))
991 return -EFAULT;
992 return 0;
993}
994
Takashi Iwai877211f2005-11-17 13:59:38 +0100995static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Takashi Iwai877211f2005-11-17 13:59:38 +0100997 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (runtime->trigger_master == NULL)
999 return;
1000 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001001 if (!runtime->trigger_tstamp_latched)
1002 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 } else {
1004 snd_pcm_trigger_tstamp(runtime->trigger_master);
1005 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1006 }
1007 runtime->trigger_master = NULL;
1008}
1009
1010struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +01001011 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1012 int (*do_action)(struct snd_pcm_substream *substream, int state);
1013 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1014 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015};
1016
1017/*
1018 * this functions is core for handling of linked stream
1019 * Note: the stream state might be changed also on failure
1020 * Note2: call with calling stream lock + link lock
1021 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001022static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001023 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int state, int do_lock)
1025{
Takashi Iwai877211f2005-11-17 13:59:38 +01001026 struct snd_pcm_substream *s = NULL;
1027 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +02001028 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Takashi Iwaief991b92007-02-22 12:52:53 +01001030 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001031 if (do_lock && s != substream) {
1032 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +02001033 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001034 else
Takashi Iwaidde1c652014-10-21 15:32:13 +02001035 spin_lock_nested(&s->self_group.lock, depth);
1036 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 res = ops->pre_action(s, state);
1039 if (res < 0)
1040 goto _unlock;
1041 }
Takashi Iwaief991b92007-02-22 12:52:53 +01001042 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 res = ops->do_action(s, state);
1044 if (res < 0) {
1045 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +01001046 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (s1 == s) /* failed stream */
1048 break;
1049 ops->undo_action(s1, state);
1050 }
1051 }
1052 s = NULL; /* unlock all */
1053 goto _unlock;
1054 }
1055 }
Takashi Iwaief991b92007-02-22 12:52:53 +01001056 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 ops->post_action(s, state);
1058 }
1059 _unlock:
1060 if (do_lock) {
1061 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +01001062 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001063 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +02001064 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001065 mutex_unlock(&s1->self_group.mutex);
1066 else
1067 spin_unlock(&s1->self_group.lock);
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (s1 == s) /* end */
1070 break;
1071 }
1072 }
1073 return res;
1074}
1075
1076/*
1077 * Note: call with stream lock
1078 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001079static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001080 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int state)
1082{
1083 int res;
1084
1085 res = ops->pre_action(substream, state);
1086 if (res < 0)
1087 return res;
1088 res = ops->do_action(substream, state);
1089 if (res == 0)
1090 ops->post_action(substream, state);
1091 else if (ops->undo_action)
1092 ops->undo_action(substream, state);
1093 return res;
1094}
1095
1096/*
1097 * Note: call with stream lock
1098 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001099static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001100 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int state)
1102{
1103 int res;
1104
Takashi Iwaiaa8edd82014-10-31 15:19:36 +01001105 if (!snd_pcm_stream_linked(substream))
1106 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001107
Takashi Iwaiaa8edd82014-10-31 15:19:36 +01001108 if (substream->pcm->nonatomic) {
1109 if (!mutex_trylock(&substream->group->mutex)) {
1110 mutex_unlock(&substream->self_group.mutex);
1111 mutex_lock(&substream->group->mutex);
1112 mutex_lock(&substream->self_group.mutex);
1113 }
1114 res = snd_pcm_action_group(ops, substream, state, 1);
1115 mutex_unlock(&substream->group->mutex);
1116 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!spin_trylock(&substream->group->lock)) {
1118 spin_unlock(&substream->self_group.lock);
1119 spin_lock(&substream->group->lock);
1120 spin_lock(&substream->self_group.lock);
1121 }
1122 res = snd_pcm_action_group(ops, substream, state, 1);
1123 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125 return res;
1126}
1127
1128/*
1129 * Note: don't use any locks before
1130 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001131static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001132 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 int state)
1134{
1135 int res;
1136
Takashi Iwaie3a4bd52014-10-31 14:45:04 +01001137 snd_pcm_stream_lock_irq(substream);
1138 res = snd_pcm_action(ops, substream, state);
1139 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return res;
1141}
1142
1143/*
1144 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001145static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001146 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int state)
1148{
1149 int res;
1150
1151 down_read(&snd_pcm_link_rwsem);
1152 if (snd_pcm_stream_linked(substream))
1153 res = snd_pcm_action_group(ops, substream, state, 0);
1154 else
1155 res = snd_pcm_action_single(ops, substream, state);
1156 up_read(&snd_pcm_link_rwsem);
1157 return res;
1158}
1159
1160/*
1161 * start callbacks
1162 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001163static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Takashi Iwai877211f2005-11-17 13:59:38 +01001165 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1167 return -EBADFD;
1168 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1169 !snd_pcm_playback_data(substream))
1170 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001171 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 runtime->trigger_master = substream;
1173 return 0;
1174}
1175
Takashi Iwai877211f2005-11-17 13:59:38 +01001176static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 if (substream->runtime->trigger_master != substream)
1179 return 0;
1180 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1181}
1182
Takashi Iwai877211f2005-11-17 13:59:38 +01001183static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 if (substream->runtime->trigger_master == substream)
1186 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1187}
1188
Takashi Iwai877211f2005-11-17 13:59:38 +01001189static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Takashi Iwai877211f2005-11-17 13:59:38 +01001191 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001193 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001194 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1195 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 runtime->status->state = state;
1197 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1198 runtime->silence_size > 0)
1199 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001200 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Julia Lawallb17154c2015-11-29 16:36:40 +01001203static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 .pre_action = snd_pcm_pre_start,
1205 .do_action = snd_pcm_do_start,
1206 .undo_action = snd_pcm_undo_start,
1207 .post_action = snd_pcm_post_start
1208};
1209
1210/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001211 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001212 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001213 *
1214 * Return: Zero if successful, or a negative error code.
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001215 * The stream lock must be acquired before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001217int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Takashi Iwai877211f2005-11-17 13:59:38 +01001219 return snd_pcm_action(&snd_pcm_action_start, substream,
1220 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001223/* take the stream lock and start the streams */
1224static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1225{
1226 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1227 SNDRV_PCM_STATE_RUNNING);
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/*
1231 * stop callbacks
1232 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001233static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Takashi Iwai877211f2005-11-17 13:59:38 +01001235 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1237 return -EBADFD;
1238 runtime->trigger_master = substream;
1239 return 0;
1240}
1241
Takashi Iwai877211f2005-11-17 13:59:38 +01001242static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 if (substream->runtime->trigger_master == substream &&
1245 snd_pcm_running(substream))
1246 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1247 return 0; /* unconditonally stop all substreams */
1248}
1249
Takashi Iwai877211f2005-11-17 13:59:38 +01001250static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Takashi Iwai877211f2005-11-17 13:59:38 +01001252 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (runtime->status->state != state) {
1254 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001255 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001256 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001259 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Julia Lawallb17154c2015-11-29 16:36:40 +01001262static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 .pre_action = snd_pcm_pre_stop,
1264 .do_action = snd_pcm_do_stop,
1265 .post_action = snd_pcm_post_stop
1266};
1267
1268/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001269 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001270 * @substream: the PCM substream instance
1271 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001273 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001274 *
Masanari Iida0a114582014-02-09 00:47:36 +09001275 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001277int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1280}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001281EXPORT_SYMBOL(snd_pcm_stop);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001284 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001285 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001287 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001289 *
1290 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001292int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Takashi Iwai877211f2005-11-17 13:59:38 +01001294 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1295 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Takashi Iwai1fb85102014-11-07 17:08:28 +01001298/**
1299 * snd_pcm_stop_xrun - stop the running streams as XRUN
1300 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001301 *
1302 * This stops the given running substream (and all linked substreams) as XRUN.
1303 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1304 *
1305 * Return: Zero if successful, or a negative error code.
1306 */
1307int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1308{
1309 unsigned long flags;
1310 int ret = 0;
1311
1312 snd_pcm_stream_lock_irqsave(substream, flags);
1313 if (snd_pcm_running(substream))
1314 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1315 snd_pcm_stream_unlock_irqrestore(substream, flags);
1316 return ret;
1317}
1318EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320/*
1321 * pause callbacks
1322 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001323static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Takashi Iwai877211f2005-11-17 13:59:38 +01001325 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1327 return -ENOSYS;
1328 if (push) {
1329 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1330 return -EBADFD;
1331 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1332 return -EBADFD;
1333 runtime->trigger_master = substream;
1334 return 0;
1335}
1336
Takashi Iwai877211f2005-11-17 13:59:38 +01001337static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 if (substream->runtime->trigger_master != substream)
1340 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001341 /* some drivers might use hw_ptr to recover from the pause -
1342 update the hw_ptr now */
1343 if (push)
1344 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001345 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001346 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001347 * delta, effectively to skip the check once.
1348 */
1349 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return substream->ops->trigger(substream,
1351 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1352 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1353}
1354
Takashi Iwai877211f2005-11-17 13:59:38 +01001355static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 if (substream->runtime->trigger_master == substream)
1358 substream->ops->trigger(substream,
1359 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1360 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1361}
1362
Takashi Iwai877211f2005-11-17 13:59:38 +01001363static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Takashi Iwai877211f2005-11-17 13:59:38 +01001365 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 snd_pcm_trigger_tstamp(substream);
1367 if (push) {
1368 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001369 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001371 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 } else {
1373 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001374 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376}
1377
Julia Lawallb17154c2015-11-29 16:36:40 +01001378static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 .pre_action = snd_pcm_pre_pause,
1380 .do_action = snd_pcm_do_pause,
1381 .undo_action = snd_pcm_undo_pause,
1382 .post_action = snd_pcm_post_pause
1383};
1384
1385/*
1386 * Push/release the pause for all linked streams.
1387 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001388static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1391}
1392
1393#ifdef CONFIG_PM
1394/* suspend */
1395
Takashi Iwai877211f2005-11-17 13:59:38 +01001396static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int 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_SUSPENDED)
1400 return -EBUSY;
1401 runtime->trigger_master = substream;
1402 return 0;
1403}
1404
Takashi Iwai877211f2005-11-17 13:59:38 +01001405static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Takashi Iwai877211f2005-11-17 13:59:38 +01001407 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (runtime->trigger_master != substream)
1409 return 0;
1410 if (! snd_pcm_running(substream))
1411 return 0;
1412 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1413 return 0; /* suspend unconditionally */
1414}
1415
Takashi Iwai877211f2005-11-17 13:59:38 +01001416static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Takashi Iwai877211f2005-11-17 13:59:38 +01001418 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001420 runtime->status->suspended_state = runtime->status->state;
1421 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001422 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001424 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Julia Lawallb17154c2015-11-29 16:36:40 +01001427static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 .pre_action = snd_pcm_pre_suspend,
1429 .do_action = snd_pcm_do_suspend,
1430 .post_action = snd_pcm_post_suspend
1431};
1432
1433/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001434 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001435 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001438 *
1439 * Return: Zero if successful (or @substream is %NULL), or a negative error
1440 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001442int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 int err;
1445 unsigned long flags;
1446
Takashi Iwai603bf522005-11-17 15:59:14 +01001447 if (! substream)
1448 return 0;
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 snd_pcm_stream_lock_irqsave(substream, flags);
1451 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1452 snd_pcm_stream_unlock_irqrestore(substream, flags);
1453 return err;
1454}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001455EXPORT_SYMBOL(snd_pcm_suspend);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001458 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001459 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001462 *
1463 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001465int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Takashi Iwai877211f2005-11-17 13:59:38 +01001467 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int stream, err = 0;
1469
Takashi Iwai603bf522005-11-17 15:59:14 +01001470 if (! pcm)
1471 return 0;
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001474 for (substream = pcm->streams[stream].substream;
1475 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* FIXME: the open/close code should lock this as well */
1477 if (substream->runtime == NULL)
1478 continue;
1479 err = snd_pcm_suspend(substream);
1480 if (err < 0 && err != -EBUSY)
1481 return err;
1482 }
1483 }
1484 return 0;
1485}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001486EXPORT_SYMBOL(snd_pcm_suspend_all);
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488/* resume */
1489
Takashi Iwai877211f2005-11-17 13:59:38 +01001490static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Takashi Iwai877211f2005-11-17 13:59:38 +01001492 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1494 return -ENOSYS;
1495 runtime->trigger_master = substream;
1496 return 0;
1497}
1498
Takashi Iwai877211f2005-11-17 13:59:38 +01001499static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Takashi Iwai877211f2005-11-17 13:59:38 +01001501 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (runtime->trigger_master != substream)
1503 return 0;
1504 /* DMA not running previously? */
1505 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1506 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1507 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1508 return 0;
1509 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1510}
1511
Takashi Iwai877211f2005-11-17 13:59:38 +01001512static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 if (substream->runtime->trigger_master == substream &&
1515 snd_pcm_running(substream))
1516 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1517}
1518
Takashi Iwai877211f2005-11-17 13:59:38 +01001519static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Takashi Iwai877211f2005-11-17 13:59:38 +01001521 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001523 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001524 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
Julia Lawallb17154c2015-11-29 16:36:40 +01001527static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 .pre_action = snd_pcm_pre_resume,
1529 .do_action = snd_pcm_do_resume,
1530 .undo_action = snd_pcm_undo_resume,
1531 .post_action = snd_pcm_post_resume
1532};
1533
Takashi Iwai877211f2005-11-17 13:59:38 +01001534static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Takashi Iwai68b4acd2016-05-24 15:07:39 +02001536 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539#else
1540
Takashi Iwai877211f2005-11-17 13:59:38 +01001541static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 return -ENOSYS;
1544}
1545
1546#endif /* CONFIG_PM */
1547
1548/*
1549 * xrun ioctl
1550 *
1551 * Change the RUNNING stream(s) to XRUN state.
1552 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001553static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Takashi Iwai877211f2005-11-17 13:59:38 +01001555 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 int result;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 snd_pcm_stream_lock_irq(substream);
1559 switch (runtime->status->state) {
1560 case SNDRV_PCM_STATE_XRUN:
1561 result = 0; /* already there */
1562 break;
1563 case SNDRV_PCM_STATE_RUNNING:
1564 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1565 break;
1566 default:
1567 result = -EBADFD;
1568 }
1569 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return result;
1571}
1572
1573/*
1574 * reset ioctl
1575 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001576static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Takashi Iwai877211f2005-11-17 13:59:38 +01001578 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 switch (runtime->status->state) {
1580 case SNDRV_PCM_STATE_RUNNING:
1581 case SNDRV_PCM_STATE_PREPARED:
1582 case SNDRV_PCM_STATE_PAUSED:
1583 case SNDRV_PCM_STATE_SUSPENDED:
1584 return 0;
1585 default:
1586 return -EBADFD;
1587 }
1588}
1589
Takashi Iwai877211f2005-11-17 13:59:38 +01001590static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Takashi Iwai877211f2005-11-17 13:59:38 +01001592 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1594 if (err < 0)
1595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001597 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1598 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 runtime->silence_start = runtime->status->hw_ptr;
1600 runtime->silence_filled = 0;
1601 return 0;
1602}
1603
Takashi Iwai877211f2005-11-17 13:59:38 +01001604static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Takashi Iwai877211f2005-11-17 13:59:38 +01001606 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 runtime->control->appl_ptr = runtime->status->hw_ptr;
1608 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1609 runtime->silence_size > 0)
1610 snd_pcm_playback_silence(substream, ULONG_MAX);
1611}
1612
Julia Lawallb17154c2015-11-29 16:36:40 +01001613static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 .pre_action = snd_pcm_pre_reset,
1615 .do_action = snd_pcm_do_reset,
1616 .post_action = snd_pcm_post_reset
1617};
1618
Takashi Iwai877211f2005-11-17 13:59:38 +01001619static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1622}
1623
1624/*
1625 * prepare ioctl
1626 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001627/* we use the second argument for updating f_flags */
1628static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1629 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Takashi Iwai877211f2005-11-17 13:59:38 +01001631 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001632 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1633 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return -EBADFD;
1635 if (snd_pcm_running(substream))
1636 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001637 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return 0;
1639}
1640
Takashi Iwai877211f2005-11-17 13:59:38 +01001641static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 int err;
1644 err = substream->ops->prepare(substream);
1645 if (err < 0)
1646 return err;
1647 return snd_pcm_do_reset(substream, 0);
1648}
1649
Takashi Iwai877211f2005-11-17 13:59:38 +01001650static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Takashi Iwai877211f2005-11-17 13:59:38 +01001652 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001654 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Julia Lawallb17154c2015-11-29 16:36:40 +01001657static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 .pre_action = snd_pcm_pre_prepare,
1659 .do_action = snd_pcm_do_prepare,
1660 .post_action = snd_pcm_post_prepare
1661};
1662
1663/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001664 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001665 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001666 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001667 *
1668 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001670static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1671 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001673 int f_flags;
1674
1675 if (file)
1676 f_flags = file->f_flags;
1677 else
1678 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Takashi Iwai1b745cd2016-05-24 15:40:03 +02001680 snd_pcm_stream_lock_irq(substream);
1681 switch (substream->runtime->status->state) {
1682 case SNDRV_PCM_STATE_PAUSED:
1683 snd_pcm_pause(substream, 0);
1684 /* fallthru */
1685 case SNDRV_PCM_STATE_SUSPENDED:
1686 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1687 break;
1688 }
1689 snd_pcm_stream_unlock_irq(substream);
1690
Takashi Iwai68b4acd2016-05-24 15:07:39 +02001691 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1692 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695/*
1696 * drain ioctl
1697 */
1698
Takashi Iwai877211f2005-11-17 13:59:38 +01001699static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001701 struct snd_pcm_runtime *runtime = substream->runtime;
1702 switch (runtime->status->state) {
1703 case SNDRV_PCM_STATE_OPEN:
1704 case SNDRV_PCM_STATE_DISCONNECTED:
1705 case SNDRV_PCM_STATE_SUSPENDED:
1706 return -EBADFD;
1707 }
1708 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return 0;
1710}
1711
Takashi Iwai877211f2005-11-17 13:59:38 +01001712static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Takashi Iwai877211f2005-11-17 13:59:38 +01001714 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1716 switch (runtime->status->state) {
1717 case SNDRV_PCM_STATE_PREPARED:
1718 /* start playback stream if possible */
1719 if (! snd_pcm_playback_empty(substream)) {
1720 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1721 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001722 } else {
1723 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725 break;
1726 case SNDRV_PCM_STATE_RUNNING:
1727 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1728 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001729 case SNDRV_PCM_STATE_XRUN:
1730 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1731 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 default:
1733 break;
1734 }
1735 } else {
1736 /* stop running stream */
1737 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001738 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001740 snd_pcm_do_stop(substream, new_state);
1741 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 }
Libin Yang48d88292014-12-31 22:09:54 +08001744
1745 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1746 runtime->trigger_master == substream &&
1747 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1748 return substream->ops->trigger(substream,
1749 SNDRV_PCM_TRIGGER_DRAIN);
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
1752}
1753
Takashi Iwai877211f2005-11-17 13:59:38 +01001754static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756}
1757
Julia Lawallb17154c2015-11-29 16:36:40 +01001758static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 .pre_action = snd_pcm_pre_drain_init,
1760 .do_action = snd_pcm_do_drain_init,
1761 .post_action = snd_pcm_post_drain_init
1762};
1763
Takashi Iwai877211f2005-11-17 13:59:38 +01001764static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766/*
1767 * Drain the stream(s).
1768 * When the substream is linked, sync until the draining of all playback streams
1769 * is finished.
1770 * After this call, all streams are supposed to be either SETUP or DRAINING
1771 * (capture only) state.
1772 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001773static int snd_pcm_drain(struct snd_pcm_substream *substream,
1774 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Takashi Iwai877211f2005-11-17 13:59:38 +01001776 struct snd_card *card;
1777 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001778 struct snd_pcm_substream *s;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001779 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001781 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 card = substream->pcm->card;
1784 runtime = substream->runtime;
1785
1786 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1787 return -EBADFD;
1788
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001789 if (file) {
1790 if (file->f_flags & O_NONBLOCK)
1791 nonblock = 1;
1792 } else if (substream->f_flags & O_NONBLOCK)
1793 nonblock = 1;
1794
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001795 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001796 snd_pcm_stream_lock_irq(substream);
1797 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001798 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001799 snd_pcm_pause(substream, 0);
1800
1801 /* pre-start/stop - all running streams are changed to DRAINING state */
1802 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001803 if (result < 0)
1804 goto unlock;
1805 /* in non-blocking, we don't wait in ioctl but let caller poll */
1806 if (nonblock) {
1807 result = -EAGAIN;
1808 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 for (;;) {
1812 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001813 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (signal_pending(current)) {
1815 result = -ERESTARTSYS;
1816 break;
1817 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001818 /* find a substream to drain */
1819 to_check = NULL;
1820 snd_pcm_group_for_each_entry(s, substream) {
1821 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1822 continue;
1823 runtime = s->runtime;
1824 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1825 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001826 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001827 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001828 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001829 if (!to_check)
1830 break; /* all drained */
1831 init_waitqueue_entry(&wait, current);
1832 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001834 up_read(&snd_pcm_link_rwsem);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001835 if (runtime->no_period_wakeup)
1836 tout = MAX_SCHEDULE_TIMEOUT;
1837 else {
1838 tout = 10;
1839 if (runtime->rate) {
1840 long t = runtime->period_size * 2 / runtime->rate;
1841 tout = max(t, tout);
1842 }
1843 tout = msecs_to_jiffies(tout * 1000);
1844 }
1845 tout = schedule_timeout_interruptible(tout);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001846 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001848 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001849 if (card->shutdown) {
1850 result = -ENODEV;
1851 break;
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (tout == 0) {
1854 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1855 result = -ESTRPIPE;
1856 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001857 dev_dbg(substream->pcm->card->dev,
1858 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1860 result = -EIO;
1861 }
1862 break;
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001865
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001866 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001867 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001868 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 return result;
1871}
1872
1873/*
1874 * drop ioctl
1875 *
1876 * Immediately put all linked substreams into SETUP state.
1877 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001878static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Takashi Iwai877211f2005-11-17 13:59:38 +01001880 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int result = 0;
1882
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001883 if (PCM_RUNTIME_CHECK(substream))
1884 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001887 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai4b95ff72016-05-24 15:08:31 +02001888 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return -EBADFD;
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 snd_pcm_stream_lock_irq(substream);
1892 /* resume pause */
1893 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1894 snd_pcm_pause(substream, 0);
1895
1896 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1897 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1898 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return result;
1901}
1902
1903
Al Viro0888c322013-06-05 14:09:55 -04001904static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Al Viro0888c322013-06-05 14:09:55 -04001906 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001907 unsigned int minor;
1908
Al Viro0888c322013-06-05 14:09:55 -04001909 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1910 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001912 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1913 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
1916/*
1917 * PCM link handling
1918 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001919static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001922 struct snd_pcm_file *pcm_file;
1923 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001924 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001925 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Al Viro0888c322013-06-05 14:09:55 -04001927 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001929 if (!is_pcm_file(f.file)) {
1930 res = -EBADFD;
1931 goto _badf;
1932 }
1933 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001935 group = kmalloc(sizeof(*group), GFP_KERNEL);
1936 if (!group) {
1937 res = -ENOMEM;
1938 goto _nolock;
1939 }
Takashi Iwai67ec10722016-02-17 14:30:26 +01001940 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 write_lock_irq(&snd_pcm_link_rwlock);
1942 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001943 substream->runtime->status->state != substream1->runtime->status->state ||
1944 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 res = -EBADFD;
1946 goto _end;
1947 }
1948 if (snd_pcm_stream_linked(substream1)) {
1949 res = -EALREADY;
1950 goto _end;
1951 }
1952 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001953 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001954 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001956 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 INIT_LIST_HEAD(&substream->group->substreams);
1958 list_add_tail(&substream->link_list, &substream->group->substreams);
1959 substream->group->count = 1;
1960 }
1961 list_add_tail(&substream1->link_list, &substream->group->substreams);
1962 substream->group->count++;
1963 substream1->group = substream->group;
1964 _end:
1965 write_unlock_irq(&snd_pcm_link_rwlock);
1966 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001967 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001968 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001969 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001970 _badf:
1971 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return res;
1973}
1974
Takashi Iwai877211f2005-11-17 13:59:38 +01001975static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 substream->group = &substream->self_group;
1978 INIT_LIST_HEAD(&substream->self_group.substreams);
1979 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1980}
1981
Takashi Iwai877211f2005-11-17 13:59:38 +01001982static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Takashi Iwaief991b92007-02-22 12:52:53 +01001984 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 int res = 0;
1986
Takashi Iwai67ec10722016-02-17 14:30:26 +01001987 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 write_lock_irq(&snd_pcm_link_rwlock);
1989 if (!snd_pcm_stream_linked(substream)) {
1990 res = -EALREADY;
1991 goto _end;
1992 }
1993 list_del(&substream->link_list);
1994 substream->group->count--;
1995 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001996 snd_pcm_group_for_each_entry(s, substream) {
1997 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 break;
1999 }
2000 kfree(substream->group);
2001 }
2002 relink_to_local(substream);
2003 _end:
2004 write_unlock_irq(&snd_pcm_link_rwlock);
2005 up_write(&snd_pcm_link_rwsem);
2006 return res;
2007}
2008
2009/*
2010 * hw configurator
2011 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002012static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2013 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Takashi Iwai877211f2005-11-17 13:59:38 +01002015 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2017 hw_param_interval_c(params, rule->deps[1]), &t);
2018 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2019}
2020
Takashi Iwai877211f2005-11-17 13:59:38 +01002021static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2022 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Takashi Iwai877211f2005-11-17 13:59:38 +01002024 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2026 hw_param_interval_c(params, rule->deps[1]), &t);
2027 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2028}
2029
Takashi Iwai877211f2005-11-17 13:59:38 +01002030static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2031 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Takashi Iwai877211f2005-11-17 13:59:38 +01002033 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2035 hw_param_interval_c(params, rule->deps[1]),
2036 (unsigned long) rule->private, &t);
2037 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2038}
2039
Takashi Iwai877211f2005-11-17 13:59:38 +01002040static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2041 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Takashi Iwai877211f2005-11-17 13:59:38 +01002043 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2045 (unsigned long) rule->private,
2046 hw_param_interval_c(params, rule->deps[1]), &t);
2047 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2048}
2049
Takashi Iwai877211f2005-11-17 13:59:38 +01002050static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2051 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 unsigned int k;
Takashi Sakamotob55f9fd2017-05-17 08:48:20 +09002054 const struct snd_interval *i =
2055 hw_param_interval_c(params, rule->deps[0]);
Takashi Iwai877211f2005-11-17 13:59:38 +01002056 struct snd_mask m;
2057 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 snd_mask_any(&m);
2059 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2060 int bits;
2061 if (! snd_mask_test(mask, k))
2062 continue;
2063 bits = snd_pcm_format_physical_width(k);
2064 if (bits <= 0)
2065 continue; /* ignore invalid formats */
2066 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2067 snd_mask_reset(&m, k);
2068 }
2069 return snd_mask_refine(mask, &m);
2070}
2071
Takashi Iwai877211f2005-11-17 13:59:38 +01002072static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2073 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Takashi Iwai877211f2005-11-17 13:59:38 +01002075 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 unsigned int k;
2077 t.min = UINT_MAX;
2078 t.max = 0;
2079 t.openmin = 0;
2080 t.openmax = 0;
2081 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2082 int bits;
2083 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2084 continue;
2085 bits = snd_pcm_format_physical_width(k);
2086 if (bits <= 0)
2087 continue; /* ignore invalid formats */
2088 if (t.min > (unsigned)bits)
2089 t.min = bits;
2090 if (t.max < (unsigned)bits)
2091 t.max = bits;
2092 }
2093 t.integer = 1;
2094 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2095}
2096
2097#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2098#error "Change this table"
2099#endif
2100
Takashi Sakamoto8b674302017-05-17 08:48:17 +09002101static const unsigned int rates[] = {
2102 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2103 48000, 64000, 88200, 96000, 176400, 192000
2104};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Clemens Ladisch7653d552007-08-13 17:38:54 +02002106const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2107 .count = ARRAY_SIZE(rates),
2108 .list = rates,
2109};
2110
Takashi Iwai877211f2005-11-17 13:59:38 +01002111static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2112 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Takashi Iwai877211f2005-11-17 13:59:38 +01002114 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002116 snd_pcm_known_rates.count,
2117 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Takashi Iwai877211f2005-11-17 13:59:38 +01002120static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2121 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Takashi Iwai877211f2005-11-17 13:59:38 +01002123 struct snd_interval t;
2124 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 t.min = 0;
2126 t.max = substream->buffer_bytes_max;
2127 t.openmin = 0;
2128 t.openmax = 0;
2129 t.integer = 1;
2130 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2131}
2132
Takashi Iwai877211f2005-11-17 13:59:38 +01002133int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Takashi Iwai877211f2005-11-17 13:59:38 +01002135 struct snd_pcm_runtime *runtime = substream->runtime;
2136 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 int k, err;
2138
2139 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2140 snd_mask_any(constrs_mask(constrs, k));
2141 }
2142
2143 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2144 snd_interval_any(constrs_interval(constrs, k));
2145 }
2146
2147 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2148 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2149 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2150 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2151 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2152
2153 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2154 snd_pcm_hw_rule_format, NULL,
2155 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2156 if (err < 0)
2157 return err;
2158 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2159 snd_pcm_hw_rule_sample_bits, NULL,
2160 SNDRV_PCM_HW_PARAM_FORMAT,
2161 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2162 if (err < 0)
2163 return err;
2164 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2165 snd_pcm_hw_rule_div, NULL,
2166 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2167 if (err < 0)
2168 return err;
2169 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2170 snd_pcm_hw_rule_mul, NULL,
2171 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2172 if (err < 0)
2173 return err;
2174 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2175 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2176 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2177 if (err < 0)
2178 return err;
2179 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2180 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2181 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2182 if (err < 0)
2183 return err;
2184 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2185 snd_pcm_hw_rule_div, NULL,
2186 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2187 if (err < 0)
2188 return err;
2189 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2190 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2191 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2192 if (err < 0)
2193 return err;
2194 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2195 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2196 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2197 if (err < 0)
2198 return err;
2199 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2200 snd_pcm_hw_rule_div, NULL,
2201 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2202 if (err < 0)
2203 return err;
2204 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2205 snd_pcm_hw_rule_div, NULL,
2206 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2207 if (err < 0)
2208 return err;
2209 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2210 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2211 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2212 if (err < 0)
2213 return err;
2214 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2215 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2216 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2217 if (err < 0)
2218 return err;
2219 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2220 snd_pcm_hw_rule_mul, NULL,
2221 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2222 if (err < 0)
2223 return err;
2224 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2225 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2226 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2227 if (err < 0)
2228 return err;
2229 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2230 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2231 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2232 if (err < 0)
2233 return err;
2234 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2235 snd_pcm_hw_rule_muldivk, (void*) 8,
2236 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2237 if (err < 0)
2238 return err;
2239 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2240 snd_pcm_hw_rule_muldivk, (void*) 8,
2241 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2242 if (err < 0)
2243 return err;
2244 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2245 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2246 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2247 if (err < 0)
2248 return err;
2249 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2250 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2251 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2252 if (err < 0)
2253 return err;
2254 return 0;
2255}
2256
Takashi Iwai877211f2005-11-17 13:59:38 +01002257int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Takashi Iwai877211f2005-11-17 13:59:38 +01002259 struct snd_pcm_runtime *runtime = substream->runtime;
2260 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 int err;
2262 unsigned int mask = 0;
2263
2264 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2265 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2266 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2267 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002268 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2270 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2271 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2272 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2273 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2274 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2275 }
2276 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002277 if (err < 0)
2278 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002281 if (err < 0)
2282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002285 if (err < 0)
2286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2289 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002290 if (err < 0)
2291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2294 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002295 if (err < 0)
2296 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2299 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002300 if (err < 0)
2301 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2304 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002305 if (err < 0)
2306 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2309 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002310 if (err < 0)
2311 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2314 snd_pcm_hw_rule_buffer_bytes_max, substream,
2315 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2316 if (err < 0)
2317 return err;
2318
2319 /* FIXME: remove */
2320 if (runtime->dma_bytes) {
2321 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002322 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302323 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325
2326 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2327 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2328 snd_pcm_hw_rule_rate, hw,
2329 SNDRV_PCM_HW_PARAM_RATE, -1);
2330 if (err < 0)
2331 return err;
2332 }
2333
2334 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2336
2337 return 0;
2338}
2339
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002340static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002343}
2344
2345void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2346{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002347 substream->ref_count--;
2348 if (substream->ref_count > 0)
2349 return;
2350
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002351 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002352 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002353 if (substream->ops->hw_free &&
2354 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 substream->ops->hw_free(substream);
2356 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002357 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002359 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2360 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002361 if (substream->pcm_release) {
2362 substream->pcm_release(substream);
2363 substream->pcm_release = NULL;
2364 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002365 snd_pcm_detach_substream(substream);
2366}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002367EXPORT_SYMBOL(snd_pcm_release_substream);
2368
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002369int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2370 struct file *file,
2371 struct snd_pcm_substream **rsubstream)
2372{
2373 struct snd_pcm_substream *substream;
2374 int err;
2375
2376 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2377 if (err < 0)
2378 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002379 if (substream->ref_count > 1) {
2380 *rsubstream = substream;
2381 return 0;
2382 }
2383
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002384 err = snd_pcm_hw_constraints_init(substream);
2385 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002386 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002387 goto error;
2388 }
2389
2390 if ((err = substream->ops->open(substream)) < 0)
2391 goto error;
2392
2393 substream->hw_opened = 1;
2394
2395 err = snd_pcm_hw_constraints_complete(substream);
2396 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002397 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002398 goto error;
2399 }
2400
2401 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002403
2404 error:
2405 snd_pcm_release_substream(substream);
2406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002408EXPORT_SYMBOL(snd_pcm_open_substream);
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002411 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002412 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Takashi Iwai877211f2005-11-17 13:59:38 +01002414 struct snd_pcm_file *pcm_file;
2415 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002416 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002418 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2419 if (err < 0)
2420 return err;
2421
Takashi Iwai548a6482006-07-31 16:51:51 +02002422 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2423 if (pcm_file == NULL) {
2424 snd_pcm_release_substream(substream);
2425 return -ENOMEM;
2426 }
2427 pcm_file->substream = substream;
2428 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002429 substream->file = pcm_file;
2430 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return 0;
2435}
2436
Clemens Ladischf87135f2005-11-20 14:06:59 +01002437static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Takashi Iwai877211f2005-11-17 13:59:38 +01002439 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002440 int err = nonseekable_open(inode, file);
2441 if (err < 0)
2442 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002443 pcm = snd_lookup_minor_data(iminor(inode),
2444 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002445 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002446 if (pcm)
2447 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002448 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002449}
2450
2451static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2452{
2453 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002454 int err = nonseekable_open(inode, file);
2455 if (err < 0)
2456 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002457 pcm = snd_lookup_minor_data(iminor(inode),
2458 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002459 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002460 if (pcm)
2461 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002462 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002463}
2464
2465static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2466{
2467 int err;
Ingo Molnarac6424b2017-06-20 12:06:13 +02002468 wait_queue_entry_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (pcm == NULL) {
2471 err = -ENODEV;
2472 goto __error1;
2473 }
2474 err = snd_card_file_add(pcm->card, file);
2475 if (err < 0)
2476 goto __error1;
2477 if (!try_module_get(pcm->card->module)) {
2478 err = -EFAULT;
2479 goto __error2;
2480 }
2481 init_waitqueue_entry(&wait, current);
2482 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002483 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002485 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (err >= 0)
2487 break;
2488 if (err == -EAGAIN) {
2489 if (file->f_flags & O_NONBLOCK) {
2490 err = -EBUSY;
2491 break;
2492 }
2493 } else
2494 break;
2495 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002496 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002498 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002499 if (pcm->card->shutdown) {
2500 err = -ENODEV;
2501 break;
2502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (signal_pending(current)) {
2504 err = -ERESTARTSYS;
2505 break;
2506 }
2507 }
2508 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002509 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (err < 0)
2511 goto __error;
2512 return err;
2513
2514 __error:
2515 module_put(pcm->card->module);
2516 __error2:
2517 snd_card_file_remove(pcm->card, file);
2518 __error1:
2519 return err;
2520}
2521
2522static int snd_pcm_release(struct inode *inode, struct file *file)
2523{
Takashi Iwai877211f2005-11-17 13:59:38 +01002524 struct snd_pcm *pcm;
2525 struct snd_pcm_substream *substream;
2526 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 pcm_file = file->private_data;
2529 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002530 if (snd_BUG_ON(!substream))
2531 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002533 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002534 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002535 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002536 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 wake_up(&pcm->open_wait);
2538 module_put(pcm->card->module);
2539 snd_card_file_remove(pcm->card, file);
2540 return 0;
2541}
2542
Takashi Iwaif839cc12017-05-19 20:04:23 +02002543/* check and update PCM state; return 0 or a negative error
2544 * call this inside PCM lock
2545 */
2546static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2547{
2548 switch (substream->runtime->status->state) {
2549 case SNDRV_PCM_STATE_DRAINING:
2550 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2551 return -EBADFD;
2552 /* Fall through */
2553 case SNDRV_PCM_STATE_RUNNING:
2554 return snd_pcm_update_hw_ptr(substream);
2555 case SNDRV_PCM_STATE_PREPARED:
2556 case SNDRV_PCM_STATE_PAUSED:
2557 return 0;
2558 case SNDRV_PCM_STATE_SUSPENDED:
2559 return -ESTRPIPE;
2560 case SNDRV_PCM_STATE_XRUN:
2561 return -EPIPE;
2562 default:
2563 return -EBADFD;
2564 }
2565}
2566
Takashi Iwai9027c462017-05-19 20:22:33 +02002567/* increase the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002568static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2569 snd_pcm_uframes_t frames,
2570 snd_pcm_sframes_t avail)
2571{
2572 struct snd_pcm_runtime *runtime = substream->runtime;
2573 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002574 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002575
2576 if (avail <= 0)
2577 return 0;
2578 if (frames > (snd_pcm_uframes_t)avail)
2579 frames = avail;
2580 appl_ptr = runtime->control->appl_ptr + frames;
2581 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2582 appl_ptr -= runtime->boundary;
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002583 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
Takashi Iwai9027c462017-05-19 20:22:33 +02002584 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002585}
2586
Takashi Iwaifb51f1c2018-01-03 15:16:30 +01002587/* decrease the appl_ptr; returns the processed frames or zero for error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002588static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2589 snd_pcm_uframes_t frames,
2590 snd_pcm_sframes_t avail)
2591{
2592 struct snd_pcm_runtime *runtime = substream->runtime;
2593 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002594 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002595
2596 if (avail <= 0)
2597 return 0;
2598 if (frames > (snd_pcm_uframes_t)avail)
2599 frames = avail;
2600 appl_ptr = runtime->control->appl_ptr - frames;
2601 if (appl_ptr < 0)
2602 appl_ptr += runtime->boundary;
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002603 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
Takashi Iwaifb51f1c2018-01-03 15:16:30 +01002604 /* NOTE: we return zero for errors because PulseAudio gets depressed
2605 * upon receiving an error from rewind ioctl and stops processing
2606 * any longer. Returning zero means that no rewind is done, so
2607 * it's not absolutely wrong to answer like that.
2608 */
2609 return ret < 0 ? 0 : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002610}
2611
Takashi Iwai763e5062018-04-11 17:56:52 +02002612static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2613 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 if (frames == 0)
2618 return 0;
2619
2620 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002621 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002622 if (!ret)
2623 ret = rewind_appl_ptr(substream, frames,
Takashi Iwai763e5062018-04-11 17:56:52 +02002624 snd_pcm_hw_avail(substream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 snd_pcm_stream_unlock_irq(substream);
2626 return ret;
2627}
2628
Takashi Iwai763e5062018-04-11 17:56:52 +02002629static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2630 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 if (frames == 0)
2635 return 0;
2636
2637 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002638 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002639 if (!ret)
2640 ret = forward_appl_ptr(substream, frames,
Takashi Iwai763e5062018-04-11 17:56:52 +02002641 snd_pcm_avail(substream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 snd_pcm_stream_unlock_irq(substream);
2643 return ret;
2644}
2645
Takashi Iwai877211f2005-11-17 13:59:38 +01002646static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 int err;
2649
2650 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002651 err = do_pcm_hwsync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 snd_pcm_stream_unlock_irq(substream);
2653 return err;
2654}
2655
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002656static snd_pcm_sframes_t snd_pcm_delay(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Takashi Iwai877211f2005-11-17 13:59:38 +01002658 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 int err;
2660 snd_pcm_sframes_t n = 0;
2661
2662 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002663 err = do_pcm_hwsync(substream);
2664 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2666 n = snd_pcm_playback_hw_avail(runtime);
2667 else
2668 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002669 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
2671 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002672 return err < 0 ? err : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
Takashi Iwai877211f2005-11-17 13:59:38 +01002675static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2676 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Takashi Iwai877211f2005-11-17 13:59:38 +01002678 struct snd_pcm_runtime *runtime = substream->runtime;
2679 struct snd_pcm_sync_ptr sync_ptr;
2680 volatile struct snd_pcm_mmap_status *status;
2681 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 int err;
2683
2684 memset(&sync_ptr, 0, sizeof(sync_ptr));
2685 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2686 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002687 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 -07002688 return -EFAULT;
2689 status = runtime->status;
2690 control = runtime->control;
2691 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2692 err = snd_pcm_hwsync(substream);
2693 if (err < 0)
2694 return err;
2695 }
2696 snd_pcm_stream_lock_irq(substream);
Takashi Iwai9027c462017-05-19 20:22:33 +02002697 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
Takashi Sakamoto66e01a52017-06-12 09:41:44 +09002698 err = pcm_lib_apply_appl_ptr(substream,
2699 sync_ptr.c.control.appl_ptr);
Takashi Iwai9027c462017-05-19 20:22:33 +02002700 if (err < 0) {
2701 snd_pcm_stream_unlock_irq(substream);
2702 return err;
2703 }
2704 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 sync_ptr.c.control.appl_ptr = control->appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2708 control->avail_min = sync_ptr.c.control.avail_min;
2709 else
2710 sync_ptr.c.control.avail_min = control->avail_min;
2711 sync_ptr.s.status.state = status->state;
2712 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2713 sync_ptr.s.status.tstamp = status->tstamp;
2714 sync_ptr.s.status.suspended_state = status->suspended_state;
2715 snd_pcm_stream_unlock_irq(substream);
2716 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2717 return -EFAULT;
2718 return 0;
2719}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002720
2721static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2722{
2723 struct snd_pcm_runtime *runtime = substream->runtime;
2724 int arg;
2725
2726 if (get_user(arg, _arg))
2727 return -EFAULT;
2728 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2729 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002730 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002731 return 0;
2732}
Takashi Iwai67616fe2017-08-30 15:39:32 +02002733
2734static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2735 struct snd_xferi __user *_xferi)
2736{
2737 struct snd_xferi xferi;
2738 struct snd_pcm_runtime *runtime = substream->runtime;
2739 snd_pcm_sframes_t result;
2740
2741 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2742 return -EBADFD;
2743 if (put_user(0, &_xferi->result))
2744 return -EFAULT;
2745 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2746 return -EFAULT;
2747 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2748 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2749 else
2750 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2751 __put_user(result, &_xferi->result);
2752 return result < 0 ? result : 0;
2753}
2754
2755static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2756 struct snd_xfern __user *_xfern)
2757{
2758 struct snd_xfern xfern;
2759 struct snd_pcm_runtime *runtime = substream->runtime;
2760 void *bufs;
2761 snd_pcm_sframes_t result;
2762
2763 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2764 return -EBADFD;
2765 if (runtime->channels > 128)
2766 return -EINVAL;
2767 if (put_user(0, &_xfern->result))
2768 return -EFAULT;
2769 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2770 return -EFAULT;
2771
2772 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2773 if (IS_ERR(bufs))
2774 return PTR_ERR(bufs);
2775 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2776 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2777 else
2778 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2779 kfree(bufs);
2780 __put_user(result, &_xfern->result);
2781 return result < 0 ? result : 0;
2782}
2783
2784static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2785 snd_pcm_uframes_t __user *_frames)
2786{
2787 snd_pcm_uframes_t frames;
2788 snd_pcm_sframes_t result;
2789
2790 if (get_user(frames, _frames))
2791 return -EFAULT;
2792 if (put_user(0, _frames))
2793 return -EFAULT;
Takashi Iwai763e5062018-04-11 17:56:52 +02002794 result = snd_pcm_rewind(substream, frames);
Takashi Iwai67616fe2017-08-30 15:39:32 +02002795 __put_user(result, _frames);
2796 return result < 0 ? result : 0;
2797}
2798
2799static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2800 snd_pcm_uframes_t __user *_frames)
2801{
2802 snd_pcm_uframes_t frames;
2803 snd_pcm_sframes_t result;
2804
2805 if (get_user(frames, _frames))
2806 return -EFAULT;
2807 if (put_user(0, _frames))
2808 return -EFAULT;
Takashi Iwai763e5062018-04-11 17:56:52 +02002809 result = snd_pcm_forward(substream, frames);
Takashi Iwai67616fe2017-08-30 15:39:32 +02002810 __put_user(result, _frames);
2811 return result < 0 ? result : 0;
2812}
2813
2814static int snd_pcm_common_ioctl(struct file *file,
Takashi Iwai0df63e42006-04-28 15:13:41 +02002815 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 unsigned int cmd, void __user *arg)
2817{
Takashi Iwai4b671f52017-06-19 23:11:54 +02002818 struct snd_pcm_file *pcm_file = file->private_data;
Takashi Iwai7d8e8292017-08-30 16:13:25 +02002819 int res;
2820
Takashi Iwai67616fe2017-08-30 15:39:32 +02002821 if (PCM_RUNTIME_CHECK(substream))
2822 return -ENXIO;
2823
Takashi Iwai7d8e8292017-08-30 16:13:25 +02002824 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2825 if (res < 0)
2826 return res;
Takashi Iwai4b671f52017-06-19 23:11:54 +02002827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 switch (cmd) {
2829 case SNDRV_PCM_IOCTL_PVERSION:
2830 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2831 case SNDRV_PCM_IOCTL_INFO:
2832 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002833 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2834 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002835 case SNDRV_PCM_IOCTL_TTSTAMP:
2836 return snd_pcm_tstamp(substream, arg);
Takashi Iwai4b671f52017-06-19 23:11:54 +02002837 case SNDRV_PCM_IOCTL_USER_PVERSION:
2838 if (get_user(pcm_file->user_pversion,
2839 (unsigned int __user *)arg))
2840 return -EFAULT;
2841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 case SNDRV_PCM_IOCTL_HW_REFINE:
2843 return snd_pcm_hw_refine_user(substream, arg);
2844 case SNDRV_PCM_IOCTL_HW_PARAMS:
2845 return snd_pcm_hw_params_user(substream, arg);
2846 case SNDRV_PCM_IOCTL_HW_FREE:
2847 return snd_pcm_hw_free(substream);
2848 case SNDRV_PCM_IOCTL_SW_PARAMS:
2849 return snd_pcm_sw_params_user(substream, arg);
2850 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002851 return snd_pcm_status_user(substream, arg, false);
2852 case SNDRV_PCM_IOCTL_STATUS_EXT:
2853 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2855 return snd_pcm_channel_info_user(substream, arg);
2856 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002857 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 case SNDRV_PCM_IOCTL_RESET:
2859 return snd_pcm_reset(substream);
2860 case SNDRV_PCM_IOCTL_START:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002861 return snd_pcm_start_lock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 case SNDRV_PCM_IOCTL_LINK:
2863 return snd_pcm_link(substream, (int)(unsigned long) arg);
2864 case SNDRV_PCM_IOCTL_UNLINK:
2865 return snd_pcm_unlink(substream);
2866 case SNDRV_PCM_IOCTL_RESUME:
2867 return snd_pcm_resume(substream);
2868 case SNDRV_PCM_IOCTL_XRUN:
2869 return snd_pcm_xrun(substream);
2870 case SNDRV_PCM_IOCTL_HWSYNC:
2871 return snd_pcm_hwsync(substream);
2872 case SNDRV_PCM_IOCTL_DELAY:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002873 {
2874 snd_pcm_sframes_t delay = snd_pcm_delay(substream);
2875 snd_pcm_sframes_t __user *res = arg;
2876
2877 if (delay < 0)
2878 return delay;
2879 if (put_user(delay, res))
2880 return -EFAULT;
2881 return 0;
2882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 case SNDRV_PCM_IOCTL_SYNC_PTR:
2884 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002885#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2887 return snd_pcm_hw_refine_old_user(substream, arg);
2888 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2889 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002890#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002892 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 case SNDRV_PCM_IOCTL_DROP:
2894 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002895 case SNDRV_PCM_IOCTL_PAUSE:
Takashi Iwai34bcc442016-05-24 14:58:04 +02002896 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
2897 substream,
2898 (int)(unsigned long)arg);
Takashi Iwai67616fe2017-08-30 15:39:32 +02002899 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2900 case SNDRV_PCM_IOCTL_READI_FRAMES:
2901 return snd_pcm_xferi_frames_ioctl(substream, arg);
2902 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2903 case SNDRV_PCM_IOCTL_READN_FRAMES:
2904 return snd_pcm_xfern_frames_ioctl(substream, arg);
2905 case SNDRV_PCM_IOCTL_REWIND:
2906 return snd_pcm_rewind_ioctl(substream, arg);
2907 case SNDRV_PCM_IOCTL_FORWARD:
2908 return snd_pcm_forward_ioctl(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002910 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 return -ENOTTY;
2912}
2913
Takashi Iwai67616fe2017-08-30 15:39:32 +02002914static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
2915 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Takashi Iwai877211f2005-11-17 13:59:38 +01002917 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 pcm_file = file->private_data;
2920
2921 if (((cmd >> 8) & 0xff) != 'A')
2922 return -ENOTTY;
2923
Takashi Iwai67616fe2017-08-30 15:39:32 +02002924 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
2925 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002928/**
2929 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
2930 * @substream: PCM substream
2931 * @cmd: IOCTL cmd
2932 * @arg: IOCTL argument
2933 *
2934 * The function is provided primarily for OSS layer and USB gadget drivers,
2935 * and it allows only the limited set of ioctls (hw_params, sw_params,
2936 * prepare, start, drain, drop, forward).
2937 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002938int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 unsigned int cmd, void *arg)
2940{
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002941 snd_pcm_uframes_t *frames = arg;
2942 snd_pcm_sframes_t result;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002943
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002944 switch (cmd) {
2945 case SNDRV_PCM_IOCTL_FORWARD:
2946 {
2947 /* provided only for OSS; capture-only and no value returned */
2948 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
2949 return -EINVAL;
Takashi Iwai763e5062018-04-11 17:56:52 +02002950 result = snd_pcm_forward(substream, *frames);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002951 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002953 case SNDRV_PCM_IOCTL_HW_PARAMS:
2954 return snd_pcm_hw_params(substream, arg);
2955 case SNDRV_PCM_IOCTL_SW_PARAMS:
2956 return snd_pcm_sw_params(substream, arg);
2957 case SNDRV_PCM_IOCTL_PREPARE:
2958 return snd_pcm_prepare(substream, NULL);
2959 case SNDRV_PCM_IOCTL_START:
2960 return snd_pcm_start_lock_irq(substream);
2961 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai7d8e8292017-08-30 16:13:25 +02002962 return snd_pcm_drain(substream, NULL);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002963 case SNDRV_PCM_IOCTL_DROP:
2964 return snd_pcm_drop(substream);
2965 case SNDRV_PCM_IOCTL_DELAY:
2966 {
2967 result = snd_pcm_delay(substream);
2968 if (result < 0)
2969 return result;
2970 *frames = result;
2971 return 0;
2972 }
2973 default:
2974 return -EINVAL;
2975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002977EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2978
Takashi Iwai877211f2005-11-17 13:59:38 +01002979static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2980 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Takashi Iwai877211f2005-11-17 13:59:38 +01002982 struct snd_pcm_file *pcm_file;
2983 struct snd_pcm_substream *substream;
2984 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 snd_pcm_sframes_t result;
2986
2987 pcm_file = file->private_data;
2988 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002989 if (PCM_RUNTIME_CHECK(substream))
2990 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 runtime = substream->runtime;
2992 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2993 return -EBADFD;
2994 if (!frame_aligned(runtime, count))
2995 return -EINVAL;
2996 count = bytes_to_frames(runtime, count);
2997 result = snd_pcm_lib_read(substream, buf, count);
2998 if (result > 0)
2999 result = frames_to_bytes(runtime, result);
3000 return result;
3001}
3002
Takashi Iwai877211f2005-11-17 13:59:38 +01003003static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3004 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Takashi Iwai877211f2005-11-17 13:59:38 +01003006 struct snd_pcm_file *pcm_file;
3007 struct snd_pcm_substream *substream;
3008 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 snd_pcm_sframes_t result;
3010
3011 pcm_file = file->private_data;
3012 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003013 if (PCM_RUNTIME_CHECK(substream))
3014 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003016 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3017 return -EBADFD;
3018 if (!frame_aligned(runtime, count))
3019 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 count = bytes_to_frames(runtime, count);
3021 result = snd_pcm_lib_write(substream, buf, count);
3022 if (result > 0)
3023 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return result;
3025}
3026
Al Viro1c65d982015-04-04 00:19:32 -04003027static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Takashi Iwai877211f2005-11-17 13:59:38 +01003029 struct snd_pcm_file *pcm_file;
3030 struct snd_pcm_substream *substream;
3031 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 snd_pcm_sframes_t result;
3033 unsigned long i;
3034 void __user **bufs;
3035 snd_pcm_uframes_t frames;
3036
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003037 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003039 if (PCM_RUNTIME_CHECK(substream))
3040 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 runtime = substream->runtime;
3042 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3043 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003044 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003046 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003048 if (!frame_aligned(runtime, to->iov->iov_len))
3049 return -EINVAL;
3050 frames = bytes_to_samples(runtime, to->iov->iov_len);
3051 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 if (bufs == NULL)
3053 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003054 for (i = 0; i < to->nr_segs; ++i)
3055 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 result = snd_pcm_lib_readv(substream, bufs, frames);
3057 if (result > 0)
3058 result = frames_to_bytes(runtime, result);
3059 kfree(bufs);
3060 return result;
3061}
3062
Al Viro1c65d982015-04-04 00:19:32 -04003063static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Takashi Iwai877211f2005-11-17 13:59:38 +01003065 struct snd_pcm_file *pcm_file;
3066 struct snd_pcm_substream *substream;
3067 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 snd_pcm_sframes_t result;
3069 unsigned long i;
3070 void __user **bufs;
3071 snd_pcm_uframes_t frames;
3072
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003073 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003075 if (PCM_RUNTIME_CHECK(substream))
3076 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003078 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3079 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003080 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003081 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003082 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3083 !frame_aligned(runtime, from->iov->iov_len))
3084 return -EINVAL;
3085 frames = bytes_to_samples(runtime, from->iov->iov_len);
3086 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (bufs == NULL)
3088 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003089 for (i = 0; i < from->nr_segs; ++i)
3090 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 result = snd_pcm_lib_writev(substream, bufs, frames);
3092 if (result > 0)
3093 result = frames_to_bytes(runtime, result);
3094 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return result;
3096}
3097
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003098static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Takashi Iwai877211f2005-11-17 13:59:38 +01003100 struct snd_pcm_file *pcm_file;
3101 struct snd_pcm_substream *substream;
3102 struct snd_pcm_runtime *runtime;
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003103 __poll_t mask, ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 snd_pcm_uframes_t avail;
3105
3106 pcm_file = file->private_data;
3107
3108 substream = pcm_file->substream;
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003109 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3110 ok = EPOLLOUT | EPOLLWRNORM;
3111 else
3112 ok = EPOLLIN | EPOLLRDNORM;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003113 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003114 return ok | EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003116 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 poll_wait(file, &runtime->sleep, wait);
3118
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003119 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 snd_pcm_stream_lock_irq(substream);
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003121 avail = snd_pcm_avail(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 switch (runtime->status->state) {
3123 case SNDRV_PCM_STATE_RUNNING:
3124 case SNDRV_PCM_STATE_PREPARED:
3125 case SNDRV_PCM_STATE_PAUSED:
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003126 if (avail >= runtime->control->avail_min)
3127 mask = ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 break;
3129 case SNDRV_PCM_STATE_DRAINING:
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003130 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3131 mask = ok;
3132 if (!avail)
3133 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003135 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 default:
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003137 mask = ok | EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 break;
3139 }
3140 snd_pcm_stream_unlock_irq(substream);
3141 return mask;
3142}
3143
3144/*
3145 * mmap support
3146 */
3147
3148/*
3149 * Only on coherent architectures, we can mmap the status and the control records
3150 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3151 */
3152#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3153/*
3154 * mmap status record
3155 */
Dave Jiang11bac802017-02-24 14:56:41 -08003156static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Dave Jiang11bac802017-02-24 14:56:41 -08003158 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003159 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003162 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003164 vmf->page = virt_to_page(runtime->status);
3165 get_page(vmf->page);
3166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003169static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003171 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172};
3173
Takashi Iwai877211f2005-11-17 13:59:38 +01003174static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 struct vm_area_struct *area)
3176{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 long size;
3178 if (!(area->vm_flags & VM_READ))
3179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003181 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return -EINVAL;
3183 area->vm_ops = &snd_pcm_vm_ops_status;
3184 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003185 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 return 0;
3187}
3188
3189/*
3190 * mmap control record
3191 */
Dave Jiang11bac802017-02-24 14:56:41 -08003192static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
Dave Jiang11bac802017-02-24 14:56:41 -08003194 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003195 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003198 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003200 vmf->page = virt_to_page(runtime->control);
3201 get_page(vmf->page);
3202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003205static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003207 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208};
3209
Takashi Iwai877211f2005-11-17 13:59:38 +01003210static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 struct vm_area_struct *area)
3212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 long size;
3214 if (!(area->vm_flags & VM_READ))
3215 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003217 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return -EINVAL;
3219 area->vm_ops = &snd_pcm_vm_ops_control;
3220 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003221 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return 0;
3223}
Takashi Iwai42f94592017-06-19 22:39:18 +02003224
3225static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3226{
3227 if (pcm_file->no_compat_mmap)
3228 return false;
Takashi Iwaib602aa82017-06-27 11:54:37 +02003229 /* See pcm_control_mmap_allowed() below.
3230 * Since older alsa-lib requires both status and control mmaps to be
3231 * coupled, we have to disable the status mmap for old alsa-lib, too.
3232 */
3233 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3234 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3235 return false;
3236 return true;
3237}
3238
3239static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3240{
3241 if (pcm_file->no_compat_mmap)
3242 return false;
3243 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
Takashi Iwai42f94592017-06-19 22:39:18 +02003244 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3245 * thus it effectively assures the manual update of appl_ptr.
Takashi Iwai42f94592017-06-19 22:39:18 +02003246 */
3247 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3248 return false;
3249 return true;
3250}
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252#else /* ! coherent mmap */
3253/*
3254 * don't support mmap for status and control records.
3255 */
Takashi Iwai42f94592017-06-19 22:39:18 +02003256#define pcm_status_mmap_allowed(pcm_file) false
Takashi Iwaib602aa82017-06-27 11:54:37 +02003257#define pcm_control_mmap_allowed(pcm_file) false
Takashi Iwai42f94592017-06-19 22:39:18 +02003258
Takashi Iwai877211f2005-11-17 13:59:38 +01003259static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 struct vm_area_struct *area)
3261{
3262 return -ENXIO;
3263}
Takashi Iwai877211f2005-11-17 13:59:38 +01003264static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct vm_area_struct *area)
3266{
3267 return -ENXIO;
3268}
3269#endif /* coherent mmap */
3270
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003271static inline struct page *
3272snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3273{
3274 void *vaddr = substream->runtime->dma_area + ofs;
3275 return virt_to_page(vaddr);
3276}
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003279 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 */
Dave Jiang11bac802017-02-24 14:56:41 -08003281static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Dave Jiang11bac802017-02-24 14:56:41 -08003283 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003284 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 unsigned long offset;
3286 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 size_t dma_bytes;
3288
3289 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003290 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003292 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3294 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003295 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003296 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003298 else
3299 page = snd_pcm_default_page_ops(substream, offset);
3300 if (!page)
3301 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003302 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003303 vmf->page = page;
3304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305}
3306
Takashi Iwai657b1982009-11-26 12:40:21 +01003307static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3308 .open = snd_pcm_mmap_data_open,
3309 .close = snd_pcm_mmap_data_close,
3310};
3311
3312static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 .open = snd_pcm_mmap_data_open,
3314 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003315 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316};
3317
3318/*
3319 * mmap the DMA buffer on RAM
3320 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003321
3322/**
3323 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3324 * @substream: PCM substream
3325 * @area: VMA
3326 *
3327 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3328 * this function is invoked implicitly.
3329 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003330int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3331 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003333 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003334#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003335 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3336 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3337 return remap_pfn_range(area, area->vm_start,
3338 substream->dma_buffer.addr >> PAGE_SHIFT,
3339 area->vm_end - area->vm_start, area->vm_page_prot);
3340 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003341#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003342#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Geert Uytterhoevenabe594c2017-07-09 21:31:02 +02003343 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
Takashi Iwai657b1982009-11-26 12:40:21 +01003344 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3345 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3346 area,
3347 substream->runtime->dma_area,
3348 substream->runtime->dma_addr,
Stefan Roese9066ae72018-03-26 16:10:21 +02003349 substream->runtime->dma_bytes);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003350#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003351 /* mmap with fault handler */
3352 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return 0;
3354}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003355EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357/*
3358 * mmap the DMA buffer on I/O memory area
3359 */
3360#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003361/**
3362 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3363 * @substream: PCM substream
3364 * @area: VMA
3365 *
3366 * When your hardware uses the iomapped pages as the hardware buffer and
3367 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3368 * is supposed to work only on limited architectures.
3369 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003370int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3371 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Luis de Bethencourt3c7f6912018-01-16 13:26:26 +00003373 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003376 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003378EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379#endif /* SNDRV_PCM_INFO_MMAP */
3380
3381/*
3382 * mmap DMA buffer
3383 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003384int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 struct vm_area_struct *area)
3386{
Takashi Iwai877211f2005-11-17 13:59:38 +01003387 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 long size;
3389 unsigned long offset;
3390 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003391 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3394 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3395 return -EINVAL;
3396 } else {
3397 if (!(area->vm_flags & VM_READ))
3398 return -EINVAL;
3399 }
3400 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3402 return -EBADFD;
3403 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3404 return -ENXIO;
3405 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3406 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3407 return -EINVAL;
3408 size = area->vm_end - area->vm_start;
3409 offset = area->vm_pgoff << PAGE_SHIFT;
3410 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3411 if ((size_t)size > dma_bytes)
3412 return -EINVAL;
3413 if (offset > dma_bytes - size)
3414 return -EINVAL;
3415
Takashi Iwai657b1982009-11-26 12:40:21 +01003416 area->vm_ops = &snd_pcm_vm_ops_data;
3417 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003419 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003421 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003422 if (!err)
3423 atomic_inc(&substream->mmap_count);
3424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003426EXPORT_SYMBOL(snd_pcm_mmap_data);
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3429{
Takashi Iwai877211f2005-11-17 13:59:38 +01003430 struct snd_pcm_file * pcm_file;
3431 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 unsigned long offset;
3433
3434 pcm_file = file->private_data;
3435 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003436 if (PCM_RUNTIME_CHECK(substream))
3437 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 offset = area->vm_pgoff << PAGE_SHIFT;
3440 switch (offset) {
3441 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai42f94592017-06-19 22:39:18 +02003442 if (!pcm_status_mmap_allowed(pcm_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return -ENXIO;
3444 return snd_pcm_mmap_status(substream, file, area);
3445 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwaib602aa82017-06-27 11:54:37 +02003446 if (!pcm_control_mmap_allowed(pcm_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return -ENXIO;
3448 return snd_pcm_mmap_control(substream, file, area);
3449 default:
3450 return snd_pcm_mmap_data(substream, file, area);
3451 }
3452 return 0;
3453}
3454
3455static int snd_pcm_fasync(int fd, struct file * file, int on)
3456{
Takashi Iwai877211f2005-11-17 13:59:38 +01003457 struct snd_pcm_file * pcm_file;
3458 struct snd_pcm_substream *substream;
3459 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 pcm_file = file->private_data;
3462 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003463 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003464 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003466 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468
3469/*
3470 * ioctl32 compat
3471 */
3472#ifdef CONFIG_COMPAT
3473#include "pcm_compat.c"
3474#else
3475#define snd_pcm_ioctl_compat NULL
3476#endif
3477
3478/*
3479 * To be removed helpers to keep binary compatibility
3480 */
3481
Takashi Iwai59d48582005-12-01 10:51:58 +01003482#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3484#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3485
Takashi Iwai877211f2005-11-17 13:59:38 +01003486static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3487 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
3489 unsigned int i;
3490
3491 memset(params, 0, sizeof(*params));
3492 params->flags = oparams->flags;
3493 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3494 params->masks[i].bits[0] = oparams->masks[i];
3495 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3496 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3497 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3498 params->info = oparams->info;
3499 params->msbits = oparams->msbits;
3500 params->rate_num = oparams->rate_num;
3501 params->rate_den = oparams->rate_den;
3502 params->fifo_size = oparams->fifo_size;
3503}
3504
Takashi Iwai877211f2005-11-17 13:59:38 +01003505static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3506 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507{
3508 unsigned int i;
3509
3510 memset(oparams, 0, sizeof(*oparams));
3511 oparams->flags = params->flags;
3512 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3513 oparams->masks[i] = params->masks[i].bits[0];
3514 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3515 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3516 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3517 oparams->info = params->info;
3518 oparams->msbits = params->msbits;
3519 oparams->rate_num = params->rate_num;
3520 oparams->rate_den = params->rate_den;
3521 oparams->fifo_size = params->fifo_size;
3522}
3523
Takashi Iwai877211f2005-11-17 13:59:38 +01003524static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3525 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
Takashi Iwai877211f2005-11-17 13:59:38 +01003527 struct snd_pcm_hw_params *params;
3528 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 int err;
3530
3531 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003532 if (!params)
3533 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Li Zefanef44a1e2009-04-10 09:43:08 +08003535 oparams = memdup_user(_oparams, sizeof(*oparams));
3536 if (IS_ERR(oparams)) {
3537 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 goto out;
3539 }
3540 snd_pcm_hw_convert_from_old_params(params, oparams);
3541 err = snd_pcm_hw_refine(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003542 if (err < 0)
3543 goto out_old;
Li Zefanef44a1e2009-04-10 09:43:08 +08003544
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003545 err = fixup_unreferenced_params(substream, params);
3546 if (err < 0)
3547 goto out_old;
3548
3549 snd_pcm_hw_convert_to_old_params(oparams, params);
3550 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3551 err = -EFAULT;
3552out_old:
Li Zefanef44a1e2009-04-10 09:43:08 +08003553 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554out:
3555 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return err;
3557}
3558
Takashi Iwai877211f2005-11-17 13:59:38 +01003559static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3560 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
Takashi Iwai877211f2005-11-17 13:59:38 +01003562 struct snd_pcm_hw_params *params;
3563 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 int err;
3565
3566 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003567 if (!params)
3568 return -ENOMEM;
3569
3570 oparams = memdup_user(_oparams, sizeof(*oparams));
3571 if (IS_ERR(oparams)) {
3572 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 goto out;
3574 }
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003575
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 snd_pcm_hw_convert_from_old_params(params, oparams);
3577 err = snd_pcm_hw_params(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003578 if (err < 0)
3579 goto out_old;
Li Zefanef44a1e2009-04-10 09:43:08 +08003580
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003581 snd_pcm_hw_convert_to_old_params(oparams, params);
3582 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3583 err = -EFAULT;
3584out_old:
Li Zefanef44a1e2009-04-10 09:43:08 +08003585 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586out:
3587 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 return err;
3589}
Takashi Iwai59d48582005-12-01 10:51:58 +01003590#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Cliff Cai70036092008-09-03 10:54:36 +02003592#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003593static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3594 unsigned long addr,
3595 unsigned long len,
3596 unsigned long pgoff,
3597 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003598{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003599 struct snd_pcm_file *pcm_file = file->private_data;
3600 struct snd_pcm_substream *substream = pcm_file->substream;
3601 struct snd_pcm_runtime *runtime = substream->runtime;
3602 unsigned long offset = pgoff << PAGE_SHIFT;
3603
3604 switch (offset) {
3605 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3606 return (unsigned long)runtime->status;
3607 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3608 return (unsigned long)runtime->control;
3609 default:
3610 return (unsigned long)runtime->dma_area + offset;
3611 }
Cliff Cai70036092008-09-03 10:54:36 +02003612}
3613#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003614# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003615#endif
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617/*
3618 * Register section
3619 */
3620
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003621const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003623 .owner = THIS_MODULE,
3624 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003625 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003626 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003627 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003628 .llseek = no_llseek,
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003629 .poll = snd_pcm_poll,
Takashi Iwai67616fe2017-08-30 15:39:32 +02003630 .unlocked_ioctl = snd_pcm_ioctl,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003631 .compat_ioctl = snd_pcm_ioctl_compat,
3632 .mmap = snd_pcm_mmap,
3633 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003634 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 },
3636 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003637 .owner = THIS_MODULE,
3638 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003639 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003640 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003641 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003642 .llseek = no_llseek,
Takashi Iwai6448fcb2018-04-11 18:05:00 +02003643 .poll = snd_pcm_poll,
Takashi Iwai67616fe2017-08-30 15:39:32 +02003644 .unlocked_ioctl = snd_pcm_ioctl,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003645 .compat_ioctl = snd_pcm_ioctl_compat,
3646 .mmap = snd_pcm_mmap,
3647 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003648 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
3650};