blob: 5099078dde93a3f774d167afbc30e6f64c7c970b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040023#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/file.h>
25#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/time.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020028#include <linux/pm_qos.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010029#include <linux/io.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010030#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/timer.h>
37#include <sound/minors.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080038#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Sakamoto2c4842d2017-05-26 09:30:46 +090040#include "pcm_local.h"
41
Takashi Sakamoto37567c52017-06-07 08:46:44 +090042#ifdef CONFIG_SND_DEBUG
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090043#define CREATE_TRACE_POINTS
44#include "pcm_param_trace.h"
Takashi Sakamoto37567c52017-06-07 08:46:44 +090045#else
46#define trace_hw_mask_param_enabled() 0
47#define trace_hw_interval_param_enabled() 0
48#define trace_hw_mask_param(substream, type, index, prev, curr)
49#define trace_hw_interval_param(substream, type, index, prev, curr)
50#endif
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * Compatibility
54 */
55
Takashi Iwai877211f2005-11-17 13:59:38 +010056struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned int flags;
58 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
59 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010060 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
62 unsigned int rmask;
63 unsigned int cmask;
64 unsigned int info;
65 unsigned int msbits;
66 unsigned int rate_num;
67 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010068 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned char reserved[64];
70};
71
Takashi Iwai59d48582005-12-01 10:51:58 +010072#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010073#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
74#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Takashi Iwai877211f2005-11-17 13:59:38 +010076static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
77 struct snd_pcm_hw_params_old __user * _oparams);
78static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
79 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010080#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010081static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 *
85 */
86
Takashi Iwai7af142f2014-09-01 11:19:37 +020087static DEFINE_RWLOCK(snd_pcm_link_rwlock);
88static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwai67ec10722016-02-17 14:30:26 +010090/* Writer in rwsem may block readers even during its waiting in queue,
91 * and this may lead to a deadlock when the code path takes read sem
92 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
93 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
94 * spin until it gets the lock.
95 */
96static inline void down_write_nonblock(struct rw_semaphore *lock)
97{
98 while (!down_write_trylock(lock))
99 cond_resched();
100}
101
Takashi Iwai30b771c2014-10-30 15:02:50 +0100102/**
103 * snd_pcm_stream_lock - Lock the PCM stream
104 * @substream: PCM substream
105 *
106 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
107 * flag of the given substream. This also takes the global link rw lock
108 * (or rw sem), too, for avoiding the race with linked streams.
109 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200110void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
111{
112 if (substream->pcm->nonatomic) {
Takashi Iwai67756e32015-07-17 15:22:33 +0200113 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200114 mutex_lock(&substream->self_group.mutex);
115 } else {
116 read_lock(&snd_pcm_link_rwlock);
117 spin_lock(&substream->self_group.lock);
118 }
119}
120EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
121
Takashi Iwai30b771c2014-10-30 15:02:50 +0100122/**
123 * snd_pcm_stream_lock - Unlock the PCM stream
124 * @substream: PCM substream
125 *
126 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
127 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200128void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
129{
130 if (substream->pcm->nonatomic) {
131 mutex_unlock(&substream->self_group.mutex);
132 up_read(&snd_pcm_link_rwsem);
133 } else {
134 spin_unlock(&substream->self_group.lock);
135 read_unlock(&snd_pcm_link_rwlock);
136 }
137}
138EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
139
Takashi Iwai30b771c2014-10-30 15:02:50 +0100140/**
141 * snd_pcm_stream_lock_irq - Lock the PCM stream
142 * @substream: PCM substream
143 *
144 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
145 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
146 * as snd_pcm_stream_lock().
147 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200148void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
149{
150 if (!substream->pcm->nonatomic)
151 local_irq_disable();
152 snd_pcm_stream_lock(substream);
153}
154EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
155
Takashi Iwai30b771c2014-10-30 15:02:50 +0100156/**
157 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
158 * @substream: PCM substream
159 *
160 * This is a counter-part of snd_pcm_stream_lock_irq().
161 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200162void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
163{
164 snd_pcm_stream_unlock(substream);
165 if (!substream->pcm->nonatomic)
166 local_irq_enable();
167}
168EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
169
170unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
171{
172 unsigned long flags = 0;
173 if (!substream->pcm->nonatomic)
174 local_irq_save(flags);
175 snd_pcm_stream_lock(substream);
176 return flags;
177}
178EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
179
Takashi Iwai30b771c2014-10-30 15:02:50 +0100180/**
181 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
182 * @substream: PCM substream
183 * @flags: irq flags
184 *
185 * This is a counter-part of snd_pcm_stream_lock_irqsave().
186 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200187void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
188 unsigned long flags)
189{
190 snd_pcm_stream_unlock(substream);
191 if (!substream->pcm->nonatomic)
192 local_irq_restore(flags);
193}
194EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Takashi Iwai877211f2005-11-17 13:59:38 +0100196int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Takashi Iwai877211f2005-11-17 13:59:38 +0100198 struct snd_pcm_runtime *runtime;
199 struct snd_pcm *pcm = substream->pcm;
200 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 memset(info, 0, sizeof(*info));
203 info->card = pcm->card->number;
204 info->device = pcm->device;
205 info->stream = substream->stream;
206 info->subdevice = substream->number;
207 strlcpy(info->id, pcm->id, sizeof(info->id));
208 strlcpy(info->name, pcm->name, sizeof(info->name));
209 info->dev_class = pcm->dev_class;
210 info->dev_subclass = pcm->dev_subclass;
211 info->subdevices_count = pstr->substream_count;
212 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
213 strlcpy(info->subname, substream->name, sizeof(info->subname));
214 runtime = substream->runtime;
215 /* AB: FIXME!!! This is definitely nonsense */
216 if (runtime) {
217 info->sync = runtime->sync;
218 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
219 }
220 return 0;
221}
222
Takashi Iwai877211f2005-11-17 13:59:38 +0100223int snd_pcm_info_user(struct snd_pcm_substream *substream,
224 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Takashi Iwai877211f2005-11-17 13:59:38 +0100226 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int err;
228
229 info = kmalloc(sizeof(*info), GFP_KERNEL);
230 if (! info)
231 return -ENOMEM;
232 err = snd_pcm_info(substream, info);
233 if (err >= 0) {
234 if (copy_to_user(_info, info, sizeof(*info)))
235 err = -EFAULT;
236 }
237 kfree(info);
238 return err;
239}
240
Takashi Iwai63825f32014-10-22 12:04:46 +0200241static bool hw_support_mmap(struct snd_pcm_substream *substream)
242{
243 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
244 return false;
245 /* check architectures that return -EINVAL from dma_mmap_coherent() */
246 /* FIXME: this should be some global flag */
247#if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
248 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
249 if (!substream->ops->mmap &&
250 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
251 return false;
252#endif
253 return true;
254}
255
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900256static int constrain_mask_params(struct snd_pcm_substream *substream,
257 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900259 struct snd_pcm_hw_constraints *constrs =
260 &substream->runtime->hw_constraints;
261 struct snd_mask *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int k;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900263 struct snd_mask old_mask;
264 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
267 m = hw_param_mask(params, k);
268 if (snd_mask_empty(m))
269 return -EINVAL;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900270
271 /* This parameter is not requested to change by a caller. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (!(params->rmask & (1 << k)))
273 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900274
275 if (trace_hw_mask_param_enabled())
276 old_mask = *m;
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 changed = snd_mask_refine(m, constrs_mask(constrs, k));
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900279 if (changed < 0)
280 return changed;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900281 if (changed == 0)
282 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900283
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900284 /* Set corresponding flag so that the caller gets it. */
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900285 trace_hw_mask_param(substream, k, 0, &old_mask, m);
286 params->cmask |= 1 << k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900289 return 0;
290}
291
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900292static int constrain_interval_params(struct snd_pcm_substream *substream,
293 struct snd_pcm_hw_params *params)
294{
295 struct snd_pcm_hw_constraints *constrs =
296 &substream->runtime->hw_constraints;
297 struct snd_interval *i;
298 unsigned int k;
299 struct snd_interval old_interval;
300 int changed;
301
302 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
303 i = hw_param_interval(params, k);
304 if (snd_interval_empty(i))
305 return -EINVAL;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900306
307 /* This parameter is not requested to change by a caller. */
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900308 if (!(params->rmask & (1 << k)))
309 continue;
310
311 if (trace_hw_interval_param_enabled())
312 old_interval = *i;
313
314 changed = snd_interval_refine(i, constrs_interval(constrs, k));
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900315 if (changed < 0)
316 return changed;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900317 if (changed == 0)
318 continue;
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900319
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900320 /* Set corresponding flag so that the caller gets it. */
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900321 trace_hw_interval_param(substream, k, 0, &old_interval, i);
322 params->cmask |= 1 << k;
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900323 }
324
325 return 0;
326}
327
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900328static int constrain_params_by_rules(struct snd_pcm_substream *substream,
329 struct snd_pcm_hw_params *params)
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900330{
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900331 struct snd_pcm_hw_constraints *constrs =
332 &substream->runtime->hw_constraints;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900333 unsigned int k;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900334 unsigned int rstamps[constrs->rules_num];
335 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900336 unsigned int stamp;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900337 struct snd_pcm_hw_rule *r;
338 unsigned int d;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900339 struct snd_mask old_mask;
340 struct snd_interval old_interval;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900341 bool again;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900342 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900344 /*
345 * Each application of rule has own sequence number.
346 *
347 * Each member of 'rstamps' array represents the sequence number of
348 * recent application of corresponding rule.
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 for (k = 0; k < constrs->rules_num; k++)
351 rstamps[k] = 0;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900352
353 /*
354 * Each member of 'vstamps' array represents the sequence number of
355 * recent application of rule in which corresponding parameters were
356 * changed.
357 *
358 * In initial state, elements corresponding to parameters requested by
359 * a caller is 1. For unrequested parameters, corresponding members
360 * have 0 so that the parameters are never changed anymore.
361 */
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900362 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900364
365 /* Due to the above design, actual sequence number starts at 2. */
366 stamp = 2;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900367retry:
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900368 /* Apply all rules in order. */
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900369 again = false;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900370 for (k = 0; k < constrs->rules_num; k++) {
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900371 r = &constrs->rules[k];
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900372
373 /*
374 * Check condition bits of this rule. When the rule has
375 * some condition bits, parameter without the bits is
376 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
377 * is an example of the condition bits.
378 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900379 if (r->cond && !(r->cond & params->flags))
380 continue;
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900381
382 /*
383 * The 'deps' array includes maximum three dependencies
384 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
385 * member of this array is a sentinel and should be
386 * negative value.
387 *
388 * This rule should be processed in this time when dependent
389 * parameters were changed at former applications of the other
390 * rules.
391 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900392 for (d = 0; r->deps[d] >= 0; d++) {
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900393 if (vstamps[r->deps[d]] > rstamps[k])
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900396 if (r->deps[d] < 0)
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900397 continue;
398
399 if (trace_hw_mask_param_enabled()) {
400 if (hw_is_mask(r->var))
401 old_mask = *hw_param_mask(params, r->var);
402 }
403 if (trace_hw_interval_param_enabled()) {
404 if (hw_is_interval(r->var))
405 old_interval = *hw_param_interval(params, r->var);
406 }
407
408 changed = r->func(params, r);
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900409 if (changed < 0)
410 return changed;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900411
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900412 /*
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900413 * When the parameter is changed, notify it to the caller
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900414 * by corresponding returned bit, then preparing for next
415 * iteration.
416 */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900417 if (changed && r->var >= 0) {
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900418 if (hw_is_mask(r->var)) {
419 trace_hw_mask_param(substream, r->var,
420 k + 1, &old_mask,
421 hw_param_mask(params, r->var));
422 }
423 if (hw_is_interval(r->var)) {
424 trace_hw_interval_param(substream, r->var,
425 k + 1, &old_interval,
426 hw_param_interval(params, r->var));
427 }
428
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900429 params->cmask |= (1 << r->var);
430 vstamps[r->var] = stamp;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900431 again = true;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900432 }
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900433
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900434 rstamps[k] = stamp++;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900435 }
436
Takashi Sakamotod81052f2017-06-09 06:37:06 +0900437 /* Iterate to evaluate all rules till no parameters are changed. */
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900438 if (again)
439 goto retry;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900440
441 return 0;
442}
443
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900444static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
445 struct snd_pcm_hw_params *params)
446{
447 const struct snd_interval *i;
448 const struct snd_mask *m;
449 int err;
450
451 if (!params->msbits) {
452 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
453 if (snd_interval_single(i))
454 params->msbits = snd_interval_value(i);
455 }
456
457 if (!params->rate_den) {
458 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
459 if (snd_interval_single(i)) {
460 params->rate_num = snd_interval_value(i);
461 params->rate_den = 1;
462 }
463 }
464
465 if (!params->fifo_size) {
466 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
467 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
468 if (snd_mask_single(m) && snd_interval_single(i)) {
469 err = substream->ops->ioctl(substream,
470 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
471 if (err < 0)
472 return err;
473 }
474 }
475
Takashi Sakamoto7802fb52017-06-09 09:34:40 +0900476 if (!params->info) {
477 params->info = substream->runtime->hw.info;
478 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
479 SNDRV_PCM_INFO_DRAIN_TRIGGER);
480 if (!hw_support_mmap(substream))
481 params->info &= ~(SNDRV_PCM_INFO_MMAP |
482 SNDRV_PCM_INFO_MMAP_VALID);
483 }
484
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900485 return 0;
486}
487
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900488int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
489 struct snd_pcm_hw_params *params)
490{
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900491 int err;
492
493 params->info = 0;
494 params->fifo_size = 0;
495 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
496 params->msbits = 0;
497 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
498 params->rate_num = 0;
499 params->rate_den = 0;
500 }
501
502 err = constrain_mask_params(substream, params);
503 if (err < 0)
504 return err;
505
506 err = constrain_interval_params(substream, params);
507 if (err < 0)
508 return err;
509
510 err = constrain_params_by_rules(substream, params);
511 if (err < 0)
512 return err;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 params->rmask = 0;
Takashi Sakamoto7802fb52017-06-09 09:34:40 +0900515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return 0;
517}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200518EXPORT_SYMBOL(snd_pcm_hw_refine);
519
Takashi Iwai877211f2005-11-17 13:59:38 +0100520static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
521 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Takashi Iwai877211f2005-11-17 13:59:38 +0100523 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int err;
525
Li Zefanef44a1e2009-04-10 09:43:08 +0800526 params = memdup_user(_params, sizeof(*params));
527 if (IS_ERR(params))
528 return PTR_ERR(params);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 err = snd_pcm_hw_refine(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900531 if (err < 0)
532 goto end;
Li Zefanef44a1e2009-04-10 09:43:08 +0800533
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900534 err = fixup_unreferenced_params(substream, params);
535 if (err < 0)
536 goto end;
537
538 if (copy_to_user(_params, params, sizeof(*params)))
539 err = -EFAULT;
540end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 kfree(params);
542 return err;
543}
544
Takashi Iwai9442e692006-09-30 23:27:19 -0700545static int period_to_usecs(struct snd_pcm_runtime *runtime)
546{
547 int usecs;
548
549 if (! runtime->rate)
550 return -1; /* invalid */
551
552 /* take 75% of period time as the deadline */
553 usecs = (750000 / runtime->rate) * runtime->period_size;
554 usecs += ((750000 % runtime->rate) * runtime->period_size) /
555 runtime->rate;
556
557 return usecs;
558}
559
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200560static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
561{
562 snd_pcm_stream_lock_irq(substream);
563 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
564 substream->runtime->status->state = state;
565 snd_pcm_stream_unlock_irq(substream);
566}
567
Jie Yang90bbaf62015-10-16 17:57:46 +0800568static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
569 int event)
570{
571#ifdef CONFIG_SND_PCM_TIMER
572 if (substream->timer)
573 snd_timer_notify(substream->timer, event,
574 &substream->runtime->trigger_tstamp);
575#endif
576}
577
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900578/**
579 * snd_pcm_hw_param_choose - choose a configuration defined by @params
580 * @pcm: PCM instance
581 * @params: the hw_params instance
582 *
583 * Choose one configuration from configuration space defined by @params.
584 * The configuration chosen is that obtained fixing in this order:
585 * first access, first format, first subformat, min channels,
586 * min rate, min period time, max buffer size, min tick time
587 *
588 * Return: Zero if successful, or a negative error code on failure.
589 */
590static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
591 struct snd_pcm_hw_params *params)
592{
593 static const int vars[] = {
594 SNDRV_PCM_HW_PARAM_ACCESS,
595 SNDRV_PCM_HW_PARAM_FORMAT,
596 SNDRV_PCM_HW_PARAM_SUBFORMAT,
597 SNDRV_PCM_HW_PARAM_CHANNELS,
598 SNDRV_PCM_HW_PARAM_RATE,
599 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
600 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
601 SNDRV_PCM_HW_PARAM_TICK_TIME,
602 -1
603 };
604 const int *v;
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900605 struct snd_mask old_mask;
606 struct snd_interval old_interval;
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900607 int changed;
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900608
609 for (v = vars; *v != -1; v++) {
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900610 /* Keep old parameter to trace. */
611 if (trace_hw_mask_param_enabled()) {
612 if (hw_is_mask(*v))
613 old_mask = *hw_param_mask(params, *v);
614 }
615 if (trace_hw_interval_param_enabled()) {
616 if (hw_is_interval(*v))
617 old_interval = *hw_param_interval(params, *v);
618 }
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900619 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900620 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900621 else
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900622 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
623 if (snd_BUG_ON(changed < 0))
624 return changed;
625 if (changed == 0)
626 continue;
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900627
Takashi Sakamoto82e7d502017-06-11 23:56:13 +0900628 /* Trace the changed parameter. */
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900629 if (hw_is_mask(*v)) {
630 trace_hw_mask_param(pcm, *v, 0, &old_mask,
631 hw_param_mask(params, *v));
632 }
633 if (hw_is_interval(*v)) {
634 trace_hw_interval_param(pcm, *v, 0, &old_interval,
635 hw_param_interval(params, *v));
636 }
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900637 }
Takashi Sakamoto7b8a54a2017-06-09 21:46:49 +0900638
Takashi Sakamoto60f96aa2017-06-09 21:46:48 +0900639 return 0;
640}
641
Takashi Iwai877211f2005-11-17 13:59:38 +0100642static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
643 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Takashi Iwai877211f2005-11-17 13:59:38 +0100645 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700646 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned int bits;
648 snd_pcm_uframes_t frames;
649
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200650 if (PCM_RUNTIME_CHECK(substream))
651 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 snd_pcm_stream_lock_irq(substream);
654 switch (runtime->status->state) {
655 case SNDRV_PCM_STATE_OPEN:
656 case SNDRV_PCM_STATE_SETUP:
657 case SNDRV_PCM_STATE_PREPARED:
658 break;
659 default:
660 snd_pcm_stream_unlock_irq(substream);
661 return -EBADFD;
662 }
663 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100664#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!substream->oss.oss)
666#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200667 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EBADFD;
669
670 params->rmask = ~0U;
671 err = snd_pcm_hw_refine(substream, params);
672 if (err < 0)
673 goto _error;
674
675 err = snd_pcm_hw_params_choose(substream, params);
676 if (err < 0)
677 goto _error;
678
Takashi Sakamotof9a076b2017-06-09 09:34:39 +0900679 err = fixup_unreferenced_params(substream, params);
680 if (err < 0)
681 goto _error;
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (substream->ops->hw_params != NULL) {
684 err = substream->ops->hw_params(substream, params);
685 if (err < 0)
686 goto _error;
687 }
688
689 runtime->access = params_access(params);
690 runtime->format = params_format(params);
691 runtime->subformat = params_subformat(params);
692 runtime->channels = params_channels(params);
693 runtime->rate = params_rate(params);
694 runtime->period_size = params_period_size(params);
695 runtime->periods = params_periods(params);
696 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 runtime->info = params->info;
698 runtime->rate_num = params->rate_num;
699 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100700 runtime->no_period_wakeup =
701 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
702 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 bits = snd_pcm_format_physical_width(runtime->format);
705 runtime->sample_bits = bits;
706 bits *= runtime->channels;
707 runtime->frame_bits = bits;
708 frames = 1;
709 while (bits % 8 != 0) {
710 bits *= 2;
711 frames *= 2;
712 }
713 runtime->byte_align = bits / 8;
714 runtime->min_align = frames;
715
716 /* Default sw params */
717 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
718 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 runtime->start_threshold = 1;
721 runtime->stop_threshold = runtime->buffer_size;
722 runtime->silence_threshold = 0;
723 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200724 runtime->boundary = runtime->buffer_size;
725 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
726 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200729 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700730
James Bottomley82f68252010-07-05 22:53:06 +0200731 if (pm_qos_request_active(&substream->latency_pm_qos_req))
732 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700733 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200734 pm_qos_add_request(&substream->latency_pm_qos_req,
735 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return 0;
737 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300738 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 so we force application to retry to set
740 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200741 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (substream->ops->hw_free != NULL)
743 substream->ops->hw_free(substream);
744 return err;
745}
746
Takashi Iwai877211f2005-11-17 13:59:38 +0100747static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
748 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Takashi Iwai877211f2005-11-17 13:59:38 +0100750 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int err;
752
Li Zefanef44a1e2009-04-10 09:43:08 +0800753 params = memdup_user(_params, sizeof(*params));
754 if (IS_ERR(params))
755 return PTR_ERR(params);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 err = snd_pcm_hw_params(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900758 if (err < 0)
759 goto end;
Li Zefanef44a1e2009-04-10 09:43:08 +0800760
Takashi Sakamotof74ae152017-06-11 23:56:12 +0900761 if (copy_to_user(_params, params, sizeof(*params)))
762 err = -EFAULT;
763end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 kfree(params);
765 return err;
766}
767
Takashi Iwai877211f2005-11-17 13:59:38 +0100768static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Takashi Iwai877211f2005-11-17 13:59:38 +0100770 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int result = 0;
772
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200773 if (PCM_RUNTIME_CHECK(substream))
774 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 snd_pcm_stream_lock_irq(substream);
777 switch (runtime->status->state) {
778 case SNDRV_PCM_STATE_SETUP:
779 case SNDRV_PCM_STATE_PREPARED:
780 break;
781 default:
782 snd_pcm_stream_unlock_irq(substream);
783 return -EBADFD;
784 }
785 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200786 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -EBADFD;
788 if (substream->ops->hw_free)
789 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200790 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200791 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return result;
793}
794
Takashi Iwai877211f2005-11-17 13:59:38 +0100795static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
796 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Takashi Iwai877211f2005-11-17 13:59:38 +0100798 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100799 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200801 if (PCM_RUNTIME_CHECK(substream))
802 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 snd_pcm_stream_lock_irq(substream);
805 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
806 snd_pcm_stream_unlock_irq(substream);
807 return -EBADFD;
808 }
809 snd_pcm_stream_unlock_irq(substream);
810
Dan Carpenter145d92e2015-09-23 12:42:28 +0300811 if (params->tstamp_mode < 0 ||
812 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return -EINVAL;
Takashi Iwai589008102014-07-16 17:45:27 +0200814 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
815 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200816 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (params->avail_min == 0)
818 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (params->silence_size >= runtime->boundary) {
820 if (params->silence_threshold != 0)
821 return -EINVAL;
822 } else {
823 if (params->silence_size > params->silence_threshold)
824 return -EINVAL;
825 if (params->silence_threshold > runtime->buffer_size)
826 return -EINVAL;
827 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100828 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 snd_pcm_stream_lock_irq(substream);
830 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai589008102014-07-16 17:45:27 +0200831 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
832 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 runtime->period_step = params->period_step;
834 runtime->control->avail_min = params->avail_min;
835 runtime->start_threshold = params->start_threshold;
836 runtime->stop_threshold = params->stop_threshold;
837 runtime->silence_threshold = params->silence_threshold;
838 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 params->boundary = runtime->boundary;
840 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
842 runtime->silence_size > 0)
843 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100844 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100847 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Takashi Iwai877211f2005-11-17 13:59:38 +0100850static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
851 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Takashi Iwai877211f2005-11-17 13:59:38 +0100853 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int err;
855 if (copy_from_user(&params, _params, sizeof(params)))
856 return -EFAULT;
857 err = snd_pcm_sw_params(substream, &params);
858 if (copy_to_user(_params, &params, sizeof(params)))
859 return -EFAULT;
860 return err;
861}
862
Takashi Iwai877211f2005-11-17 13:59:38 +0100863int snd_pcm_status(struct snd_pcm_substream *substream,
864 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Takashi Iwai877211f2005-11-17 13:59:38 +0100866 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600869
870 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
871 &runtime->audio_tstamp_config);
872
873 /* backwards compatible behavior */
874 if (runtime->audio_tstamp_config.type_requested ==
875 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
876 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
877 runtime->audio_tstamp_config.type_requested =
878 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
879 else
880 runtime->audio_tstamp_config.type_requested =
881 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
882 runtime->audio_tstamp_report.valid = 0;
883 } else
884 runtime->audio_tstamp_report.valid = 1;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 status->state = runtime->status->state;
887 status->suspended_state = runtime->status->suspended_state;
888 if (status->state == SNDRV_PCM_STATE_OPEN)
889 goto _end;
890 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100891 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100893 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
894 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600895 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500896 status->audio_tstamp =
897 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600898 if (runtime->audio_tstamp_report.valid == 1)
899 /* backwards compatibility, no report provided in COMPAT mode */
900 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
901 &status->audio_tstamp_accuracy,
902 &runtime->audio_tstamp_report);
903
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100904 goto _tstamp_end;
905 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600906 } else {
907 /* get tstamp only in fallback mode and only if enabled */
908 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
909 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100910 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100911 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 status->appl_ptr = runtime->control->appl_ptr;
913 status->hw_ptr = runtime->status->hw_ptr;
914 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
915 status->avail = snd_pcm_playback_avail(runtime);
916 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200917 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200919 status->delay += runtime->delay;
920 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 status->delay = 0;
922 } else {
923 status->avail = snd_pcm_capture_avail(runtime);
924 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200925 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 else
927 status->delay = 0;
928 }
929 status->avail_max = runtime->avail_max;
930 status->overrange = runtime->overrange;
931 runtime->avail_max = 0;
932 runtime->overrange = 0;
933 _end:
934 snd_pcm_stream_unlock_irq(substream);
935 return 0;
936}
937
Takashi Iwai877211f2005-11-17 13:59:38 +0100938static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600939 struct snd_pcm_status __user * _status,
940 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Takashi Iwai877211f2005-11-17 13:59:38 +0100942 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600946 /*
947 * with extension, parameters are read/write,
948 * get audio_tstamp_data from user,
949 * ignore rest of status structure
950 */
951 if (ext && get_user(status.audio_tstamp_data,
952 (u32 __user *)(&_status->audio_tstamp_data)))
953 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 res = snd_pcm_status(substream, &status);
955 if (res < 0)
956 return res;
957 if (copy_to_user(_status, &status, sizeof(status)))
958 return -EFAULT;
959 return 0;
960}
961
Takashi Iwai877211f2005-11-17 13:59:38 +0100962static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
963 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Takashi Iwai877211f2005-11-17 13:59:38 +0100965 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 unsigned int channel;
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 channel = info->channel;
969 runtime = substream->runtime;
970 snd_pcm_stream_lock_irq(substream);
971 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
972 snd_pcm_stream_unlock_irq(substream);
973 return -EBADFD;
974 }
975 snd_pcm_stream_unlock_irq(substream);
976 if (channel >= runtime->channels)
977 return -EINVAL;
978 memset(info, 0, sizeof(*info));
979 info->channel = channel;
980 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
981}
982
Takashi Iwai877211f2005-11-17 13:59:38 +0100983static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
984 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwai877211f2005-11-17 13:59:38 +0100986 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int res;
988
989 if (copy_from_user(&info, _info, sizeof(info)))
990 return -EFAULT;
991 res = snd_pcm_channel_info(substream, &info);
992 if (res < 0)
993 return res;
994 if (copy_to_user(_info, &info, sizeof(info)))
995 return -EFAULT;
996 return 0;
997}
998
Takashi Iwai877211f2005-11-17 13:59:38 +0100999static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Takashi Iwai877211f2005-11-17 13:59:38 +01001001 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (runtime->trigger_master == NULL)
1003 return;
1004 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001005 if (!runtime->trigger_tstamp_latched)
1006 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else {
1008 snd_pcm_trigger_tstamp(runtime->trigger_master);
1009 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1010 }
1011 runtime->trigger_master = NULL;
1012}
1013
1014struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +01001015 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1016 int (*do_action)(struct snd_pcm_substream *substream, int state);
1017 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1018 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019};
1020
1021/*
1022 * this functions is core for handling of linked stream
1023 * Note: the stream state might be changed also on failure
1024 * Note2: call with calling stream lock + link lock
1025 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001026static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001027 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 int state, int do_lock)
1029{
Takashi Iwai877211f2005-11-17 13:59:38 +01001030 struct snd_pcm_substream *s = NULL;
1031 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +02001032 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Takashi Iwaief991b92007-02-22 12:52:53 +01001034 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001035 if (do_lock && s != substream) {
1036 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +02001037 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001038 else
Takashi Iwaidde1c652014-10-21 15:32:13 +02001039 spin_lock_nested(&s->self_group.lock, depth);
1040 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 res = ops->pre_action(s, state);
1043 if (res < 0)
1044 goto _unlock;
1045 }
Takashi Iwaief991b92007-02-22 12:52:53 +01001046 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 res = ops->do_action(s, state);
1048 if (res < 0) {
1049 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +01001050 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (s1 == s) /* failed stream */
1052 break;
1053 ops->undo_action(s1, state);
1054 }
1055 }
1056 s = NULL; /* unlock all */
1057 goto _unlock;
1058 }
1059 }
Takashi Iwaief991b92007-02-22 12:52:53 +01001060 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 ops->post_action(s, state);
1062 }
1063 _unlock:
1064 if (do_lock) {
1065 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +01001066 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001067 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +02001068 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001069 mutex_unlock(&s1->self_group.mutex);
1070 else
1071 spin_unlock(&s1->self_group.lock);
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (s1 == s) /* end */
1074 break;
1075 }
1076 }
1077 return res;
1078}
1079
1080/*
1081 * Note: call with stream lock
1082 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001083static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001084 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int state)
1086{
1087 int res;
1088
1089 res = ops->pre_action(substream, state);
1090 if (res < 0)
1091 return res;
1092 res = ops->do_action(substream, state);
1093 if (res == 0)
1094 ops->post_action(substream, state);
1095 else if (ops->undo_action)
1096 ops->undo_action(substream, state);
1097 return res;
1098}
1099
1100/*
1101 * Note: call with stream lock
1102 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001103static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001104 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int state)
1106{
1107 int res;
1108
Takashi Iwaiaa8edd82014-10-31 15:19:36 +01001109 if (!snd_pcm_stream_linked(substream))
1110 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001111
Takashi Iwaiaa8edd82014-10-31 15:19:36 +01001112 if (substream->pcm->nonatomic) {
1113 if (!mutex_trylock(&substream->group->mutex)) {
1114 mutex_unlock(&substream->self_group.mutex);
1115 mutex_lock(&substream->group->mutex);
1116 mutex_lock(&substream->self_group.mutex);
1117 }
1118 res = snd_pcm_action_group(ops, substream, state, 1);
1119 mutex_unlock(&substream->group->mutex);
1120 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (!spin_trylock(&substream->group->lock)) {
1122 spin_unlock(&substream->self_group.lock);
1123 spin_lock(&substream->group->lock);
1124 spin_lock(&substream->self_group.lock);
1125 }
1126 res = snd_pcm_action_group(ops, substream, state, 1);
1127 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129 return res;
1130}
1131
1132/*
1133 * Note: don't use any locks before
1134 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001135static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001136 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int state)
1138{
1139 int res;
1140
Takashi Iwaie3a4bd52014-10-31 14:45:04 +01001141 snd_pcm_stream_lock_irq(substream);
1142 res = snd_pcm_action(ops, substream, state);
1143 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return res;
1145}
1146
1147/*
1148 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001149static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001150 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int state)
1152{
1153 int res;
1154
1155 down_read(&snd_pcm_link_rwsem);
1156 if (snd_pcm_stream_linked(substream))
1157 res = snd_pcm_action_group(ops, substream, state, 0);
1158 else
1159 res = snd_pcm_action_single(ops, substream, state);
1160 up_read(&snd_pcm_link_rwsem);
1161 return res;
1162}
1163
1164/*
1165 * start callbacks
1166 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001167static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Takashi Iwai877211f2005-11-17 13:59:38 +01001169 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1171 return -EBADFD;
1172 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1173 !snd_pcm_playback_data(substream))
1174 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001175 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 runtime->trigger_master = substream;
1177 return 0;
1178}
1179
Takashi Iwai877211f2005-11-17 13:59:38 +01001180static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 if (substream->runtime->trigger_master != substream)
1183 return 0;
1184 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1185}
1186
Takashi Iwai877211f2005-11-17 13:59:38 +01001187static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 if (substream->runtime->trigger_master == substream)
1190 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1191}
1192
Takashi Iwai877211f2005-11-17 13:59:38 +01001193static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Takashi Iwai877211f2005-11-17 13:59:38 +01001195 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001197 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001198 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1199 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 runtime->status->state = state;
1201 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1202 runtime->silence_size > 0)
1203 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001204 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Julia Lawallb17154c2015-11-29 16:36:40 +01001207static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 .pre_action = snd_pcm_pre_start,
1209 .do_action = snd_pcm_do_start,
1210 .undo_action = snd_pcm_undo_start,
1211 .post_action = snd_pcm_post_start
1212};
1213
1214/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001215 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001216 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001217 *
1218 * Return: Zero if successful, or a negative error code.
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001219 * The stream lock must be acquired before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001221int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Takashi Iwai877211f2005-11-17 13:59:38 +01001223 return snd_pcm_action(&snd_pcm_action_start, substream,
1224 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001227/* take the stream lock and start the streams */
1228static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1229{
1230 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1231 SNDRV_PCM_STATE_RUNNING);
1232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234/*
1235 * stop callbacks
1236 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001237static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Takashi Iwai877211f2005-11-17 13:59:38 +01001239 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1241 return -EBADFD;
1242 runtime->trigger_master = substream;
1243 return 0;
1244}
1245
Takashi Iwai877211f2005-11-17 13:59:38 +01001246static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 if (substream->runtime->trigger_master == substream &&
1249 snd_pcm_running(substream))
1250 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1251 return 0; /* unconditonally stop all substreams */
1252}
1253
Takashi Iwai877211f2005-11-17 13:59:38 +01001254static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Takashi Iwai877211f2005-11-17 13:59:38 +01001256 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (runtime->status->state != state) {
1258 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001259 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001260 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001263 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Julia Lawallb17154c2015-11-29 16:36:40 +01001266static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 .pre_action = snd_pcm_pre_stop,
1268 .do_action = snd_pcm_do_stop,
1269 .post_action = snd_pcm_post_stop
1270};
1271
1272/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001273 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001274 * @substream: the PCM substream instance
1275 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001277 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001278 *
Masanari Iida0a114582014-02-09 00:47:36 +09001279 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001281int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1284}
1285
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001286EXPORT_SYMBOL(snd_pcm_stop);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001289 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001290 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001292 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001294 *
1295 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001297int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Takashi Iwai877211f2005-11-17 13:59:38 +01001299 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1300 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Takashi Iwai1fb85102014-11-07 17:08:28 +01001303/**
1304 * snd_pcm_stop_xrun - stop the running streams as XRUN
1305 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001306 *
1307 * This stops the given running substream (and all linked substreams) as XRUN.
1308 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1309 *
1310 * Return: Zero if successful, or a negative error code.
1311 */
1312int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1313{
1314 unsigned long flags;
1315 int ret = 0;
1316
1317 snd_pcm_stream_lock_irqsave(substream, flags);
1318 if (snd_pcm_running(substream))
1319 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1320 snd_pcm_stream_unlock_irqrestore(substream, flags);
1321 return ret;
1322}
1323EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325/*
1326 * pause callbacks
1327 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001328static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Takashi Iwai877211f2005-11-17 13:59:38 +01001330 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1332 return -ENOSYS;
1333 if (push) {
1334 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1335 return -EBADFD;
1336 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1337 return -EBADFD;
1338 runtime->trigger_master = substream;
1339 return 0;
1340}
1341
Takashi Iwai877211f2005-11-17 13:59:38 +01001342static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 if (substream->runtime->trigger_master != substream)
1345 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001346 /* some drivers might use hw_ptr to recover from the pause -
1347 update the hw_ptr now */
1348 if (push)
1349 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001350 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001351 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001352 * delta, effectively to skip the check once.
1353 */
1354 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return substream->ops->trigger(substream,
1356 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1357 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1358}
1359
Takashi Iwai877211f2005-11-17 13:59:38 +01001360static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 if (substream->runtime->trigger_master == substream)
1363 substream->ops->trigger(substream,
1364 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1365 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1366}
1367
Takashi Iwai877211f2005-11-17 13:59:38 +01001368static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Takashi Iwai877211f2005-11-17 13:59:38 +01001370 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 snd_pcm_trigger_tstamp(substream);
1372 if (push) {
1373 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001374 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001376 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 } else {
1378 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001379 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381}
1382
Julia Lawallb17154c2015-11-29 16:36:40 +01001383static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 .pre_action = snd_pcm_pre_pause,
1385 .do_action = snd_pcm_do_pause,
1386 .undo_action = snd_pcm_undo_pause,
1387 .post_action = snd_pcm_post_pause
1388};
1389
1390/*
1391 * Push/release the pause for all linked streams.
1392 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001393static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1396}
1397
1398#ifdef CONFIG_PM
1399/* suspend */
1400
Takashi Iwai877211f2005-11-17 13:59:38 +01001401static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwai877211f2005-11-17 13:59:38 +01001403 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1405 return -EBUSY;
1406 runtime->trigger_master = substream;
1407 return 0;
1408}
1409
Takashi Iwai877211f2005-11-17 13:59:38 +01001410static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Takashi Iwai877211f2005-11-17 13:59:38 +01001412 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (runtime->trigger_master != substream)
1414 return 0;
1415 if (! snd_pcm_running(substream))
1416 return 0;
1417 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1418 return 0; /* suspend unconditionally */
1419}
1420
Takashi Iwai877211f2005-11-17 13:59:38 +01001421static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Takashi Iwai877211f2005-11-17 13:59:38 +01001423 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001425 runtime->status->suspended_state = runtime->status->state;
1426 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001427 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001429 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Julia Lawallb17154c2015-11-29 16:36:40 +01001432static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 .pre_action = snd_pcm_pre_suspend,
1434 .do_action = snd_pcm_do_suspend,
1435 .post_action = snd_pcm_post_suspend
1436};
1437
1438/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001439 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001440 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001443 *
1444 * Return: Zero if successful (or @substream is %NULL), or a negative error
1445 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001447int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 int err;
1450 unsigned long flags;
1451
Takashi Iwai603bf522005-11-17 15:59:14 +01001452 if (! substream)
1453 return 0;
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 snd_pcm_stream_lock_irqsave(substream, flags);
1456 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1457 snd_pcm_stream_unlock_irqrestore(substream, flags);
1458 return err;
1459}
1460
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001461EXPORT_SYMBOL(snd_pcm_suspend);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001464 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001465 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001468 *
1469 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001471int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Takashi Iwai877211f2005-11-17 13:59:38 +01001473 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int stream, err = 0;
1475
Takashi Iwai603bf522005-11-17 15:59:14 +01001476 if (! pcm)
1477 return 0;
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001480 for (substream = pcm->streams[stream].substream;
1481 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /* FIXME: the open/close code should lock this as well */
1483 if (substream->runtime == NULL)
1484 continue;
1485 err = snd_pcm_suspend(substream);
1486 if (err < 0 && err != -EBUSY)
1487 return err;
1488 }
1489 }
1490 return 0;
1491}
1492
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001493EXPORT_SYMBOL(snd_pcm_suspend_all);
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/* resume */
1496
Takashi Iwai877211f2005-11-17 13:59:38 +01001497static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Takashi Iwai877211f2005-11-17 13:59:38 +01001499 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1501 return -ENOSYS;
1502 runtime->trigger_master = substream;
1503 return 0;
1504}
1505
Takashi Iwai877211f2005-11-17 13:59:38 +01001506static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Takashi Iwai877211f2005-11-17 13:59:38 +01001508 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (runtime->trigger_master != substream)
1510 return 0;
1511 /* DMA not running previously? */
1512 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1513 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1514 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1515 return 0;
1516 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1517}
1518
Takashi Iwai877211f2005-11-17 13:59:38 +01001519static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 if (substream->runtime->trigger_master == substream &&
1522 snd_pcm_running(substream))
1523 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1524}
1525
Takashi Iwai877211f2005-11-17 13:59:38 +01001526static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Takashi Iwai877211f2005-11-17 13:59:38 +01001528 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001530 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001531 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Julia Lawallb17154c2015-11-29 16:36:40 +01001534static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 .pre_action = snd_pcm_pre_resume,
1536 .do_action = snd_pcm_do_resume,
1537 .undo_action = snd_pcm_undo_resume,
1538 .post_action = snd_pcm_post_resume
1539};
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{
Takashi Iwai877211f2005-11-17 13:59:38 +01001543 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int res;
1545
1546 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001547 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1549 snd_power_unlock(card);
1550 return res;
1551}
1552
1553#else
1554
Takashi Iwai877211f2005-11-17 13:59:38 +01001555static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 return -ENOSYS;
1558}
1559
1560#endif /* CONFIG_PM */
1561
1562/*
1563 * xrun ioctl
1564 *
1565 * Change the RUNNING stream(s) to XRUN state.
1566 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001567static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Takashi Iwai877211f2005-11-17 13:59:38 +01001569 struct snd_card *card = substream->pcm->card;
1570 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int result;
1572
1573 snd_power_lock(card);
1574 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001575 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (result < 0)
1577 goto _unlock;
1578 }
1579
1580 snd_pcm_stream_lock_irq(substream);
1581 switch (runtime->status->state) {
1582 case SNDRV_PCM_STATE_XRUN:
1583 result = 0; /* already there */
1584 break;
1585 case SNDRV_PCM_STATE_RUNNING:
1586 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1587 break;
1588 default:
1589 result = -EBADFD;
1590 }
1591 snd_pcm_stream_unlock_irq(substream);
1592 _unlock:
1593 snd_power_unlock(card);
1594 return result;
1595}
1596
1597/*
1598 * reset ioctl
1599 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001600static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Takashi Iwai877211f2005-11-17 13:59:38 +01001602 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 switch (runtime->status->state) {
1604 case SNDRV_PCM_STATE_RUNNING:
1605 case SNDRV_PCM_STATE_PREPARED:
1606 case SNDRV_PCM_STATE_PAUSED:
1607 case SNDRV_PCM_STATE_SUSPENDED:
1608 return 0;
1609 default:
1610 return -EBADFD;
1611 }
1612}
1613
Takashi Iwai877211f2005-11-17 13:59:38 +01001614static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Takashi Iwai877211f2005-11-17 13:59:38 +01001616 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1618 if (err < 0)
1619 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001621 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1622 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 runtime->silence_start = runtime->status->hw_ptr;
1624 runtime->silence_filled = 0;
1625 return 0;
1626}
1627
Takashi Iwai877211f2005-11-17 13:59:38 +01001628static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Takashi Iwai877211f2005-11-17 13:59:38 +01001630 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 runtime->control->appl_ptr = runtime->status->hw_ptr;
1632 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1633 runtime->silence_size > 0)
1634 snd_pcm_playback_silence(substream, ULONG_MAX);
1635}
1636
Julia Lawallb17154c2015-11-29 16:36:40 +01001637static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 .pre_action = snd_pcm_pre_reset,
1639 .do_action = snd_pcm_do_reset,
1640 .post_action = snd_pcm_post_reset
1641};
1642
Takashi Iwai877211f2005-11-17 13:59:38 +01001643static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1646}
1647
1648/*
1649 * prepare ioctl
1650 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001651/* we use the second argument for updating f_flags */
1652static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1653 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwai877211f2005-11-17 13:59:38 +01001655 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001656 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1657 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return -EBADFD;
1659 if (snd_pcm_running(substream))
1660 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001661 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return 0;
1663}
1664
Takashi Iwai877211f2005-11-17 13:59:38 +01001665static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 int err;
1668 err = substream->ops->prepare(substream);
1669 if (err < 0)
1670 return err;
1671 return snd_pcm_do_reset(substream, 0);
1672}
1673
Takashi Iwai877211f2005-11-17 13:59:38 +01001674static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Takashi Iwai877211f2005-11-17 13:59:38 +01001676 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001678 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Julia Lawallb17154c2015-11-29 16:36:40 +01001681static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 .pre_action = snd_pcm_pre_prepare,
1683 .do_action = snd_pcm_do_prepare,
1684 .post_action = snd_pcm_post_prepare
1685};
1686
1687/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001688 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001689 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001690 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001691 *
1692 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001694static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1695 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001698 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001699 int f_flags;
1700
1701 if (file)
1702 f_flags = file->f_flags;
1703 else
1704 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001707 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001708 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1709 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 snd_power_unlock(card);
1711 return res;
1712}
1713
1714/*
1715 * drain ioctl
1716 */
1717
Takashi Iwai877211f2005-11-17 13:59:38 +01001718static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001720 struct snd_pcm_runtime *runtime = substream->runtime;
1721 switch (runtime->status->state) {
1722 case SNDRV_PCM_STATE_OPEN:
1723 case SNDRV_PCM_STATE_DISCONNECTED:
1724 case SNDRV_PCM_STATE_SUSPENDED:
1725 return -EBADFD;
1726 }
1727 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return 0;
1729}
1730
Takashi Iwai877211f2005-11-17 13:59:38 +01001731static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Takashi Iwai877211f2005-11-17 13:59:38 +01001733 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1735 switch (runtime->status->state) {
1736 case SNDRV_PCM_STATE_PREPARED:
1737 /* start playback stream if possible */
1738 if (! snd_pcm_playback_empty(substream)) {
1739 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1740 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001741 } else {
1742 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744 break;
1745 case SNDRV_PCM_STATE_RUNNING:
1746 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1747 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001748 case SNDRV_PCM_STATE_XRUN:
1749 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1750 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 default:
1752 break;
1753 }
1754 } else {
1755 /* stop running stream */
1756 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001757 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001759 snd_pcm_do_stop(substream, new_state);
1760 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762 }
Libin Yang48d88292014-12-31 22:09:54 +08001763
1764 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1765 runtime->trigger_master == substream &&
1766 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1767 return substream->ops->trigger(substream,
1768 SNDRV_PCM_TRIGGER_DRAIN);
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return 0;
1771}
1772
Takashi Iwai877211f2005-11-17 13:59:38 +01001773static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775}
1776
Julia Lawallb17154c2015-11-29 16:36:40 +01001777static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 .pre_action = snd_pcm_pre_drain_init,
1779 .do_action = snd_pcm_do_drain_init,
1780 .post_action = snd_pcm_post_drain_init
1781};
1782
Takashi Iwai877211f2005-11-17 13:59:38 +01001783static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785/*
1786 * Drain the stream(s).
1787 * When the substream is linked, sync until the draining of all playback streams
1788 * is finished.
1789 * After this call, all streams are supposed to be either SETUP or DRAINING
1790 * (capture only) state.
1791 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001792static int snd_pcm_drain(struct snd_pcm_substream *substream,
1793 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Takashi Iwai877211f2005-11-17 13:59:38 +01001795 struct snd_card *card;
1796 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001797 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001798 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001800 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 card = substream->pcm->card;
1803 runtime = substream->runtime;
1804
1805 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1806 return -EBADFD;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 snd_power_lock(card);
1809 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001810 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001811 if (result < 0) {
1812 snd_power_unlock(card);
1813 return result;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001817 if (file) {
1818 if (file->f_flags & O_NONBLOCK)
1819 nonblock = 1;
1820 } else if (substream->f_flags & O_NONBLOCK)
1821 nonblock = 1;
1822
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001823 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001824 snd_pcm_stream_lock_irq(substream);
1825 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001826 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001827 snd_pcm_pause(substream, 0);
1828
1829 /* pre-start/stop - all running streams are changed to DRAINING state */
1830 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001831 if (result < 0)
1832 goto unlock;
1833 /* in non-blocking, we don't wait in ioctl but let caller poll */
1834 if (nonblock) {
1835 result = -EAGAIN;
1836 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 for (;;) {
1840 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001841 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (signal_pending(current)) {
1843 result = -ERESTARTSYS;
1844 break;
1845 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001846 /* find a substream to drain */
1847 to_check = NULL;
1848 snd_pcm_group_for_each_entry(s, substream) {
1849 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1850 continue;
1851 runtime = s->runtime;
1852 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1853 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001854 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001855 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001856 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001857 if (!to_check)
1858 break; /* all drained */
1859 init_waitqueue_entry(&wait, current);
1860 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001862 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001864 if (runtime->no_period_wakeup)
1865 tout = MAX_SCHEDULE_TIMEOUT;
1866 else {
1867 tout = 10;
1868 if (runtime->rate) {
1869 long t = runtime->period_size * 2 / runtime->rate;
1870 tout = max(t, tout);
1871 }
1872 tout = msecs_to_jiffies(tout * 1000);
1873 }
1874 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001876 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001878 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001879 if (card->shutdown) {
1880 result = -ENODEV;
1881 break;
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (tout == 0) {
1884 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1885 result = -ESTRPIPE;
1886 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001887 dev_dbg(substream->pcm->card->dev,
1888 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1890 result = -EIO;
1891 }
1892 break;
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001895
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001896 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001897 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001898 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 return result;
1902}
1903
1904/*
1905 * drop ioctl
1906 *
1907 * Immediately put all linked substreams into SETUP state.
1908 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001909static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Takashi Iwai877211f2005-11-17 13:59:38 +01001911 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 int result = 0;
1913
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001914 if (PCM_RUNTIME_CHECK(substream))
1915 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001918 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001919 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1920 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return -EBADFD;
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 snd_pcm_stream_lock_irq(substream);
1924 /* resume pause */
1925 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1926 snd_pcm_pause(substream, 0);
1927
1928 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1929 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1930 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return result;
1933}
1934
1935
Al Viro0888c322013-06-05 14:09:55 -04001936static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Al Viro0888c322013-06-05 14:09:55 -04001938 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001939 unsigned int minor;
1940
Al Viro0888c322013-06-05 14:09:55 -04001941 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1942 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001944 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1945 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
1948/*
1949 * PCM link handling
1950 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001951static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001954 struct snd_pcm_file *pcm_file;
1955 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001956 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001957 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Al Viro0888c322013-06-05 14:09:55 -04001959 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001961 if (!is_pcm_file(f.file)) {
1962 res = -EBADFD;
1963 goto _badf;
1964 }
1965 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001967 group = kmalloc(sizeof(*group), GFP_KERNEL);
1968 if (!group) {
1969 res = -ENOMEM;
1970 goto _nolock;
1971 }
Takashi Iwai67ec10722016-02-17 14:30:26 +01001972 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 write_lock_irq(&snd_pcm_link_rwlock);
1974 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001975 substream->runtime->status->state != substream1->runtime->status->state ||
1976 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 res = -EBADFD;
1978 goto _end;
1979 }
1980 if (snd_pcm_stream_linked(substream1)) {
1981 res = -EALREADY;
1982 goto _end;
1983 }
1984 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001985 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001986 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001988 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 INIT_LIST_HEAD(&substream->group->substreams);
1990 list_add_tail(&substream->link_list, &substream->group->substreams);
1991 substream->group->count = 1;
1992 }
1993 list_add_tail(&substream1->link_list, &substream->group->substreams);
1994 substream->group->count++;
1995 substream1->group = substream->group;
1996 _end:
1997 write_unlock_irq(&snd_pcm_link_rwlock);
1998 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001999 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02002000 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04002001 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04002002 _badf:
2003 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return res;
2005}
2006
Takashi Iwai877211f2005-11-17 13:59:38 +01002007static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 substream->group = &substream->self_group;
2010 INIT_LIST_HEAD(&substream->self_group.substreams);
2011 list_add_tail(&substream->link_list, &substream->self_group.substreams);
2012}
2013
Takashi Iwai877211f2005-11-17 13:59:38 +01002014static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Takashi Iwaief991b92007-02-22 12:52:53 +01002016 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int res = 0;
2018
Takashi Iwai67ec10722016-02-17 14:30:26 +01002019 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 write_lock_irq(&snd_pcm_link_rwlock);
2021 if (!snd_pcm_stream_linked(substream)) {
2022 res = -EALREADY;
2023 goto _end;
2024 }
2025 list_del(&substream->link_list);
2026 substream->group->count--;
2027 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01002028 snd_pcm_group_for_each_entry(s, substream) {
2029 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 break;
2031 }
2032 kfree(substream->group);
2033 }
2034 relink_to_local(substream);
2035 _end:
2036 write_unlock_irq(&snd_pcm_link_rwlock);
2037 up_write(&snd_pcm_link_rwsem);
2038 return res;
2039}
2040
2041/*
2042 * hw configurator
2043 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002044static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2045 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Takashi Iwai877211f2005-11-17 13:59:38 +01002047 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2049 hw_param_interval_c(params, rule->deps[1]), &t);
2050 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2051}
2052
Takashi Iwai877211f2005-11-17 13:59:38 +01002053static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2054 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Takashi Iwai877211f2005-11-17 13:59:38 +01002056 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2058 hw_param_interval_c(params, rule->deps[1]), &t);
2059 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2060}
2061
Takashi Iwai877211f2005-11-17 13:59:38 +01002062static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2063 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Takashi Iwai877211f2005-11-17 13:59:38 +01002065 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2067 hw_param_interval_c(params, rule->deps[1]),
2068 (unsigned long) rule->private, &t);
2069 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2070}
2071
Takashi Iwai877211f2005-11-17 13:59:38 +01002072static int snd_pcm_hw_rule_mulkdiv(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 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2077 (unsigned long) rule->private,
2078 hw_param_interval_c(params, rule->deps[1]), &t);
2079 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2080}
2081
Takashi Iwai877211f2005-11-17 13:59:38 +01002082static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2083 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 unsigned int k;
Takashi Sakamotob55f9fd2017-05-17 08:48:20 +09002086 const struct snd_interval *i =
2087 hw_param_interval_c(params, rule->deps[0]);
Takashi Iwai877211f2005-11-17 13:59:38 +01002088 struct snd_mask m;
2089 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 snd_mask_any(&m);
2091 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2092 int bits;
2093 if (! snd_mask_test(mask, k))
2094 continue;
2095 bits = snd_pcm_format_physical_width(k);
2096 if (bits <= 0)
2097 continue; /* ignore invalid formats */
2098 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2099 snd_mask_reset(&m, k);
2100 }
2101 return snd_mask_refine(mask, &m);
2102}
2103
Takashi Iwai877211f2005-11-17 13:59:38 +01002104static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2105 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Takashi Iwai877211f2005-11-17 13:59:38 +01002107 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 unsigned int k;
2109 t.min = UINT_MAX;
2110 t.max = 0;
2111 t.openmin = 0;
2112 t.openmax = 0;
2113 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2114 int bits;
2115 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2116 continue;
2117 bits = snd_pcm_format_physical_width(k);
2118 if (bits <= 0)
2119 continue; /* ignore invalid formats */
2120 if (t.min > (unsigned)bits)
2121 t.min = bits;
2122 if (t.max < (unsigned)bits)
2123 t.max = bits;
2124 }
2125 t.integer = 1;
2126 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2127}
2128
2129#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2130#error "Change this table"
2131#endif
2132
Takashi Sakamoto8b674302017-05-17 08:48:17 +09002133static const unsigned int rates[] = {
2134 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2135 48000, 64000, 88200, 96000, 176400, 192000
2136};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Clemens Ladisch7653d552007-08-13 17:38:54 +02002138const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2139 .count = ARRAY_SIZE(rates),
2140 .list = rates,
2141};
2142
Takashi Iwai877211f2005-11-17 13:59:38 +01002143static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2144 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Takashi Iwai877211f2005-11-17 13:59:38 +01002146 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002148 snd_pcm_known_rates.count,
2149 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Takashi Iwai877211f2005-11-17 13:59:38 +01002152static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2153 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Takashi Iwai877211f2005-11-17 13:59:38 +01002155 struct snd_interval t;
2156 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 t.min = 0;
2158 t.max = substream->buffer_bytes_max;
2159 t.openmin = 0;
2160 t.openmax = 0;
2161 t.integer = 1;
2162 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2163}
2164
Takashi Iwai877211f2005-11-17 13:59:38 +01002165int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Takashi Iwai877211f2005-11-17 13:59:38 +01002167 struct snd_pcm_runtime *runtime = substream->runtime;
2168 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int k, err;
2170
2171 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2172 snd_mask_any(constrs_mask(constrs, k));
2173 }
2174
2175 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2176 snd_interval_any(constrs_interval(constrs, k));
2177 }
2178
2179 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2180 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2181 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2182 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2183 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2184
2185 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2186 snd_pcm_hw_rule_format, NULL,
2187 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2188 if (err < 0)
2189 return err;
2190 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2191 snd_pcm_hw_rule_sample_bits, NULL,
2192 SNDRV_PCM_HW_PARAM_FORMAT,
2193 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2194 if (err < 0)
2195 return err;
2196 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2197 snd_pcm_hw_rule_div, NULL,
2198 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2199 if (err < 0)
2200 return err;
2201 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2202 snd_pcm_hw_rule_mul, NULL,
2203 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2204 if (err < 0)
2205 return err;
2206 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2207 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2208 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2209 if (err < 0)
2210 return err;
2211 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2212 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2213 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2214 if (err < 0)
2215 return err;
2216 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2217 snd_pcm_hw_rule_div, NULL,
2218 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2219 if (err < 0)
2220 return err;
2221 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2222 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2223 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2224 if (err < 0)
2225 return err;
2226 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2227 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2228 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2229 if (err < 0)
2230 return err;
2231 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2232 snd_pcm_hw_rule_div, NULL,
2233 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2234 if (err < 0)
2235 return err;
2236 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2237 snd_pcm_hw_rule_div, NULL,
2238 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2239 if (err < 0)
2240 return err;
2241 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2242 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2243 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2244 if (err < 0)
2245 return err;
2246 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2247 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2248 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2249 if (err < 0)
2250 return err;
2251 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2252 snd_pcm_hw_rule_mul, NULL,
2253 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2254 if (err < 0)
2255 return err;
2256 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2257 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2258 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2259 if (err < 0)
2260 return err;
2261 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2262 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2263 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2264 if (err < 0)
2265 return err;
2266 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2267 snd_pcm_hw_rule_muldivk, (void*) 8,
2268 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2269 if (err < 0)
2270 return err;
2271 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2272 snd_pcm_hw_rule_muldivk, (void*) 8,
2273 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2274 if (err < 0)
2275 return err;
2276 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2277 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2278 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2279 if (err < 0)
2280 return err;
2281 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2282 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2283 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2284 if (err < 0)
2285 return err;
2286 return 0;
2287}
2288
Takashi Iwai877211f2005-11-17 13:59:38 +01002289int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Takashi Iwai877211f2005-11-17 13:59:38 +01002291 struct snd_pcm_runtime *runtime = substream->runtime;
2292 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 int err;
2294 unsigned int mask = 0;
2295
2296 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2297 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2298 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2299 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002300 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2302 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2303 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2304 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2305 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2306 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2307 }
2308 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002309 if (err < 0)
2310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002313 if (err < 0)
2314 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002317 if (err < 0)
2318 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2321 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002322 if (err < 0)
2323 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2326 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002327 if (err < 0)
2328 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2331 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002332 if (err < 0)
2333 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2336 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002337 if (err < 0)
2338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2341 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002342 if (err < 0)
2343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2346 snd_pcm_hw_rule_buffer_bytes_max, substream,
2347 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2348 if (err < 0)
2349 return err;
2350
2351 /* FIXME: remove */
2352 if (runtime->dma_bytes) {
2353 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002354 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302355 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
2357
2358 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2359 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2360 snd_pcm_hw_rule_rate, hw,
2361 SNDRV_PCM_HW_PARAM_RATE, -1);
2362 if (err < 0)
2363 return err;
2364 }
2365
2366 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2368
2369 return 0;
2370}
2371
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002372static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002375}
2376
2377void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2378{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002379 substream->ref_count--;
2380 if (substream->ref_count > 0)
2381 return;
2382
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002383 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002384 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002385 if (substream->ops->hw_free &&
2386 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 substream->ops->hw_free(substream);
2388 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002389 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002391 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2392 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002393 if (substream->pcm_release) {
2394 substream->pcm_release(substream);
2395 substream->pcm_release = NULL;
2396 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002397 snd_pcm_detach_substream(substream);
2398}
2399
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002400EXPORT_SYMBOL(snd_pcm_release_substream);
2401
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002402int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2403 struct file *file,
2404 struct snd_pcm_substream **rsubstream)
2405{
2406 struct snd_pcm_substream *substream;
2407 int err;
2408
2409 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2410 if (err < 0)
2411 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002412 if (substream->ref_count > 1) {
2413 *rsubstream = substream;
2414 return 0;
2415 }
2416
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002417 err = snd_pcm_hw_constraints_init(substream);
2418 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002419 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002420 goto error;
2421 }
2422
2423 if ((err = substream->ops->open(substream)) < 0)
2424 goto error;
2425
2426 substream->hw_opened = 1;
2427
2428 err = snd_pcm_hw_constraints_complete(substream);
2429 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002430 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002431 goto error;
2432 }
2433
2434 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002436
2437 error:
2438 snd_pcm_release_substream(substream);
2439 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002442EXPORT_SYMBOL(snd_pcm_open_substream);
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002445 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002446 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Takashi Iwai877211f2005-11-17 13:59:38 +01002448 struct snd_pcm_file *pcm_file;
2449 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002450 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002452 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2453 if (err < 0)
2454 return err;
2455
Takashi Iwai548a6482006-07-31 16:51:51 +02002456 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2457 if (pcm_file == NULL) {
2458 snd_pcm_release_substream(substream);
2459 return -ENOMEM;
2460 }
2461 pcm_file->substream = substream;
2462 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002463 substream->file = pcm_file;
2464 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return 0;
2469}
2470
Clemens Ladischf87135f2005-11-20 14:06:59 +01002471static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Takashi Iwai877211f2005-11-17 13:59:38 +01002473 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002474 int err = nonseekable_open(inode, file);
2475 if (err < 0)
2476 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002477 pcm = snd_lookup_minor_data(iminor(inode),
2478 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002479 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002480 if (pcm)
2481 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002482 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002483}
2484
2485static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2486{
2487 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002488 int err = nonseekable_open(inode, file);
2489 if (err < 0)
2490 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002491 pcm = snd_lookup_minor_data(iminor(inode),
2492 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002493 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002494 if (pcm)
2495 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002496 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002497}
2498
2499static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2500{
2501 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 wait_queue_t wait;
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (pcm == NULL) {
2505 err = -ENODEV;
2506 goto __error1;
2507 }
2508 err = snd_card_file_add(pcm->card, file);
2509 if (err < 0)
2510 goto __error1;
2511 if (!try_module_get(pcm->card->module)) {
2512 err = -EFAULT;
2513 goto __error2;
2514 }
2515 init_waitqueue_entry(&wait, current);
2516 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002517 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002519 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (err >= 0)
2521 break;
2522 if (err == -EAGAIN) {
2523 if (file->f_flags & O_NONBLOCK) {
2524 err = -EBUSY;
2525 break;
2526 }
2527 } else
2528 break;
2529 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002530 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002532 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002533 if (pcm->card->shutdown) {
2534 err = -ENODEV;
2535 break;
2536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (signal_pending(current)) {
2538 err = -ERESTARTSYS;
2539 break;
2540 }
2541 }
2542 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002543 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (err < 0)
2545 goto __error;
2546 return err;
2547
2548 __error:
2549 module_put(pcm->card->module);
2550 __error2:
2551 snd_card_file_remove(pcm->card, file);
2552 __error1:
2553 return err;
2554}
2555
2556static int snd_pcm_release(struct inode *inode, struct file *file)
2557{
Takashi Iwai877211f2005-11-17 13:59:38 +01002558 struct snd_pcm *pcm;
2559 struct snd_pcm_substream *substream;
2560 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 pcm_file = file->private_data;
2563 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002564 if (snd_BUG_ON(!substream))
2565 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002567 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002568 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002569 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002570 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 wake_up(&pcm->open_wait);
2572 module_put(pcm->card->module);
2573 snd_card_file_remove(pcm->card, file);
2574 return 0;
2575}
2576
Takashi Iwaif839cc12017-05-19 20:04:23 +02002577/* check and update PCM state; return 0 or a negative error
2578 * call this inside PCM lock
2579 */
2580static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2581{
2582 switch (substream->runtime->status->state) {
2583 case SNDRV_PCM_STATE_DRAINING:
2584 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2585 return -EBADFD;
2586 /* Fall through */
2587 case SNDRV_PCM_STATE_RUNNING:
2588 return snd_pcm_update_hw_ptr(substream);
2589 case SNDRV_PCM_STATE_PREPARED:
2590 case SNDRV_PCM_STATE_PAUSED:
2591 return 0;
2592 case SNDRV_PCM_STATE_SUSPENDED:
2593 return -ESTRPIPE;
2594 case SNDRV_PCM_STATE_XRUN:
2595 return -EPIPE;
2596 default:
2597 return -EBADFD;
2598 }
2599}
2600
Takashi Iwai9027c462017-05-19 20:22:33 +02002601/* update to the given appl_ptr and call ack callback if needed;
2602 * when an error is returned, take back to the original value
2603 */
2604static int apply_appl_ptr(struct snd_pcm_substream *substream,
2605 snd_pcm_uframes_t appl_ptr)
2606{
2607 struct snd_pcm_runtime *runtime = substream->runtime;
2608 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2609 int ret;
2610
2611 runtime->control->appl_ptr = appl_ptr;
2612 if (substream->ops->ack) {
2613 ret = substream->ops->ack(substream);
2614 if (ret < 0) {
2615 runtime->control->appl_ptr = old_appl_ptr;
2616 return ret;
2617 }
2618 }
2619 return 0;
2620}
2621
2622/* increase the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002623static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2624 snd_pcm_uframes_t frames,
2625 snd_pcm_sframes_t avail)
2626{
2627 struct snd_pcm_runtime *runtime = substream->runtime;
2628 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002629 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002630
2631 if (avail <= 0)
2632 return 0;
2633 if (frames > (snd_pcm_uframes_t)avail)
2634 frames = avail;
2635 appl_ptr = runtime->control->appl_ptr + frames;
2636 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2637 appl_ptr -= runtime->boundary;
Takashi Iwai9027c462017-05-19 20:22:33 +02002638 ret = apply_appl_ptr(substream, appl_ptr);
2639 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002640}
2641
Takashi Iwai9027c462017-05-19 20:22:33 +02002642/* decrease the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002643static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2644 snd_pcm_uframes_t frames,
2645 snd_pcm_sframes_t avail)
2646{
2647 struct snd_pcm_runtime *runtime = substream->runtime;
2648 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002649 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002650
2651 if (avail <= 0)
2652 return 0;
2653 if (frames > (snd_pcm_uframes_t)avail)
2654 frames = avail;
2655 appl_ptr = runtime->control->appl_ptr - frames;
2656 if (appl_ptr < 0)
2657 appl_ptr += runtime->boundary;
Takashi Iwai9027c462017-05-19 20:22:33 +02002658 ret = apply_appl_ptr(substream, appl_ptr);
2659 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002660}
2661
Takashi Iwai877211f2005-11-17 13:59:38 +01002662static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2663 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Takashi Iwai877211f2005-11-17 13:59:38 +01002665 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 if (frames == 0)
2669 return 0;
2670
2671 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002672 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002673 if (!ret)
2674 ret = rewind_appl_ptr(substream, frames,
2675 snd_pcm_playback_hw_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 snd_pcm_stream_unlock_irq(substream);
2677 return ret;
2678}
2679
Takashi Iwai877211f2005-11-17 13:59:38 +01002680static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2681 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
Takashi Iwai877211f2005-11-17 13:59:38 +01002683 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 if (frames == 0)
2687 return 0;
2688
2689 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002690 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002691 if (!ret)
2692 ret = rewind_appl_ptr(substream, frames,
2693 snd_pcm_capture_hw_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 snd_pcm_stream_unlock_irq(substream);
2695 return ret;
2696}
2697
Takashi Iwai877211f2005-11-17 13:59:38 +01002698static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2699 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Takashi Iwai877211f2005-11-17 13:59:38 +01002701 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 if (frames == 0)
2705 return 0;
2706
2707 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002708 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002709 if (!ret)
2710 ret = forward_appl_ptr(substream, frames,
2711 snd_pcm_playback_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 snd_pcm_stream_unlock_irq(substream);
2713 return ret;
2714}
2715
Takashi Iwai877211f2005-11-17 13:59:38 +01002716static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2717 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
Takashi Iwai877211f2005-11-17 13:59:38 +01002719 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 if (frames == 0)
2723 return 0;
2724
2725 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002726 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002727 if (!ret)
2728 ret = forward_appl_ptr(substream, frames,
2729 snd_pcm_capture_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 snd_pcm_stream_unlock_irq(substream);
2731 return ret;
2732}
2733
Takashi Iwai877211f2005-11-17 13:59:38 +01002734static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 int err;
2737
2738 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002739 err = do_pcm_hwsync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 snd_pcm_stream_unlock_irq(substream);
2741 return err;
2742}
2743
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002744static snd_pcm_sframes_t snd_pcm_delay(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Takashi Iwai877211f2005-11-17 13:59:38 +01002746 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 int err;
2748 snd_pcm_sframes_t n = 0;
2749
2750 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002751 err = do_pcm_hwsync(substream);
2752 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2754 n = snd_pcm_playback_hw_avail(runtime);
2755 else
2756 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002757 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
2759 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002760 return err < 0 ? err : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Takashi Iwai877211f2005-11-17 13:59:38 +01002763static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2764 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Takashi Iwai877211f2005-11-17 13:59:38 +01002766 struct snd_pcm_runtime *runtime = substream->runtime;
2767 struct snd_pcm_sync_ptr sync_ptr;
2768 volatile struct snd_pcm_mmap_status *status;
2769 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 int err;
2771
2772 memset(&sync_ptr, 0, sizeof(sync_ptr));
2773 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2774 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002775 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 -07002776 return -EFAULT;
2777 status = runtime->status;
2778 control = runtime->control;
2779 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2780 err = snd_pcm_hwsync(substream);
2781 if (err < 0)
2782 return err;
2783 }
2784 snd_pcm_stream_lock_irq(substream);
Takashi Iwai9027c462017-05-19 20:22:33 +02002785 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2786 err = apply_appl_ptr(substream, sync_ptr.c.control.appl_ptr);
2787 if (err < 0) {
2788 snd_pcm_stream_unlock_irq(substream);
2789 return err;
2790 }
2791 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 sync_ptr.c.control.appl_ptr = control->appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2795 control->avail_min = sync_ptr.c.control.avail_min;
2796 else
2797 sync_ptr.c.control.avail_min = control->avail_min;
2798 sync_ptr.s.status.state = status->state;
2799 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2800 sync_ptr.s.status.tstamp = status->tstamp;
2801 sync_ptr.s.status.suspended_state = status->suspended_state;
2802 snd_pcm_stream_unlock_irq(substream);
2803 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2804 return -EFAULT;
2805 return 0;
2806}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002807
2808static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2809{
2810 struct snd_pcm_runtime *runtime = substream->runtime;
2811 int arg;
2812
2813 if (get_user(arg, _arg))
2814 return -EFAULT;
2815 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2816 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002817 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002818 return 0;
2819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Takashi Iwai0df63e42006-04-28 15:13:41 +02002821static int snd_pcm_common_ioctl1(struct file *file,
2822 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 unsigned int cmd, void __user *arg)
2824{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 switch (cmd) {
2826 case SNDRV_PCM_IOCTL_PVERSION:
2827 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2828 case SNDRV_PCM_IOCTL_INFO:
2829 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002830 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2831 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002832 case SNDRV_PCM_IOCTL_TTSTAMP:
2833 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 case SNDRV_PCM_IOCTL_HW_REFINE:
2835 return snd_pcm_hw_refine_user(substream, arg);
2836 case SNDRV_PCM_IOCTL_HW_PARAMS:
2837 return snd_pcm_hw_params_user(substream, arg);
2838 case SNDRV_PCM_IOCTL_HW_FREE:
2839 return snd_pcm_hw_free(substream);
2840 case SNDRV_PCM_IOCTL_SW_PARAMS:
2841 return snd_pcm_sw_params_user(substream, arg);
2842 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002843 return snd_pcm_status_user(substream, arg, false);
2844 case SNDRV_PCM_IOCTL_STATUS_EXT:
2845 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2847 return snd_pcm_channel_info_user(substream, arg);
2848 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002849 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 case SNDRV_PCM_IOCTL_RESET:
2851 return snd_pcm_reset(substream);
2852 case SNDRV_PCM_IOCTL_START:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002853 return snd_pcm_start_lock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 case SNDRV_PCM_IOCTL_LINK:
2855 return snd_pcm_link(substream, (int)(unsigned long) arg);
2856 case SNDRV_PCM_IOCTL_UNLINK:
2857 return snd_pcm_unlink(substream);
2858 case SNDRV_PCM_IOCTL_RESUME:
2859 return snd_pcm_resume(substream);
2860 case SNDRV_PCM_IOCTL_XRUN:
2861 return snd_pcm_xrun(substream);
2862 case SNDRV_PCM_IOCTL_HWSYNC:
2863 return snd_pcm_hwsync(substream);
2864 case SNDRV_PCM_IOCTL_DELAY:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002865 {
2866 snd_pcm_sframes_t delay = snd_pcm_delay(substream);
2867 snd_pcm_sframes_t __user *res = arg;
2868
2869 if (delay < 0)
2870 return delay;
2871 if (put_user(delay, res))
2872 return -EFAULT;
2873 return 0;
2874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 case SNDRV_PCM_IOCTL_SYNC_PTR:
2876 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002877#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2879 return snd_pcm_hw_refine_old_user(substream, arg);
2880 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2881 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002882#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002884 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 case SNDRV_PCM_IOCTL_DROP:
2886 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002887 case SNDRV_PCM_IOCTL_PAUSE:
2888 {
2889 int res;
2890 snd_pcm_stream_lock_irq(substream);
2891 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2892 snd_pcm_stream_unlock_irq(substream);
2893 return res;
2894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002896 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return -ENOTTY;
2898}
2899
Takashi Iwai0df63e42006-04-28 15:13:41 +02002900static int snd_pcm_playback_ioctl1(struct file *file,
2901 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 unsigned int cmd, void __user *arg)
2903{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002904 if (snd_BUG_ON(!substream))
2905 return -ENXIO;
2906 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2907 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 switch (cmd) {
2909 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2910 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002911 struct snd_xferi xferi;
2912 struct snd_xferi __user *_xferi = arg;
2913 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 snd_pcm_sframes_t result;
2915 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2916 return -EBADFD;
2917 if (put_user(0, &_xferi->result))
2918 return -EFAULT;
2919 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2920 return -EFAULT;
2921 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2922 __put_user(result, &_xferi->result);
2923 return result < 0 ? result : 0;
2924 }
2925 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2926 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002927 struct snd_xfern xfern;
2928 struct snd_xfern __user *_xfern = arg;
2929 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 void __user **bufs;
2931 snd_pcm_sframes_t result;
2932 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2933 return -EBADFD;
2934 if (runtime->channels > 128)
2935 return -EINVAL;
2936 if (put_user(0, &_xfern->result))
2937 return -EFAULT;
2938 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2939 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002940
2941 bufs = memdup_user(xfern.bufs,
2942 sizeof(void *) * runtime->channels);
2943 if (IS_ERR(bufs))
2944 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2946 kfree(bufs);
2947 __put_user(result, &_xfern->result);
2948 return result < 0 ? result : 0;
2949 }
2950 case SNDRV_PCM_IOCTL_REWIND:
2951 {
2952 snd_pcm_uframes_t frames;
2953 snd_pcm_uframes_t __user *_frames = arg;
2954 snd_pcm_sframes_t result;
2955 if (get_user(frames, _frames))
2956 return -EFAULT;
2957 if (put_user(0, _frames))
2958 return -EFAULT;
2959 result = snd_pcm_playback_rewind(substream, frames);
2960 __put_user(result, _frames);
2961 return result < 0 ? result : 0;
2962 }
2963 case SNDRV_PCM_IOCTL_FORWARD:
2964 {
2965 snd_pcm_uframes_t frames;
2966 snd_pcm_uframes_t __user *_frames = arg;
2967 snd_pcm_sframes_t result;
2968 if (get_user(frames, _frames))
2969 return -EFAULT;
2970 if (put_user(0, _frames))
2971 return -EFAULT;
2972 result = snd_pcm_playback_forward(substream, frames);
2973 __put_user(result, _frames);
2974 return result < 0 ? result : 0;
2975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002977 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
Takashi Iwai0df63e42006-04-28 15:13:41 +02002980static int snd_pcm_capture_ioctl1(struct file *file,
2981 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 unsigned int cmd, void __user *arg)
2983{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002984 if (snd_BUG_ON(!substream))
2985 return -ENXIO;
2986 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2987 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 switch (cmd) {
2989 case SNDRV_PCM_IOCTL_READI_FRAMES:
2990 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002991 struct snd_xferi xferi;
2992 struct snd_xferi __user *_xferi = arg;
2993 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 snd_pcm_sframes_t result;
2995 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2996 return -EBADFD;
2997 if (put_user(0, &_xferi->result))
2998 return -EFAULT;
2999 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3000 return -EFAULT;
3001 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3002 __put_user(result, &_xferi->result);
3003 return result < 0 ? result : 0;
3004 }
3005 case SNDRV_PCM_IOCTL_READN_FRAMES:
3006 {
Takashi Iwai877211f2005-11-17 13:59:38 +01003007 struct snd_xfern xfern;
3008 struct snd_xfern __user *_xfern = arg;
3009 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 void *bufs;
3011 snd_pcm_sframes_t result;
3012 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3013 return -EBADFD;
3014 if (runtime->channels > 128)
3015 return -EINVAL;
3016 if (put_user(0, &_xfern->result))
3017 return -EFAULT;
3018 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3019 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08003020
3021 bufs = memdup_user(xfern.bufs,
3022 sizeof(void *) * runtime->channels);
3023 if (IS_ERR(bufs))
3024 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3026 kfree(bufs);
3027 __put_user(result, &_xfern->result);
3028 return result < 0 ? result : 0;
3029 }
3030 case SNDRV_PCM_IOCTL_REWIND:
3031 {
3032 snd_pcm_uframes_t frames;
3033 snd_pcm_uframes_t __user *_frames = arg;
3034 snd_pcm_sframes_t result;
3035 if (get_user(frames, _frames))
3036 return -EFAULT;
3037 if (put_user(0, _frames))
3038 return -EFAULT;
3039 result = snd_pcm_capture_rewind(substream, frames);
3040 __put_user(result, _frames);
3041 return result < 0 ? result : 0;
3042 }
3043 case SNDRV_PCM_IOCTL_FORWARD:
3044 {
3045 snd_pcm_uframes_t frames;
3046 snd_pcm_uframes_t __user *_frames = arg;
3047 snd_pcm_sframes_t result;
3048 if (get_user(frames, _frames))
3049 return -EFAULT;
3050 if (put_user(0, _frames))
3051 return -EFAULT;
3052 result = snd_pcm_capture_forward(substream, frames);
3053 __put_user(result, _frames);
3054 return result < 0 ? result : 0;
3055 }
3056 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02003057 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
Takashi Iwai877211f2005-11-17 13:59:38 +01003060static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
3061 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Takashi Iwai877211f2005-11-17 13:59:38 +01003063 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065 pcm_file = file->private_data;
3066
3067 if (((cmd >> 8) & 0xff) != 'A')
3068 return -ENOTTY;
3069
Takashi Iwai0df63e42006-04-28 15:13:41 +02003070 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3071 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
Takashi Iwai877211f2005-11-17 13:59:38 +01003074static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3075 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
Takashi Iwai877211f2005-11-17 13:59:38 +01003077 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 pcm_file = file->private_data;
3080
3081 if (((cmd >> 8) & 0xff) != 'A')
3082 return -ENOTTY;
3083
Takashi Iwai0df63e42006-04-28 15:13:41 +02003084 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3085 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003088/**
3089 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3090 * @substream: PCM substream
3091 * @cmd: IOCTL cmd
3092 * @arg: IOCTL argument
3093 *
3094 * The function is provided primarily for OSS layer and USB gadget drivers,
3095 * and it allows only the limited set of ioctls (hw_params, sw_params,
3096 * prepare, start, drain, drop, forward).
3097 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003098int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 unsigned int cmd, void *arg)
3100{
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003101 snd_pcm_uframes_t *frames = arg;
3102 snd_pcm_sframes_t result;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003103
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003104 switch (cmd) {
3105 case SNDRV_PCM_IOCTL_FORWARD:
3106 {
3107 /* provided only for OSS; capture-only and no value returned */
3108 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3109 return -EINVAL;
3110 result = snd_pcm_capture_forward(substream, *frames);
3111 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
Takashi Iwaic2c86a92017-05-10 14:33:44 +02003113 case SNDRV_PCM_IOCTL_HW_PARAMS:
3114 return snd_pcm_hw_params(substream, arg);
3115 case SNDRV_PCM_IOCTL_SW_PARAMS:
3116 return snd_pcm_sw_params(substream, arg);
3117 case SNDRV_PCM_IOCTL_PREPARE:
3118 return snd_pcm_prepare(substream, NULL);
3119 case SNDRV_PCM_IOCTL_START:
3120 return snd_pcm_start_lock_irq(substream);
3121 case SNDRV_PCM_IOCTL_DRAIN:
3122 return snd_pcm_drain(substream, NULL);
3123 case SNDRV_PCM_IOCTL_DROP:
3124 return snd_pcm_drop(substream);
3125 case SNDRV_PCM_IOCTL_DELAY:
3126 {
3127 result = snd_pcm_delay(substream);
3128 if (result < 0)
3129 return result;
3130 *frames = result;
3131 return 0;
3132 }
3133 default:
3134 return -EINVAL;
3135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003137EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3138
Takashi Iwai877211f2005-11-17 13:59:38 +01003139static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3140 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
Takashi Iwai877211f2005-11-17 13:59:38 +01003142 struct snd_pcm_file *pcm_file;
3143 struct snd_pcm_substream *substream;
3144 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 snd_pcm_sframes_t result;
3146
3147 pcm_file = file->private_data;
3148 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003149 if (PCM_RUNTIME_CHECK(substream))
3150 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 runtime = substream->runtime;
3152 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3153 return -EBADFD;
3154 if (!frame_aligned(runtime, count))
3155 return -EINVAL;
3156 count = bytes_to_frames(runtime, count);
3157 result = snd_pcm_lib_read(substream, buf, count);
3158 if (result > 0)
3159 result = frames_to_bytes(runtime, result);
3160 return result;
3161}
3162
Takashi Iwai877211f2005-11-17 13:59:38 +01003163static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3164 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Takashi Iwai877211f2005-11-17 13:59:38 +01003166 struct snd_pcm_file *pcm_file;
3167 struct snd_pcm_substream *substream;
3168 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 snd_pcm_sframes_t result;
3170
3171 pcm_file = file->private_data;
3172 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003173 if (PCM_RUNTIME_CHECK(substream))
3174 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003176 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3177 return -EBADFD;
3178 if (!frame_aligned(runtime, count))
3179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 count = bytes_to_frames(runtime, count);
3181 result = snd_pcm_lib_write(substream, buf, count);
3182 if (result > 0)
3183 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return result;
3185}
3186
Al Viro1c65d982015-04-04 00:19:32 -04003187static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Takashi Iwai877211f2005-11-17 13:59:38 +01003189 struct snd_pcm_file *pcm_file;
3190 struct snd_pcm_substream *substream;
3191 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 snd_pcm_sframes_t result;
3193 unsigned long i;
3194 void __user **bufs;
3195 snd_pcm_uframes_t frames;
3196
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003197 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003199 if (PCM_RUNTIME_CHECK(substream))
3200 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 runtime = substream->runtime;
3202 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3203 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003204 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003206 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003208 if (!frame_aligned(runtime, to->iov->iov_len))
3209 return -EINVAL;
3210 frames = bytes_to_samples(runtime, to->iov->iov_len);
3211 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (bufs == NULL)
3213 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003214 for (i = 0; i < to->nr_segs; ++i)
3215 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 result = snd_pcm_lib_readv(substream, bufs, frames);
3217 if (result > 0)
3218 result = frames_to_bytes(runtime, result);
3219 kfree(bufs);
3220 return result;
3221}
3222
Al Viro1c65d982015-04-04 00:19:32 -04003223static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Takashi Iwai877211f2005-11-17 13:59:38 +01003225 struct snd_pcm_file *pcm_file;
3226 struct snd_pcm_substream *substream;
3227 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 snd_pcm_sframes_t result;
3229 unsigned long i;
3230 void __user **bufs;
3231 snd_pcm_uframes_t frames;
3232
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003233 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003235 if (PCM_RUNTIME_CHECK(substream))
3236 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003238 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3239 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003240 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003241 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003242 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3243 !frame_aligned(runtime, from->iov->iov_len))
3244 return -EINVAL;
3245 frames = bytes_to_samples(runtime, from->iov->iov_len);
3246 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 if (bufs == NULL)
3248 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003249 for (i = 0; i < from->nr_segs; ++i)
3250 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 result = snd_pcm_lib_writev(substream, bufs, frames);
3252 if (result > 0)
3253 result = frames_to_bytes(runtime, result);
3254 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 return result;
3256}
3257
3258static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3259{
Takashi Iwai877211f2005-11-17 13:59:38 +01003260 struct snd_pcm_file *pcm_file;
3261 struct snd_pcm_substream *substream;
3262 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 unsigned int mask;
3264 snd_pcm_uframes_t avail;
3265
3266 pcm_file = file->private_data;
3267
3268 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003269 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003270 return POLLOUT | POLLWRNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 runtime = substream->runtime;
3272
3273 poll_wait(file, &runtime->sleep, wait);
3274
3275 snd_pcm_stream_lock_irq(substream);
3276 avail = snd_pcm_playback_avail(runtime);
3277 switch (runtime->status->state) {
3278 case SNDRV_PCM_STATE_RUNNING:
3279 case SNDRV_PCM_STATE_PREPARED:
3280 case SNDRV_PCM_STATE_PAUSED:
3281 if (avail >= runtime->control->avail_min) {
3282 mask = POLLOUT | POLLWRNORM;
3283 break;
3284 }
3285 /* Fall through */
3286 case SNDRV_PCM_STATE_DRAINING:
3287 mask = 0;
3288 break;
3289 default:
3290 mask = POLLOUT | POLLWRNORM | POLLERR;
3291 break;
3292 }
3293 snd_pcm_stream_unlock_irq(substream);
3294 return mask;
3295}
3296
3297static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3298{
Takashi Iwai877211f2005-11-17 13:59:38 +01003299 struct snd_pcm_file *pcm_file;
3300 struct snd_pcm_substream *substream;
3301 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 unsigned int mask;
3303 snd_pcm_uframes_t avail;
3304
3305 pcm_file = file->private_data;
3306
3307 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003308 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003309 return POLLIN | POLLRDNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 runtime = substream->runtime;
3311
3312 poll_wait(file, &runtime->sleep, wait);
3313
3314 snd_pcm_stream_lock_irq(substream);
3315 avail = snd_pcm_capture_avail(runtime);
3316 switch (runtime->status->state) {
3317 case SNDRV_PCM_STATE_RUNNING:
3318 case SNDRV_PCM_STATE_PREPARED:
3319 case SNDRV_PCM_STATE_PAUSED:
3320 if (avail >= runtime->control->avail_min) {
3321 mask = POLLIN | POLLRDNORM;
3322 break;
3323 }
3324 mask = 0;
3325 break;
3326 case SNDRV_PCM_STATE_DRAINING:
3327 if (avail > 0) {
3328 mask = POLLIN | POLLRDNORM;
3329 break;
3330 }
3331 /* Fall through */
3332 default:
3333 mask = POLLIN | POLLRDNORM | POLLERR;
3334 break;
3335 }
3336 snd_pcm_stream_unlock_irq(substream);
3337 return mask;
3338}
3339
3340/*
3341 * mmap support
3342 */
3343
3344/*
3345 * Only on coherent architectures, we can mmap the status and the control records
3346 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3347 */
3348#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3349/*
3350 * mmap status record
3351 */
Dave Jiang11bac802017-02-24 14:56:41 -08003352static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
Dave Jiang11bac802017-02-24 14:56:41 -08003354 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003355 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003358 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003360 vmf->page = virt_to_page(runtime->status);
3361 get_page(vmf->page);
3362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
3364
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003365static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003367 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368};
3369
Takashi Iwai877211f2005-11-17 13:59:38 +01003370static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 struct vm_area_struct *area)
3372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 long size;
3374 if (!(area->vm_flags & VM_READ))
3375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003377 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return -EINVAL;
3379 area->vm_ops = &snd_pcm_vm_ops_status;
3380 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003381 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return 0;
3383}
3384
3385/*
3386 * mmap control record
3387 */
Dave Jiang11bac802017-02-24 14:56:41 -08003388static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
Dave Jiang11bac802017-02-24 14:56:41 -08003390 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003391 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003394 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003396 vmf->page = virt_to_page(runtime->control);
3397 get_page(vmf->page);
3398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003401static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003403 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404};
3405
Takashi Iwai877211f2005-11-17 13:59:38 +01003406static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 struct vm_area_struct *area)
3408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 long size;
3410 if (!(area->vm_flags & VM_READ))
3411 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003413 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return -EINVAL;
3415 area->vm_ops = &snd_pcm_vm_ops_control;
3416 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003417 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return 0;
3419}
3420#else /* ! coherent mmap */
3421/*
3422 * don't support mmap for status and control records.
3423 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003424static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 struct vm_area_struct *area)
3426{
3427 return -ENXIO;
3428}
Takashi Iwai877211f2005-11-17 13:59:38 +01003429static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 struct vm_area_struct *area)
3431{
3432 return -ENXIO;
3433}
3434#endif /* coherent mmap */
3435
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003436static inline struct page *
3437snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3438{
3439 void *vaddr = substream->runtime->dma_area + ofs;
3440 return virt_to_page(vaddr);
3441}
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003444 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 */
Dave Jiang11bac802017-02-24 14:56:41 -08003446static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
Dave Jiang11bac802017-02-24 14:56:41 -08003448 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003449 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 unsigned long offset;
3451 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 size_t dma_bytes;
3453
3454 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003455 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003457 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3459 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003460 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003461 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003463 else
3464 page = snd_pcm_default_page_ops(substream, offset);
3465 if (!page)
3466 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003467 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003468 vmf->page = page;
3469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
Takashi Iwai657b1982009-11-26 12:40:21 +01003472static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3473 .open = snd_pcm_mmap_data_open,
3474 .close = snd_pcm_mmap_data_close,
3475};
3476
3477static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 .open = snd_pcm_mmap_data_open,
3479 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003480 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481};
3482
3483/*
3484 * mmap the DMA buffer on RAM
3485 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003486
3487/**
3488 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3489 * @substream: PCM substream
3490 * @area: VMA
3491 *
3492 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3493 * this function is invoked implicitly.
3494 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003495int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3496 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003498 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003499#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003500 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3501 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3502 return remap_pfn_range(area, area->vm_start,
3503 substream->dma_buffer.addr >> PAGE_SHIFT,
3504 area->vm_end - area->vm_start, area->vm_page_prot);
3505 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003506#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003507#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003508 if (!substream->ops->page &&
3509 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3510 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3511 area,
3512 substream->runtime->dma_area,
3513 substream->runtime->dma_addr,
3514 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003515#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003516 /* mmap with fault handler */
3517 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return 0;
3519}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003520EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522/*
3523 * mmap the DMA buffer on I/O memory area
3524 */
3525#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003526/**
3527 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3528 * @substream: PCM substream
3529 * @area: VMA
3530 *
3531 * When your hardware uses the iomapped pages as the hardware buffer and
3532 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3533 * is supposed to work only on limited architectures.
3534 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003535int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3536 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003538 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003541 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003543
3544EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545#endif /* SNDRV_PCM_INFO_MMAP */
3546
3547/*
3548 * mmap DMA buffer
3549 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003550int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 struct vm_area_struct *area)
3552{
Takashi Iwai877211f2005-11-17 13:59:38 +01003553 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 long size;
3555 unsigned long offset;
3556 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003557 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3560 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3561 return -EINVAL;
3562 } else {
3563 if (!(area->vm_flags & VM_READ))
3564 return -EINVAL;
3565 }
3566 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3568 return -EBADFD;
3569 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3570 return -ENXIO;
3571 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3572 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3573 return -EINVAL;
3574 size = area->vm_end - area->vm_start;
3575 offset = area->vm_pgoff << PAGE_SHIFT;
3576 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3577 if ((size_t)size > dma_bytes)
3578 return -EINVAL;
3579 if (offset > dma_bytes - size)
3580 return -EINVAL;
3581
Takashi Iwai657b1982009-11-26 12:40:21 +01003582 area->vm_ops = &snd_pcm_vm_ops_data;
3583 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003585 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003587 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003588 if (!err)
3589 atomic_inc(&substream->mmap_count);
3590 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003593EXPORT_SYMBOL(snd_pcm_mmap_data);
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3596{
Takashi Iwai877211f2005-11-17 13:59:38 +01003597 struct snd_pcm_file * pcm_file;
3598 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 unsigned long offset;
3600
3601 pcm_file = file->private_data;
3602 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003603 if (PCM_RUNTIME_CHECK(substream))
3604 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606 offset = area->vm_pgoff << PAGE_SHIFT;
3607 switch (offset) {
3608 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003609 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 return -ENXIO;
3611 return snd_pcm_mmap_status(substream, file, area);
3612 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003613 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 return -ENXIO;
3615 return snd_pcm_mmap_control(substream, file, area);
3616 default:
3617 return snd_pcm_mmap_data(substream, file, area);
3618 }
3619 return 0;
3620}
3621
3622static int snd_pcm_fasync(int fd, struct file * file, int on)
3623{
Takashi Iwai877211f2005-11-17 13:59:38 +01003624 struct snd_pcm_file * pcm_file;
3625 struct snd_pcm_substream *substream;
3626 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
3628 pcm_file = file->private_data;
3629 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003630 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003631 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003633 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
3636/*
3637 * ioctl32 compat
3638 */
3639#ifdef CONFIG_COMPAT
3640#include "pcm_compat.c"
3641#else
3642#define snd_pcm_ioctl_compat NULL
3643#endif
3644
3645/*
3646 * To be removed helpers to keep binary compatibility
3647 */
3648
Takashi Iwai59d48582005-12-01 10:51:58 +01003649#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3651#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3652
Takashi Iwai877211f2005-11-17 13:59:38 +01003653static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3654 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
3656 unsigned int i;
3657
3658 memset(params, 0, sizeof(*params));
3659 params->flags = oparams->flags;
3660 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3661 params->masks[i].bits[0] = oparams->masks[i];
3662 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3663 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3664 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3665 params->info = oparams->info;
3666 params->msbits = oparams->msbits;
3667 params->rate_num = oparams->rate_num;
3668 params->rate_den = oparams->rate_den;
3669 params->fifo_size = oparams->fifo_size;
3670}
3671
Takashi Iwai877211f2005-11-17 13:59:38 +01003672static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3673 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
3675 unsigned int i;
3676
3677 memset(oparams, 0, sizeof(*oparams));
3678 oparams->flags = params->flags;
3679 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3680 oparams->masks[i] = params->masks[i].bits[0];
3681 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3682 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3683 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3684 oparams->info = params->info;
3685 oparams->msbits = params->msbits;
3686 oparams->rate_num = params->rate_num;
3687 oparams->rate_den = params->rate_den;
3688 oparams->fifo_size = params->fifo_size;
3689}
3690
Takashi Iwai877211f2005-11-17 13:59:38 +01003691static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3692 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
Takashi Iwai877211f2005-11-17 13:59:38 +01003694 struct snd_pcm_hw_params *params;
3695 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 int err;
3697
3698 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003699 if (!params)
3700 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Li Zefanef44a1e2009-04-10 09:43:08 +08003702 oparams = memdup_user(_oparams, sizeof(*oparams));
3703 if (IS_ERR(oparams)) {
3704 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 goto out;
3706 }
3707 snd_pcm_hw_convert_from_old_params(params, oparams);
3708 err = snd_pcm_hw_refine(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003709 if (err < 0)
3710 goto out_old;
Li Zefanef44a1e2009-04-10 09:43:08 +08003711
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003712 err = fixup_unreferenced_params(substream, params);
3713 if (err < 0)
3714 goto out_old;
3715
3716 snd_pcm_hw_convert_to_old_params(oparams, params);
3717 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3718 err = -EFAULT;
3719out_old:
Li Zefanef44a1e2009-04-10 09:43:08 +08003720 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721out:
3722 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 return err;
3724}
3725
Takashi Iwai877211f2005-11-17 13:59:38 +01003726static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3727 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
Takashi Iwai877211f2005-11-17 13:59:38 +01003729 struct snd_pcm_hw_params *params;
3730 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 int err;
3732
3733 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003734 if (!params)
3735 return -ENOMEM;
3736
3737 oparams = memdup_user(_oparams, sizeof(*oparams));
3738 if (IS_ERR(oparams)) {
3739 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 goto out;
3741 }
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 snd_pcm_hw_convert_from_old_params(params, oparams);
3744 err = snd_pcm_hw_params(substream, params);
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003745 if (err < 0)
3746 goto out_old;
Li Zefanef44a1e2009-04-10 09:43:08 +08003747
Takashi Sakamotof74ae152017-06-11 23:56:12 +09003748 snd_pcm_hw_convert_to_old_params(oparams, params);
3749 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3750 err = -EFAULT;
3751out_old:
Li Zefanef44a1e2009-04-10 09:43:08 +08003752 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753out:
3754 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return err;
3756}
Takashi Iwai59d48582005-12-01 10:51:58 +01003757#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Cliff Cai70036092008-09-03 10:54:36 +02003759#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003760static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3761 unsigned long addr,
3762 unsigned long len,
3763 unsigned long pgoff,
3764 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003765{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003766 struct snd_pcm_file *pcm_file = file->private_data;
3767 struct snd_pcm_substream *substream = pcm_file->substream;
3768 struct snd_pcm_runtime *runtime = substream->runtime;
3769 unsigned long offset = pgoff << PAGE_SHIFT;
3770
3771 switch (offset) {
3772 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3773 return (unsigned long)runtime->status;
3774 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3775 return (unsigned long)runtime->control;
3776 default:
3777 return (unsigned long)runtime->dma_area + offset;
3778 }
Cliff Cai70036092008-09-03 10:54:36 +02003779}
3780#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003781# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003782#endif
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784/*
3785 * Register section
3786 */
3787
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003788const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003790 .owner = THIS_MODULE,
3791 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003792 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003793 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003794 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003795 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003796 .poll = snd_pcm_playback_poll,
3797 .unlocked_ioctl = snd_pcm_playback_ioctl,
3798 .compat_ioctl = snd_pcm_ioctl_compat,
3799 .mmap = snd_pcm_mmap,
3800 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003801 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 },
3803 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003804 .owner = THIS_MODULE,
3805 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003806 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003807 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003808 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003809 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003810 .poll = snd_pcm_capture_poll,
3811 .unlocked_ioctl = snd_pcm_capture_ioctl,
3812 .compat_ioctl = snd_pcm_ioctl_compat,
3813 .mmap = snd_pcm_mmap,
3814 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003815 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 }
3817};