blob: 13dec5ec93f20d4cfd7b9b7c9d3d8b17ecd33324 [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
40/*
41 * Compatibility
42 */
43
Takashi Iwai877211f2005-11-17 13:59:38 +010044struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned int flags;
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010048 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
50 unsigned int rmask;
51 unsigned int cmask;
52 unsigned int info;
53 unsigned int msbits;
54 unsigned int rate_num;
55 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010056 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned char reserved[64];
58};
59
Takashi Iwai59d48582005-12-01 10:51:58 +010060#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010061#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Takashi Iwai877211f2005-11-17 13:59:38 +010064static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010068#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010069static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 *
73 */
74
Takashi Iwai7af142f2014-09-01 11:19:37 +020075static DEFINE_RWLOCK(snd_pcm_link_rwlock);
76static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Takashi Iwai67ec10722016-02-17 14:30:26 +010078/* Writer in rwsem may block readers even during its waiting in queue,
79 * and this may lead to a deadlock when the code path takes read sem
80 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
81 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
82 * spin until it gets the lock.
83 */
84static inline void down_write_nonblock(struct rw_semaphore *lock)
85{
86 while (!down_write_trylock(lock))
87 cond_resched();
88}
89
Takashi Iwai30b771c2014-10-30 15:02:50 +010090/**
91 * snd_pcm_stream_lock - Lock the PCM stream
92 * @substream: PCM substream
93 *
94 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
95 * flag of the given substream. This also takes the global link rw lock
96 * (or rw sem), too, for avoiding the race with linked streams.
97 */
Takashi Iwai7af142f2014-09-01 11:19:37 +020098void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
99{
100 if (substream->pcm->nonatomic) {
Takashi Iwai67756e32015-07-17 15:22:33 +0200101 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200102 mutex_lock(&substream->self_group.mutex);
103 } else {
104 read_lock(&snd_pcm_link_rwlock);
105 spin_lock(&substream->self_group.lock);
106 }
107}
108EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
109
Takashi Iwai30b771c2014-10-30 15:02:50 +0100110/**
111 * snd_pcm_stream_lock - Unlock the PCM stream
112 * @substream: PCM substream
113 *
114 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
115 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200116void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
117{
118 if (substream->pcm->nonatomic) {
119 mutex_unlock(&substream->self_group.mutex);
120 up_read(&snd_pcm_link_rwsem);
121 } else {
122 spin_unlock(&substream->self_group.lock);
123 read_unlock(&snd_pcm_link_rwlock);
124 }
125}
126EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
127
Takashi Iwai30b771c2014-10-30 15:02:50 +0100128/**
129 * snd_pcm_stream_lock_irq - Lock the PCM stream
130 * @substream: PCM substream
131 *
132 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
133 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
134 * as snd_pcm_stream_lock().
135 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200136void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
137{
138 if (!substream->pcm->nonatomic)
139 local_irq_disable();
140 snd_pcm_stream_lock(substream);
141}
142EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
143
Takashi Iwai30b771c2014-10-30 15:02:50 +0100144/**
145 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
146 * @substream: PCM substream
147 *
148 * This is a counter-part of snd_pcm_stream_lock_irq().
149 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200150void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
151{
152 snd_pcm_stream_unlock(substream);
153 if (!substream->pcm->nonatomic)
154 local_irq_enable();
155}
156EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
157
158unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
159{
160 unsigned long flags = 0;
161 if (!substream->pcm->nonatomic)
162 local_irq_save(flags);
163 snd_pcm_stream_lock(substream);
164 return flags;
165}
166EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
167
Takashi Iwai30b771c2014-10-30 15:02:50 +0100168/**
169 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
170 * @substream: PCM substream
171 * @flags: irq flags
172 *
173 * This is a counter-part of snd_pcm_stream_lock_irqsave().
174 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200175void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
176 unsigned long flags)
177{
178 snd_pcm_stream_unlock(substream);
179 if (!substream->pcm->nonatomic)
180 local_irq_restore(flags);
181}
182EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184static inline mm_segment_t snd_enter_user(void)
185{
186 mm_segment_t fs = get_fs();
187 set_fs(get_ds());
188 return fs;
189}
190
191static inline void snd_leave_user(mm_segment_t fs)
192{
193 set_fs(fs);
194}
195
196
197
Takashi Iwai877211f2005-11-17 13:59:38 +0100198int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Takashi Iwai877211f2005-11-17 13:59:38 +0100200 struct snd_pcm_runtime *runtime;
201 struct snd_pcm *pcm = substream->pcm;
202 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 memset(info, 0, sizeof(*info));
205 info->card = pcm->card->number;
206 info->device = pcm->device;
207 info->stream = substream->stream;
208 info->subdevice = substream->number;
209 strlcpy(info->id, pcm->id, sizeof(info->id));
210 strlcpy(info->name, pcm->name, sizeof(info->name));
211 info->dev_class = pcm->dev_class;
212 info->dev_subclass = pcm->dev_subclass;
213 info->subdevices_count = pstr->substream_count;
214 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
215 strlcpy(info->subname, substream->name, sizeof(info->subname));
216 runtime = substream->runtime;
217 /* AB: FIXME!!! This is definitely nonsense */
218 if (runtime) {
219 info->sync = runtime->sync;
220 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
221 }
222 return 0;
223}
224
Takashi Iwai877211f2005-11-17 13:59:38 +0100225int snd_pcm_info_user(struct snd_pcm_substream *substream,
226 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Takashi Iwai877211f2005-11-17 13:59:38 +0100228 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int err;
230
231 info = kmalloc(sizeof(*info), GFP_KERNEL);
232 if (! info)
233 return -ENOMEM;
234 err = snd_pcm_info(substream, info);
235 if (err >= 0) {
236 if (copy_to_user(_info, info, sizeof(*info)))
237 err = -EFAULT;
238 }
239 kfree(info);
240 return err;
241}
242
Takashi Iwai63825f32014-10-22 12:04:46 +0200243static bool hw_support_mmap(struct snd_pcm_substream *substream)
244{
245 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
246 return false;
247 /* check architectures that return -EINVAL from dma_mmap_coherent() */
248 /* FIXME: this should be some global flag */
249#if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
250 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
251 if (!substream->ops->mmap &&
252 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
253 return false;
254#endif
255 return true;
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#undef RULES_DEBUG
259
260#ifdef RULES_DEBUG
261#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
Joe Perches47023ec2010-09-13 21:24:02 -0700262static const char * const snd_pcm_hw_param_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 HW_PARAM(ACCESS),
264 HW_PARAM(FORMAT),
265 HW_PARAM(SUBFORMAT),
266 HW_PARAM(SAMPLE_BITS),
267 HW_PARAM(FRAME_BITS),
268 HW_PARAM(CHANNELS),
269 HW_PARAM(RATE),
270 HW_PARAM(PERIOD_TIME),
271 HW_PARAM(PERIOD_SIZE),
272 HW_PARAM(PERIOD_BYTES),
273 HW_PARAM(PERIODS),
274 HW_PARAM(BUFFER_TIME),
275 HW_PARAM(BUFFER_SIZE),
276 HW_PARAM(BUFFER_BYTES),
277 HW_PARAM(TICK_TIME),
278};
279#endif
280
Takashi Iwai877211f2005-11-17 13:59:38 +0100281int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
282 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100285 struct snd_pcm_hardware *hw;
286 struct snd_interval *i = NULL;
287 struct snd_mask *m = NULL;
288 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 unsigned int rstamps[constrs->rules_num];
290 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
291 unsigned int stamp = 2;
292 int changed, again;
293
294 params->info = 0;
295 params->fifo_size = 0;
296 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
297 params->msbits = 0;
298 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
299 params->rate_num = 0;
300 params->rate_den = 0;
301 }
302
303 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
304 m = hw_param_mask(params, k);
305 if (snd_mask_empty(m))
306 return -EINVAL;
307 if (!(params->rmask & (1 << k)))
308 continue;
309#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100310 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
311 pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312#endif
313 changed = snd_mask_refine(m, constrs_mask(constrs, k));
314#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100315 pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#endif
317 if (changed)
318 params->cmask |= 1 << k;
319 if (changed < 0)
320 return changed;
321 }
322
323 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
324 i = hw_param_interval(params, k);
325 if (snd_interval_empty(i))
326 return -EINVAL;
327 if (!(params->rmask & (1 << k)))
328 continue;
329#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100330 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100332 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100334 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 i->openmin ? '(' : '[', i->min,
336 i->max, i->openmax ? ')' : ']');
Takashi Iwai09e56df2014-02-04 18:19:48 +0100337 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#endif
339 changed = snd_interval_refine(i, constrs_interval(constrs, k));
340#ifdef RULES_DEBUG
341 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100342 pr_cont("empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100344 pr_cont("%c%u %u%c\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 i->openmin ? '(' : '[', i->min,
346 i->max, i->openmax ? ')' : ']');
347#endif
348 if (changed)
349 params->cmask |= 1 << k;
350 if (changed < 0)
351 return changed;
352 }
353
354 for (k = 0; k < constrs->rules_num; k++)
355 rstamps[k] = 0;
356 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
357 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
358 do {
359 again = 0;
360 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100361 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned int d;
363 int doit = 0;
364 if (r->cond && !(r->cond & params->flags))
365 continue;
366 for (d = 0; r->deps[d] >= 0; d++) {
367 if (vstamps[r->deps[d]] > rstamps[k]) {
368 doit = 1;
369 break;
370 }
371 }
372 if (!doit)
373 continue;
374#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100375 pr_debug("Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100377 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (hw_is_mask(r->var)) {
379 m = hw_param_mask(params, r->var);
Takashi Iwai09e56df2014-02-04 18:19:48 +0100380 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 } else {
382 i = hw_param_interval(params, r->var);
383 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100384 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100386 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 i->openmin ? '(' : '[', i->min,
388 i->max, i->openmax ? ')' : ']');
389 }
390 }
391#endif
392 changed = r->func(params, r);
393#ifdef RULES_DEBUG
394 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100395 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (hw_is_mask(r->var))
Takashi Iwai09e56df2014-02-04 18:19:48 +0100397 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 else {
399 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100400 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100402 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 i->openmin ? '(' : '[', i->min,
404 i->max, i->openmax ? ')' : ']');
405 }
406 }
Takashi Iwai09e56df2014-02-04 18:19:48 +0100407 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#endif
409 rstamps[k] = stamp;
410 if (changed && r->var >= 0) {
411 params->cmask |= (1 << r->var);
412 vstamps[r->var] = stamp;
413 again = 1;
414 }
415 if (changed < 0)
416 return changed;
417 stamp++;
418 }
419 } while (again);
420 if (!params->msbits) {
421 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
422 if (snd_interval_single(i))
423 params->msbits = snd_interval_value(i);
424 }
425
426 if (!params->rate_den) {
427 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
428 if (snd_interval_single(i)) {
429 params->rate_num = snd_interval_value(i);
430 params->rate_den = 1;
431 }
432 }
433
434 hw = &substream->runtime->hw;
Takashi Iwai63825f32014-10-22 12:04:46 +0200435 if (!params->info) {
Libin Yang48d88292014-12-31 22:09:54 +0800436 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
437 SNDRV_PCM_INFO_DRAIN_TRIGGER);
Takashi Iwai63825f32014-10-22 12:04:46 +0200438 if (!hw_support_mmap(substream))
439 params->info &= ~(SNDRV_PCM_INFO_MMAP |
440 SNDRV_PCM_INFO_MMAP_VALID);
441 }
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200442 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100443 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
444 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
445 if (snd_mask_min(m) == snd_mask_max(m) &&
446 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200447 changed = substream->ops->ioctl(substream,
448 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200449 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200450 return changed;
451 }
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 params->rmask = 0;
454 return 0;
455}
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001085int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Takashi Iwai877211f2005-11-17 13:59:38 +01001087 return snd_pcm_action(&snd_pcm_action_start, substream,
1088 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091/*
1092 * stop callbacks
1093 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001094static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Takashi Iwai877211f2005-11-17 13:59:38 +01001096 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1098 return -EBADFD;
1099 runtime->trigger_master = substream;
1100 return 0;
1101}
1102
Takashi Iwai877211f2005-11-17 13:59:38 +01001103static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 if (substream->runtime->trigger_master == substream &&
1106 snd_pcm_running(substream))
1107 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1108 return 0; /* unconditonally stop all substreams */
1109}
1110
Takashi Iwai877211f2005-11-17 13:59:38 +01001111static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Takashi Iwai877211f2005-11-17 13:59:38 +01001113 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (runtime->status->state != state) {
1115 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001116 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001117 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001120 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Julia Lawallb17154c2015-11-29 16:36:40 +01001123static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 .pre_action = snd_pcm_pre_stop,
1125 .do_action = snd_pcm_do_stop,
1126 .post_action = snd_pcm_post_stop
1127};
1128
1129/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001130 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001131 * @substream: the PCM substream instance
1132 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001134 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001135 *
Masanari Iida0a114582014-02-09 00:47:36 +09001136 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001138int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1141}
1142
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001143EXPORT_SYMBOL(snd_pcm_stop);
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001146 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001147 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001149 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001151 *
1152 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001154int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Takashi Iwai877211f2005-11-17 13:59:38 +01001156 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1157 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
Takashi Iwai1fb85102014-11-07 17:08:28 +01001160/**
1161 * snd_pcm_stop_xrun - stop the running streams as XRUN
1162 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001163 *
1164 * This stops the given running substream (and all linked substreams) as XRUN.
1165 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1166 *
1167 * Return: Zero if successful, or a negative error code.
1168 */
1169int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1170{
1171 unsigned long flags;
1172 int ret = 0;
1173
1174 snd_pcm_stream_lock_irqsave(substream, flags);
1175 if (snd_pcm_running(substream))
1176 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1177 snd_pcm_stream_unlock_irqrestore(substream, flags);
1178 return ret;
1179}
1180EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/*
1183 * pause callbacks
1184 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001185static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Takashi Iwai877211f2005-11-17 13:59:38 +01001187 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1189 return -ENOSYS;
1190 if (push) {
1191 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1192 return -EBADFD;
1193 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1194 return -EBADFD;
1195 runtime->trigger_master = substream;
1196 return 0;
1197}
1198
Takashi Iwai877211f2005-11-17 13:59:38 +01001199static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 if (substream->runtime->trigger_master != substream)
1202 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001203 /* some drivers might use hw_ptr to recover from the pause -
1204 update the hw_ptr now */
1205 if (push)
1206 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001207 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001208 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001209 * delta, effectively to skip the check once.
1210 */
1211 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return substream->ops->trigger(substream,
1213 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1214 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1215}
1216
Takashi Iwai877211f2005-11-17 13:59:38 +01001217static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 if (substream->runtime->trigger_master == substream)
1220 substream->ops->trigger(substream,
1221 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1222 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1223}
1224
Takashi Iwai877211f2005-11-17 13:59:38 +01001225static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Takashi Iwai877211f2005-11-17 13:59:38 +01001227 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 snd_pcm_trigger_tstamp(substream);
1229 if (push) {
1230 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001231 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001233 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 } else {
1235 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001236 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238}
1239
Julia Lawallb17154c2015-11-29 16:36:40 +01001240static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 .pre_action = snd_pcm_pre_pause,
1242 .do_action = snd_pcm_do_pause,
1243 .undo_action = snd_pcm_undo_pause,
1244 .post_action = snd_pcm_post_pause
1245};
1246
1247/*
1248 * Push/release the pause for all linked streams.
1249 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001250static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1253}
1254
1255#ifdef CONFIG_PM
1256/* suspend */
1257
Takashi Iwai877211f2005-11-17 13:59:38 +01001258static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Takashi Iwai877211f2005-11-17 13:59:38 +01001260 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1262 return -EBUSY;
1263 runtime->trigger_master = substream;
1264 return 0;
1265}
1266
Takashi Iwai877211f2005-11-17 13:59:38 +01001267static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Takashi Iwai877211f2005-11-17 13:59:38 +01001269 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (runtime->trigger_master != substream)
1271 return 0;
1272 if (! snd_pcm_running(substream))
1273 return 0;
1274 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1275 return 0; /* suspend unconditionally */
1276}
1277
Takashi Iwai877211f2005-11-17 13:59:38 +01001278static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Takashi Iwai877211f2005-11-17 13:59:38 +01001280 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001282 runtime->status->suspended_state = runtime->status->state;
1283 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001284 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001286 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Julia Lawallb17154c2015-11-29 16:36:40 +01001289static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 .pre_action = snd_pcm_pre_suspend,
1291 .do_action = snd_pcm_do_suspend,
1292 .post_action = snd_pcm_post_suspend
1293};
1294
1295/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001296 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001297 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001300 *
1301 * Return: Zero if successful (or @substream is %NULL), or a negative error
1302 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001304int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 int err;
1307 unsigned long flags;
1308
Takashi Iwai603bf5242005-11-17 15:59:14 +01001309 if (! substream)
1310 return 0;
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 snd_pcm_stream_lock_irqsave(substream, flags);
1313 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1314 snd_pcm_stream_unlock_irqrestore(substream, flags);
1315 return err;
1316}
1317
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001318EXPORT_SYMBOL(snd_pcm_suspend);
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001321 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001322 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001325 *
1326 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001328int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Takashi Iwai877211f2005-11-17 13:59:38 +01001330 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int stream, err = 0;
1332
Takashi Iwai603bf5242005-11-17 15:59:14 +01001333 if (! pcm)
1334 return 0;
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001337 for (substream = pcm->streams[stream].substream;
1338 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* FIXME: the open/close code should lock this as well */
1340 if (substream->runtime == NULL)
1341 continue;
1342 err = snd_pcm_suspend(substream);
1343 if (err < 0 && err != -EBUSY)
1344 return err;
1345 }
1346 }
1347 return 0;
1348}
1349
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001350EXPORT_SYMBOL(snd_pcm_suspend_all);
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352/* resume */
1353
Takashi Iwai877211f2005-11-17 13:59:38 +01001354static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Takashi Iwai877211f2005-11-17 13:59:38 +01001356 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1358 return -ENOSYS;
1359 runtime->trigger_master = substream;
1360 return 0;
1361}
1362
Takashi Iwai877211f2005-11-17 13:59:38 +01001363static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Takashi Iwai877211f2005-11-17 13:59:38 +01001365 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (runtime->trigger_master != substream)
1367 return 0;
1368 /* DMA not running previously? */
1369 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1370 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1371 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1372 return 0;
1373 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1374}
1375
Takashi Iwai877211f2005-11-17 13:59:38 +01001376static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 if (substream->runtime->trigger_master == substream &&
1379 snd_pcm_running(substream))
1380 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1381}
1382
Takashi Iwai877211f2005-11-17 13:59:38 +01001383static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Takashi Iwai877211f2005-11-17 13:59:38 +01001385 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001387 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001388 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Julia Lawallb17154c2015-11-29 16:36:40 +01001391static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 .pre_action = snd_pcm_pre_resume,
1393 .do_action = snd_pcm_do_resume,
1394 .undo_action = snd_pcm_undo_resume,
1395 .post_action = snd_pcm_post_resume
1396};
1397
Takashi Iwai877211f2005-11-17 13:59:38 +01001398static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Takashi Iwai877211f2005-11-17 13:59:38 +01001400 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 int res;
1402
1403 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001404 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1406 snd_power_unlock(card);
1407 return res;
1408}
1409
1410#else
1411
Takashi Iwai877211f2005-11-17 13:59:38 +01001412static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 return -ENOSYS;
1415}
1416
1417#endif /* CONFIG_PM */
1418
1419/*
1420 * xrun ioctl
1421 *
1422 * Change the RUNNING stream(s) to XRUN state.
1423 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001424static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Takashi Iwai877211f2005-11-17 13:59:38 +01001426 struct snd_card *card = substream->pcm->card;
1427 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int result;
1429
1430 snd_power_lock(card);
1431 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001432 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (result < 0)
1434 goto _unlock;
1435 }
1436
1437 snd_pcm_stream_lock_irq(substream);
1438 switch (runtime->status->state) {
1439 case SNDRV_PCM_STATE_XRUN:
1440 result = 0; /* already there */
1441 break;
1442 case SNDRV_PCM_STATE_RUNNING:
1443 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1444 break;
1445 default:
1446 result = -EBADFD;
1447 }
1448 snd_pcm_stream_unlock_irq(substream);
1449 _unlock:
1450 snd_power_unlock(card);
1451 return result;
1452}
1453
1454/*
1455 * reset ioctl
1456 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001457static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Takashi Iwai877211f2005-11-17 13:59:38 +01001459 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 switch (runtime->status->state) {
1461 case SNDRV_PCM_STATE_RUNNING:
1462 case SNDRV_PCM_STATE_PREPARED:
1463 case SNDRV_PCM_STATE_PAUSED:
1464 case SNDRV_PCM_STATE_SUSPENDED:
1465 return 0;
1466 default:
1467 return -EBADFD;
1468 }
1469}
1470
Takashi Iwai877211f2005-11-17 13:59:38 +01001471static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Takashi Iwai877211f2005-11-17 13:59:38 +01001473 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1475 if (err < 0)
1476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001478 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1479 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 runtime->silence_start = runtime->status->hw_ptr;
1481 runtime->silence_filled = 0;
1482 return 0;
1483}
1484
Takashi Iwai877211f2005-11-17 13:59:38 +01001485static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Takashi Iwai877211f2005-11-17 13:59:38 +01001487 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 runtime->control->appl_ptr = runtime->status->hw_ptr;
1489 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1490 runtime->silence_size > 0)
1491 snd_pcm_playback_silence(substream, ULONG_MAX);
1492}
1493
Julia Lawallb17154c2015-11-29 16:36:40 +01001494static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .pre_action = snd_pcm_pre_reset,
1496 .do_action = snd_pcm_do_reset,
1497 .post_action = snd_pcm_post_reset
1498};
1499
Takashi Iwai877211f2005-11-17 13:59:38 +01001500static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1503}
1504
1505/*
1506 * prepare ioctl
1507 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001508/* we use the second argument for updating f_flags */
1509static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1510 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Takashi Iwai877211f2005-11-17 13:59:38 +01001512 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001513 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1514 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EBADFD;
1516 if (snd_pcm_running(substream))
1517 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001518 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
1520}
1521
Takashi Iwai877211f2005-11-17 13:59:38 +01001522static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 int err;
1525 err = substream->ops->prepare(substream);
1526 if (err < 0)
1527 return err;
1528 return snd_pcm_do_reset(substream, 0);
1529}
1530
Takashi Iwai877211f2005-11-17 13:59:38 +01001531static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Takashi Iwai877211f2005-11-17 13:59:38 +01001533 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001535 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Julia Lawallb17154c2015-11-29 16:36:40 +01001538static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 .pre_action = snd_pcm_pre_prepare,
1540 .do_action = snd_pcm_do_prepare,
1541 .post_action = snd_pcm_post_prepare
1542};
1543
1544/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001545 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001546 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001547 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001548 *
1549 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001551static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1552 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001555 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001556 int f_flags;
1557
1558 if (file)
1559 f_flags = file->f_flags;
1560 else
1561 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001564 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001565 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1566 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 snd_power_unlock(card);
1568 return res;
1569}
1570
1571/*
1572 * drain ioctl
1573 */
1574
Takashi Iwai877211f2005-11-17 13:59:38 +01001575static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001577 struct snd_pcm_runtime *runtime = substream->runtime;
1578 switch (runtime->status->state) {
1579 case SNDRV_PCM_STATE_OPEN:
1580 case SNDRV_PCM_STATE_DISCONNECTED:
1581 case SNDRV_PCM_STATE_SUSPENDED:
1582 return -EBADFD;
1583 }
1584 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return 0;
1586}
1587
Takashi Iwai877211f2005-11-17 13:59:38 +01001588static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Takashi Iwai877211f2005-11-17 13:59:38 +01001590 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1592 switch (runtime->status->state) {
1593 case SNDRV_PCM_STATE_PREPARED:
1594 /* start playback stream if possible */
1595 if (! snd_pcm_playback_empty(substream)) {
1596 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1597 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001598 } else {
1599 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601 break;
1602 case SNDRV_PCM_STATE_RUNNING:
1603 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1604 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001605 case SNDRV_PCM_STATE_XRUN:
1606 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1607 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 default:
1609 break;
1610 }
1611 } else {
1612 /* stop running stream */
1613 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001614 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001616 snd_pcm_do_stop(substream, new_state);
1617 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619 }
Libin Yang48d88292014-12-31 22:09:54 +08001620
1621 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1622 runtime->trigger_master == substream &&
1623 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1624 return substream->ops->trigger(substream,
1625 SNDRV_PCM_TRIGGER_DRAIN);
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return 0;
1628}
1629
Takashi Iwai877211f2005-11-17 13:59:38 +01001630static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632}
1633
Julia Lawallb17154c2015-11-29 16:36:40 +01001634static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 .pre_action = snd_pcm_pre_drain_init,
1636 .do_action = snd_pcm_do_drain_init,
1637 .post_action = snd_pcm_post_drain_init
1638};
1639
Takashi Iwai877211f2005-11-17 13:59:38 +01001640static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642/*
1643 * Drain the stream(s).
1644 * When the substream is linked, sync until the draining of all playback streams
1645 * is finished.
1646 * After this call, all streams are supposed to be either SETUP or DRAINING
1647 * (capture only) state.
1648 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001649static int snd_pcm_drain(struct snd_pcm_substream *substream,
1650 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Takashi Iwai877211f2005-11-17 13:59:38 +01001652 struct snd_card *card;
1653 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001654 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001655 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001657 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 card = substream->pcm->card;
1660 runtime = substream->runtime;
1661
1662 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1663 return -EBADFD;
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 snd_power_lock(card);
1666 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001667 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001668 if (result < 0) {
1669 snd_power_unlock(card);
1670 return result;
1671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001674 if (file) {
1675 if (file->f_flags & O_NONBLOCK)
1676 nonblock = 1;
1677 } else if (substream->f_flags & O_NONBLOCK)
1678 nonblock = 1;
1679
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001680 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001681 snd_pcm_stream_lock_irq(substream);
1682 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001683 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001684 snd_pcm_pause(substream, 0);
1685
1686 /* pre-start/stop - all running streams are changed to DRAINING state */
1687 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001688 if (result < 0)
1689 goto unlock;
1690 /* in non-blocking, we don't wait in ioctl but let caller poll */
1691 if (nonblock) {
1692 result = -EAGAIN;
1693 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 for (;;) {
1697 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001698 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (signal_pending(current)) {
1700 result = -ERESTARTSYS;
1701 break;
1702 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001703 /* find a substream to drain */
1704 to_check = NULL;
1705 snd_pcm_group_for_each_entry(s, substream) {
1706 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1707 continue;
1708 runtime = s->runtime;
1709 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1710 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001711 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001712 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001713 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001714 if (!to_check)
1715 break; /* all drained */
1716 init_waitqueue_entry(&wait, current);
1717 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001719 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001721 if (runtime->no_period_wakeup)
1722 tout = MAX_SCHEDULE_TIMEOUT;
1723 else {
1724 tout = 10;
1725 if (runtime->rate) {
1726 long t = runtime->period_size * 2 / runtime->rate;
1727 tout = max(t, tout);
1728 }
1729 tout = msecs_to_jiffies(tout * 1000);
1730 }
1731 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001733 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001735 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001736 if (card->shutdown) {
1737 result = -ENODEV;
1738 break;
1739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (tout == 0) {
1741 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1742 result = -ESTRPIPE;
1743 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001744 dev_dbg(substream->pcm->card->dev,
1745 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1747 result = -EIO;
1748 }
1749 break;
1750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001752
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001753 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001754 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001755 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 return result;
1759}
1760
1761/*
1762 * drop ioctl
1763 *
1764 * Immediately put all linked substreams into SETUP state.
1765 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001766static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Takashi Iwai877211f2005-11-17 13:59:38 +01001768 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 int result = 0;
1770
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001771 if (PCM_RUNTIME_CHECK(substream))
1772 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001775 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001776 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1777 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return -EBADFD;
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 snd_pcm_stream_lock_irq(substream);
1781 /* resume pause */
1782 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1783 snd_pcm_pause(substream, 0);
1784
1785 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1786 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1787 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return result;
1790}
1791
1792
Al Viro0888c322013-06-05 14:09:55 -04001793static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Al Viro0888c322013-06-05 14:09:55 -04001795 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001796 unsigned int minor;
1797
Al Viro0888c322013-06-05 14:09:55 -04001798 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1799 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001801 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1802 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805/*
1806 * PCM link handling
1807 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001808static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001811 struct snd_pcm_file *pcm_file;
1812 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001813 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001814 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Al Viro0888c322013-06-05 14:09:55 -04001816 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001818 if (!is_pcm_file(f.file)) {
1819 res = -EBADFD;
1820 goto _badf;
1821 }
1822 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001824 group = kmalloc(sizeof(*group), GFP_KERNEL);
1825 if (!group) {
1826 res = -ENOMEM;
1827 goto _nolock;
1828 }
Takashi Iwai67ec10722016-02-17 14:30:26 +01001829 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 write_lock_irq(&snd_pcm_link_rwlock);
1831 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001832 substream->runtime->status->state != substream1->runtime->status->state ||
1833 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 res = -EBADFD;
1835 goto _end;
1836 }
1837 if (snd_pcm_stream_linked(substream1)) {
1838 res = -EALREADY;
1839 goto _end;
1840 }
1841 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001842 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001843 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001845 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 INIT_LIST_HEAD(&substream->group->substreams);
1847 list_add_tail(&substream->link_list, &substream->group->substreams);
1848 substream->group->count = 1;
1849 }
1850 list_add_tail(&substream1->link_list, &substream->group->substreams);
1851 substream->group->count++;
1852 substream1->group = substream->group;
1853 _end:
1854 write_unlock_irq(&snd_pcm_link_rwlock);
1855 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001856 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001857 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001858 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001859 _badf:
1860 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return res;
1862}
1863
Takashi Iwai877211f2005-11-17 13:59:38 +01001864static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 substream->group = &substream->self_group;
1867 INIT_LIST_HEAD(&substream->self_group.substreams);
1868 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1869}
1870
Takashi Iwai877211f2005-11-17 13:59:38 +01001871static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwaief991b92007-02-22 12:52:53 +01001873 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 int res = 0;
1875
Takashi Iwai67ec10722016-02-17 14:30:26 +01001876 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 write_lock_irq(&snd_pcm_link_rwlock);
1878 if (!snd_pcm_stream_linked(substream)) {
1879 res = -EALREADY;
1880 goto _end;
1881 }
1882 list_del(&substream->link_list);
1883 substream->group->count--;
1884 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001885 snd_pcm_group_for_each_entry(s, substream) {
1886 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 break;
1888 }
1889 kfree(substream->group);
1890 }
1891 relink_to_local(substream);
1892 _end:
1893 write_unlock_irq(&snd_pcm_link_rwlock);
1894 up_write(&snd_pcm_link_rwsem);
1895 return res;
1896}
1897
1898/*
1899 * hw configurator
1900 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001901static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1902 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Takashi Iwai877211f2005-11-17 13:59:38 +01001904 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1906 hw_param_interval_c(params, rule->deps[1]), &t);
1907 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1908}
1909
Takashi Iwai877211f2005-11-17 13:59:38 +01001910static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1911 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Takashi Iwai877211f2005-11-17 13:59:38 +01001913 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1915 hw_param_interval_c(params, rule->deps[1]), &t);
1916 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1917}
1918
Takashi Iwai877211f2005-11-17 13:59:38 +01001919static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1920 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Takashi Iwai877211f2005-11-17 13:59:38 +01001922 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1924 hw_param_interval_c(params, rule->deps[1]),
1925 (unsigned long) rule->private, &t);
1926 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1927}
1928
Takashi Iwai877211f2005-11-17 13:59:38 +01001929static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1930 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Takashi Iwai877211f2005-11-17 13:59:38 +01001932 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1934 (unsigned long) rule->private,
1935 hw_param_interval_c(params, rule->deps[1]), &t);
1936 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1937}
1938
Takashi Iwai877211f2005-11-17 13:59:38 +01001939static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1940 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001943 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1944 struct snd_mask m;
1945 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 snd_mask_any(&m);
1947 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1948 int bits;
1949 if (! snd_mask_test(mask, k))
1950 continue;
1951 bits = snd_pcm_format_physical_width(k);
1952 if (bits <= 0)
1953 continue; /* ignore invalid formats */
1954 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1955 snd_mask_reset(&m, k);
1956 }
1957 return snd_mask_refine(mask, &m);
1958}
1959
Takashi Iwai877211f2005-11-17 13:59:38 +01001960static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1961 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Takashi Iwai877211f2005-11-17 13:59:38 +01001963 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 unsigned int k;
1965 t.min = UINT_MAX;
1966 t.max = 0;
1967 t.openmin = 0;
1968 t.openmax = 0;
1969 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1970 int bits;
1971 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1972 continue;
1973 bits = snd_pcm_format_physical_width(k);
1974 if (bits <= 0)
1975 continue; /* ignore invalid formats */
1976 if (t.min > (unsigned)bits)
1977 t.min = bits;
1978 if (t.max < (unsigned)bits)
1979 t.max = bits;
1980 }
1981 t.integer = 1;
1982 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1983}
1984
1985#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1986#error "Change this table"
1987#endif
1988
1989static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1990 48000, 64000, 88200, 96000, 176400, 192000 };
1991
Clemens Ladisch7653d552007-08-13 17:38:54 +02001992const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1993 .count = ARRAY_SIZE(rates),
1994 .list = rates,
1995};
1996
Takashi Iwai877211f2005-11-17 13:59:38 +01001997static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1998 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Takashi Iwai877211f2005-11-17 13:59:38 +01002000 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002002 snd_pcm_known_rates.count,
2003 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Takashi Iwai877211f2005-11-17 13:59:38 +01002006static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2007 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Takashi Iwai877211f2005-11-17 13:59:38 +01002009 struct snd_interval t;
2010 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 t.min = 0;
2012 t.max = substream->buffer_bytes_max;
2013 t.openmin = 0;
2014 t.openmax = 0;
2015 t.integer = 1;
2016 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2017}
2018
Takashi Iwai877211f2005-11-17 13:59:38 +01002019int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Takashi Iwai877211f2005-11-17 13:59:38 +01002021 struct snd_pcm_runtime *runtime = substream->runtime;
2022 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 int k, err;
2024
2025 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2026 snd_mask_any(constrs_mask(constrs, k));
2027 }
2028
2029 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2030 snd_interval_any(constrs_interval(constrs, k));
2031 }
2032
2033 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2034 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2035 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2036 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2037 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2038
2039 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2040 snd_pcm_hw_rule_format, NULL,
2041 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2042 if (err < 0)
2043 return err;
2044 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2045 snd_pcm_hw_rule_sample_bits, NULL,
2046 SNDRV_PCM_HW_PARAM_FORMAT,
2047 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2048 if (err < 0)
2049 return err;
2050 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2051 snd_pcm_hw_rule_div, NULL,
2052 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2053 if (err < 0)
2054 return err;
2055 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2056 snd_pcm_hw_rule_mul, NULL,
2057 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2058 if (err < 0)
2059 return err;
2060 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2061 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2062 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2063 if (err < 0)
2064 return err;
2065 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2066 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2067 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2068 if (err < 0)
2069 return err;
2070 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2071 snd_pcm_hw_rule_div, NULL,
2072 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2073 if (err < 0)
2074 return err;
2075 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2076 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2077 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2078 if (err < 0)
2079 return err;
2080 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2081 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2082 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2083 if (err < 0)
2084 return err;
2085 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2086 snd_pcm_hw_rule_div, NULL,
2087 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2088 if (err < 0)
2089 return err;
2090 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2091 snd_pcm_hw_rule_div, NULL,
2092 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2093 if (err < 0)
2094 return err;
2095 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2096 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2097 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2098 if (err < 0)
2099 return err;
2100 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2101 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2102 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2103 if (err < 0)
2104 return err;
2105 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2106 snd_pcm_hw_rule_mul, NULL,
2107 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2108 if (err < 0)
2109 return err;
2110 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2111 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2112 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2113 if (err < 0)
2114 return err;
2115 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2116 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2117 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2118 if (err < 0)
2119 return err;
2120 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2121 snd_pcm_hw_rule_muldivk, (void*) 8,
2122 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2123 if (err < 0)
2124 return err;
2125 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2126 snd_pcm_hw_rule_muldivk, (void*) 8,
2127 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2128 if (err < 0)
2129 return err;
2130 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2131 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2132 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2133 if (err < 0)
2134 return err;
2135 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2136 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2137 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2138 if (err < 0)
2139 return err;
2140 return 0;
2141}
2142
Takashi Iwai877211f2005-11-17 13:59:38 +01002143int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Takashi Iwai877211f2005-11-17 13:59:38 +01002145 struct snd_pcm_runtime *runtime = substream->runtime;
2146 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int err;
2148 unsigned int mask = 0;
2149
2150 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2151 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2152 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2153 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002154 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2156 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2157 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2158 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2159 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2160 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2161 }
2162 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002163 if (err < 0)
2164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002167 if (err < 0)
2168 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002171 if (err < 0)
2172 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2175 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002176 if (err < 0)
2177 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2180 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002181 if (err < 0)
2182 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2185 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002186 if (err < 0)
2187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2190 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002191 if (err < 0)
2192 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2195 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002196 if (err < 0)
2197 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2200 snd_pcm_hw_rule_buffer_bytes_max, substream,
2201 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2202 if (err < 0)
2203 return err;
2204
2205 /* FIXME: remove */
2206 if (runtime->dma_bytes) {
2207 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002208 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211
2212 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2213 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2214 snd_pcm_hw_rule_rate, hw,
2215 SNDRV_PCM_HW_PARAM_RATE, -1);
2216 if (err < 0)
2217 return err;
2218 }
2219
2220 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2222
2223 return 0;
2224}
2225
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002226static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002229}
2230
2231void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2232{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002233 substream->ref_count--;
2234 if (substream->ref_count > 0)
2235 return;
2236
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002237 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002238 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002239 if (substream->ops->hw_free &&
2240 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 substream->ops->hw_free(substream);
2242 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002243 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002245 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2246 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002247 if (substream->pcm_release) {
2248 substream->pcm_release(substream);
2249 substream->pcm_release = NULL;
2250 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002251 snd_pcm_detach_substream(substream);
2252}
2253
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002254EXPORT_SYMBOL(snd_pcm_release_substream);
2255
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002256int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2257 struct file *file,
2258 struct snd_pcm_substream **rsubstream)
2259{
2260 struct snd_pcm_substream *substream;
2261 int err;
2262
2263 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2264 if (err < 0)
2265 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002266 if (substream->ref_count > 1) {
2267 *rsubstream = substream;
2268 return 0;
2269 }
2270
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002271 err = snd_pcm_hw_constraints_init(substream);
2272 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002273 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002274 goto error;
2275 }
2276
2277 if ((err = substream->ops->open(substream)) < 0)
2278 goto error;
2279
2280 substream->hw_opened = 1;
2281
2282 err = snd_pcm_hw_constraints_complete(substream);
2283 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002284 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002285 goto error;
2286 }
2287
2288 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002290
2291 error:
2292 snd_pcm_release_substream(substream);
2293 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002296EXPORT_SYMBOL(snd_pcm_open_substream);
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002299 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002300 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Takashi Iwai877211f2005-11-17 13:59:38 +01002302 struct snd_pcm_file *pcm_file;
2303 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002304 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002306 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2307 if (err < 0)
2308 return err;
2309
Takashi Iwai548a6482006-07-31 16:51:51 +02002310 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2311 if (pcm_file == NULL) {
2312 snd_pcm_release_substream(substream);
2313 return -ENOMEM;
2314 }
2315 pcm_file->substream = substream;
2316 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002317 substream->file = pcm_file;
2318 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return 0;
2323}
2324
Clemens Ladischf87135f2005-11-20 14:06:59 +01002325static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Takashi Iwai877211f2005-11-17 13:59:38 +01002327 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002328 int err = nonseekable_open(inode, file);
2329 if (err < 0)
2330 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002331 pcm = snd_lookup_minor_data(iminor(inode),
2332 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002333 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002334 if (pcm)
2335 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002336 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002337}
2338
2339static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2340{
2341 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002342 int err = nonseekable_open(inode, file);
2343 if (err < 0)
2344 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002345 pcm = snd_lookup_minor_data(iminor(inode),
2346 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002347 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002348 if (pcm)
2349 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002350 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002351}
2352
2353static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2354{
2355 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 wait_queue_t wait;
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (pcm == NULL) {
2359 err = -ENODEV;
2360 goto __error1;
2361 }
2362 err = snd_card_file_add(pcm->card, file);
2363 if (err < 0)
2364 goto __error1;
2365 if (!try_module_get(pcm->card->module)) {
2366 err = -EFAULT;
2367 goto __error2;
2368 }
2369 init_waitqueue_entry(&wait, current);
2370 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002371 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002373 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (err >= 0)
2375 break;
2376 if (err == -EAGAIN) {
2377 if (file->f_flags & O_NONBLOCK) {
2378 err = -EBUSY;
2379 break;
2380 }
2381 } else
2382 break;
2383 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002384 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002386 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002387 if (pcm->card->shutdown) {
2388 err = -ENODEV;
2389 break;
2390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (signal_pending(current)) {
2392 err = -ERESTARTSYS;
2393 break;
2394 }
2395 }
2396 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002397 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (err < 0)
2399 goto __error;
2400 return err;
2401
2402 __error:
2403 module_put(pcm->card->module);
2404 __error2:
2405 snd_card_file_remove(pcm->card, file);
2406 __error1:
2407 return err;
2408}
2409
2410static int snd_pcm_release(struct inode *inode, struct file *file)
2411{
Takashi Iwai877211f2005-11-17 13:59:38 +01002412 struct snd_pcm *pcm;
2413 struct snd_pcm_substream *substream;
2414 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 pcm_file = file->private_data;
2417 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002418 if (snd_BUG_ON(!substream))
2419 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002421 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002422 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002423 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002424 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 wake_up(&pcm->open_wait);
2426 module_put(pcm->card->module);
2427 snd_card_file_remove(pcm->card, file);
2428 return 0;
2429}
2430
Takashi Iwai877211f2005-11-17 13:59:38 +01002431static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2432 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Takashi Iwai877211f2005-11-17 13:59:38 +01002434 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 snd_pcm_sframes_t appl_ptr;
2436 snd_pcm_sframes_t ret;
2437 snd_pcm_sframes_t hw_avail;
2438
2439 if (frames == 0)
2440 return 0;
2441
2442 snd_pcm_stream_lock_irq(substream);
2443 switch (runtime->status->state) {
2444 case SNDRV_PCM_STATE_PREPARED:
2445 break;
2446 case SNDRV_PCM_STATE_DRAINING:
2447 case SNDRV_PCM_STATE_RUNNING:
2448 if (snd_pcm_update_hw_ptr(substream) >= 0)
2449 break;
2450 /* Fall through */
2451 case SNDRV_PCM_STATE_XRUN:
2452 ret = -EPIPE;
2453 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002454 case SNDRV_PCM_STATE_SUSPENDED:
2455 ret = -ESTRPIPE;
2456 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 default:
2458 ret = -EBADFD;
2459 goto __end;
2460 }
2461
2462 hw_avail = snd_pcm_playback_hw_avail(runtime);
2463 if (hw_avail <= 0) {
2464 ret = 0;
2465 goto __end;
2466 }
2467 if (frames > (snd_pcm_uframes_t)hw_avail)
2468 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 appl_ptr = runtime->control->appl_ptr - frames;
2470 if (appl_ptr < 0)
2471 appl_ptr += runtime->boundary;
2472 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 ret = frames;
2474 __end:
2475 snd_pcm_stream_unlock_irq(substream);
2476 return ret;
2477}
2478
Takashi Iwai877211f2005-11-17 13:59:38 +01002479static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2480 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Takashi Iwai877211f2005-11-17 13:59:38 +01002482 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 snd_pcm_sframes_t appl_ptr;
2484 snd_pcm_sframes_t ret;
2485 snd_pcm_sframes_t hw_avail;
2486
2487 if (frames == 0)
2488 return 0;
2489
2490 snd_pcm_stream_lock_irq(substream);
2491 switch (runtime->status->state) {
2492 case SNDRV_PCM_STATE_PREPARED:
2493 case SNDRV_PCM_STATE_DRAINING:
2494 break;
2495 case SNDRV_PCM_STATE_RUNNING:
2496 if (snd_pcm_update_hw_ptr(substream) >= 0)
2497 break;
2498 /* Fall through */
2499 case SNDRV_PCM_STATE_XRUN:
2500 ret = -EPIPE;
2501 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002502 case SNDRV_PCM_STATE_SUSPENDED:
2503 ret = -ESTRPIPE;
2504 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 default:
2506 ret = -EBADFD;
2507 goto __end;
2508 }
2509
2510 hw_avail = snd_pcm_capture_hw_avail(runtime);
2511 if (hw_avail <= 0) {
2512 ret = 0;
2513 goto __end;
2514 }
2515 if (frames > (snd_pcm_uframes_t)hw_avail)
2516 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 appl_ptr = runtime->control->appl_ptr - frames;
2518 if (appl_ptr < 0)
2519 appl_ptr += runtime->boundary;
2520 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 ret = frames;
2522 __end:
2523 snd_pcm_stream_unlock_irq(substream);
2524 return ret;
2525}
2526
Takashi Iwai877211f2005-11-17 13:59:38 +01002527static snd_pcm_sframes_t snd_pcm_playback_forward(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 appl_ptr;
2532 snd_pcm_sframes_t ret;
2533 snd_pcm_sframes_t avail;
2534
2535 if (frames == 0)
2536 return 0;
2537
2538 snd_pcm_stream_lock_irq(substream);
2539 switch (runtime->status->state) {
2540 case SNDRV_PCM_STATE_PREPARED:
2541 case SNDRV_PCM_STATE_PAUSED:
2542 break;
2543 case SNDRV_PCM_STATE_DRAINING:
2544 case SNDRV_PCM_STATE_RUNNING:
2545 if (snd_pcm_update_hw_ptr(substream) >= 0)
2546 break;
2547 /* Fall through */
2548 case SNDRV_PCM_STATE_XRUN:
2549 ret = -EPIPE;
2550 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002551 case SNDRV_PCM_STATE_SUSPENDED:
2552 ret = -ESTRPIPE;
2553 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 default:
2555 ret = -EBADFD;
2556 goto __end;
2557 }
2558
2559 avail = snd_pcm_playback_avail(runtime);
2560 if (avail <= 0) {
2561 ret = 0;
2562 goto __end;
2563 }
2564 if (frames > (snd_pcm_uframes_t)avail)
2565 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 appl_ptr = runtime->control->appl_ptr + frames;
2567 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2568 appl_ptr -= runtime->boundary;
2569 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 ret = frames;
2571 __end:
2572 snd_pcm_stream_unlock_irq(substream);
2573 return ret;
2574}
2575
Takashi Iwai877211f2005-11-17 13:59:38 +01002576static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2577 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Takashi Iwai877211f2005-11-17 13:59:38 +01002579 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 snd_pcm_sframes_t appl_ptr;
2581 snd_pcm_sframes_t ret;
2582 snd_pcm_sframes_t avail;
2583
2584 if (frames == 0)
2585 return 0;
2586
2587 snd_pcm_stream_lock_irq(substream);
2588 switch (runtime->status->state) {
2589 case SNDRV_PCM_STATE_PREPARED:
2590 case SNDRV_PCM_STATE_DRAINING:
2591 case SNDRV_PCM_STATE_PAUSED:
2592 break;
2593 case SNDRV_PCM_STATE_RUNNING:
2594 if (snd_pcm_update_hw_ptr(substream) >= 0)
2595 break;
2596 /* Fall through */
2597 case SNDRV_PCM_STATE_XRUN:
2598 ret = -EPIPE;
2599 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002600 case SNDRV_PCM_STATE_SUSPENDED:
2601 ret = -ESTRPIPE;
2602 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 default:
2604 ret = -EBADFD;
2605 goto __end;
2606 }
2607
2608 avail = snd_pcm_capture_avail(runtime);
2609 if (avail <= 0) {
2610 ret = 0;
2611 goto __end;
2612 }
2613 if (frames > (snd_pcm_uframes_t)avail)
2614 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 appl_ptr = runtime->control->appl_ptr + frames;
2616 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2617 appl_ptr -= runtime->boundary;
2618 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 ret = frames;
2620 __end:
2621 snd_pcm_stream_unlock_irq(substream);
2622 return ret;
2623}
2624
Takashi Iwai877211f2005-11-17 13:59:38 +01002625static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Takashi Iwai877211f2005-11-17 13:59:38 +01002627 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 int err;
2629
2630 snd_pcm_stream_lock_irq(substream);
2631 switch (runtime->status->state) {
2632 case SNDRV_PCM_STATE_DRAINING:
2633 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2634 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002635 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 case SNDRV_PCM_STATE_RUNNING:
2637 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2638 break;
2639 /* Fall through */
2640 case SNDRV_PCM_STATE_PREPARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 err = 0;
2642 break;
Jeeja KPf3f6c612016-09-02 21:49:44 +05302643 case SNDRV_PCM_STATE_SUSPENDED:
2644 err = -ESTRPIPE;
2645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 case SNDRV_PCM_STATE_XRUN:
2647 err = -EPIPE;
2648 break;
2649 default:
2650 __badfd:
2651 err = -EBADFD;
2652 break;
2653 }
2654 snd_pcm_stream_unlock_irq(substream);
2655 return err;
2656}
2657
Takashi Iwai877211f2005-11-17 13:59:38 +01002658static int snd_pcm_delay(struct snd_pcm_substream *substream,
2659 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Takashi Iwai877211f2005-11-17 13:59:38 +01002661 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 int err;
2663 snd_pcm_sframes_t n = 0;
2664
2665 snd_pcm_stream_lock_irq(substream);
2666 switch (runtime->status->state) {
2667 case SNDRV_PCM_STATE_DRAINING:
2668 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2669 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002670 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 case SNDRV_PCM_STATE_RUNNING:
2672 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2673 break;
2674 /* Fall through */
2675 case SNDRV_PCM_STATE_PREPARED:
2676 case SNDRV_PCM_STATE_SUSPENDED:
2677 err = 0;
2678 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2679 n = snd_pcm_playback_hw_avail(runtime);
2680 else
2681 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002682 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 break;
2684 case SNDRV_PCM_STATE_XRUN:
2685 err = -EPIPE;
2686 break;
2687 default:
2688 __badfd:
2689 err = -EBADFD;
2690 break;
2691 }
2692 snd_pcm_stream_unlock_irq(substream);
2693 if (!err)
2694 if (put_user(n, res))
2695 err = -EFAULT;
2696 return err;
2697}
2698
Takashi Iwai877211f2005-11-17 13:59:38 +01002699static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2700 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Takashi Iwai877211f2005-11-17 13:59:38 +01002702 struct snd_pcm_runtime *runtime = substream->runtime;
2703 struct snd_pcm_sync_ptr sync_ptr;
2704 volatile struct snd_pcm_mmap_status *status;
2705 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 int err;
2707
2708 memset(&sync_ptr, 0, sizeof(sync_ptr));
2709 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2710 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002711 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 -07002712 return -EFAULT;
2713 status = runtime->status;
2714 control = runtime->control;
2715 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2716 err = snd_pcm_hwsync(substream);
2717 if (err < 0)
2718 return err;
2719 }
2720 snd_pcm_stream_lock_irq(substream);
2721 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2722 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2723 else
2724 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2725 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2726 control->avail_min = sync_ptr.c.control.avail_min;
2727 else
2728 sync_ptr.c.control.avail_min = control->avail_min;
2729 sync_ptr.s.status.state = status->state;
2730 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2731 sync_ptr.s.status.tstamp = status->tstamp;
2732 sync_ptr.s.status.suspended_state = status->suspended_state;
2733 snd_pcm_stream_unlock_irq(substream);
2734 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2735 return -EFAULT;
2736 return 0;
2737}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002738
2739static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2740{
2741 struct snd_pcm_runtime *runtime = substream->runtime;
2742 int arg;
2743
2744 if (get_user(arg, _arg))
2745 return -EFAULT;
2746 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2747 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002748 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002749 return 0;
2750}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Takashi Iwai0df63e42006-04-28 15:13:41 +02002752static int snd_pcm_common_ioctl1(struct file *file,
2753 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 unsigned int cmd, void __user *arg)
2755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 switch (cmd) {
2757 case SNDRV_PCM_IOCTL_PVERSION:
2758 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2759 case SNDRV_PCM_IOCTL_INFO:
2760 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002761 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2762 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002763 case SNDRV_PCM_IOCTL_TTSTAMP:
2764 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 case SNDRV_PCM_IOCTL_HW_REFINE:
2766 return snd_pcm_hw_refine_user(substream, arg);
2767 case SNDRV_PCM_IOCTL_HW_PARAMS:
2768 return snd_pcm_hw_params_user(substream, arg);
2769 case SNDRV_PCM_IOCTL_HW_FREE:
2770 return snd_pcm_hw_free(substream);
2771 case SNDRV_PCM_IOCTL_SW_PARAMS:
2772 return snd_pcm_sw_params_user(substream, arg);
2773 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002774 return snd_pcm_status_user(substream, arg, false);
2775 case SNDRV_PCM_IOCTL_STATUS_EXT:
2776 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2778 return snd_pcm_channel_info_user(substream, arg);
2779 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002780 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 case SNDRV_PCM_IOCTL_RESET:
2782 return snd_pcm_reset(substream);
2783 case SNDRV_PCM_IOCTL_START:
2784 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2785 case SNDRV_PCM_IOCTL_LINK:
2786 return snd_pcm_link(substream, (int)(unsigned long) arg);
2787 case SNDRV_PCM_IOCTL_UNLINK:
2788 return snd_pcm_unlink(substream);
2789 case SNDRV_PCM_IOCTL_RESUME:
2790 return snd_pcm_resume(substream);
2791 case SNDRV_PCM_IOCTL_XRUN:
2792 return snd_pcm_xrun(substream);
2793 case SNDRV_PCM_IOCTL_HWSYNC:
2794 return snd_pcm_hwsync(substream);
2795 case SNDRV_PCM_IOCTL_DELAY:
2796 return snd_pcm_delay(substream, arg);
2797 case SNDRV_PCM_IOCTL_SYNC_PTR:
2798 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002799#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2801 return snd_pcm_hw_refine_old_user(substream, arg);
2802 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2803 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002804#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002806 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 case SNDRV_PCM_IOCTL_DROP:
2808 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002809 case SNDRV_PCM_IOCTL_PAUSE:
2810 {
2811 int res;
2812 snd_pcm_stream_lock_irq(substream);
2813 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2814 snd_pcm_stream_unlock_irq(substream);
2815 return res;
2816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002818 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return -ENOTTY;
2820}
2821
Takashi Iwai0df63e42006-04-28 15:13:41 +02002822static int snd_pcm_playback_ioctl1(struct file *file,
2823 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 unsigned int cmd, void __user *arg)
2825{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002826 if (snd_BUG_ON(!substream))
2827 return -ENXIO;
2828 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2829 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 switch (cmd) {
2831 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2832 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002833 struct snd_xferi xferi;
2834 struct snd_xferi __user *_xferi = arg;
2835 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 snd_pcm_sframes_t result;
2837 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2838 return -EBADFD;
2839 if (put_user(0, &_xferi->result))
2840 return -EFAULT;
2841 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2842 return -EFAULT;
2843 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2844 __put_user(result, &_xferi->result);
2845 return result < 0 ? result : 0;
2846 }
2847 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2848 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002849 struct snd_xfern xfern;
2850 struct snd_xfern __user *_xfern = arg;
2851 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 void __user **bufs;
2853 snd_pcm_sframes_t result;
2854 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2855 return -EBADFD;
2856 if (runtime->channels > 128)
2857 return -EINVAL;
2858 if (put_user(0, &_xfern->result))
2859 return -EFAULT;
2860 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2861 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002862
2863 bufs = memdup_user(xfern.bufs,
2864 sizeof(void *) * runtime->channels);
2865 if (IS_ERR(bufs))
2866 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2868 kfree(bufs);
2869 __put_user(result, &_xfern->result);
2870 return result < 0 ? result : 0;
2871 }
2872 case SNDRV_PCM_IOCTL_REWIND:
2873 {
2874 snd_pcm_uframes_t frames;
2875 snd_pcm_uframes_t __user *_frames = arg;
2876 snd_pcm_sframes_t result;
2877 if (get_user(frames, _frames))
2878 return -EFAULT;
2879 if (put_user(0, _frames))
2880 return -EFAULT;
2881 result = snd_pcm_playback_rewind(substream, frames);
2882 __put_user(result, _frames);
2883 return result < 0 ? result : 0;
2884 }
2885 case SNDRV_PCM_IOCTL_FORWARD:
2886 {
2887 snd_pcm_uframes_t frames;
2888 snd_pcm_uframes_t __user *_frames = arg;
2889 snd_pcm_sframes_t result;
2890 if (get_user(frames, _frames))
2891 return -EFAULT;
2892 if (put_user(0, _frames))
2893 return -EFAULT;
2894 result = snd_pcm_playback_forward(substream, frames);
2895 __put_user(result, _frames);
2896 return result < 0 ? result : 0;
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002899 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
Takashi Iwai0df63e42006-04-28 15:13:41 +02002902static int snd_pcm_capture_ioctl1(struct file *file,
2903 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 unsigned int cmd, void __user *arg)
2905{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002906 if (snd_BUG_ON(!substream))
2907 return -ENXIO;
2908 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2909 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 switch (cmd) {
2911 case SNDRV_PCM_IOCTL_READI_FRAMES:
2912 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002913 struct snd_xferi xferi;
2914 struct snd_xferi __user *_xferi = arg;
2915 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 snd_pcm_sframes_t result;
2917 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2918 return -EBADFD;
2919 if (put_user(0, &_xferi->result))
2920 return -EFAULT;
2921 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2922 return -EFAULT;
2923 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2924 __put_user(result, &_xferi->result);
2925 return result < 0 ? result : 0;
2926 }
2927 case SNDRV_PCM_IOCTL_READN_FRAMES:
2928 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002929 struct snd_xfern xfern;
2930 struct snd_xfern __user *_xfern = arg;
2931 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 void *bufs;
2933 snd_pcm_sframes_t result;
2934 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2935 return -EBADFD;
2936 if (runtime->channels > 128)
2937 return -EINVAL;
2938 if (put_user(0, &_xfern->result))
2939 return -EFAULT;
2940 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2941 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002942
2943 bufs = memdup_user(xfern.bufs,
2944 sizeof(void *) * runtime->channels);
2945 if (IS_ERR(bufs))
2946 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2948 kfree(bufs);
2949 __put_user(result, &_xfern->result);
2950 return result < 0 ? result : 0;
2951 }
2952 case SNDRV_PCM_IOCTL_REWIND:
2953 {
2954 snd_pcm_uframes_t frames;
2955 snd_pcm_uframes_t __user *_frames = arg;
2956 snd_pcm_sframes_t result;
2957 if (get_user(frames, _frames))
2958 return -EFAULT;
2959 if (put_user(0, _frames))
2960 return -EFAULT;
2961 result = snd_pcm_capture_rewind(substream, frames);
2962 __put_user(result, _frames);
2963 return result < 0 ? result : 0;
2964 }
2965 case SNDRV_PCM_IOCTL_FORWARD:
2966 {
2967 snd_pcm_uframes_t frames;
2968 snd_pcm_uframes_t __user *_frames = arg;
2969 snd_pcm_sframes_t result;
2970 if (get_user(frames, _frames))
2971 return -EFAULT;
2972 if (put_user(0, _frames))
2973 return -EFAULT;
2974 result = snd_pcm_capture_forward(substream, frames);
2975 __put_user(result, _frames);
2976 return result < 0 ? result : 0;
2977 }
2978 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002979 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
Takashi Iwai877211f2005-11-17 13:59:38 +01002982static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2983 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
Takashi Iwai877211f2005-11-17 13:59:38 +01002985 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 pcm_file = file->private_data;
2988
2989 if (((cmd >> 8) & 0xff) != 'A')
2990 return -ENOTTY;
2991
Takashi Iwai0df63e42006-04-28 15:13:41 +02002992 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2993 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994}
2995
Takashi Iwai877211f2005-11-17 13:59:38 +01002996static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2997 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Takashi Iwai877211f2005-11-17 13:59:38 +01002999 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
3001 pcm_file = file->private_data;
3002
3003 if (((cmd >> 8) & 0xff) != 'A')
3004 return -ENOTTY;
3005
Takashi Iwai0df63e42006-04-28 15:13:41 +02003006 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3007 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Takashi Iwai877211f2005-11-17 13:59:38 +01003010int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 unsigned int cmd, void *arg)
3012{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003013 mm_segment_t fs;
3014 int result;
3015
3016 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 switch (substream->stream) {
3018 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003019 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3020 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003023 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3024 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003027 result = -EINVAL;
3028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003030 snd_leave_user(fs);
3031 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003034EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3035
Takashi Iwai877211f2005-11-17 13:59:38 +01003036static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3037 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Takashi Iwai877211f2005-11-17 13:59:38 +01003039 struct snd_pcm_file *pcm_file;
3040 struct snd_pcm_substream *substream;
3041 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 snd_pcm_sframes_t result;
3043
3044 pcm_file = file->private_data;
3045 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003046 if (PCM_RUNTIME_CHECK(substream))
3047 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 runtime = substream->runtime;
3049 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3050 return -EBADFD;
3051 if (!frame_aligned(runtime, count))
3052 return -EINVAL;
3053 count = bytes_to_frames(runtime, count);
3054 result = snd_pcm_lib_read(substream, buf, count);
3055 if (result > 0)
3056 result = frames_to_bytes(runtime, result);
3057 return result;
3058}
3059
Takashi Iwai877211f2005-11-17 13:59:38 +01003060static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3061 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Takashi Iwai877211f2005-11-17 13:59:38 +01003063 struct snd_pcm_file *pcm_file;
3064 struct snd_pcm_substream *substream;
3065 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 snd_pcm_sframes_t result;
3067
3068 pcm_file = file->private_data;
3069 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003070 if (PCM_RUNTIME_CHECK(substream))
3071 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003073 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3074 return -EBADFD;
3075 if (!frame_aligned(runtime, count))
3076 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 count = bytes_to_frames(runtime, count);
3078 result = snd_pcm_lib_write(substream, buf, count);
3079 if (result > 0)
3080 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return result;
3082}
3083
Al Viro1c65d982015-04-04 00:19:32 -04003084static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Takashi Iwai877211f2005-11-17 13:59:38 +01003086 struct snd_pcm_file *pcm_file;
3087 struct snd_pcm_substream *substream;
3088 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 snd_pcm_sframes_t result;
3090 unsigned long i;
3091 void __user **bufs;
3092 snd_pcm_uframes_t frames;
3093
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003094 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003096 if (PCM_RUNTIME_CHECK(substream))
3097 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 runtime = substream->runtime;
3099 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3100 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003101 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003103 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003105 if (!frame_aligned(runtime, to->iov->iov_len))
3106 return -EINVAL;
3107 frames = bytes_to_samples(runtime, to->iov->iov_len);
3108 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 if (bufs == NULL)
3110 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003111 for (i = 0; i < to->nr_segs; ++i)
3112 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 result = snd_pcm_lib_readv(substream, bufs, frames);
3114 if (result > 0)
3115 result = frames_to_bytes(runtime, result);
3116 kfree(bufs);
3117 return result;
3118}
3119
Al Viro1c65d982015-04-04 00:19:32 -04003120static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
Takashi Iwai877211f2005-11-17 13:59:38 +01003122 struct snd_pcm_file *pcm_file;
3123 struct snd_pcm_substream *substream;
3124 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 snd_pcm_sframes_t result;
3126 unsigned long i;
3127 void __user **bufs;
3128 snd_pcm_uframes_t frames;
3129
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003130 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003132 if (PCM_RUNTIME_CHECK(substream))
3133 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003135 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3136 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003137 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003138 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003139 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3140 !frame_aligned(runtime, from->iov->iov_len))
3141 return -EINVAL;
3142 frames = bytes_to_samples(runtime, from->iov->iov_len);
3143 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (bufs == NULL)
3145 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003146 for (i = 0; i < from->nr_segs; ++i)
3147 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 result = snd_pcm_lib_writev(substream, bufs, frames);
3149 if (result > 0)
3150 result = frames_to_bytes(runtime, result);
3151 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return result;
3153}
3154
3155static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3156{
Takashi Iwai877211f2005-11-17 13:59:38 +01003157 struct snd_pcm_file *pcm_file;
3158 struct snd_pcm_substream *substream;
3159 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 unsigned int mask;
3161 snd_pcm_uframes_t avail;
3162
3163 pcm_file = file->private_data;
3164
3165 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003166 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003167 return POLLOUT | POLLWRNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 runtime = substream->runtime;
3169
3170 poll_wait(file, &runtime->sleep, wait);
3171
3172 snd_pcm_stream_lock_irq(substream);
3173 avail = snd_pcm_playback_avail(runtime);
3174 switch (runtime->status->state) {
3175 case SNDRV_PCM_STATE_RUNNING:
3176 case SNDRV_PCM_STATE_PREPARED:
3177 case SNDRV_PCM_STATE_PAUSED:
3178 if (avail >= runtime->control->avail_min) {
3179 mask = POLLOUT | POLLWRNORM;
3180 break;
3181 }
3182 /* Fall through */
3183 case SNDRV_PCM_STATE_DRAINING:
3184 mask = 0;
3185 break;
3186 default:
3187 mask = POLLOUT | POLLWRNORM | POLLERR;
3188 break;
3189 }
3190 snd_pcm_stream_unlock_irq(substream);
3191 return mask;
3192}
3193
3194static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3195{
Takashi Iwai877211f2005-11-17 13:59:38 +01003196 struct snd_pcm_file *pcm_file;
3197 struct snd_pcm_substream *substream;
3198 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 unsigned int mask;
3200 snd_pcm_uframes_t avail;
3201
3202 pcm_file = file->private_data;
3203
3204 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003205 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003206 return POLLIN | POLLRDNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 runtime = substream->runtime;
3208
3209 poll_wait(file, &runtime->sleep, wait);
3210
3211 snd_pcm_stream_lock_irq(substream);
3212 avail = snd_pcm_capture_avail(runtime);
3213 switch (runtime->status->state) {
3214 case SNDRV_PCM_STATE_RUNNING:
3215 case SNDRV_PCM_STATE_PREPARED:
3216 case SNDRV_PCM_STATE_PAUSED:
3217 if (avail >= runtime->control->avail_min) {
3218 mask = POLLIN | POLLRDNORM;
3219 break;
3220 }
3221 mask = 0;
3222 break;
3223 case SNDRV_PCM_STATE_DRAINING:
3224 if (avail > 0) {
3225 mask = POLLIN | POLLRDNORM;
3226 break;
3227 }
3228 /* Fall through */
3229 default:
3230 mask = POLLIN | POLLRDNORM | POLLERR;
3231 break;
3232 }
3233 snd_pcm_stream_unlock_irq(substream);
3234 return mask;
3235}
3236
3237/*
3238 * mmap support
3239 */
3240
3241/*
3242 * Only on coherent architectures, we can mmap the status and the control records
3243 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3244 */
3245#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3246/*
3247 * mmap status record
3248 */
Dave Jiang11bac802017-02-24 14:56:41 -08003249static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Dave Jiang11bac802017-02-24 14:56:41 -08003251 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003252 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003255 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003257 vmf->page = virt_to_page(runtime->status);
3258 get_page(vmf->page);
3259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003262static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003264 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265};
3266
Takashi Iwai877211f2005-11-17 13:59:38 +01003267static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct vm_area_struct *area)
3269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 long size;
3271 if (!(area->vm_flags & VM_READ))
3272 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003274 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return -EINVAL;
3276 area->vm_ops = &snd_pcm_vm_ops_status;
3277 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003278 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return 0;
3280}
3281
3282/*
3283 * mmap control record
3284 */
Dave Jiang11bac802017-02-24 14:56:41 -08003285static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
Dave Jiang11bac802017-02-24 14:56:41 -08003287 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003288 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003291 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003293 vmf->page = virt_to_page(runtime->control);
3294 get_page(vmf->page);
3295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003298static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003300 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301};
3302
Takashi Iwai877211f2005-11-17 13:59:38 +01003303static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 struct vm_area_struct *area)
3305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 long size;
3307 if (!(area->vm_flags & VM_READ))
3308 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003310 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 return -EINVAL;
3312 area->vm_ops = &snd_pcm_vm_ops_control;
3313 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003314 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return 0;
3316}
3317#else /* ! coherent mmap */
3318/*
3319 * don't support mmap for status and control records.
3320 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003321static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 struct vm_area_struct *area)
3323{
3324 return -ENXIO;
3325}
Takashi Iwai877211f2005-11-17 13:59:38 +01003326static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 struct vm_area_struct *area)
3328{
3329 return -ENXIO;
3330}
3331#endif /* coherent mmap */
3332
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003333static inline struct page *
3334snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3335{
3336 void *vaddr = substream->runtime->dma_area + ofs;
3337 return virt_to_page(vaddr);
3338}
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003341 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 */
Dave Jiang11bac802017-02-24 14:56:41 -08003343static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
Dave Jiang11bac802017-02-24 14:56:41 -08003345 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
Takashi Iwai877211f2005-11-17 13:59:38 +01003346 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 unsigned long offset;
3348 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 size_t dma_bytes;
3350
3351 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003352 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003354 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3356 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003357 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003358 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003360 else
3361 page = snd_pcm_default_page_ops(substream, offset);
3362 if (!page)
3363 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003364 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003365 vmf->page = page;
3366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
Takashi Iwai657b1982009-11-26 12:40:21 +01003369static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3370 .open = snd_pcm_mmap_data_open,
3371 .close = snd_pcm_mmap_data_close,
3372};
3373
3374static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 .open = snd_pcm_mmap_data_open,
3376 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003377 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378};
3379
3380/*
3381 * mmap the DMA buffer on RAM
3382 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003383
3384/**
3385 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3386 * @substream: PCM substream
3387 * @area: VMA
3388 *
3389 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3390 * this function is invoked implicitly.
3391 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003392int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3393 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003395 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003396#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003397 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3398 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3399 return remap_pfn_range(area, area->vm_start,
3400 substream->dma_buffer.addr >> PAGE_SHIFT,
3401 area->vm_end - area->vm_start, area->vm_page_prot);
3402 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003403#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003404#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003405 if (!substream->ops->page &&
3406 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3407 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3408 area,
3409 substream->runtime->dma_area,
3410 substream->runtime->dma_addr,
3411 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003412#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003413 /* mmap with fault handler */
3414 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return 0;
3416}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003417EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
3419/*
3420 * mmap the DMA buffer on I/O memory area
3421 */
3422#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003423/**
3424 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3425 * @substream: PCM substream
3426 * @area: VMA
3427 *
3428 * When your hardware uses the iomapped pages as the hardware buffer and
3429 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3430 * is supposed to work only on limited architectures.
3431 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003432int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3433 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003435 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003438 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003440
3441EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442#endif /* SNDRV_PCM_INFO_MMAP */
3443
3444/*
3445 * mmap DMA buffer
3446 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003447int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 struct vm_area_struct *area)
3449{
Takashi Iwai877211f2005-11-17 13:59:38 +01003450 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 long size;
3452 unsigned long offset;
3453 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003454 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
3456 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3457 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3458 return -EINVAL;
3459 } else {
3460 if (!(area->vm_flags & VM_READ))
3461 return -EINVAL;
3462 }
3463 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3465 return -EBADFD;
3466 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3467 return -ENXIO;
3468 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3469 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3470 return -EINVAL;
3471 size = area->vm_end - area->vm_start;
3472 offset = area->vm_pgoff << PAGE_SHIFT;
3473 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3474 if ((size_t)size > dma_bytes)
3475 return -EINVAL;
3476 if (offset > dma_bytes - size)
3477 return -EINVAL;
3478
Takashi Iwai657b1982009-11-26 12:40:21 +01003479 area->vm_ops = &snd_pcm_vm_ops_data;
3480 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003482 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003484 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003485 if (!err)
3486 atomic_inc(&substream->mmap_count);
3487 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003490EXPORT_SYMBOL(snd_pcm_mmap_data);
3491
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3493{
Takashi Iwai877211f2005-11-17 13:59:38 +01003494 struct snd_pcm_file * pcm_file;
3495 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 unsigned long offset;
3497
3498 pcm_file = file->private_data;
3499 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003500 if (PCM_RUNTIME_CHECK(substream))
3501 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 offset = area->vm_pgoff << PAGE_SHIFT;
3504 switch (offset) {
3505 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003506 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return -ENXIO;
3508 return snd_pcm_mmap_status(substream, file, area);
3509 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003510 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 return -ENXIO;
3512 return snd_pcm_mmap_control(substream, file, area);
3513 default:
3514 return snd_pcm_mmap_data(substream, file, area);
3515 }
3516 return 0;
3517}
3518
3519static int snd_pcm_fasync(int fd, struct file * file, int on)
3520{
Takashi Iwai877211f2005-11-17 13:59:38 +01003521 struct snd_pcm_file * pcm_file;
3522 struct snd_pcm_substream *substream;
3523 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 pcm_file = file->private_data;
3526 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003527 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003528 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003530 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
3533/*
3534 * ioctl32 compat
3535 */
3536#ifdef CONFIG_COMPAT
3537#include "pcm_compat.c"
3538#else
3539#define snd_pcm_ioctl_compat NULL
3540#endif
3541
3542/*
3543 * To be removed helpers to keep binary compatibility
3544 */
3545
Takashi Iwai59d48582005-12-01 10:51:58 +01003546#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3548#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3549
Takashi Iwai877211f2005-11-17 13:59:38 +01003550static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3551 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
3553 unsigned int i;
3554
3555 memset(params, 0, sizeof(*params));
3556 params->flags = oparams->flags;
3557 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3558 params->masks[i].bits[0] = oparams->masks[i];
3559 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3560 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3561 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3562 params->info = oparams->info;
3563 params->msbits = oparams->msbits;
3564 params->rate_num = oparams->rate_num;
3565 params->rate_den = oparams->rate_den;
3566 params->fifo_size = oparams->fifo_size;
3567}
3568
Takashi Iwai877211f2005-11-17 13:59:38 +01003569static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3570 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
3572 unsigned int i;
3573
3574 memset(oparams, 0, sizeof(*oparams));
3575 oparams->flags = params->flags;
3576 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3577 oparams->masks[i] = params->masks[i].bits[0];
3578 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3579 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3580 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3581 oparams->info = params->info;
3582 oparams->msbits = params->msbits;
3583 oparams->rate_num = params->rate_num;
3584 oparams->rate_den = params->rate_den;
3585 oparams->fifo_size = params->fifo_size;
3586}
3587
Takashi Iwai877211f2005-11-17 13:59:38 +01003588static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3589 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
Takashi Iwai877211f2005-11-17 13:59:38 +01003591 struct snd_pcm_hw_params *params;
3592 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 int err;
3594
3595 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003596 if (!params)
3597 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Li Zefanef44a1e2009-04-10 09:43:08 +08003599 oparams = memdup_user(_oparams, sizeof(*oparams));
3600 if (IS_ERR(oparams)) {
3601 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 goto out;
3603 }
3604 snd_pcm_hw_convert_from_old_params(params, oparams);
3605 err = snd_pcm_hw_refine(substream, params);
3606 snd_pcm_hw_convert_to_old_params(oparams, params);
3607 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3608 if (!err)
3609 err = -EFAULT;
3610 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003611
3612 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613out:
3614 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 return err;
3616}
3617
Takashi Iwai877211f2005-11-17 13:59:38 +01003618static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3619 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
Takashi Iwai877211f2005-11-17 13:59:38 +01003621 struct snd_pcm_hw_params *params;
3622 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 int err;
3624
3625 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003626 if (!params)
3627 return -ENOMEM;
3628
3629 oparams = memdup_user(_oparams, sizeof(*oparams));
3630 if (IS_ERR(oparams)) {
3631 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 goto out;
3633 }
3634 snd_pcm_hw_convert_from_old_params(params, oparams);
3635 err = snd_pcm_hw_params(substream, params);
3636 snd_pcm_hw_convert_to_old_params(oparams, params);
3637 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3638 if (!err)
3639 err = -EFAULT;
3640 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003641
3642 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643out:
3644 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return err;
3646}
Takashi Iwai59d48582005-12-01 10:51:58 +01003647#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Cliff Cai70036092008-09-03 10:54:36 +02003649#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003650static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3651 unsigned long addr,
3652 unsigned long len,
3653 unsigned long pgoff,
3654 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003655{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003656 struct snd_pcm_file *pcm_file = file->private_data;
3657 struct snd_pcm_substream *substream = pcm_file->substream;
3658 struct snd_pcm_runtime *runtime = substream->runtime;
3659 unsigned long offset = pgoff << PAGE_SHIFT;
3660
3661 switch (offset) {
3662 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3663 return (unsigned long)runtime->status;
3664 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3665 return (unsigned long)runtime->control;
3666 default:
3667 return (unsigned long)runtime->dma_area + offset;
3668 }
Cliff Cai70036092008-09-03 10:54:36 +02003669}
3670#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003671# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003672#endif
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674/*
3675 * Register section
3676 */
3677
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003678const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003680 .owner = THIS_MODULE,
3681 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003682 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003683 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003684 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003685 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003686 .poll = snd_pcm_playback_poll,
3687 .unlocked_ioctl = snd_pcm_playback_ioctl,
3688 .compat_ioctl = snd_pcm_ioctl_compat,
3689 .mmap = snd_pcm_mmap,
3690 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003691 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 },
3693 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003694 .owner = THIS_MODULE,
3695 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003696 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003697 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003698 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003699 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003700 .poll = snd_pcm_capture_poll,
3701 .unlocked_ioctl = snd_pcm_capture_ioctl,
3702 .compat_ioctl = snd_pcm_ioctl_compat,
3703 .mmap = snd_pcm_mmap,
3704 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003705 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 }
3707};