blob: 40560e579d33faa7f788e992a8bb0724db3aceed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040023#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/file.h>
25#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/time.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020028#include <linux/pm_qos.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010029#include <linux/io.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010030#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/timer.h>
37#include <sound/minors.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080038#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Sakamoto2c4842d2017-05-26 09:30:46 +090040#include "pcm_local.h"
41
Takashi Sakamoto37567c52017-06-07 08:46:44 +090042#ifdef CONFIG_SND_DEBUG
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090043#define CREATE_TRACE_POINTS
44#include "pcm_param_trace.h"
Takashi Sakamoto37567c52017-06-07 08:46:44 +090045#else
46#define trace_hw_mask_param_enabled() 0
47#define trace_hw_interval_param_enabled() 0
48#define trace_hw_mask_param(substream, type, index, prev, curr)
49#define trace_hw_interval_param(substream, type, index, prev, curr)
50#endif
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +090051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * Compatibility
54 */
55
Takashi Iwai877211f2005-11-17 13:59:38 +010056struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned int flags;
58 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
59 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010060 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
62 unsigned int rmask;
63 unsigned int cmask;
64 unsigned int info;
65 unsigned int msbits;
66 unsigned int rate_num;
67 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010068 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned char reserved[64];
70};
71
Takashi Iwai59d48582005-12-01 10:51:58 +010072#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010073#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
74#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Takashi Iwai877211f2005-11-17 13:59:38 +010076static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
77 struct snd_pcm_hw_params_old __user * _oparams);
78static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
79 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010080#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010081static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 *
85 */
86
Takashi Iwai7af142f2014-09-01 11:19:37 +020087static DEFINE_RWLOCK(snd_pcm_link_rwlock);
88static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwai67ec10722016-02-17 14:30:26 +010090/* Writer in rwsem may block readers even during its waiting in queue,
91 * and this may lead to a deadlock when the code path takes read sem
92 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
93 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
94 * spin until it gets the lock.
95 */
96static inline void down_write_nonblock(struct rw_semaphore *lock)
97{
98 while (!down_write_trylock(lock))
99 cond_resched();
100}
101
Takashi Iwai30b771c2014-10-30 15:02:50 +0100102/**
103 * snd_pcm_stream_lock - Lock the PCM stream
104 * @substream: PCM substream
105 *
106 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
107 * flag of the given substream. This also takes the global link rw lock
108 * (or rw sem), too, for avoiding the race with linked streams.
109 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200110void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
111{
112 if (substream->pcm->nonatomic) {
Takashi Iwai67756e32015-07-17 15:22:33 +0200113 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200114 mutex_lock(&substream->self_group.mutex);
115 } else {
116 read_lock(&snd_pcm_link_rwlock);
117 spin_lock(&substream->self_group.lock);
118 }
119}
120EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
121
Takashi Iwai30b771c2014-10-30 15:02:50 +0100122/**
123 * snd_pcm_stream_lock - Unlock the PCM stream
124 * @substream: PCM substream
125 *
126 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
127 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200128void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
129{
130 if (substream->pcm->nonatomic) {
131 mutex_unlock(&substream->self_group.mutex);
132 up_read(&snd_pcm_link_rwsem);
133 } else {
134 spin_unlock(&substream->self_group.lock);
135 read_unlock(&snd_pcm_link_rwlock);
136 }
137}
138EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
139
Takashi Iwai30b771c2014-10-30 15:02:50 +0100140/**
141 * snd_pcm_stream_lock_irq - Lock the PCM stream
142 * @substream: PCM substream
143 *
144 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
145 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
146 * as snd_pcm_stream_lock().
147 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200148void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
149{
150 if (!substream->pcm->nonatomic)
151 local_irq_disable();
152 snd_pcm_stream_lock(substream);
153}
154EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
155
Takashi Iwai30b771c2014-10-30 15:02:50 +0100156/**
157 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
158 * @substream: PCM substream
159 *
160 * This is a counter-part of snd_pcm_stream_lock_irq().
161 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200162void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
163{
164 snd_pcm_stream_unlock(substream);
165 if (!substream->pcm->nonatomic)
166 local_irq_enable();
167}
168EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
169
170unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
171{
172 unsigned long flags = 0;
173 if (!substream->pcm->nonatomic)
174 local_irq_save(flags);
175 snd_pcm_stream_lock(substream);
176 return flags;
177}
178EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
179
Takashi Iwai30b771c2014-10-30 15:02:50 +0100180/**
181 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
182 * @substream: PCM substream
183 * @flags: irq flags
184 *
185 * This is a counter-part of snd_pcm_stream_lock_irqsave().
186 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200187void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
188 unsigned long flags)
189{
190 snd_pcm_stream_unlock(substream);
191 if (!substream->pcm->nonatomic)
192 local_irq_restore(flags);
193}
194EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Takashi Iwai877211f2005-11-17 13:59:38 +0100196int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Takashi Iwai877211f2005-11-17 13:59:38 +0100198 struct snd_pcm_runtime *runtime;
199 struct snd_pcm *pcm = substream->pcm;
200 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 memset(info, 0, sizeof(*info));
203 info->card = pcm->card->number;
204 info->device = pcm->device;
205 info->stream = substream->stream;
206 info->subdevice = substream->number;
207 strlcpy(info->id, pcm->id, sizeof(info->id));
208 strlcpy(info->name, pcm->name, sizeof(info->name));
209 info->dev_class = pcm->dev_class;
210 info->dev_subclass = pcm->dev_subclass;
211 info->subdevices_count = pstr->substream_count;
212 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
213 strlcpy(info->subname, substream->name, sizeof(info->subname));
214 runtime = substream->runtime;
215 /* AB: FIXME!!! This is definitely nonsense */
216 if (runtime) {
217 info->sync = runtime->sync;
218 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
219 }
220 return 0;
221}
222
Takashi Iwai877211f2005-11-17 13:59:38 +0100223int snd_pcm_info_user(struct snd_pcm_substream *substream,
224 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Takashi Iwai877211f2005-11-17 13:59:38 +0100226 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int err;
228
229 info = kmalloc(sizeof(*info), GFP_KERNEL);
230 if (! info)
231 return -ENOMEM;
232 err = snd_pcm_info(substream, info);
233 if (err >= 0) {
234 if (copy_to_user(_info, info, sizeof(*info)))
235 err = -EFAULT;
236 }
237 kfree(info);
238 return err;
239}
240
Takashi Iwai63825f32014-10-22 12:04:46 +0200241static bool hw_support_mmap(struct snd_pcm_substream *substream)
242{
243 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
244 return false;
245 /* check architectures that return -EINVAL from dma_mmap_coherent() */
246 /* FIXME: this should be some global flag */
247#if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
248 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
249 if (!substream->ops->mmap &&
250 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
251 return false;
252#endif
253 return true;
254}
255
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900256static int constrain_mask_params(struct snd_pcm_substream *substream,
257 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900259 struct snd_pcm_hw_constraints *constrs =
260 &substream->runtime->hw_constraints;
261 struct snd_mask *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int k;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900263 struct snd_mask old_mask;
264 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
267 m = hw_param_mask(params, k);
268 if (snd_mask_empty(m))
269 return -EINVAL;
270 if (!(params->rmask & (1 << k)))
271 continue;
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900272
273 if (trace_hw_mask_param_enabled())
274 old_mask = *m;
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 changed = snd_mask_refine(m, constrs_mask(constrs, k));
Takashi Sakamotoc6706de2017-06-07 08:46:45 +0900277
Takashi Sakamotobe4e31d2017-06-07 08:46:43 +0900278 trace_hw_mask_param(substream, k, 0, &old_mask, m);
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (changed)
281 params->cmask |= 1 << k;
282 if (changed < 0)
283 return changed;
284 }
285
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900286 return 0;
287}
288
Takashi Sakamoto3432fa02017-06-09 06:37:00 +0900289static int constrain_interval_params(struct snd_pcm_substream *substream,
290 struct snd_pcm_hw_params *params)
291{
292 struct snd_pcm_hw_constraints *constrs =
293 &substream->runtime->hw_constraints;
294 struct snd_interval *i;
295 unsigned int k;
296 struct snd_interval old_interval;
297 int changed;
298
299 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
300 i = hw_param_interval(params, k);
301 if (snd_interval_empty(i))
302 return -EINVAL;
303 if (!(params->rmask & (1 << k)))
304 continue;
305
306 if (trace_hw_interval_param_enabled())
307 old_interval = *i;
308
309 changed = snd_interval_refine(i, constrs_interval(constrs, k));
310
311 trace_hw_interval_param(substream, k, 0, &old_interval, i);
312
313 if (changed)
314 params->cmask |= 1 << k;
315 if (changed < 0)
316 return changed;
317 }
318
319 return 0;
320}
321
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900322static int constrain_params_by_rules(struct snd_pcm_substream *substream,
323 struct snd_pcm_hw_params *params)
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900324{
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900325 struct snd_pcm_hw_constraints *constrs =
326 &substream->runtime->hw_constraints;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900327 unsigned int k;
Takashi Sakamoto561e1ca2017-06-09 06:36:59 +0900328 unsigned int rstamps[constrs->rules_num];
329 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
330 unsigned int stamp = 2;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900331 struct snd_pcm_hw_rule *r;
332 unsigned int d;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900333 struct snd_mask old_mask;
334 struct snd_interval old_interval;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900335 bool again;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900336 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 for (k = 0; k < constrs->rules_num; k++)
339 rstamps[k] = 0;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900340 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900342retry:
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900343 again = false;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900344 for (k = 0; k < constrs->rules_num; k++) {
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900345 r = &constrs->rules[k];
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900346 if (r->cond && !(r->cond & params->flags))
347 continue;
348 for (d = 0; r->deps[d] >= 0; d++) {
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900349 if (vstamps[r->deps[d]] > rstamps[k])
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Takashi Sakamotod656b4a2017-06-09 06:37:03 +0900352 if (r->deps[d] < 0)
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900353 continue;
354
355 if (trace_hw_mask_param_enabled()) {
356 if (hw_is_mask(r->var))
357 old_mask = *hw_param_mask(params, r->var);
358 }
359 if (trace_hw_interval_param_enabled()) {
360 if (hw_is_interval(r->var))
361 old_interval = *hw_param_interval(params, r->var);
362 }
363
364 changed = r->func(params, r);
365
366 if (hw_is_mask(r->var)) {
367 trace_hw_mask_param(substream, r->var, k + 1,
368 &old_mask, hw_param_mask(params, r->var));
369 }
370 if (hw_is_interval(r->var)) {
371 trace_hw_interval_param(substream, r->var, k + 1,
372 &old_interval, hw_param_interval(params, r->var));
373 }
374
375 rstamps[k] = stamp;
376 if (changed && r->var >= 0) {
377 params->cmask |= (1 << r->var);
378 vstamps[r->var] = stamp;
Takashi Sakamotoa1c06e32017-06-09 06:37:04 +0900379 again = true;
Takashi Sakamoto0d4e3992017-06-09 06:37:02 +0900380 }
381 if (changed < 0)
382 return changed;
383 stamp++;
384 }
385
386 if (again)
387 goto retry;
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900388
389 return 0;
390}
391
392int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
393 struct snd_pcm_hw_params *params)
394{
395 struct snd_pcm_hardware *hw;
396 struct snd_interval *i = NULL;
397 struct snd_mask *m = NULL;
398 int err;
399
400 params->info = 0;
401 params->fifo_size = 0;
402 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
403 params->msbits = 0;
404 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
405 params->rate_num = 0;
406 params->rate_den = 0;
407 }
408
409 err = constrain_mask_params(substream, params);
410 if (err < 0)
411 return err;
412
413 err = constrain_interval_params(substream, params);
414 if (err < 0)
415 return err;
416
417 err = constrain_params_by_rules(substream, params);
418 if (err < 0)
419 return err;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!params->msbits) {
422 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
423 if (snd_interval_single(i))
424 params->msbits = snd_interval_value(i);
425 }
426
427 if (!params->rate_den) {
428 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
429 if (snd_interval_single(i)) {
430 params->rate_num = snd_interval_value(i);
431 params->rate_den = 1;
432 }
433 }
434
435 hw = &substream->runtime->hw;
Takashi Iwai63825f32014-10-22 12:04:46 +0200436 if (!params->info) {
Libin Yang48d88292014-12-31 22:09:54 +0800437 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
438 SNDRV_PCM_INFO_DRAIN_TRIGGER);
Takashi Iwai63825f32014-10-22 12:04:46 +0200439 if (!hw_support_mmap(substream))
440 params->info &= ~(SNDRV_PCM_INFO_MMAP |
441 SNDRV_PCM_INFO_MMAP_VALID);
442 }
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200443 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100444 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
445 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
446 if (snd_mask_min(m) == snd_mask_max(m) &&
447 snd_interval_min(i) == snd_interval_max(i)) {
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900448 err = substream->ops->ioctl(substream,
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200449 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Takashi Sakamoto9cc07f52017-06-09 06:37:01 +0900450 if (err < 0)
451 return err;
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200452 }
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 params->rmask = 0;
455 return 0;
456}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200457EXPORT_SYMBOL(snd_pcm_hw_refine);
458
Takashi Iwai877211f2005-11-17 13:59:38 +0100459static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
460 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Takashi Iwai877211f2005-11-17 13:59:38 +0100462 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int err;
464
Li Zefanef44a1e2009-04-10 09:43:08 +0800465 params = memdup_user(_params, sizeof(*params));
466 if (IS_ERR(params))
467 return PTR_ERR(params);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 err = snd_pcm_hw_refine(substream, params);
470 if (copy_to_user(_params, params, sizeof(*params))) {
471 if (!err)
472 err = -EFAULT;
473 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 kfree(params);
476 return err;
477}
478
Takashi Iwai9442e692006-09-30 23:27:19 -0700479static int period_to_usecs(struct snd_pcm_runtime *runtime)
480{
481 int usecs;
482
483 if (! runtime->rate)
484 return -1; /* invalid */
485
486 /* take 75% of period time as the deadline */
487 usecs = (750000 / runtime->rate) * runtime->period_size;
488 usecs += ((750000 % runtime->rate) * runtime->period_size) /
489 runtime->rate;
490
491 return usecs;
492}
493
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200494static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
495{
496 snd_pcm_stream_lock_irq(substream);
497 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
498 substream->runtime->status->state = state;
499 snd_pcm_stream_unlock_irq(substream);
500}
501
Jie Yang90bbaf62015-10-16 17:57:46 +0800502static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
503 int event)
504{
505#ifdef CONFIG_SND_PCM_TIMER
506 if (substream->timer)
507 snd_timer_notify(substream->timer, event,
508 &substream->runtime->trigger_tstamp);
509#endif
510}
511
Takashi Iwai877211f2005-11-17 13:59:38 +0100512static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
513 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Takashi Iwai877211f2005-11-17 13:59:38 +0100515 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700516 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unsigned int bits;
518 snd_pcm_uframes_t frames;
519
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200520 if (PCM_RUNTIME_CHECK(substream))
521 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 snd_pcm_stream_lock_irq(substream);
524 switch (runtime->status->state) {
525 case SNDRV_PCM_STATE_OPEN:
526 case SNDRV_PCM_STATE_SETUP:
527 case SNDRV_PCM_STATE_PREPARED:
528 break;
529 default:
530 snd_pcm_stream_unlock_irq(substream);
531 return -EBADFD;
532 }
533 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100534#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (!substream->oss.oss)
536#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200537 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -EBADFD;
539
540 params->rmask = ~0U;
541 err = snd_pcm_hw_refine(substream, params);
542 if (err < 0)
543 goto _error;
544
545 err = snd_pcm_hw_params_choose(substream, params);
546 if (err < 0)
547 goto _error;
548
549 if (substream->ops->hw_params != NULL) {
550 err = substream->ops->hw_params(substream, params);
551 if (err < 0)
552 goto _error;
553 }
554
555 runtime->access = params_access(params);
556 runtime->format = params_format(params);
557 runtime->subformat = params_subformat(params);
558 runtime->channels = params_channels(params);
559 runtime->rate = params_rate(params);
560 runtime->period_size = params_period_size(params);
561 runtime->periods = params_periods(params);
562 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 runtime->info = params->info;
564 runtime->rate_num = params->rate_num;
565 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100566 runtime->no_period_wakeup =
567 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
568 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 bits = snd_pcm_format_physical_width(runtime->format);
571 runtime->sample_bits = bits;
572 bits *= runtime->channels;
573 runtime->frame_bits = bits;
574 frames = 1;
575 while (bits % 8 != 0) {
576 bits *= 2;
577 frames *= 2;
578 }
579 runtime->byte_align = bits / 8;
580 runtime->min_align = frames;
581
582 /* Default sw params */
583 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
584 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 runtime->start_threshold = 1;
587 runtime->stop_threshold = runtime->buffer_size;
588 runtime->silence_threshold = 0;
589 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200590 runtime->boundary = runtime->buffer_size;
591 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
592 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200595 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700596
James Bottomley82f68252010-07-05 22:53:06 +0200597 if (pm_qos_request_active(&substream->latency_pm_qos_req))
598 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700599 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200600 pm_qos_add_request(&substream->latency_pm_qos_req,
601 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300604 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 so we force application to retry to set
606 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200607 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (substream->ops->hw_free != NULL)
609 substream->ops->hw_free(substream);
610 return err;
611}
612
Takashi Iwai877211f2005-11-17 13:59:38 +0100613static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
614 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwai877211f2005-11-17 13:59:38 +0100616 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int err;
618
Li Zefanef44a1e2009-04-10 09:43:08 +0800619 params = memdup_user(_params, sizeof(*params));
620 if (IS_ERR(params))
621 return PTR_ERR(params);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 err = snd_pcm_hw_params(substream, params);
624 if (copy_to_user(_params, params, sizeof(*params))) {
625 if (!err)
626 err = -EFAULT;
627 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 kfree(params);
630 return err;
631}
632
Takashi Iwai877211f2005-11-17 13:59:38 +0100633static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwai877211f2005-11-17 13:59:38 +0100635 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int result = 0;
637
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200638 if (PCM_RUNTIME_CHECK(substream))
639 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 snd_pcm_stream_lock_irq(substream);
642 switch (runtime->status->state) {
643 case SNDRV_PCM_STATE_SETUP:
644 case SNDRV_PCM_STATE_PREPARED:
645 break;
646 default:
647 snd_pcm_stream_unlock_irq(substream);
648 return -EBADFD;
649 }
650 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200651 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -EBADFD;
653 if (substream->ops->hw_free)
654 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200655 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200656 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return result;
658}
659
Takashi Iwai877211f2005-11-17 13:59:38 +0100660static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
661 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Takashi Iwai877211f2005-11-17 13:59:38 +0100663 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100664 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200666 if (PCM_RUNTIME_CHECK(substream))
667 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 snd_pcm_stream_lock_irq(substream);
670 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
671 snd_pcm_stream_unlock_irq(substream);
672 return -EBADFD;
673 }
674 snd_pcm_stream_unlock_irq(substream);
675
Dan Carpenter145d92e2015-09-23 12:42:28 +0300676 if (params->tstamp_mode < 0 ||
677 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EINVAL;
Takashi Iwai58900812014-07-16 17:45:27 +0200679 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
680 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200681 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (params->avail_min == 0)
683 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (params->silence_size >= runtime->boundary) {
685 if (params->silence_threshold != 0)
686 return -EINVAL;
687 } else {
688 if (params->silence_size > params->silence_threshold)
689 return -EINVAL;
690 if (params->silence_threshold > runtime->buffer_size)
691 return -EINVAL;
692 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100693 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 snd_pcm_stream_lock_irq(substream);
695 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai58900812014-07-16 17:45:27 +0200696 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
697 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 runtime->period_step = params->period_step;
699 runtime->control->avail_min = params->avail_min;
700 runtime->start_threshold = params->start_threshold;
701 runtime->stop_threshold = params->stop_threshold;
702 runtime->silence_threshold = params->silence_threshold;
703 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 params->boundary = runtime->boundary;
705 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
707 runtime->silence_size > 0)
708 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100709 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Takashi Iwai877211f2005-11-17 13:59:38 +0100715static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
716 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Takashi Iwai877211f2005-11-17 13:59:38 +0100718 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int err;
720 if (copy_from_user(&params, _params, sizeof(params)))
721 return -EFAULT;
722 err = snd_pcm_sw_params(substream, &params);
723 if (copy_to_user(_params, &params, sizeof(params)))
724 return -EFAULT;
725 return err;
726}
727
Takashi Iwai877211f2005-11-17 13:59:38 +0100728int snd_pcm_status(struct snd_pcm_substream *substream,
729 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai877211f2005-11-17 13:59:38 +0100731 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600734
735 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
736 &runtime->audio_tstamp_config);
737
738 /* backwards compatible behavior */
739 if (runtime->audio_tstamp_config.type_requested ==
740 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
741 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
742 runtime->audio_tstamp_config.type_requested =
743 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
744 else
745 runtime->audio_tstamp_config.type_requested =
746 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
747 runtime->audio_tstamp_report.valid = 0;
748 } else
749 runtime->audio_tstamp_report.valid = 1;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 status->state = runtime->status->state;
752 status->suspended_state = runtime->status->suspended_state;
753 if (status->state == SNDRV_PCM_STATE_OPEN)
754 goto _end;
755 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100756 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100758 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
759 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600760 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500761 status->audio_tstamp =
762 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600763 if (runtime->audio_tstamp_report.valid == 1)
764 /* backwards compatibility, no report provided in COMPAT mode */
765 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
766 &status->audio_tstamp_accuracy,
767 &runtime->audio_tstamp_report);
768
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100769 goto _tstamp_end;
770 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600771 } else {
772 /* get tstamp only in fallback mode and only if enabled */
773 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
774 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100775 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100776 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 status->appl_ptr = runtime->control->appl_ptr;
778 status->hw_ptr = runtime->status->hw_ptr;
779 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
780 status->avail = snd_pcm_playback_avail(runtime);
781 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200782 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200784 status->delay += runtime->delay;
785 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 status->delay = 0;
787 } else {
788 status->avail = snd_pcm_capture_avail(runtime);
789 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200790 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 else
792 status->delay = 0;
793 }
794 status->avail_max = runtime->avail_max;
795 status->overrange = runtime->overrange;
796 runtime->avail_max = 0;
797 runtime->overrange = 0;
798 _end:
799 snd_pcm_stream_unlock_irq(substream);
800 return 0;
801}
802
Takashi Iwai877211f2005-11-17 13:59:38 +0100803static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600804 struct snd_pcm_status __user * _status,
805 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Takashi Iwai877211f2005-11-17 13:59:38 +0100807 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600811 /*
812 * with extension, parameters are read/write,
813 * get audio_tstamp_data from user,
814 * ignore rest of status structure
815 */
816 if (ext && get_user(status.audio_tstamp_data,
817 (u32 __user *)(&_status->audio_tstamp_data)))
818 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 res = snd_pcm_status(substream, &status);
820 if (res < 0)
821 return res;
822 if (copy_to_user(_status, &status, sizeof(status)))
823 return -EFAULT;
824 return 0;
825}
826
Takashi Iwai877211f2005-11-17 13:59:38 +0100827static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
828 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai877211f2005-11-17 13:59:38 +0100830 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 unsigned int channel;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 channel = info->channel;
834 runtime = substream->runtime;
835 snd_pcm_stream_lock_irq(substream);
836 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
837 snd_pcm_stream_unlock_irq(substream);
838 return -EBADFD;
839 }
840 snd_pcm_stream_unlock_irq(substream);
841 if (channel >= runtime->channels)
842 return -EINVAL;
843 memset(info, 0, sizeof(*info));
844 info->channel = channel;
845 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
846}
847
Takashi Iwai877211f2005-11-17 13:59:38 +0100848static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
849 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Takashi Iwai877211f2005-11-17 13:59:38 +0100851 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int res;
853
854 if (copy_from_user(&info, _info, sizeof(info)))
855 return -EFAULT;
856 res = snd_pcm_channel_info(substream, &info);
857 if (res < 0)
858 return res;
859 if (copy_to_user(_info, &info, sizeof(info)))
860 return -EFAULT;
861 return 0;
862}
863
Takashi Iwai877211f2005-11-17 13:59:38 +0100864static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
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 if (runtime->trigger_master == NULL)
868 return;
869 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600870 if (!runtime->trigger_tstamp_latched)
871 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 } else {
873 snd_pcm_trigger_tstamp(runtime->trigger_master);
874 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
875 }
876 runtime->trigger_master = NULL;
877}
878
879struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100880 int (*pre_action)(struct snd_pcm_substream *substream, int state);
881 int (*do_action)(struct snd_pcm_substream *substream, int state);
882 void (*undo_action)(struct snd_pcm_substream *substream, int state);
883 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884};
885
886/*
887 * this functions is core for handling of linked stream
888 * Note: the stream state might be changed also on failure
889 * Note2: call with calling stream lock + link lock
890 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100891static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100892 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int state, int do_lock)
894{
Takashi Iwai877211f2005-11-17 13:59:38 +0100895 struct snd_pcm_substream *s = NULL;
896 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200897 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Takashi Iwaief991b92007-02-22 12:52:53 +0100899 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200900 if (do_lock && s != substream) {
901 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200902 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200903 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200904 spin_lock_nested(&s->self_group.lock, depth);
905 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 res = ops->pre_action(s, state);
908 if (res < 0)
909 goto _unlock;
910 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100911 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 res = ops->do_action(s, state);
913 if (res < 0) {
914 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100915 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (s1 == s) /* failed stream */
917 break;
918 ops->undo_action(s1, state);
919 }
920 }
921 s = NULL; /* unlock all */
922 goto _unlock;
923 }
924 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100925 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ops->post_action(s, state);
927 }
928 _unlock:
929 if (do_lock) {
930 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100931 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200932 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200933 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200934 mutex_unlock(&s1->self_group.mutex);
935 else
936 spin_unlock(&s1->self_group.lock);
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (s1 == s) /* end */
939 break;
940 }
941 }
942 return res;
943}
944
945/*
946 * Note: call with stream lock
947 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100948static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100949 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int state)
951{
952 int res;
953
954 res = ops->pre_action(substream, state);
955 if (res < 0)
956 return res;
957 res = ops->do_action(substream, state);
958 if (res == 0)
959 ops->post_action(substream, state);
960 else if (ops->undo_action)
961 ops->undo_action(substream, state);
962 return res;
963}
964
965/*
966 * Note: call with stream lock
967 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100968static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100969 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int state)
971{
972 int res;
973
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100974 if (!snd_pcm_stream_linked(substream))
975 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200976
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100977 if (substream->pcm->nonatomic) {
978 if (!mutex_trylock(&substream->group->mutex)) {
979 mutex_unlock(&substream->self_group.mutex);
980 mutex_lock(&substream->group->mutex);
981 mutex_lock(&substream->self_group.mutex);
982 }
983 res = snd_pcm_action_group(ops, substream, state, 1);
984 mutex_unlock(&substream->group->mutex);
985 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!spin_trylock(&substream->group->lock)) {
987 spin_unlock(&substream->self_group.lock);
988 spin_lock(&substream->group->lock);
989 spin_lock(&substream->self_group.lock);
990 }
991 res = snd_pcm_action_group(ops, substream, state, 1);
992 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 return res;
995}
996
997/*
998 * Note: don't use any locks before
999 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001000static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001001 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int state)
1003{
1004 int res;
1005
Takashi Iwaie3a4bd52014-10-31 14:45:04 +01001006 snd_pcm_stream_lock_irq(substream);
1007 res = snd_pcm_action(ops, substream, state);
1008 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return res;
1010}
1011
1012/*
1013 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001014static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001015 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int state)
1017{
1018 int res;
1019
1020 down_read(&snd_pcm_link_rwsem);
1021 if (snd_pcm_stream_linked(substream))
1022 res = snd_pcm_action_group(ops, substream, state, 0);
1023 else
1024 res = snd_pcm_action_single(ops, substream, state);
1025 up_read(&snd_pcm_link_rwsem);
1026 return res;
1027}
1028
1029/*
1030 * start callbacks
1031 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001032static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwai877211f2005-11-17 13:59:38 +01001034 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1036 return -EBADFD;
1037 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1038 !snd_pcm_playback_data(substream))
1039 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001040 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 runtime->trigger_master = substream;
1042 return 0;
1043}
1044
Takashi Iwai877211f2005-11-17 13:59:38 +01001045static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 if (substream->runtime->trigger_master != substream)
1048 return 0;
1049 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1050}
1051
Takashi Iwai877211f2005-11-17 13:59:38 +01001052static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 if (substream->runtime->trigger_master == substream)
1055 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1056}
1057
Takashi Iwai877211f2005-11-17 13:59:38 +01001058static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Takashi Iwai877211f2005-11-17 13:59:38 +01001060 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001062 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001063 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1064 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 runtime->status->state = state;
1066 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1067 runtime->silence_size > 0)
1068 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001069 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
Julia Lawallb17154c2015-11-29 16:36:40 +01001072static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 .pre_action = snd_pcm_pre_start,
1074 .do_action = snd_pcm_do_start,
1075 .undo_action = snd_pcm_undo_start,
1076 .post_action = snd_pcm_post_start
1077};
1078
1079/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001080 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001081 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001082 *
1083 * Return: Zero if successful, or a negative error code.
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001084 * The stream lock must be acquired before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001086int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Takashi Iwai877211f2005-11-17 13:59:38 +01001088 return snd_pcm_action(&snd_pcm_action_start, substream,
1089 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Takashi Iwaic2c86a92017-05-10 14:33:44 +02001092/* take the stream lock and start the streams */
1093static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1094{
1095 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1096 SNDRV_PCM_STATE_RUNNING);
1097}
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099/*
1100 * stop callbacks
1101 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001102static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Takashi Iwai877211f2005-11-17 13:59:38 +01001104 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1106 return -EBADFD;
1107 runtime->trigger_master = substream;
1108 return 0;
1109}
1110
Takashi Iwai877211f2005-11-17 13:59:38 +01001111static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 if (substream->runtime->trigger_master == substream &&
1114 snd_pcm_running(substream))
1115 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1116 return 0; /* unconditonally stop all substreams */
1117}
1118
Takashi Iwai877211f2005-11-17 13:59:38 +01001119static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Takashi Iwai877211f2005-11-17 13:59:38 +01001121 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (runtime->status->state != state) {
1123 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001124 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001125 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001128 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Julia Lawallb17154c2015-11-29 16:36:40 +01001131static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 .pre_action = snd_pcm_pre_stop,
1133 .do_action = snd_pcm_do_stop,
1134 .post_action = snd_pcm_post_stop
1135};
1136
1137/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001138 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001139 * @substream: the PCM substream instance
1140 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001142 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001143 *
Masanari Iida0a114582014-02-09 00:47:36 +09001144 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001146int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1149}
1150
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001151EXPORT_SYMBOL(snd_pcm_stop);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001154 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001155 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001157 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001159 *
1160 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001162int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Takashi Iwai877211f2005-11-17 13:59:38 +01001164 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1165 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Takashi Iwai1fb85102014-11-07 17:08:28 +01001168/**
1169 * snd_pcm_stop_xrun - stop the running streams as XRUN
1170 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001171 *
1172 * This stops the given running substream (and all linked substreams) as XRUN.
1173 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1174 *
1175 * Return: Zero if successful, or a negative error code.
1176 */
1177int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1178{
1179 unsigned long flags;
1180 int ret = 0;
1181
1182 snd_pcm_stream_lock_irqsave(substream, flags);
1183 if (snd_pcm_running(substream))
1184 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1185 snd_pcm_stream_unlock_irqrestore(substream, flags);
1186 return ret;
1187}
1188EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190/*
1191 * pause callbacks
1192 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001193static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
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 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1197 return -ENOSYS;
1198 if (push) {
1199 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1200 return -EBADFD;
1201 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1202 return -EBADFD;
1203 runtime->trigger_master = substream;
1204 return 0;
1205}
1206
Takashi Iwai877211f2005-11-17 13:59:38 +01001207static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 if (substream->runtime->trigger_master != substream)
1210 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001211 /* some drivers might use hw_ptr to recover from the pause -
1212 update the hw_ptr now */
1213 if (push)
1214 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001215 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001216 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001217 * delta, effectively to skip the check once.
1218 */
1219 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return substream->ops->trigger(substream,
1221 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1222 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1223}
1224
Takashi Iwai877211f2005-11-17 13:59:38 +01001225static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 if (substream->runtime->trigger_master == substream)
1228 substream->ops->trigger(substream,
1229 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1230 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1231}
1232
Takashi Iwai877211f2005-11-17 13:59:38 +01001233static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Takashi Iwai877211f2005-11-17 13:59:38 +01001235 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 snd_pcm_trigger_tstamp(substream);
1237 if (push) {
1238 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001239 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001241 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 } else {
1243 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001244 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246}
1247
Julia Lawallb17154c2015-11-29 16:36:40 +01001248static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 .pre_action = snd_pcm_pre_pause,
1250 .do_action = snd_pcm_do_pause,
1251 .undo_action = snd_pcm_undo_pause,
1252 .post_action = snd_pcm_post_pause
1253};
1254
1255/*
1256 * Push/release the pause for all linked streams.
1257 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001258static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1261}
1262
1263#ifdef CONFIG_PM
1264/* suspend */
1265
Takashi Iwai877211f2005-11-17 13:59:38 +01001266static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Takashi Iwai877211f2005-11-17 13:59:38 +01001268 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1270 return -EBUSY;
1271 runtime->trigger_master = substream;
1272 return 0;
1273}
1274
Takashi Iwai877211f2005-11-17 13:59:38 +01001275static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Takashi Iwai877211f2005-11-17 13:59:38 +01001277 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (runtime->trigger_master != substream)
1279 return 0;
1280 if (! snd_pcm_running(substream))
1281 return 0;
1282 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1283 return 0; /* suspend unconditionally */
1284}
1285
Takashi Iwai877211f2005-11-17 13:59:38 +01001286static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Takashi Iwai877211f2005-11-17 13:59:38 +01001288 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001290 runtime->status->suspended_state = runtime->status->state;
1291 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001292 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001294 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Julia Lawallb17154c2015-11-29 16:36:40 +01001297static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 .pre_action = snd_pcm_pre_suspend,
1299 .do_action = snd_pcm_do_suspend,
1300 .post_action = snd_pcm_post_suspend
1301};
1302
1303/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001304 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001305 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001308 *
1309 * Return: Zero if successful (or @substream is %NULL), or a negative error
1310 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001312int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 int err;
1315 unsigned long flags;
1316
Takashi Iwai603bf5242005-11-17 15:59:14 +01001317 if (! substream)
1318 return 0;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 snd_pcm_stream_lock_irqsave(substream, flags);
1321 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1322 snd_pcm_stream_unlock_irqrestore(substream, flags);
1323 return err;
1324}
1325
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001326EXPORT_SYMBOL(snd_pcm_suspend);
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001329 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001330 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001333 *
1334 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001336int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Takashi Iwai877211f2005-11-17 13:59:38 +01001338 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int stream, err = 0;
1340
Takashi Iwai603bf5242005-11-17 15:59:14 +01001341 if (! pcm)
1342 return 0;
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001345 for (substream = pcm->streams[stream].substream;
1346 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* FIXME: the open/close code should lock this as well */
1348 if (substream->runtime == NULL)
1349 continue;
1350 err = snd_pcm_suspend(substream);
1351 if (err < 0 && err != -EBUSY)
1352 return err;
1353 }
1354 }
1355 return 0;
1356}
1357
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001358EXPORT_SYMBOL(snd_pcm_suspend_all);
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360/* resume */
1361
Takashi Iwai877211f2005-11-17 13:59:38 +01001362static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Takashi Iwai877211f2005-11-17 13:59:38 +01001364 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1366 return -ENOSYS;
1367 runtime->trigger_master = substream;
1368 return 0;
1369}
1370
Takashi Iwai877211f2005-11-17 13:59:38 +01001371static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Takashi Iwai877211f2005-11-17 13:59:38 +01001373 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (runtime->trigger_master != substream)
1375 return 0;
1376 /* DMA not running previously? */
1377 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1378 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1379 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1380 return 0;
1381 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1382}
1383
Takashi Iwai877211f2005-11-17 13:59:38 +01001384static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 if (substream->runtime->trigger_master == substream &&
1387 snd_pcm_running(substream))
1388 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1389}
1390
Takashi Iwai877211f2005-11-17 13:59:38 +01001391static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Takashi Iwai877211f2005-11-17 13:59:38 +01001393 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001395 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001396 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Julia Lawallb17154c2015-11-29 16:36:40 +01001399static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 .pre_action = snd_pcm_pre_resume,
1401 .do_action = snd_pcm_do_resume,
1402 .undo_action = snd_pcm_undo_resume,
1403 .post_action = snd_pcm_post_resume
1404};
1405
Takashi Iwai877211f2005-11-17 13:59:38 +01001406static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Takashi Iwai877211f2005-11-17 13:59:38 +01001408 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int res;
1410
1411 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001412 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1414 snd_power_unlock(card);
1415 return res;
1416}
1417
1418#else
1419
Takashi Iwai877211f2005-11-17 13:59:38 +01001420static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 return -ENOSYS;
1423}
1424
1425#endif /* CONFIG_PM */
1426
1427/*
1428 * xrun ioctl
1429 *
1430 * Change the RUNNING stream(s) to XRUN state.
1431 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001432static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwai877211f2005-11-17 13:59:38 +01001434 struct snd_card *card = substream->pcm->card;
1435 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int result;
1437
1438 snd_power_lock(card);
1439 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001440 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (result < 0)
1442 goto _unlock;
1443 }
1444
1445 snd_pcm_stream_lock_irq(substream);
1446 switch (runtime->status->state) {
1447 case SNDRV_PCM_STATE_XRUN:
1448 result = 0; /* already there */
1449 break;
1450 case SNDRV_PCM_STATE_RUNNING:
1451 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1452 break;
1453 default:
1454 result = -EBADFD;
1455 }
1456 snd_pcm_stream_unlock_irq(substream);
1457 _unlock:
1458 snd_power_unlock(card);
1459 return result;
1460}
1461
1462/*
1463 * reset ioctl
1464 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001465static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Takashi Iwai877211f2005-11-17 13:59:38 +01001467 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 switch (runtime->status->state) {
1469 case SNDRV_PCM_STATE_RUNNING:
1470 case SNDRV_PCM_STATE_PREPARED:
1471 case SNDRV_PCM_STATE_PAUSED:
1472 case SNDRV_PCM_STATE_SUSPENDED:
1473 return 0;
1474 default:
1475 return -EBADFD;
1476 }
1477}
1478
Takashi Iwai877211f2005-11-17 13:59:38 +01001479static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Takashi Iwai877211f2005-11-17 13:59:38 +01001481 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1483 if (err < 0)
1484 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001486 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1487 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 runtime->silence_start = runtime->status->hw_ptr;
1489 runtime->silence_filled = 0;
1490 return 0;
1491}
1492
Takashi Iwai877211f2005-11-17 13:59:38 +01001493static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Takashi Iwai877211f2005-11-17 13:59:38 +01001495 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 runtime->control->appl_ptr = runtime->status->hw_ptr;
1497 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1498 runtime->silence_size > 0)
1499 snd_pcm_playback_silence(substream, ULONG_MAX);
1500}
1501
Julia Lawallb17154c2015-11-29 16:36:40 +01001502static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 .pre_action = snd_pcm_pre_reset,
1504 .do_action = snd_pcm_do_reset,
1505 .post_action = snd_pcm_post_reset
1506};
1507
Takashi Iwai877211f2005-11-17 13:59:38 +01001508static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1511}
1512
1513/*
1514 * prepare ioctl
1515 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001516/* we use the second argument for updating f_flags */
1517static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1518 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Takashi Iwai877211f2005-11-17 13:59:38 +01001520 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001521 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1522 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -EBADFD;
1524 if (snd_pcm_running(substream))
1525 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001526 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return 0;
1528}
1529
Takashi Iwai877211f2005-11-17 13:59:38 +01001530static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 int err;
1533 err = substream->ops->prepare(substream);
1534 if (err < 0)
1535 return err;
1536 return snd_pcm_do_reset(substream, 0);
1537}
1538
Takashi Iwai877211f2005-11-17 13:59:38 +01001539static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Takashi Iwai877211f2005-11-17 13:59:38 +01001541 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001543 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Julia Lawallb17154c2015-11-29 16:36:40 +01001546static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 .pre_action = snd_pcm_pre_prepare,
1548 .do_action = snd_pcm_do_prepare,
1549 .post_action = snd_pcm_post_prepare
1550};
1551
1552/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001553 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001554 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001555 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001556 *
1557 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001559static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1560 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001563 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001564 int f_flags;
1565
1566 if (file)
1567 f_flags = file->f_flags;
1568 else
1569 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001572 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001573 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1574 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 snd_power_unlock(card);
1576 return res;
1577}
1578
1579/*
1580 * drain ioctl
1581 */
1582
Takashi Iwai877211f2005-11-17 13:59:38 +01001583static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001585 struct snd_pcm_runtime *runtime = substream->runtime;
1586 switch (runtime->status->state) {
1587 case SNDRV_PCM_STATE_OPEN:
1588 case SNDRV_PCM_STATE_DISCONNECTED:
1589 case SNDRV_PCM_STATE_SUSPENDED:
1590 return -EBADFD;
1591 }
1592 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return 0;
1594}
1595
Takashi Iwai877211f2005-11-17 13:59:38 +01001596static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Takashi Iwai877211f2005-11-17 13:59:38 +01001598 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1600 switch (runtime->status->state) {
1601 case SNDRV_PCM_STATE_PREPARED:
1602 /* start playback stream if possible */
1603 if (! snd_pcm_playback_empty(substream)) {
1604 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1605 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001606 } else {
1607 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609 break;
1610 case SNDRV_PCM_STATE_RUNNING:
1611 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1612 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001613 case SNDRV_PCM_STATE_XRUN:
1614 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1615 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 default:
1617 break;
1618 }
1619 } else {
1620 /* stop running stream */
1621 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001622 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001624 snd_pcm_do_stop(substream, new_state);
1625 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627 }
Libin Yang48d88292014-12-31 22:09:54 +08001628
1629 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1630 runtime->trigger_master == substream &&
1631 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1632 return substream->ops->trigger(substream,
1633 SNDRV_PCM_TRIGGER_DRAIN);
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
1636}
1637
Takashi Iwai877211f2005-11-17 13:59:38 +01001638static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640}
1641
Julia Lawallb17154c2015-11-29 16:36:40 +01001642static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 .pre_action = snd_pcm_pre_drain_init,
1644 .do_action = snd_pcm_do_drain_init,
1645 .post_action = snd_pcm_post_drain_init
1646};
1647
Takashi Iwai877211f2005-11-17 13:59:38 +01001648static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650/*
1651 * Drain the stream(s).
1652 * When the substream is linked, sync until the draining of all playback streams
1653 * is finished.
1654 * After this call, all streams are supposed to be either SETUP or DRAINING
1655 * (capture only) state.
1656 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001657static int snd_pcm_drain(struct snd_pcm_substream *substream,
1658 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Takashi Iwai877211f2005-11-17 13:59:38 +01001660 struct snd_card *card;
1661 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001662 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001663 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001665 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 card = substream->pcm->card;
1668 runtime = substream->runtime;
1669
1670 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1671 return -EBADFD;
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 snd_power_lock(card);
1674 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001675 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001676 if (result < 0) {
1677 snd_power_unlock(card);
1678 return result;
1679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001682 if (file) {
1683 if (file->f_flags & O_NONBLOCK)
1684 nonblock = 1;
1685 } else if (substream->f_flags & O_NONBLOCK)
1686 nonblock = 1;
1687
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001688 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001689 snd_pcm_stream_lock_irq(substream);
1690 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001691 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001692 snd_pcm_pause(substream, 0);
1693
1694 /* pre-start/stop - all running streams are changed to DRAINING state */
1695 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001696 if (result < 0)
1697 goto unlock;
1698 /* in non-blocking, we don't wait in ioctl but let caller poll */
1699 if (nonblock) {
1700 result = -EAGAIN;
1701 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 for (;;) {
1705 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001706 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (signal_pending(current)) {
1708 result = -ERESTARTSYS;
1709 break;
1710 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001711 /* find a substream to drain */
1712 to_check = NULL;
1713 snd_pcm_group_for_each_entry(s, substream) {
1714 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1715 continue;
1716 runtime = s->runtime;
1717 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1718 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001719 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001720 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001721 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001722 if (!to_check)
1723 break; /* all drained */
1724 init_waitqueue_entry(&wait, current);
1725 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001727 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001729 if (runtime->no_period_wakeup)
1730 tout = MAX_SCHEDULE_TIMEOUT;
1731 else {
1732 tout = 10;
1733 if (runtime->rate) {
1734 long t = runtime->period_size * 2 / runtime->rate;
1735 tout = max(t, tout);
1736 }
1737 tout = msecs_to_jiffies(tout * 1000);
1738 }
1739 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001741 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001743 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001744 if (card->shutdown) {
1745 result = -ENODEV;
1746 break;
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (tout == 0) {
1749 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1750 result = -ESTRPIPE;
1751 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001752 dev_dbg(substream->pcm->card->dev,
1753 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1755 result = -EIO;
1756 }
1757 break;
1758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001760
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001761 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001762 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001763 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 return result;
1767}
1768
1769/*
1770 * drop ioctl
1771 *
1772 * Immediately put all linked substreams into SETUP state.
1773 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001774static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Takashi Iwai877211f2005-11-17 13:59:38 +01001776 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int result = 0;
1778
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001779 if (PCM_RUNTIME_CHECK(substream))
1780 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001783 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001784 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1785 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return -EBADFD;
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 snd_pcm_stream_lock_irq(substream);
1789 /* resume pause */
1790 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1791 snd_pcm_pause(substream, 0);
1792
1793 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1794 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1795 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return result;
1798}
1799
1800
Al Viro0888c322013-06-05 14:09:55 -04001801static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Al Viro0888c322013-06-05 14:09:55 -04001803 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001804 unsigned int minor;
1805
Al Viro0888c322013-06-05 14:09:55 -04001806 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1807 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001809 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1810 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
1813/*
1814 * PCM link handling
1815 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001816static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001819 struct snd_pcm_file *pcm_file;
1820 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001821 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001822 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Al Viro0888c322013-06-05 14:09:55 -04001824 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001826 if (!is_pcm_file(f.file)) {
1827 res = -EBADFD;
1828 goto _badf;
1829 }
1830 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001832 group = kmalloc(sizeof(*group), GFP_KERNEL);
1833 if (!group) {
1834 res = -ENOMEM;
1835 goto _nolock;
1836 }
Takashi Iwai67ec10722016-02-17 14:30:26 +01001837 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 write_lock_irq(&snd_pcm_link_rwlock);
1839 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001840 substream->runtime->status->state != substream1->runtime->status->state ||
1841 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 res = -EBADFD;
1843 goto _end;
1844 }
1845 if (snd_pcm_stream_linked(substream1)) {
1846 res = -EALREADY;
1847 goto _end;
1848 }
1849 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001850 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001851 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001853 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 INIT_LIST_HEAD(&substream->group->substreams);
1855 list_add_tail(&substream->link_list, &substream->group->substreams);
1856 substream->group->count = 1;
1857 }
1858 list_add_tail(&substream1->link_list, &substream->group->substreams);
1859 substream->group->count++;
1860 substream1->group = substream->group;
1861 _end:
1862 write_unlock_irq(&snd_pcm_link_rwlock);
1863 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001864 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001865 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001866 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001867 _badf:
1868 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return res;
1870}
1871
Takashi Iwai877211f2005-11-17 13:59:38 +01001872static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 substream->group = &substream->self_group;
1875 INIT_LIST_HEAD(&substream->self_group.substreams);
1876 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1877}
1878
Takashi Iwai877211f2005-11-17 13:59:38 +01001879static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Takashi Iwaief991b92007-02-22 12:52:53 +01001881 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 int res = 0;
1883
Takashi Iwai67ec10722016-02-17 14:30:26 +01001884 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 write_lock_irq(&snd_pcm_link_rwlock);
1886 if (!snd_pcm_stream_linked(substream)) {
1887 res = -EALREADY;
1888 goto _end;
1889 }
1890 list_del(&substream->link_list);
1891 substream->group->count--;
1892 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001893 snd_pcm_group_for_each_entry(s, substream) {
1894 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 break;
1896 }
1897 kfree(substream->group);
1898 }
1899 relink_to_local(substream);
1900 _end:
1901 write_unlock_irq(&snd_pcm_link_rwlock);
1902 up_write(&snd_pcm_link_rwsem);
1903 return res;
1904}
1905
1906/*
1907 * hw configurator
1908 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001909static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1910 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Takashi Iwai877211f2005-11-17 13:59:38 +01001912 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1914 hw_param_interval_c(params, rule->deps[1]), &t);
1915 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1916}
1917
Takashi Iwai877211f2005-11-17 13:59:38 +01001918static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1919 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Takashi Iwai877211f2005-11-17 13:59:38 +01001921 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1923 hw_param_interval_c(params, rule->deps[1]), &t);
1924 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1925}
1926
Takashi Iwai877211f2005-11-17 13:59:38 +01001927static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1928 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Takashi Iwai877211f2005-11-17 13:59:38 +01001930 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1932 hw_param_interval_c(params, rule->deps[1]),
1933 (unsigned long) rule->private, &t);
1934 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1935}
1936
Takashi Iwai877211f2005-11-17 13:59:38 +01001937static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1938 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Takashi Iwai877211f2005-11-17 13:59:38 +01001940 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1942 (unsigned long) rule->private,
1943 hw_param_interval_c(params, rule->deps[1]), &t);
1944 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1945}
1946
Takashi Iwai877211f2005-11-17 13:59:38 +01001947static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1948 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 unsigned int k;
Takashi Sakamotob55f9fd2017-05-17 08:48:20 +09001951 const struct snd_interval *i =
1952 hw_param_interval_c(params, rule->deps[0]);
Takashi Iwai877211f2005-11-17 13:59:38 +01001953 struct snd_mask m;
1954 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 snd_mask_any(&m);
1956 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1957 int bits;
1958 if (! snd_mask_test(mask, k))
1959 continue;
1960 bits = snd_pcm_format_physical_width(k);
1961 if (bits <= 0)
1962 continue; /* ignore invalid formats */
1963 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1964 snd_mask_reset(&m, k);
1965 }
1966 return snd_mask_refine(mask, &m);
1967}
1968
Takashi Iwai877211f2005-11-17 13:59:38 +01001969static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1970 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Takashi Iwai877211f2005-11-17 13:59:38 +01001972 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 unsigned int k;
1974 t.min = UINT_MAX;
1975 t.max = 0;
1976 t.openmin = 0;
1977 t.openmax = 0;
1978 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1979 int bits;
1980 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1981 continue;
1982 bits = snd_pcm_format_physical_width(k);
1983 if (bits <= 0)
1984 continue; /* ignore invalid formats */
1985 if (t.min > (unsigned)bits)
1986 t.min = bits;
1987 if (t.max < (unsigned)bits)
1988 t.max = bits;
1989 }
1990 t.integer = 1;
1991 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1992}
1993
1994#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1995#error "Change this table"
1996#endif
1997
Takashi Sakamoto8b674302017-05-17 08:48:17 +09001998static const unsigned int rates[] = {
1999 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2000 48000, 64000, 88200, 96000, 176400, 192000
2001};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Clemens Ladisch7653d552007-08-13 17:38:54 +02002003const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2004 .count = ARRAY_SIZE(rates),
2005 .list = rates,
2006};
2007
Takashi Iwai877211f2005-11-17 13:59:38 +01002008static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2009 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Takashi Iwai877211f2005-11-17 13:59:38 +01002011 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002013 snd_pcm_known_rates.count,
2014 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Takashi Iwai877211f2005-11-17 13:59:38 +01002017static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2018 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Takashi Iwai877211f2005-11-17 13:59:38 +01002020 struct snd_interval t;
2021 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 t.min = 0;
2023 t.max = substream->buffer_bytes_max;
2024 t.openmin = 0;
2025 t.openmax = 0;
2026 t.integer = 1;
2027 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2028}
2029
Takashi Iwai877211f2005-11-17 13:59:38 +01002030int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Takashi Iwai877211f2005-11-17 13:59:38 +01002032 struct snd_pcm_runtime *runtime = substream->runtime;
2033 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int k, err;
2035
2036 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2037 snd_mask_any(constrs_mask(constrs, k));
2038 }
2039
2040 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2041 snd_interval_any(constrs_interval(constrs, k));
2042 }
2043
2044 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2045 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2046 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2047 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2048 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2049
2050 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2051 snd_pcm_hw_rule_format, NULL,
2052 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2053 if (err < 0)
2054 return err;
2055 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2056 snd_pcm_hw_rule_sample_bits, NULL,
2057 SNDRV_PCM_HW_PARAM_FORMAT,
2058 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2059 if (err < 0)
2060 return err;
2061 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2062 snd_pcm_hw_rule_div, NULL,
2063 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2064 if (err < 0)
2065 return err;
2066 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2067 snd_pcm_hw_rule_mul, NULL,
2068 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2069 if (err < 0)
2070 return err;
2071 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2072 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2073 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2074 if (err < 0)
2075 return err;
2076 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2077 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2078 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2079 if (err < 0)
2080 return err;
2081 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2082 snd_pcm_hw_rule_div, NULL,
2083 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2084 if (err < 0)
2085 return err;
2086 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2087 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2088 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2089 if (err < 0)
2090 return err;
2091 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2092 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2093 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2094 if (err < 0)
2095 return err;
2096 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2097 snd_pcm_hw_rule_div, NULL,
2098 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2099 if (err < 0)
2100 return err;
2101 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2102 snd_pcm_hw_rule_div, NULL,
2103 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2104 if (err < 0)
2105 return err;
2106 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2107 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2108 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2109 if (err < 0)
2110 return err;
2111 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2112 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2113 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2114 if (err < 0)
2115 return err;
2116 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2117 snd_pcm_hw_rule_mul, NULL,
2118 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2119 if (err < 0)
2120 return err;
2121 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2122 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2123 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2124 if (err < 0)
2125 return err;
2126 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2127 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2128 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2129 if (err < 0)
2130 return err;
2131 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2132 snd_pcm_hw_rule_muldivk, (void*) 8,
2133 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2134 if (err < 0)
2135 return err;
2136 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2137 snd_pcm_hw_rule_muldivk, (void*) 8,
2138 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2139 if (err < 0)
2140 return err;
2141 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2142 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2143 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2144 if (err < 0)
2145 return err;
2146 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2147 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2148 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2149 if (err < 0)
2150 return err;
2151 return 0;
2152}
2153
Takashi Iwai877211f2005-11-17 13:59:38 +01002154int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Takashi Iwai877211f2005-11-17 13:59:38 +01002156 struct snd_pcm_runtime *runtime = substream->runtime;
2157 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 int err;
2159 unsigned int mask = 0;
2160
2161 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2162 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2163 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2164 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002165 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2167 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2168 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2169 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2170 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2171 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2172 }
2173 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002174 if (err < 0)
2175 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002178 if (err < 0)
2179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002182 if (err < 0)
2183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2186 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002187 if (err < 0)
2188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2191 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002192 if (err < 0)
2193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2196 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002197 if (err < 0)
2198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2201 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002202 if (err < 0)
2203 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2206 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002207 if (err < 0)
2208 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2211 snd_pcm_hw_rule_buffer_bytes_max, substream,
2212 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2213 if (err < 0)
2214 return err;
2215
2216 /* FIXME: remove */
2217 if (runtime->dma_bytes) {
2218 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002219 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302220 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222
2223 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2224 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2225 snd_pcm_hw_rule_rate, hw,
2226 SNDRV_PCM_HW_PARAM_RATE, -1);
2227 if (err < 0)
2228 return err;
2229 }
2230
2231 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2233
2234 return 0;
2235}
2236
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002237static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002240}
2241
2242void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2243{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002244 substream->ref_count--;
2245 if (substream->ref_count > 0)
2246 return;
2247
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002248 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002249 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002250 if (substream->ops->hw_free &&
2251 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 substream->ops->hw_free(substream);
2253 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002254 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002256 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2257 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002258 if (substream->pcm_release) {
2259 substream->pcm_release(substream);
2260 substream->pcm_release = NULL;
2261 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002262 snd_pcm_detach_substream(substream);
2263}
2264
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002265EXPORT_SYMBOL(snd_pcm_release_substream);
2266
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002267int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2268 struct file *file,
2269 struct snd_pcm_substream **rsubstream)
2270{
2271 struct snd_pcm_substream *substream;
2272 int err;
2273
2274 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2275 if (err < 0)
2276 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002277 if (substream->ref_count > 1) {
2278 *rsubstream = substream;
2279 return 0;
2280 }
2281
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002282 err = snd_pcm_hw_constraints_init(substream);
2283 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002284 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002285 goto error;
2286 }
2287
2288 if ((err = substream->ops->open(substream)) < 0)
2289 goto error;
2290
2291 substream->hw_opened = 1;
2292
2293 err = snd_pcm_hw_constraints_complete(substream);
2294 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002295 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002296 goto error;
2297 }
2298
2299 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002301
2302 error:
2303 snd_pcm_release_substream(substream);
2304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002307EXPORT_SYMBOL(snd_pcm_open_substream);
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002310 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002311 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Takashi Iwai877211f2005-11-17 13:59:38 +01002313 struct snd_pcm_file *pcm_file;
2314 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002315 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002317 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2318 if (err < 0)
2319 return err;
2320
Takashi Iwai548a6482006-07-31 16:51:51 +02002321 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2322 if (pcm_file == NULL) {
2323 snd_pcm_release_substream(substream);
2324 return -ENOMEM;
2325 }
2326 pcm_file->substream = substream;
2327 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002328 substream->file = pcm_file;
2329 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return 0;
2334}
2335
Clemens Ladischf87135f2005-11-20 14:06:59 +01002336static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Takashi Iwai877211f2005-11-17 13:59:38 +01002338 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002339 int err = nonseekable_open(inode, file);
2340 if (err < 0)
2341 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002342 pcm = snd_lookup_minor_data(iminor(inode),
2343 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002344 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002345 if (pcm)
2346 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002347 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002348}
2349
2350static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2351{
2352 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002353 int err = nonseekable_open(inode, file);
2354 if (err < 0)
2355 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002356 pcm = snd_lookup_minor_data(iminor(inode),
2357 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002358 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002359 if (pcm)
2360 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002361 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002362}
2363
2364static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2365{
2366 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 wait_queue_t wait;
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (pcm == NULL) {
2370 err = -ENODEV;
2371 goto __error1;
2372 }
2373 err = snd_card_file_add(pcm->card, file);
2374 if (err < 0)
2375 goto __error1;
2376 if (!try_module_get(pcm->card->module)) {
2377 err = -EFAULT;
2378 goto __error2;
2379 }
2380 init_waitqueue_entry(&wait, current);
2381 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002382 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002384 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (err >= 0)
2386 break;
2387 if (err == -EAGAIN) {
2388 if (file->f_flags & O_NONBLOCK) {
2389 err = -EBUSY;
2390 break;
2391 }
2392 } else
2393 break;
2394 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002395 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002397 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002398 if (pcm->card->shutdown) {
2399 err = -ENODEV;
2400 break;
2401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (signal_pending(current)) {
2403 err = -ERESTARTSYS;
2404 break;
2405 }
2406 }
2407 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002408 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (err < 0)
2410 goto __error;
2411 return err;
2412
2413 __error:
2414 module_put(pcm->card->module);
2415 __error2:
2416 snd_card_file_remove(pcm->card, file);
2417 __error1:
2418 return err;
2419}
2420
2421static int snd_pcm_release(struct inode *inode, struct file *file)
2422{
Takashi Iwai877211f2005-11-17 13:59:38 +01002423 struct snd_pcm *pcm;
2424 struct snd_pcm_substream *substream;
2425 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 pcm_file = file->private_data;
2428 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002429 if (snd_BUG_ON(!substream))
2430 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002432 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002433 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002434 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002435 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 wake_up(&pcm->open_wait);
2437 module_put(pcm->card->module);
2438 snd_card_file_remove(pcm->card, file);
2439 return 0;
2440}
2441
Takashi Iwaif839cc12017-05-19 20:04:23 +02002442/* check and update PCM state; return 0 or a negative error
2443 * call this inside PCM lock
2444 */
2445static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2446{
2447 switch (substream->runtime->status->state) {
2448 case SNDRV_PCM_STATE_DRAINING:
2449 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2450 return -EBADFD;
2451 /* Fall through */
2452 case SNDRV_PCM_STATE_RUNNING:
2453 return snd_pcm_update_hw_ptr(substream);
2454 case SNDRV_PCM_STATE_PREPARED:
2455 case SNDRV_PCM_STATE_PAUSED:
2456 return 0;
2457 case SNDRV_PCM_STATE_SUSPENDED:
2458 return -ESTRPIPE;
2459 case SNDRV_PCM_STATE_XRUN:
2460 return -EPIPE;
2461 default:
2462 return -EBADFD;
2463 }
2464}
2465
Takashi Iwai9027c462017-05-19 20:22:33 +02002466/* update to the given appl_ptr and call ack callback if needed;
2467 * when an error is returned, take back to the original value
2468 */
2469static int apply_appl_ptr(struct snd_pcm_substream *substream,
2470 snd_pcm_uframes_t appl_ptr)
2471{
2472 struct snd_pcm_runtime *runtime = substream->runtime;
2473 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2474 int ret;
2475
2476 runtime->control->appl_ptr = appl_ptr;
2477 if (substream->ops->ack) {
2478 ret = substream->ops->ack(substream);
2479 if (ret < 0) {
2480 runtime->control->appl_ptr = old_appl_ptr;
2481 return ret;
2482 }
2483 }
2484 return 0;
2485}
2486
2487/* increase the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002488static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2489 snd_pcm_uframes_t frames,
2490 snd_pcm_sframes_t avail)
2491{
2492 struct snd_pcm_runtime *runtime = substream->runtime;
2493 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002494 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002495
2496 if (avail <= 0)
2497 return 0;
2498 if (frames > (snd_pcm_uframes_t)avail)
2499 frames = avail;
2500 appl_ptr = runtime->control->appl_ptr + frames;
2501 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2502 appl_ptr -= runtime->boundary;
Takashi Iwai9027c462017-05-19 20:22:33 +02002503 ret = apply_appl_ptr(substream, appl_ptr);
2504 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002505}
2506
Takashi Iwai9027c462017-05-19 20:22:33 +02002507/* decrease the appl_ptr; returns the processed frames or a negative error */
Takashi Iwaie0327a02017-05-19 20:16:44 +02002508static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2509 snd_pcm_uframes_t frames,
2510 snd_pcm_sframes_t avail)
2511{
2512 struct snd_pcm_runtime *runtime = substream->runtime;
2513 snd_pcm_sframes_t appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002514 int ret;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002515
2516 if (avail <= 0)
2517 return 0;
2518 if (frames > (snd_pcm_uframes_t)avail)
2519 frames = avail;
2520 appl_ptr = runtime->control->appl_ptr - frames;
2521 if (appl_ptr < 0)
2522 appl_ptr += runtime->boundary;
Takashi Iwai9027c462017-05-19 20:22:33 +02002523 ret = apply_appl_ptr(substream, appl_ptr);
2524 return ret < 0 ? ret : frames;
Takashi Iwaie0327a02017-05-19 20:16:44 +02002525}
2526
Takashi Iwai877211f2005-11-17 13:59:38 +01002527static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2528 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Takashi Iwai877211f2005-11-17 13:59:38 +01002530 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 if (frames == 0)
2534 return 0;
2535
2536 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002537 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002538 if (!ret)
2539 ret = rewind_appl_ptr(substream, frames,
2540 snd_pcm_playback_hw_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 snd_pcm_stream_unlock_irq(substream);
2542 return ret;
2543}
2544
Takashi Iwai877211f2005-11-17 13:59:38 +01002545static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2546 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Takashi Iwai877211f2005-11-17 13:59:38 +01002548 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 if (frames == 0)
2552 return 0;
2553
2554 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002555 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002556 if (!ret)
2557 ret = rewind_appl_ptr(substream, frames,
2558 snd_pcm_capture_hw_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 snd_pcm_stream_unlock_irq(substream);
2560 return ret;
2561}
2562
Takashi Iwai877211f2005-11-17 13:59:38 +01002563static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2564 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Takashi Iwai877211f2005-11-17 13:59:38 +01002566 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 if (frames == 0)
2570 return 0;
2571
2572 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002573 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002574 if (!ret)
2575 ret = forward_appl_ptr(substream, frames,
2576 snd_pcm_playback_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 snd_pcm_stream_unlock_irq(substream);
2578 return ret;
2579}
2580
Takashi Iwai877211f2005-11-17 13:59:38 +01002581static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2582 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Takashi Iwai877211f2005-11-17 13:59:38 +01002584 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 snd_pcm_sframes_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 if (frames == 0)
2588 return 0;
2589
2590 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002591 ret = do_pcm_hwsync(substream);
Takashi Iwaie0327a02017-05-19 20:16:44 +02002592 if (!ret)
2593 ret = forward_appl_ptr(substream, frames,
2594 snd_pcm_capture_avail(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 snd_pcm_stream_unlock_irq(substream);
2596 return ret;
2597}
2598
Takashi Iwai877211f2005-11-17 13:59:38 +01002599static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 int err;
2602
2603 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002604 err = do_pcm_hwsync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 snd_pcm_stream_unlock_irq(substream);
2606 return err;
2607}
2608
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002609static snd_pcm_sframes_t snd_pcm_delay(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Takashi Iwai877211f2005-11-17 13:59:38 +01002611 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 int err;
2613 snd_pcm_sframes_t n = 0;
2614
2615 snd_pcm_stream_lock_irq(substream);
Takashi Iwaif839cc12017-05-19 20:04:23 +02002616 err = do_pcm_hwsync(substream);
2617 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2619 n = snd_pcm_playback_hw_avail(runtime);
2620 else
2621 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002622 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
2624 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002625 return err < 0 ? err : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
Takashi Iwai877211f2005-11-17 13:59:38 +01002628static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2629 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Takashi Iwai877211f2005-11-17 13:59:38 +01002631 struct snd_pcm_runtime *runtime = substream->runtime;
2632 struct snd_pcm_sync_ptr sync_ptr;
2633 volatile struct snd_pcm_mmap_status *status;
2634 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 int err;
2636
2637 memset(&sync_ptr, 0, sizeof(sync_ptr));
2638 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2639 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002640 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 -07002641 return -EFAULT;
2642 status = runtime->status;
2643 control = runtime->control;
2644 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2645 err = snd_pcm_hwsync(substream);
2646 if (err < 0)
2647 return err;
2648 }
2649 snd_pcm_stream_lock_irq(substream);
Takashi Iwai9027c462017-05-19 20:22:33 +02002650 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2651 err = apply_appl_ptr(substream, sync_ptr.c.control.appl_ptr);
2652 if (err < 0) {
2653 snd_pcm_stream_unlock_irq(substream);
2654 return err;
2655 }
2656 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 sync_ptr.c.control.appl_ptr = control->appl_ptr;
Takashi Iwai9027c462017-05-19 20:22:33 +02002658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2660 control->avail_min = sync_ptr.c.control.avail_min;
2661 else
2662 sync_ptr.c.control.avail_min = control->avail_min;
2663 sync_ptr.s.status.state = status->state;
2664 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2665 sync_ptr.s.status.tstamp = status->tstamp;
2666 sync_ptr.s.status.suspended_state = status->suspended_state;
2667 snd_pcm_stream_unlock_irq(substream);
2668 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2669 return -EFAULT;
2670 return 0;
2671}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002672
2673static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2674{
2675 struct snd_pcm_runtime *runtime = substream->runtime;
2676 int arg;
2677
2678 if (get_user(arg, _arg))
2679 return -EFAULT;
2680 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2681 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002682 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002683 return 0;
2684}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Takashi Iwai0df63e42006-04-28 15:13:41 +02002686static int snd_pcm_common_ioctl1(struct file *file,
2687 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 unsigned int cmd, void __user *arg)
2689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 switch (cmd) {
2691 case SNDRV_PCM_IOCTL_PVERSION:
2692 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2693 case SNDRV_PCM_IOCTL_INFO:
2694 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002695 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2696 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002697 case SNDRV_PCM_IOCTL_TTSTAMP:
2698 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 case SNDRV_PCM_IOCTL_HW_REFINE:
2700 return snd_pcm_hw_refine_user(substream, arg);
2701 case SNDRV_PCM_IOCTL_HW_PARAMS:
2702 return snd_pcm_hw_params_user(substream, arg);
2703 case SNDRV_PCM_IOCTL_HW_FREE:
2704 return snd_pcm_hw_free(substream);
2705 case SNDRV_PCM_IOCTL_SW_PARAMS:
2706 return snd_pcm_sw_params_user(substream, arg);
2707 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002708 return snd_pcm_status_user(substream, arg, false);
2709 case SNDRV_PCM_IOCTL_STATUS_EXT:
2710 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2712 return snd_pcm_channel_info_user(substream, arg);
2713 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002714 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 case SNDRV_PCM_IOCTL_RESET:
2716 return snd_pcm_reset(substream);
2717 case SNDRV_PCM_IOCTL_START:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002718 return snd_pcm_start_lock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 case SNDRV_PCM_IOCTL_LINK:
2720 return snd_pcm_link(substream, (int)(unsigned long) arg);
2721 case SNDRV_PCM_IOCTL_UNLINK:
2722 return snd_pcm_unlink(substream);
2723 case SNDRV_PCM_IOCTL_RESUME:
2724 return snd_pcm_resume(substream);
2725 case SNDRV_PCM_IOCTL_XRUN:
2726 return snd_pcm_xrun(substream);
2727 case SNDRV_PCM_IOCTL_HWSYNC:
2728 return snd_pcm_hwsync(substream);
2729 case SNDRV_PCM_IOCTL_DELAY:
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002730 {
2731 snd_pcm_sframes_t delay = snd_pcm_delay(substream);
2732 snd_pcm_sframes_t __user *res = arg;
2733
2734 if (delay < 0)
2735 return delay;
2736 if (put_user(delay, res))
2737 return -EFAULT;
2738 return 0;
2739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 case SNDRV_PCM_IOCTL_SYNC_PTR:
2741 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002742#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2744 return snd_pcm_hw_refine_old_user(substream, arg);
2745 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2746 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002747#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002749 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 case SNDRV_PCM_IOCTL_DROP:
2751 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002752 case SNDRV_PCM_IOCTL_PAUSE:
2753 {
2754 int res;
2755 snd_pcm_stream_lock_irq(substream);
2756 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2757 snd_pcm_stream_unlock_irq(substream);
2758 return res;
2759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002761 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return -ENOTTY;
2763}
2764
Takashi Iwai0df63e42006-04-28 15:13:41 +02002765static int snd_pcm_playback_ioctl1(struct file *file,
2766 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 unsigned int cmd, void __user *arg)
2768{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002769 if (snd_BUG_ON(!substream))
2770 return -ENXIO;
2771 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 switch (cmd) {
2774 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2775 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002776 struct snd_xferi xferi;
2777 struct snd_xferi __user *_xferi = arg;
2778 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 snd_pcm_sframes_t result;
2780 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2781 return -EBADFD;
2782 if (put_user(0, &_xferi->result))
2783 return -EFAULT;
2784 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2785 return -EFAULT;
2786 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2787 __put_user(result, &_xferi->result);
2788 return result < 0 ? result : 0;
2789 }
2790 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2791 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002792 struct snd_xfern xfern;
2793 struct snd_xfern __user *_xfern = arg;
2794 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 void __user **bufs;
2796 snd_pcm_sframes_t result;
2797 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2798 return -EBADFD;
2799 if (runtime->channels > 128)
2800 return -EINVAL;
2801 if (put_user(0, &_xfern->result))
2802 return -EFAULT;
2803 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2804 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002805
2806 bufs = memdup_user(xfern.bufs,
2807 sizeof(void *) * runtime->channels);
2808 if (IS_ERR(bufs))
2809 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2811 kfree(bufs);
2812 __put_user(result, &_xfern->result);
2813 return result < 0 ? result : 0;
2814 }
2815 case SNDRV_PCM_IOCTL_REWIND:
2816 {
2817 snd_pcm_uframes_t frames;
2818 snd_pcm_uframes_t __user *_frames = arg;
2819 snd_pcm_sframes_t result;
2820 if (get_user(frames, _frames))
2821 return -EFAULT;
2822 if (put_user(0, _frames))
2823 return -EFAULT;
2824 result = snd_pcm_playback_rewind(substream, frames);
2825 __put_user(result, _frames);
2826 return result < 0 ? result : 0;
2827 }
2828 case SNDRV_PCM_IOCTL_FORWARD:
2829 {
2830 snd_pcm_uframes_t frames;
2831 snd_pcm_uframes_t __user *_frames = arg;
2832 snd_pcm_sframes_t result;
2833 if (get_user(frames, _frames))
2834 return -EFAULT;
2835 if (put_user(0, _frames))
2836 return -EFAULT;
2837 result = snd_pcm_playback_forward(substream, frames);
2838 __put_user(result, _frames);
2839 return result < 0 ? result : 0;
2840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002842 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
2844
Takashi Iwai0df63e42006-04-28 15:13:41 +02002845static int snd_pcm_capture_ioctl1(struct file *file,
2846 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 unsigned int cmd, void __user *arg)
2848{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002849 if (snd_BUG_ON(!substream))
2850 return -ENXIO;
2851 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2852 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 switch (cmd) {
2854 case SNDRV_PCM_IOCTL_READI_FRAMES:
2855 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002856 struct snd_xferi xferi;
2857 struct snd_xferi __user *_xferi = arg;
2858 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 snd_pcm_sframes_t result;
2860 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2861 return -EBADFD;
2862 if (put_user(0, &_xferi->result))
2863 return -EFAULT;
2864 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2865 return -EFAULT;
2866 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2867 __put_user(result, &_xferi->result);
2868 return result < 0 ? result : 0;
2869 }
2870 case SNDRV_PCM_IOCTL_READN_FRAMES:
2871 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002872 struct snd_xfern xfern;
2873 struct snd_xfern __user *_xfern = arg;
2874 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 void *bufs;
2876 snd_pcm_sframes_t result;
2877 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2878 return -EBADFD;
2879 if (runtime->channels > 128)
2880 return -EINVAL;
2881 if (put_user(0, &_xfern->result))
2882 return -EFAULT;
2883 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2884 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002885
2886 bufs = memdup_user(xfern.bufs,
2887 sizeof(void *) * runtime->channels);
2888 if (IS_ERR(bufs))
2889 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2891 kfree(bufs);
2892 __put_user(result, &_xfern->result);
2893 return result < 0 ? result : 0;
2894 }
2895 case SNDRV_PCM_IOCTL_REWIND:
2896 {
2897 snd_pcm_uframes_t frames;
2898 snd_pcm_uframes_t __user *_frames = arg;
2899 snd_pcm_sframes_t result;
2900 if (get_user(frames, _frames))
2901 return -EFAULT;
2902 if (put_user(0, _frames))
2903 return -EFAULT;
2904 result = snd_pcm_capture_rewind(substream, frames);
2905 __put_user(result, _frames);
2906 return result < 0 ? result : 0;
2907 }
2908 case SNDRV_PCM_IOCTL_FORWARD:
2909 {
2910 snd_pcm_uframes_t frames;
2911 snd_pcm_uframes_t __user *_frames = arg;
2912 snd_pcm_sframes_t result;
2913 if (get_user(frames, _frames))
2914 return -EFAULT;
2915 if (put_user(0, _frames))
2916 return -EFAULT;
2917 result = snd_pcm_capture_forward(substream, frames);
2918 __put_user(result, _frames);
2919 return result < 0 ? result : 0;
2920 }
2921 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002922 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923}
2924
Takashi Iwai877211f2005-11-17 13:59:38 +01002925static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2926 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
Takashi Iwai877211f2005-11-17 13:59:38 +01002928 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 pcm_file = file->private_data;
2931
2932 if (((cmd >> 8) & 0xff) != 'A')
2933 return -ENOTTY;
2934
Takashi Iwai0df63e42006-04-28 15:13:41 +02002935 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2936 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
Takashi Iwai877211f2005-11-17 13:59:38 +01002939static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2940 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Takashi Iwai877211f2005-11-17 13:59:38 +01002942 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 pcm_file = file->private_data;
2945
2946 if (((cmd >> 8) & 0xff) != 'A')
2947 return -ENOTTY;
2948
Takashi Iwai0df63e42006-04-28 15:13:41 +02002949 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2950 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002953/**
2954 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
2955 * @substream: PCM substream
2956 * @cmd: IOCTL cmd
2957 * @arg: IOCTL argument
2958 *
2959 * The function is provided primarily for OSS layer and USB gadget drivers,
2960 * and it allows only the limited set of ioctls (hw_params, sw_params,
2961 * prepare, start, drain, drop, forward).
2962 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002963int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 unsigned int cmd, void *arg)
2965{
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002966 snd_pcm_uframes_t *frames = arg;
2967 snd_pcm_sframes_t result;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002968
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002969 switch (cmd) {
2970 case SNDRV_PCM_IOCTL_FORWARD:
2971 {
2972 /* provided only for OSS; capture-only and no value returned */
2973 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
2974 return -EINVAL;
2975 result = snd_pcm_capture_forward(substream, *frames);
2976 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
Takashi Iwaic2c86a92017-05-10 14:33:44 +02002978 case SNDRV_PCM_IOCTL_HW_PARAMS:
2979 return snd_pcm_hw_params(substream, arg);
2980 case SNDRV_PCM_IOCTL_SW_PARAMS:
2981 return snd_pcm_sw_params(substream, arg);
2982 case SNDRV_PCM_IOCTL_PREPARE:
2983 return snd_pcm_prepare(substream, NULL);
2984 case SNDRV_PCM_IOCTL_START:
2985 return snd_pcm_start_lock_irq(substream);
2986 case SNDRV_PCM_IOCTL_DRAIN:
2987 return snd_pcm_drain(substream, NULL);
2988 case SNDRV_PCM_IOCTL_DROP:
2989 return snd_pcm_drop(substream);
2990 case SNDRV_PCM_IOCTL_DELAY:
2991 {
2992 result = snd_pcm_delay(substream);
2993 if (result < 0)
2994 return result;
2995 *frames = result;
2996 return 0;
2997 }
2998 default:
2999 return -EINVAL;
3000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003002EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3003
Takashi Iwai877211f2005-11-17 13:59:38 +01003004static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3005 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
Takashi Iwai877211f2005-11-17 13:59:38 +01003007 struct snd_pcm_file *pcm_file;
3008 struct snd_pcm_substream *substream;
3009 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 snd_pcm_sframes_t result;
3011
3012 pcm_file = file->private_data;
3013 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003014 if (PCM_RUNTIME_CHECK(substream))
3015 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 runtime = substream->runtime;
3017 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3018 return -EBADFD;
3019 if (!frame_aligned(runtime, count))
3020 return -EINVAL;
3021 count = bytes_to_frames(runtime, count);
3022 result = snd_pcm_lib_read(substream, buf, count);
3023 if (result > 0)
3024 result = frames_to_bytes(runtime, result);
3025 return result;
3026}
3027
Takashi Iwai877211f2005-11-17 13:59:38 +01003028static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3029 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Takashi Iwai877211f2005-11-17 13:59:38 +01003031 struct snd_pcm_file *pcm_file;
3032 struct snd_pcm_substream *substream;
3033 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 snd_pcm_sframes_t result;
3035
3036 pcm_file = file->private_data;
3037 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003038 if (PCM_RUNTIME_CHECK(substream))
3039 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003041 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3042 return -EBADFD;
3043 if (!frame_aligned(runtime, count))
3044 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 count = bytes_to_frames(runtime, count);
3046 result = snd_pcm_lib_write(substream, buf, count);
3047 if (result > 0)
3048 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return result;
3050}
3051
Al Viro1c65d982015-04-04 00:19:32 -04003052static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Takashi Iwai877211f2005-11-17 13:59:38 +01003054 struct snd_pcm_file *pcm_file;
3055 struct snd_pcm_substream *substream;
3056 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 snd_pcm_sframes_t result;
3058 unsigned long i;
3059 void __user **bufs;
3060 snd_pcm_uframes_t frames;
3061
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003062 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003064 if (PCM_RUNTIME_CHECK(substream))
3065 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 runtime = substream->runtime;
3067 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3068 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003069 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003071 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003073 if (!frame_aligned(runtime, to->iov->iov_len))
3074 return -EINVAL;
3075 frames = bytes_to_samples(runtime, to->iov->iov_len);
3076 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 if (bufs == NULL)
3078 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003079 for (i = 0; i < to->nr_segs; ++i)
3080 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 result = snd_pcm_lib_readv(substream, bufs, frames);
3082 if (result > 0)
3083 result = frames_to_bytes(runtime, result);
3084 kfree(bufs);
3085 return result;
3086}
3087
Al Viro1c65d982015-04-04 00:19:32 -04003088static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Takashi Iwai877211f2005-11-17 13:59:38 +01003090 struct snd_pcm_file *pcm_file;
3091 struct snd_pcm_substream *substream;
3092 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 snd_pcm_sframes_t result;
3094 unsigned long i;
3095 void __user **bufs;
3096 snd_pcm_uframes_t frames;
3097
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003098 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003100 if (PCM_RUNTIME_CHECK(substream))
3101 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003103 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3104 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003105 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003106 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003107 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3108 !frame_aligned(runtime, from->iov->iov_len))
3109 return -EINVAL;
3110 frames = bytes_to_samples(runtime, from->iov->iov_len);
3111 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (bufs == NULL)
3113 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003114 for (i = 0; i < from->nr_segs; ++i)
3115 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 result = snd_pcm_lib_writev(substream, bufs, frames);
3117 if (result > 0)
3118 result = frames_to_bytes(runtime, result);
3119 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return result;
3121}
3122
3123static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3124{
Takashi Iwai877211f2005-11-17 13:59:38 +01003125 struct snd_pcm_file *pcm_file;
3126 struct snd_pcm_substream *substream;
3127 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 unsigned int mask;
3129 snd_pcm_uframes_t avail;
3130
3131 pcm_file = file->private_data;
3132
3133 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003134 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003135 return POLLOUT | POLLWRNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 runtime = substream->runtime;
3137
3138 poll_wait(file, &runtime->sleep, wait);
3139
3140 snd_pcm_stream_lock_irq(substream);
3141 avail = snd_pcm_playback_avail(runtime);
3142 switch (runtime->status->state) {
3143 case SNDRV_PCM_STATE_RUNNING:
3144 case SNDRV_PCM_STATE_PREPARED:
3145 case SNDRV_PCM_STATE_PAUSED:
3146 if (avail >= runtime->control->avail_min) {
3147 mask = POLLOUT | POLLWRNORM;
3148 break;
3149 }
3150 /* Fall through */
3151 case SNDRV_PCM_STATE_DRAINING:
3152 mask = 0;
3153 break;
3154 default:
3155 mask = POLLOUT | POLLWRNORM | POLLERR;
3156 break;
3157 }
3158 snd_pcm_stream_unlock_irq(substream);
3159 return mask;
3160}
3161
3162static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3163{
Takashi Iwai877211f2005-11-17 13:59:38 +01003164 struct snd_pcm_file *pcm_file;
3165 struct snd_pcm_substream *substream;
3166 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 unsigned int mask;
3168 snd_pcm_uframes_t avail;
3169
3170 pcm_file = file->private_data;
3171
3172 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003173 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003174 return POLLIN | POLLRDNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 runtime = substream->runtime;
3176
3177 poll_wait(file, &runtime->sleep, wait);
3178
3179 snd_pcm_stream_lock_irq(substream);
3180 avail = snd_pcm_capture_avail(runtime);
3181 switch (runtime->status->state) {
3182 case SNDRV_PCM_STATE_RUNNING:
3183 case SNDRV_PCM_STATE_PREPARED:
3184 case SNDRV_PCM_STATE_PAUSED:
3185 if (avail >= runtime->control->avail_min) {
3186 mask = POLLIN | POLLRDNORM;
3187 break;
3188 }
3189 mask = 0;
3190 break;
3191 case SNDRV_PCM_STATE_DRAINING:
3192 if (avail > 0) {
3193 mask = POLLIN | POLLRDNORM;
3194 break;
3195 }
3196 /* Fall through */
3197 default:
3198 mask = POLLIN | POLLRDNORM | POLLERR;
3199 break;
3200 }
3201 snd_pcm_stream_unlock_irq(substream);
3202 return mask;
3203}
3204
3205/*
3206 * mmap support
3207 */
3208
3209/*
3210 * Only on coherent architectures, we can mmap the status and the control records
3211 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3212 */
3213#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3214/*
3215 * mmap status record
3216 */
Dave Jiang11bac802017-02-24 14:56:41 -08003217static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
Dave Jiang11bac802017-02-24 14:56:41 -08003219 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003220 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003223 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003225 vmf->page = virt_to_page(runtime->status);
3226 get_page(vmf->page);
3227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003230static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003232 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233};
3234
Takashi Iwai877211f2005-11-17 13:59:38 +01003235static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 struct vm_area_struct *area)
3237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 long size;
3239 if (!(area->vm_flags & VM_READ))
3240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003242 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return -EINVAL;
3244 area->vm_ops = &snd_pcm_vm_ops_status;
3245 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003246 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return 0;
3248}
3249
3250/*
3251 * mmap control record
3252 */
Dave Jiang11bac802017-02-24 14:56:41 -08003253static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
Dave Jiang11bac802017-02-24 14:56:41 -08003255 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003256 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003259 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003261 vmf->page = virt_to_page(runtime->control);
3262 get_page(vmf->page);
3263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003266static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003268 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269};
3270
Takashi Iwai877211f2005-11-17 13:59:38 +01003271static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 struct vm_area_struct *area)
3273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 long size;
3275 if (!(area->vm_flags & VM_READ))
3276 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003278 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return -EINVAL;
3280 area->vm_ops = &snd_pcm_vm_ops_control;
3281 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003282 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return 0;
3284}
3285#else /* ! coherent mmap */
3286/*
3287 * don't support mmap for status and control records.
3288 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003289static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 struct vm_area_struct *area)
3291{
3292 return -ENXIO;
3293}
Takashi Iwai877211f2005-11-17 13:59:38 +01003294static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 struct vm_area_struct *area)
3296{
3297 return -ENXIO;
3298}
3299#endif /* coherent mmap */
3300
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003301static inline struct page *
3302snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3303{
3304 void *vaddr = substream->runtime->dma_area + ofs;
3305 return virt_to_page(vaddr);
3306}
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003309 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 */
Dave Jiang11bac802017-02-24 14:56:41 -08003311static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Dave Jiang11bac802017-02-24 14:56:41 -08003313 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003314 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 unsigned long offset;
3316 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 size_t dma_bytes;
3318
3319 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003320 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003322 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3324 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003325 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003326 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003328 else
3329 page = snd_pcm_default_page_ops(substream, offset);
3330 if (!page)
3331 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003332 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003333 vmf->page = page;
3334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
Takashi Iwai657b1982009-11-26 12:40:21 +01003337static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3338 .open = snd_pcm_mmap_data_open,
3339 .close = snd_pcm_mmap_data_close,
3340};
3341
3342static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 .open = snd_pcm_mmap_data_open,
3344 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003345 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346};
3347
3348/*
3349 * mmap the DMA buffer on RAM
3350 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003351
3352/**
3353 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3354 * @substream: PCM substream
3355 * @area: VMA
3356 *
3357 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3358 * this function is invoked implicitly.
3359 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003360int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3361 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003363 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003364#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003365 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3366 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3367 return remap_pfn_range(area, area->vm_start,
3368 substream->dma_buffer.addr >> PAGE_SHIFT,
3369 area->vm_end - area->vm_start, area->vm_page_prot);
3370 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003371#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003372#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003373 if (!substream->ops->page &&
3374 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3375 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3376 area,
3377 substream->runtime->dma_area,
3378 substream->runtime->dma_addr,
3379 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003380#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003381 /* mmap with fault handler */
3382 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return 0;
3384}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003385EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
3387/*
3388 * mmap the DMA buffer on I/O memory area
3389 */
3390#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003391/**
3392 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3393 * @substream: PCM substream
3394 * @area: VMA
3395 *
3396 * When your hardware uses the iomapped pages as the hardware buffer and
3397 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3398 * is supposed to work only on limited architectures.
3399 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003400int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3401 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003403 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003406 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003408
3409EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410#endif /* SNDRV_PCM_INFO_MMAP */
3411
3412/*
3413 * mmap DMA buffer
3414 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003415int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 struct vm_area_struct *area)
3417{
Takashi Iwai877211f2005-11-17 13:59:38 +01003418 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 long size;
3420 unsigned long offset;
3421 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003422 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3425 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3426 return -EINVAL;
3427 } else {
3428 if (!(area->vm_flags & VM_READ))
3429 return -EINVAL;
3430 }
3431 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3433 return -EBADFD;
3434 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3435 return -ENXIO;
3436 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3437 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3438 return -EINVAL;
3439 size = area->vm_end - area->vm_start;
3440 offset = area->vm_pgoff << PAGE_SHIFT;
3441 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3442 if ((size_t)size > dma_bytes)
3443 return -EINVAL;
3444 if (offset > dma_bytes - size)
3445 return -EINVAL;
3446
Takashi Iwai657b1982009-11-26 12:40:21 +01003447 area->vm_ops = &snd_pcm_vm_ops_data;
3448 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003450 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003452 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003453 if (!err)
3454 atomic_inc(&substream->mmap_count);
3455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456}
3457
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003458EXPORT_SYMBOL(snd_pcm_mmap_data);
3459
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3461{
Takashi Iwai877211f2005-11-17 13:59:38 +01003462 struct snd_pcm_file * pcm_file;
3463 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 unsigned long offset;
3465
3466 pcm_file = file->private_data;
3467 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003468 if (PCM_RUNTIME_CHECK(substream))
3469 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 offset = area->vm_pgoff << PAGE_SHIFT;
3472 switch (offset) {
3473 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003474 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 return -ENXIO;
3476 return snd_pcm_mmap_status(substream, file, area);
3477 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003478 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 return -ENXIO;
3480 return snd_pcm_mmap_control(substream, file, area);
3481 default:
3482 return snd_pcm_mmap_data(substream, file, area);
3483 }
3484 return 0;
3485}
3486
3487static int snd_pcm_fasync(int fd, struct file * file, int on)
3488{
Takashi Iwai877211f2005-11-17 13:59:38 +01003489 struct snd_pcm_file * pcm_file;
3490 struct snd_pcm_substream *substream;
3491 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 pcm_file = file->private_data;
3494 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003495 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003496 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003498 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
3501/*
3502 * ioctl32 compat
3503 */
3504#ifdef CONFIG_COMPAT
3505#include "pcm_compat.c"
3506#else
3507#define snd_pcm_ioctl_compat NULL
3508#endif
3509
3510/*
3511 * To be removed helpers to keep binary compatibility
3512 */
3513
Takashi Iwai59d48582005-12-01 10:51:58 +01003514#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3516#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3517
Takashi Iwai877211f2005-11-17 13:59:38 +01003518static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3519 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
3521 unsigned int i;
3522
3523 memset(params, 0, sizeof(*params));
3524 params->flags = oparams->flags;
3525 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3526 params->masks[i].bits[0] = oparams->masks[i];
3527 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3528 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3529 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3530 params->info = oparams->info;
3531 params->msbits = oparams->msbits;
3532 params->rate_num = oparams->rate_num;
3533 params->rate_den = oparams->rate_den;
3534 params->fifo_size = oparams->fifo_size;
3535}
3536
Takashi Iwai877211f2005-11-17 13:59:38 +01003537static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3538 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
3540 unsigned int i;
3541
3542 memset(oparams, 0, sizeof(*oparams));
3543 oparams->flags = params->flags;
3544 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3545 oparams->masks[i] = params->masks[i].bits[0];
3546 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3547 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3548 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3549 oparams->info = params->info;
3550 oparams->msbits = params->msbits;
3551 oparams->rate_num = params->rate_num;
3552 oparams->rate_den = params->rate_den;
3553 oparams->fifo_size = params->fifo_size;
3554}
3555
Takashi Iwai877211f2005-11-17 13:59:38 +01003556static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3557 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Takashi Iwai877211f2005-11-17 13:59:38 +01003559 struct snd_pcm_hw_params *params;
3560 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 int err;
3562
3563 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003564 if (!params)
3565 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Li Zefanef44a1e2009-04-10 09:43:08 +08003567 oparams = memdup_user(_oparams, sizeof(*oparams));
3568 if (IS_ERR(oparams)) {
3569 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 goto out;
3571 }
3572 snd_pcm_hw_convert_from_old_params(params, oparams);
3573 err = snd_pcm_hw_refine(substream, params);
3574 snd_pcm_hw_convert_to_old_params(oparams, params);
3575 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3576 if (!err)
3577 err = -EFAULT;
3578 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003579
3580 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581out:
3582 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 return err;
3584}
3585
Takashi Iwai877211f2005-11-17 13:59:38 +01003586static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3587 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Takashi Iwai877211f2005-11-17 13:59:38 +01003589 struct snd_pcm_hw_params *params;
3590 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 int err;
3592
3593 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003594 if (!params)
3595 return -ENOMEM;
3596
3597 oparams = memdup_user(_oparams, sizeof(*oparams));
3598 if (IS_ERR(oparams)) {
3599 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 goto out;
3601 }
3602 snd_pcm_hw_convert_from_old_params(params, oparams);
3603 err = snd_pcm_hw_params(substream, params);
3604 snd_pcm_hw_convert_to_old_params(oparams, params);
3605 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3606 if (!err)
3607 err = -EFAULT;
3608 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003609
3610 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611out:
3612 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return err;
3614}
Takashi Iwai59d48582005-12-01 10:51:58 +01003615#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Cliff Cai70036092008-09-03 10:54:36 +02003617#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003618static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3619 unsigned long addr,
3620 unsigned long len,
3621 unsigned long pgoff,
3622 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003623{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003624 struct snd_pcm_file *pcm_file = file->private_data;
3625 struct snd_pcm_substream *substream = pcm_file->substream;
3626 struct snd_pcm_runtime *runtime = substream->runtime;
3627 unsigned long offset = pgoff << PAGE_SHIFT;
3628
3629 switch (offset) {
3630 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3631 return (unsigned long)runtime->status;
3632 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3633 return (unsigned long)runtime->control;
3634 default:
3635 return (unsigned long)runtime->dma_area + offset;
3636 }
Cliff Cai70036092008-09-03 10:54:36 +02003637}
3638#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003639# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003640#endif
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642/*
3643 * Register section
3644 */
3645
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003646const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003648 .owner = THIS_MODULE,
3649 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003650 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003651 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003652 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003653 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003654 .poll = snd_pcm_playback_poll,
3655 .unlocked_ioctl = snd_pcm_playback_ioctl,
3656 .compat_ioctl = snd_pcm_ioctl_compat,
3657 .mmap = snd_pcm_mmap,
3658 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003659 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 },
3661 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003662 .owner = THIS_MODULE,
3663 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003664 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003665 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003666 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003667 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003668 .poll = snd_pcm_capture_poll,
3669 .unlocked_ioctl = snd_pcm_capture_ioctl,
3670 .compat_ioctl = snd_pcm_ioctl_compat,
3671 .mmap = snd_pcm_mmap,
3672 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003673 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
3675};