blob: e0e5394b18ff4558ab51e392fea29f75bfb03443 [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>
26#include <linux/time.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020027#include <linux/pm_qos.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010028#include <linux/io.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010029#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/control.h>
32#include <sound/info.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/timer.h>
36#include <sound/minors.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080037#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Compatibility
41 */
42
Takashi Iwai877211f2005-11-17 13:59:38 +010043struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 unsigned int flags;
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010047 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
49 unsigned int rmask;
50 unsigned int cmask;
51 unsigned int info;
52 unsigned int msbits;
53 unsigned int rate_num;
54 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010055 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned char reserved[64];
57};
58
Takashi Iwai59d48582005-12-01 10:51:58 +010059#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010060#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Takashi Iwai877211f2005-11-17 13:59:38 +010063static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010067#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010068static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 *
72 */
73
Takashi Iwai7af142f2014-09-01 11:19:37 +020074static DEFINE_RWLOCK(snd_pcm_link_rwlock);
75static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Takashi Iwai67ec1072016-02-17 14:30:26 +010077/* Writer in rwsem may block readers even during its waiting in queue,
78 * and this may lead to a deadlock when the code path takes read sem
79 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
80 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
81 * spin until it gets the lock.
82 */
83static inline void down_write_nonblock(struct rw_semaphore *lock)
84{
85 while (!down_write_trylock(lock))
86 cond_resched();
87}
88
Takashi Iwai30b771c2014-10-30 15:02:50 +010089/**
90 * snd_pcm_stream_lock - Lock the PCM stream
91 * @substream: PCM substream
92 *
93 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
94 * flag of the given substream. This also takes the global link rw lock
95 * (or rw sem), too, for avoiding the race with linked streams.
96 */
Takashi Iwai7af142f2014-09-01 11:19:37 +020097void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
98{
99 if (substream->pcm->nonatomic) {
Takashi Iwai67756e32015-07-17 15:22:33 +0200100 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200101 mutex_lock(&substream->self_group.mutex);
102 } else {
103 read_lock(&snd_pcm_link_rwlock);
104 spin_lock(&substream->self_group.lock);
105 }
106}
107EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
108
Takashi Iwai30b771c2014-10-30 15:02:50 +0100109/**
110 * snd_pcm_stream_lock - Unlock the PCM stream
111 * @substream: PCM substream
112 *
113 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
114 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200115void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
116{
117 if (substream->pcm->nonatomic) {
118 mutex_unlock(&substream->self_group.mutex);
119 up_read(&snd_pcm_link_rwsem);
120 } else {
121 spin_unlock(&substream->self_group.lock);
122 read_unlock(&snd_pcm_link_rwlock);
123 }
124}
125EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
126
Takashi Iwai30b771c2014-10-30 15:02:50 +0100127/**
128 * snd_pcm_stream_lock_irq - Lock the PCM stream
129 * @substream: PCM substream
130 *
131 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
132 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
133 * as snd_pcm_stream_lock().
134 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200135void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
136{
137 if (!substream->pcm->nonatomic)
138 local_irq_disable();
139 snd_pcm_stream_lock(substream);
140}
141EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
142
Takashi Iwai30b771c2014-10-30 15:02:50 +0100143/**
144 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
145 * @substream: PCM substream
146 *
147 * This is a counter-part of snd_pcm_stream_lock_irq().
148 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200149void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
150{
151 snd_pcm_stream_unlock(substream);
152 if (!substream->pcm->nonatomic)
153 local_irq_enable();
154}
155EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
156
157unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
158{
159 unsigned long flags = 0;
160 if (!substream->pcm->nonatomic)
161 local_irq_save(flags);
162 snd_pcm_stream_lock(substream);
163 return flags;
164}
165EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
166
Takashi Iwai30b771c2014-10-30 15:02:50 +0100167/**
168 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
169 * @substream: PCM substream
170 * @flags: irq flags
171 *
172 * This is a counter-part of snd_pcm_stream_lock_irqsave().
173 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200174void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
175 unsigned long flags)
176{
177 snd_pcm_stream_unlock(substream);
178 if (!substream->pcm->nonatomic)
179 local_irq_restore(flags);
180}
181EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183static inline mm_segment_t snd_enter_user(void)
184{
185 mm_segment_t fs = get_fs();
186 set_fs(get_ds());
187 return fs;
188}
189
190static inline void snd_leave_user(mm_segment_t fs)
191{
192 set_fs(fs);
193}
194
195
196
Takashi Iwai877211f2005-11-17 13:59:38 +0100197int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Takashi Iwai877211f2005-11-17 13:59:38 +0100199 struct snd_pcm_runtime *runtime;
200 struct snd_pcm *pcm = substream->pcm;
201 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 memset(info, 0, sizeof(*info));
204 info->card = pcm->card->number;
205 info->device = pcm->device;
206 info->stream = substream->stream;
207 info->subdevice = substream->number;
208 strlcpy(info->id, pcm->id, sizeof(info->id));
209 strlcpy(info->name, pcm->name, sizeof(info->name));
210 info->dev_class = pcm->dev_class;
211 info->dev_subclass = pcm->dev_subclass;
212 info->subdevices_count = pstr->substream_count;
213 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
214 strlcpy(info->subname, substream->name, sizeof(info->subname));
215 runtime = substream->runtime;
216 /* AB: FIXME!!! This is definitely nonsense */
217 if (runtime) {
218 info->sync = runtime->sync;
219 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
220 }
221 return 0;
222}
223
Takashi Iwai877211f2005-11-17 13:59:38 +0100224int snd_pcm_info_user(struct snd_pcm_substream *substream,
225 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Takashi Iwai877211f2005-11-17 13:59:38 +0100227 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int err;
229
230 info = kmalloc(sizeof(*info), GFP_KERNEL);
231 if (! info)
232 return -ENOMEM;
233 err = snd_pcm_info(substream, info);
234 if (err >= 0) {
235 if (copy_to_user(_info, info, sizeof(*info)))
236 err = -EFAULT;
237 }
238 kfree(info);
239 return err;
240}
241
Takashi Iwai63825f32014-10-22 12:04:46 +0200242static bool hw_support_mmap(struct snd_pcm_substream *substream)
243{
244 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
245 return false;
246 /* check architectures that return -EINVAL from dma_mmap_coherent() */
247 /* FIXME: this should be some global flag */
248#if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
249 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
250 if (!substream->ops->mmap &&
251 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
252 return false;
253#endif
254 return true;
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#undef RULES_DEBUG
258
259#ifdef RULES_DEBUG
260#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
Joe Perches47023ec2010-09-13 21:24:02 -0700261static const char * const snd_pcm_hw_param_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 HW_PARAM(ACCESS),
263 HW_PARAM(FORMAT),
264 HW_PARAM(SUBFORMAT),
265 HW_PARAM(SAMPLE_BITS),
266 HW_PARAM(FRAME_BITS),
267 HW_PARAM(CHANNELS),
268 HW_PARAM(RATE),
269 HW_PARAM(PERIOD_TIME),
270 HW_PARAM(PERIOD_SIZE),
271 HW_PARAM(PERIOD_BYTES),
272 HW_PARAM(PERIODS),
273 HW_PARAM(BUFFER_TIME),
274 HW_PARAM(BUFFER_SIZE),
275 HW_PARAM(BUFFER_BYTES),
276 HW_PARAM(TICK_TIME),
277};
278#endif
279
Takashi Iwai877211f2005-11-17 13:59:38 +0100280int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
281 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100284 struct snd_pcm_hardware *hw;
285 struct snd_interval *i = NULL;
286 struct snd_mask *m = NULL;
287 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 unsigned int rstamps[constrs->rules_num];
289 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
290 unsigned int stamp = 2;
291 int changed, again;
292
293 params->info = 0;
294 params->fifo_size = 0;
295 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
296 params->msbits = 0;
297 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
298 params->rate_num = 0;
299 params->rate_den = 0;
300 }
301
302 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
303 m = hw_param_mask(params, k);
304 if (snd_mask_empty(m))
305 return -EINVAL;
306 if (!(params->rmask & (1 << k)))
307 continue;
308#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100309 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
310 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 -0700311#endif
312 changed = snd_mask_refine(m, constrs_mask(constrs, k));
313#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100314 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 -0700315#endif
316 if (changed)
317 params->cmask |= 1 << k;
318 if (changed < 0)
319 return changed;
320 }
321
322 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
323 i = hw_param_interval(params, k);
324 if (snd_interval_empty(i))
325 return -EINVAL;
326 if (!(params->rmask & (1 << k)))
327 continue;
328#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100329 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100331 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100333 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 i->openmin ? '(' : '[', i->min,
335 i->max, i->openmax ? ')' : ']');
Takashi Iwai09e56df2014-02-04 18:19:48 +0100336 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#endif
338 changed = snd_interval_refine(i, constrs_interval(constrs, k));
339#ifdef RULES_DEBUG
340 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100341 pr_cont("empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100343 pr_cont("%c%u %u%c\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 i->openmin ? '(' : '[', i->min,
345 i->max, i->openmax ? ')' : ']');
346#endif
347 if (changed)
348 params->cmask |= 1 << k;
349 if (changed < 0)
350 return changed;
351 }
352
353 for (k = 0; k < constrs->rules_num; k++)
354 rstamps[k] = 0;
355 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
356 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
357 do {
358 again = 0;
359 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100360 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 unsigned int d;
362 int doit = 0;
363 if (r->cond && !(r->cond & params->flags))
364 continue;
365 for (d = 0; r->deps[d] >= 0; d++) {
366 if (vstamps[r->deps[d]] > rstamps[k]) {
367 doit = 1;
368 break;
369 }
370 }
371 if (!doit)
372 continue;
373#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100374 pr_debug("Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100376 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (hw_is_mask(r->var)) {
378 m = hw_param_mask(params, r->var);
Takashi Iwai09e56df2014-02-04 18:19:48 +0100379 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 } else {
381 i = hw_param_interval(params, r->var);
382 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100383 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100385 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 i->openmin ? '(' : '[', i->min,
387 i->max, i->openmax ? ')' : ']');
388 }
389 }
390#endif
391 changed = r->func(params, r);
392#ifdef RULES_DEBUG
393 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100394 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (hw_is_mask(r->var))
Takashi Iwai09e56df2014-02-04 18:19:48 +0100396 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 else {
398 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100399 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100401 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 i->openmin ? '(' : '[', i->min,
403 i->max, i->openmax ? ')' : ']');
404 }
405 }
Takashi Iwai09e56df2014-02-04 18:19:48 +0100406 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#endif
408 rstamps[k] = stamp;
409 if (changed && r->var >= 0) {
410 params->cmask |= (1 << r->var);
411 vstamps[r->var] = stamp;
412 again = 1;
413 }
414 if (changed < 0)
415 return changed;
416 stamp++;
417 }
418 } while (again);
419 if (!params->msbits) {
420 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
421 if (snd_interval_single(i))
422 params->msbits = snd_interval_value(i);
423 }
424
425 if (!params->rate_den) {
426 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
427 if (snd_interval_single(i)) {
428 params->rate_num = snd_interval_value(i);
429 params->rate_den = 1;
430 }
431 }
432
433 hw = &substream->runtime->hw;
Takashi Iwai63825f32014-10-22 12:04:46 +0200434 if (!params->info) {
Libin Yang48d88292014-12-31 22:09:54 +0800435 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
436 SNDRV_PCM_INFO_DRAIN_TRIGGER);
Takashi Iwai63825f32014-10-22 12:04:46 +0200437 if (!hw_support_mmap(substream))
438 params->info &= ~(SNDRV_PCM_INFO_MMAP |
439 SNDRV_PCM_INFO_MMAP_VALID);
440 }
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200441 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100442 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
443 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
444 if (snd_mask_min(m) == snd_mask_max(m) &&
445 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200446 changed = substream->ops->ioctl(substream,
447 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200448 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200449 return changed;
450 }
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 params->rmask = 0;
453 return 0;
454}
455
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200456EXPORT_SYMBOL(snd_pcm_hw_refine);
457
Takashi Iwai877211f2005-11-17 13:59:38 +0100458static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
459 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Takashi Iwai877211f2005-11-17 13:59:38 +0100461 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int err;
463
Li Zefanef44a1e2009-04-10 09:43:08 +0800464 params = memdup_user(_params, sizeof(*params));
465 if (IS_ERR(params))
466 return PTR_ERR(params);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 err = snd_pcm_hw_refine(substream, params);
469 if (copy_to_user(_params, params, sizeof(*params))) {
470 if (!err)
471 err = -EFAULT;
472 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 kfree(params);
475 return err;
476}
477
Takashi Iwai9442e692006-09-30 23:27:19 -0700478static int period_to_usecs(struct snd_pcm_runtime *runtime)
479{
480 int usecs;
481
482 if (! runtime->rate)
483 return -1; /* invalid */
484
485 /* take 75% of period time as the deadline */
486 usecs = (750000 / runtime->rate) * runtime->period_size;
487 usecs += ((750000 % runtime->rate) * runtime->period_size) /
488 runtime->rate;
489
490 return usecs;
491}
492
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200493static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
494{
495 snd_pcm_stream_lock_irq(substream);
496 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
497 substream->runtime->status->state = state;
498 snd_pcm_stream_unlock_irq(substream);
499}
500
Jie Yang90bbaf62015-10-16 17:57:46 +0800501static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
502 int event)
503{
504#ifdef CONFIG_SND_PCM_TIMER
505 if (substream->timer)
506 snd_timer_notify(substream->timer, event,
507 &substream->runtime->trigger_tstamp);
508#endif
509}
510
Takashi Iwai877211f2005-11-17 13:59:38 +0100511static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
512 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Takashi Iwai877211f2005-11-17 13:59:38 +0100514 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700515 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned int bits;
517 snd_pcm_uframes_t frames;
518
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200519 if (PCM_RUNTIME_CHECK(substream))
520 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 snd_pcm_stream_lock_irq(substream);
523 switch (runtime->status->state) {
524 case SNDRV_PCM_STATE_OPEN:
525 case SNDRV_PCM_STATE_SETUP:
526 case SNDRV_PCM_STATE_PREPARED:
527 break;
528 default:
529 snd_pcm_stream_unlock_irq(substream);
530 return -EBADFD;
531 }
532 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100533#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (!substream->oss.oss)
535#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200536 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -EBADFD;
538
539 params->rmask = ~0U;
540 err = snd_pcm_hw_refine(substream, params);
541 if (err < 0)
542 goto _error;
543
544 err = snd_pcm_hw_params_choose(substream, params);
545 if (err < 0)
546 goto _error;
547
548 if (substream->ops->hw_params != NULL) {
549 err = substream->ops->hw_params(substream, params);
550 if (err < 0)
551 goto _error;
552 }
553
554 runtime->access = params_access(params);
555 runtime->format = params_format(params);
556 runtime->subformat = params_subformat(params);
557 runtime->channels = params_channels(params);
558 runtime->rate = params_rate(params);
559 runtime->period_size = params_period_size(params);
560 runtime->periods = params_periods(params);
561 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 runtime->info = params->info;
563 runtime->rate_num = params->rate_num;
564 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100565 runtime->no_period_wakeup =
566 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
567 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 bits = snd_pcm_format_physical_width(runtime->format);
570 runtime->sample_bits = bits;
571 bits *= runtime->channels;
572 runtime->frame_bits = bits;
573 frames = 1;
574 while (bits % 8 != 0) {
575 bits *= 2;
576 frames *= 2;
577 }
578 runtime->byte_align = bits / 8;
579 runtime->min_align = frames;
580
581 /* Default sw params */
582 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
583 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 runtime->start_threshold = 1;
586 runtime->stop_threshold = runtime->buffer_size;
587 runtime->silence_threshold = 0;
588 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200589 runtime->boundary = runtime->buffer_size;
Laxminath Kasam8428b712013-07-11 10:55:26 +0530590 while (runtime->boundary * 2 * runtime->channels <=
591 LONG_MAX - runtime->buffer_size)
Clemens Ladischead40462010-05-21 09:15:59 +0200592 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 &&
Liam Girdwoodf2727332016-01-29 02:27:17 +05301038 !substream->hw_no_buffer &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 !snd_pcm_playback_data(substream))
1040 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001041 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 runtime->trigger_master = substream;
1043 return 0;
1044}
1045
Takashi Iwai877211f2005-11-17 13:59:38 +01001046static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 if (substream->runtime->trigger_master != substream)
1049 return 0;
1050 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1051}
1052
Takashi Iwai877211f2005-11-17 13:59:38 +01001053static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 if (substream->runtime->trigger_master == substream)
1056 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1057}
1058
Takashi Iwai877211f2005-11-17 13:59:38 +01001059static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Takashi Iwai877211f2005-11-17 13:59:38 +01001061 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001063 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001064 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1065 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 runtime->status->state = state;
1067 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1068 runtime->silence_size > 0)
1069 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001070 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Julia Lawallb17154c2015-11-29 16:36:40 +01001073static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 .pre_action = snd_pcm_pre_start,
1075 .do_action = snd_pcm_do_start,
1076 .undo_action = snd_pcm_undo_start,
1077 .post_action = snd_pcm_post_start
1078};
1079
1080/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001081 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001082 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001083 *
1084 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001086int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Takashi Iwai877211f2005-11-17 13:59:38 +01001088 return snd_pcm_action(&snd_pcm_action_start, substream,
1089 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092/*
1093 * stop callbacks
1094 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001095static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Takashi Iwai877211f2005-11-17 13:59:38 +01001097 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1099 return -EBADFD;
1100 runtime->trigger_master = substream;
1101 return 0;
1102}
1103
Takashi Iwai877211f2005-11-17 13:59:38 +01001104static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 if (substream->runtime->trigger_master == substream &&
1107 snd_pcm_running(substream))
1108 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1109 return 0; /* unconditonally stop all substreams */
1110}
1111
Takashi Iwai877211f2005-11-17 13:59:38 +01001112static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Takashi Iwai877211f2005-11-17 13:59:38 +01001114 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (runtime->status->state != state) {
1116 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001117 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001118 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001121 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Julia Lawallb17154c2015-11-29 16:36:40 +01001124static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 .pre_action = snd_pcm_pre_stop,
1126 .do_action = snd_pcm_do_stop,
1127 .post_action = snd_pcm_post_stop
1128};
1129
1130/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001131 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001132 * @substream: the PCM substream instance
1133 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001135 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001136 *
Masanari Iida0a114582014-02-09 00:47:36 +09001137 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001139int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1142}
1143
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001144EXPORT_SYMBOL(snd_pcm_stop);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001147 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001148 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001150 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001152 *
1153 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001155int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Takashi Iwai877211f2005-11-17 13:59:38 +01001157 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1158 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Takashi Iwai1fb85102014-11-07 17:08:28 +01001161/**
1162 * snd_pcm_stop_xrun - stop the running streams as XRUN
1163 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001164 *
1165 * This stops the given running substream (and all linked substreams) as XRUN.
1166 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1167 *
1168 * Return: Zero if successful, or a negative error code.
1169 */
1170int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1171{
1172 unsigned long flags;
1173 int ret = 0;
1174
1175 snd_pcm_stream_lock_irqsave(substream, flags);
1176 if (snd_pcm_running(substream))
1177 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1178 snd_pcm_stream_unlock_irqrestore(substream, flags);
1179 return ret;
1180}
1181EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183/*
1184 * pause callbacks
1185 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001186static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Takashi Iwai877211f2005-11-17 13:59:38 +01001188 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1190 return -ENOSYS;
1191 if (push) {
1192 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1193 return -EBADFD;
1194 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1195 return -EBADFD;
1196 runtime->trigger_master = substream;
1197 return 0;
1198}
1199
Takashi Iwai877211f2005-11-17 13:59:38 +01001200static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 if (substream->runtime->trigger_master != substream)
1203 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001204 /* some drivers might use hw_ptr to recover from the pause -
1205 update the hw_ptr now */
1206 if (push)
1207 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001208 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001209 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001210 * delta, effectively to skip the check once.
1211 */
1212 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return substream->ops->trigger(substream,
1214 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1215 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1216}
1217
Takashi Iwai877211f2005-11-17 13:59:38 +01001218static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 if (substream->runtime->trigger_master == substream)
1221 substream->ops->trigger(substream,
1222 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1223 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1224}
1225
Takashi Iwai877211f2005-11-17 13:59:38 +01001226static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Takashi Iwai877211f2005-11-17 13:59:38 +01001228 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 snd_pcm_trigger_tstamp(substream);
1230 if (push) {
1231 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001232 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001234 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 } else {
1236 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001237 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239}
1240
Julia Lawallb17154c2015-11-29 16:36:40 +01001241static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 .pre_action = snd_pcm_pre_pause,
1243 .do_action = snd_pcm_do_pause,
1244 .undo_action = snd_pcm_undo_pause,
1245 .post_action = snd_pcm_post_pause
1246};
1247
1248/*
1249 * Push/release the pause for all linked streams.
1250 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001251static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1254}
1255
1256#ifdef CONFIG_PM
1257/* suspend */
1258
Takashi Iwai877211f2005-11-17 13:59:38 +01001259static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Takashi Iwai877211f2005-11-17 13:59:38 +01001261 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1263 return -EBUSY;
1264 runtime->trigger_master = substream;
1265 return 0;
1266}
1267
Takashi Iwai877211f2005-11-17 13:59:38 +01001268static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Takashi Iwai877211f2005-11-17 13:59:38 +01001270 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (runtime->trigger_master != substream)
1272 return 0;
1273 if (! snd_pcm_running(substream))
1274 return 0;
1275 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1276 return 0; /* suspend unconditionally */
1277}
1278
Takashi Iwai877211f2005-11-17 13:59:38 +01001279static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Takashi Iwai877211f2005-11-17 13:59:38 +01001281 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001283 runtime->status->suspended_state = runtime->status->state;
1284 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001285 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001287 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Julia Lawallb17154c2015-11-29 16:36:40 +01001290static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 .pre_action = snd_pcm_pre_suspend,
1292 .do_action = snd_pcm_do_suspend,
1293 .post_action = snd_pcm_post_suspend
1294};
1295
1296/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001297 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001298 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001301 *
1302 * Return: Zero if successful (or @substream is %NULL), or a negative error
1303 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001305int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 int err;
1308 unsigned long flags;
1309
Takashi Iwai603bf522005-11-17 15:59:14 +01001310 if (! substream)
1311 return 0;
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 snd_pcm_stream_lock_irqsave(substream, flags);
1314 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1315 snd_pcm_stream_unlock_irqrestore(substream, flags);
1316 return err;
1317}
1318
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001319EXPORT_SYMBOL(snd_pcm_suspend);
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001322 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001323 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001326 *
1327 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001329int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Takashi Iwai877211f2005-11-17 13:59:38 +01001331 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int stream, err = 0;
1333
Takashi Iwai603bf522005-11-17 15:59:14 +01001334 if (! pcm)
1335 return 0;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001338 for (substream = pcm->streams[stream].substream;
1339 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /* FIXME: the open/close code should lock this as well */
1341 if (substream->runtime == NULL)
1342 continue;
1343 err = snd_pcm_suspend(substream);
1344 if (err < 0 && err != -EBUSY)
1345 return err;
1346 }
1347 }
1348 return 0;
1349}
1350
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001351EXPORT_SYMBOL(snd_pcm_suspend_all);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353/* resume */
1354
Takashi Iwai877211f2005-11-17 13:59:38 +01001355static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Takashi Iwai877211f2005-11-17 13:59:38 +01001357 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1359 return -ENOSYS;
1360 runtime->trigger_master = substream;
1361 return 0;
1362}
1363
Takashi Iwai877211f2005-11-17 13:59:38 +01001364static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Takashi Iwai877211f2005-11-17 13:59:38 +01001366 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (runtime->trigger_master != substream)
1368 return 0;
1369 /* DMA not running previously? */
1370 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1371 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1372 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1373 return 0;
1374 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1375}
1376
Takashi Iwai877211f2005-11-17 13:59:38 +01001377static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 if (substream->runtime->trigger_master == substream &&
1380 snd_pcm_running(substream))
1381 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1382}
1383
Takashi Iwai877211f2005-11-17 13:59:38 +01001384static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Takashi Iwai877211f2005-11-17 13:59:38 +01001386 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001388 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001389 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Julia Lawallb17154c2015-11-29 16:36:40 +01001392static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 .pre_action = snd_pcm_pre_resume,
1394 .do_action = snd_pcm_do_resume,
1395 .undo_action = snd_pcm_undo_resume,
1396 .post_action = snd_pcm_post_resume
1397};
1398
Takashi Iwai877211f2005-11-17 13:59:38 +01001399static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Takashi Iwai877211f2005-11-17 13:59:38 +01001401 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 int res;
1403
1404 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001405 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1407 snd_power_unlock(card);
1408 return res;
1409}
1410
1411#else
1412
Takashi Iwai877211f2005-11-17 13:59:38 +01001413static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 return -ENOSYS;
1416}
1417
1418#endif /* CONFIG_PM */
1419
1420/*
1421 * xrun ioctl
1422 *
1423 * Change the RUNNING stream(s) to XRUN state.
1424 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001425static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Takashi Iwai877211f2005-11-17 13:59:38 +01001427 struct snd_card *card = substream->pcm->card;
1428 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int result;
1430
1431 snd_power_lock(card);
1432 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001433 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (result < 0)
1435 goto _unlock;
1436 }
1437
1438 snd_pcm_stream_lock_irq(substream);
1439 switch (runtime->status->state) {
1440 case SNDRV_PCM_STATE_XRUN:
1441 result = 0; /* already there */
1442 break;
1443 case SNDRV_PCM_STATE_RUNNING:
1444 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1445 break;
1446 default:
1447 result = -EBADFD;
1448 }
1449 snd_pcm_stream_unlock_irq(substream);
1450 _unlock:
1451 snd_power_unlock(card);
1452 return result;
1453}
1454
1455/*
1456 * reset ioctl
1457 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001458static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Takashi Iwai877211f2005-11-17 13:59:38 +01001460 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 switch (runtime->status->state) {
1462 case SNDRV_PCM_STATE_RUNNING:
1463 case SNDRV_PCM_STATE_PREPARED:
1464 case SNDRV_PCM_STATE_PAUSED:
1465 case SNDRV_PCM_STATE_SUSPENDED:
1466 return 0;
1467 default:
1468 return -EBADFD;
1469 }
1470}
1471
Takashi Iwai877211f2005-11-17 13:59:38 +01001472static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Takashi Iwai877211f2005-11-17 13:59:38 +01001474 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1476 if (err < 0)
1477 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001479 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1480 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 runtime->silence_start = runtime->status->hw_ptr;
1482 runtime->silence_filled = 0;
1483 return 0;
1484}
1485
Takashi Iwai877211f2005-11-17 13:59:38 +01001486static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Takashi Iwai877211f2005-11-17 13:59:38 +01001488 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 runtime->control->appl_ptr = runtime->status->hw_ptr;
1490 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1491 runtime->silence_size > 0)
1492 snd_pcm_playback_silence(substream, ULONG_MAX);
1493}
1494
Julia Lawallb17154c2015-11-29 16:36:40 +01001495static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 .pre_action = snd_pcm_pre_reset,
1497 .do_action = snd_pcm_do_reset,
1498 .post_action = snd_pcm_post_reset
1499};
1500
Takashi Iwai877211f2005-11-17 13:59:38 +01001501static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1504}
1505
1506/*
1507 * prepare ioctl
1508 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001509/* we use the second argument for updating f_flags */
1510static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1511 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Takashi Iwai877211f2005-11-17 13:59:38 +01001513 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001514 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1515 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -EBADFD;
1517 if (snd_pcm_running(substream))
1518 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001519 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return 0;
1521}
1522
Takashi Iwai877211f2005-11-17 13:59:38 +01001523static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 int err;
1526 err = substream->ops->prepare(substream);
1527 if (err < 0)
1528 return err;
1529 return snd_pcm_do_reset(substream, 0);
1530}
1531
Takashi Iwai877211f2005-11-17 13:59:38 +01001532static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Takashi Iwai877211f2005-11-17 13:59:38 +01001534 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001536 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Julia Lawallb17154c2015-11-29 16:36:40 +01001539static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 .pre_action = snd_pcm_pre_prepare,
1541 .do_action = snd_pcm_do_prepare,
1542 .post_action = snd_pcm_post_prepare
1543};
1544
1545/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001546 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001547 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001548 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001549 *
1550 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001552static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1553 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001556 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001557 int f_flags;
1558
1559 if (file)
1560 f_flags = file->f_flags;
1561 else
1562 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001565 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001566 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1567 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 snd_power_unlock(card);
1569 return res;
1570}
1571
1572/*
1573 * drain ioctl
1574 */
1575
Takashi Iwai877211f2005-11-17 13:59:38 +01001576static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001578 struct snd_pcm_runtime *runtime = substream->runtime;
1579 switch (runtime->status->state) {
1580 case SNDRV_PCM_STATE_OPEN:
1581 case SNDRV_PCM_STATE_DISCONNECTED:
1582 case SNDRV_PCM_STATE_SUSPENDED:
1583 return -EBADFD;
1584 }
1585 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return 0;
1587}
1588
Takashi Iwai877211f2005-11-17 13:59:38 +01001589static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Takashi Iwai877211f2005-11-17 13:59:38 +01001591 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1593 switch (runtime->status->state) {
1594 case SNDRV_PCM_STATE_PREPARED:
1595 /* start playback stream if possible */
1596 if (! snd_pcm_playback_empty(substream)) {
1597 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1598 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001599 } else {
1600 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602 break;
1603 case SNDRV_PCM_STATE_RUNNING:
1604 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1605 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001606 case SNDRV_PCM_STATE_XRUN:
1607 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1608 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 default:
1610 break;
1611 }
1612 } else {
1613 /* stop running stream */
1614 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001615 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001617 snd_pcm_do_stop(substream, new_state);
1618 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
1620 }
Libin Yang48d88292014-12-31 22:09:54 +08001621
1622 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1623 runtime->trigger_master == substream &&
1624 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1625 return substream->ops->trigger(substream,
1626 SNDRV_PCM_TRIGGER_DRAIN);
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
1629}
1630
Takashi Iwai877211f2005-11-17 13:59:38 +01001631static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633}
1634
Julia Lawallb17154c2015-11-29 16:36:40 +01001635static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 .pre_action = snd_pcm_pre_drain_init,
1637 .do_action = snd_pcm_do_drain_init,
1638 .post_action = snd_pcm_post_drain_init
1639};
1640
Takashi Iwai877211f2005-11-17 13:59:38 +01001641static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643/*
1644 * Drain the stream(s).
1645 * When the substream is linked, sync until the draining of all playback streams
1646 * is finished.
1647 * After this call, all streams are supposed to be either SETUP or DRAINING
1648 * (capture only) state.
1649 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001650static int snd_pcm_drain(struct snd_pcm_substream *substream,
1651 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Takashi Iwai877211f2005-11-17 13:59:38 +01001653 struct snd_card *card;
1654 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001655 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001656 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001658 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 card = substream->pcm->card;
1661 runtime = substream->runtime;
1662
1663 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1664 return -EBADFD;
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 snd_power_lock(card);
1667 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001668 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001669 if (result < 0) {
1670 snd_power_unlock(card);
1671 return result;
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001675 if (file) {
1676 if (file->f_flags & O_NONBLOCK)
1677 nonblock = 1;
1678 } else if (substream->f_flags & O_NONBLOCK)
1679 nonblock = 1;
1680
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001681 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001682 snd_pcm_stream_lock_irq(substream);
1683 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001684 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001685 snd_pcm_pause(substream, 0);
1686
1687 /* pre-start/stop - all running streams are changed to DRAINING state */
1688 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001689 if (result < 0)
1690 goto unlock;
1691 /* in non-blocking, we don't wait in ioctl but let caller poll */
1692 if (nonblock) {
1693 result = -EAGAIN;
1694 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 for (;;) {
1698 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001699 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (signal_pending(current)) {
1701 result = -ERESTARTSYS;
1702 break;
1703 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001704 /* find a substream to drain */
1705 to_check = NULL;
1706 snd_pcm_group_for_each_entry(s, substream) {
1707 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1708 continue;
1709 runtime = s->runtime;
1710 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1711 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001712 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001713 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001714 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001715 if (!to_check)
1716 break; /* all drained */
1717 init_waitqueue_entry(&wait, current);
1718 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001720 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001722 if (runtime->no_period_wakeup)
1723 tout = MAX_SCHEDULE_TIMEOUT;
1724 else {
1725 tout = 10;
1726 if (runtime->rate) {
1727 long t = runtime->period_size * 2 / runtime->rate;
1728 tout = max(t, tout);
1729 }
1730 tout = msecs_to_jiffies(tout * 1000);
1731 }
1732 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001734 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001736 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001737 if (card->shutdown) {
1738 result = -ENODEV;
1739 break;
1740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (tout == 0) {
1742 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1743 result = -ESTRPIPE;
1744 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001745 dev_dbg(substream->pcm->card->dev,
1746 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1748 result = -EIO;
1749 }
1750 break;
1751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001753
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001754 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001755 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001756 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 return result;
1760}
1761
1762/*
1763 * drop ioctl
1764 *
1765 * Immediately put all linked substreams into SETUP state.
1766 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001767static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Takashi Iwai877211f2005-11-17 13:59:38 +01001769 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 int result = 0;
1771
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001772 if (PCM_RUNTIME_CHECK(substream))
1773 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001776 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001777 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1778 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return -EBADFD;
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 snd_pcm_stream_lock_irq(substream);
1782 /* resume pause */
1783 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1784 snd_pcm_pause(substream, 0);
1785
1786 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1787 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1788 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return result;
1791}
1792
1793
Al Viro0888c322013-06-05 14:09:55 -04001794static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Al Viro0888c322013-06-05 14:09:55 -04001796 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001797 unsigned int minor;
1798
Al Viro0888c322013-06-05 14:09:55 -04001799 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1800 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001802 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1803 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806/*
1807 * PCM link handling
1808 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001809static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
1811 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001812 struct snd_pcm_file *pcm_file;
1813 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001814 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001815 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Al Viro0888c322013-06-05 14:09:55 -04001817 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001819 if (!is_pcm_file(f.file)) {
1820 res = -EBADFD;
1821 goto _badf;
1822 }
1823 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001825 group = kmalloc(sizeof(*group), GFP_KERNEL);
1826 if (!group) {
1827 res = -ENOMEM;
1828 goto _nolock;
1829 }
Takashi Iwai67ec1072016-02-17 14:30:26 +01001830 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 write_lock_irq(&snd_pcm_link_rwlock);
1832 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001833 substream->runtime->status->state != substream1->runtime->status->state ||
1834 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 res = -EBADFD;
1836 goto _end;
1837 }
1838 if (snd_pcm_stream_linked(substream1)) {
1839 res = -EALREADY;
1840 goto _end;
1841 }
1842 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001843 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001844 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001846 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 INIT_LIST_HEAD(&substream->group->substreams);
1848 list_add_tail(&substream->link_list, &substream->group->substreams);
1849 substream->group->count = 1;
1850 }
1851 list_add_tail(&substream1->link_list, &substream->group->substreams);
1852 substream->group->count++;
1853 substream1->group = substream->group;
1854 _end:
1855 write_unlock_irq(&snd_pcm_link_rwlock);
1856 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001857 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001858 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001859 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001860 _badf:
1861 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return res;
1863}
1864
Takashi Iwai877211f2005-11-17 13:59:38 +01001865static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 substream->group = &substream->self_group;
1868 INIT_LIST_HEAD(&substream->self_group.substreams);
1869 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1870}
1871
Takashi Iwai877211f2005-11-17 13:59:38 +01001872static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Takashi Iwaief991b92007-02-22 12:52:53 +01001874 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 int res = 0;
1876
Takashi Iwai67ec1072016-02-17 14:30:26 +01001877 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 write_lock_irq(&snd_pcm_link_rwlock);
1879 if (!snd_pcm_stream_linked(substream)) {
1880 res = -EALREADY;
1881 goto _end;
1882 }
1883 list_del(&substream->link_list);
1884 substream->group->count--;
1885 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001886 snd_pcm_group_for_each_entry(s, substream) {
1887 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 break;
1889 }
1890 kfree(substream->group);
1891 }
1892 relink_to_local(substream);
1893 _end:
1894 write_unlock_irq(&snd_pcm_link_rwlock);
1895 up_write(&snd_pcm_link_rwsem);
1896 return res;
1897}
1898
1899/*
1900 * hw configurator
1901 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001902static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1903 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Takashi Iwai877211f2005-11-17 13:59:38 +01001905 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1907 hw_param_interval_c(params, rule->deps[1]), &t);
1908 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1909}
1910
Takashi Iwai877211f2005-11-17 13:59:38 +01001911static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1912 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Takashi Iwai877211f2005-11-17 13:59:38 +01001914 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1916 hw_param_interval_c(params, rule->deps[1]), &t);
1917 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1918}
1919
Takashi Iwai877211f2005-11-17 13:59:38 +01001920static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1921 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Takashi Iwai877211f2005-11-17 13:59:38 +01001923 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1925 hw_param_interval_c(params, rule->deps[1]),
1926 (unsigned long) rule->private, &t);
1927 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1928}
1929
Takashi Iwai877211f2005-11-17 13:59:38 +01001930static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1931 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Takashi Iwai877211f2005-11-17 13:59:38 +01001933 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1935 (unsigned long) rule->private,
1936 hw_param_interval_c(params, rule->deps[1]), &t);
1937 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1938}
1939
Takashi Iwai877211f2005-11-17 13:59:38 +01001940static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1941 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001944 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1945 struct snd_mask m;
1946 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 snd_mask_any(&m);
1948 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1949 int bits;
1950 if (! snd_mask_test(mask, k))
1951 continue;
1952 bits = snd_pcm_format_physical_width(k);
1953 if (bits <= 0)
1954 continue; /* ignore invalid formats */
1955 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1956 snd_mask_reset(&m, k);
1957 }
1958 return snd_mask_refine(mask, &m);
1959}
1960
Takashi Iwai877211f2005-11-17 13:59:38 +01001961static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1962 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Takashi Iwai877211f2005-11-17 13:59:38 +01001964 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 unsigned int k;
1966 t.min = UINT_MAX;
1967 t.max = 0;
1968 t.openmin = 0;
1969 t.openmax = 0;
1970 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1971 int bits;
1972 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1973 continue;
1974 bits = snd_pcm_format_physical_width(k);
1975 if (bits <= 0)
1976 continue; /* ignore invalid formats */
1977 if (t.min > (unsigned)bits)
1978 t.min = bits;
1979 if (t.max < (unsigned)bits)
1980 t.max = bits;
1981 }
1982 t.integer = 1;
1983 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1984}
1985
1986#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1987#error "Change this table"
1988#endif
1989
1990static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1991 48000, 64000, 88200, 96000, 176400, 192000 };
1992
Clemens Ladisch7653d552007-08-13 17:38:54 +02001993const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1994 .count = ARRAY_SIZE(rates),
1995 .list = rates,
1996};
1997
Takashi Iwai877211f2005-11-17 13:59:38 +01001998static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1999 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Takashi Iwai877211f2005-11-17 13:59:38 +01002001 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002003 snd_pcm_known_rates.count,
2004 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Takashi Iwai877211f2005-11-17 13:59:38 +01002007static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2008 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Takashi Iwai877211f2005-11-17 13:59:38 +01002010 struct snd_interval t;
2011 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 t.min = 0;
2013 t.max = substream->buffer_bytes_max;
2014 t.openmin = 0;
2015 t.openmax = 0;
2016 t.integer = 1;
2017 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2018}
2019
Takashi Iwai877211f2005-11-17 13:59:38 +01002020int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Takashi Iwai877211f2005-11-17 13:59:38 +01002022 struct snd_pcm_runtime *runtime = substream->runtime;
2023 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 int k, err;
2025
2026 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2027 snd_mask_any(constrs_mask(constrs, k));
2028 }
2029
2030 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2031 snd_interval_any(constrs_interval(constrs, k));
2032 }
2033
2034 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2035 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2036 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2037 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2038 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2039
2040 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2041 snd_pcm_hw_rule_format, NULL,
2042 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2043 if (err < 0)
2044 return err;
2045 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2046 snd_pcm_hw_rule_sample_bits, NULL,
2047 SNDRV_PCM_HW_PARAM_FORMAT,
2048 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2049 if (err < 0)
2050 return err;
2051 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2052 snd_pcm_hw_rule_div, NULL,
2053 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2054 if (err < 0)
2055 return err;
2056 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2057 snd_pcm_hw_rule_mul, NULL,
2058 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2059 if (err < 0)
2060 return err;
2061 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2062 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2063 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2064 if (err < 0)
2065 return err;
2066 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2067 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2068 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2069 if (err < 0)
2070 return err;
2071 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2072 snd_pcm_hw_rule_div, NULL,
2073 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2074 if (err < 0)
2075 return err;
2076 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2077 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2078 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2079 if (err < 0)
2080 return err;
2081 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2082 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2083 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2084 if (err < 0)
2085 return err;
2086 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2087 snd_pcm_hw_rule_div, NULL,
2088 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2089 if (err < 0)
2090 return err;
2091 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2092 snd_pcm_hw_rule_div, NULL,
2093 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2094 if (err < 0)
2095 return err;
2096 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2097 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2098 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2099 if (err < 0)
2100 return err;
2101 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2102 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2103 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2104 if (err < 0)
2105 return err;
2106 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2107 snd_pcm_hw_rule_mul, NULL,
2108 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2109 if (err < 0)
2110 return err;
2111 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2112 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2113 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2114 if (err < 0)
2115 return err;
2116 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2117 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2118 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2119 if (err < 0)
2120 return err;
2121 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2122 snd_pcm_hw_rule_muldivk, (void*) 8,
2123 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2124 if (err < 0)
2125 return err;
2126 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2127 snd_pcm_hw_rule_muldivk, (void*) 8,
2128 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2129 if (err < 0)
2130 return err;
2131 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2132 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2133 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2134 if (err < 0)
2135 return err;
2136 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2137 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2138 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2139 if (err < 0)
2140 return err;
2141 return 0;
2142}
2143
Takashi Iwai877211f2005-11-17 13:59:38 +01002144int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Takashi Iwai877211f2005-11-17 13:59:38 +01002146 struct snd_pcm_runtime *runtime = substream->runtime;
2147 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 int err;
2149 unsigned int mask = 0;
2150
2151 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2152 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2153 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2154 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002155 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2157 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2158 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2159 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2160 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2161 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2162 }
2163 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002164 if (err < 0)
2165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002168 if (err < 0)
2169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002172 if (err < 0)
2173 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2176 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002177 if (err < 0)
2178 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2181 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002182 if (err < 0)
2183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2186 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002187 if (err < 0)
2188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2191 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002192 if (err < 0)
2193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2196 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002197 if (err < 0)
2198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2201 snd_pcm_hw_rule_buffer_bytes_max, substream,
2202 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2203 if (err < 0)
2204 return err;
2205
2206 /* FIXME: remove */
2207 if (runtime->dma_bytes) {
2208 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002209 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302210 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212
2213 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2214 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2215 snd_pcm_hw_rule_rate, hw,
2216 SNDRV_PCM_HW_PARAM_RATE, -1);
2217 if (err < 0)
2218 return err;
2219 }
2220
2221 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2223
2224 return 0;
2225}
2226
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002227static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002230}
2231
2232void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2233{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002234 substream->ref_count--;
2235 if (substream->ref_count > 0)
2236 return;
2237
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002238 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002239 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002240 if (substream->ops->hw_free &&
2241 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 substream->ops->hw_free(substream);
2243 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002244 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002246 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2247 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002248 if (substream->pcm_release) {
2249 substream->pcm_release(substream);
2250 substream->pcm_release = NULL;
2251 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002252 snd_pcm_detach_substream(substream);
2253}
2254
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002255EXPORT_SYMBOL(snd_pcm_release_substream);
2256
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002257int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2258 struct file *file,
2259 struct snd_pcm_substream **rsubstream)
2260{
2261 struct snd_pcm_substream *substream;
2262 int err;
2263
2264 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2265 if (err < 0)
2266 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002267 if (substream->ref_count > 1) {
2268 *rsubstream = substream;
2269 return 0;
2270 }
2271
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002272 err = snd_pcm_hw_constraints_init(substream);
2273 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002274 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002275 goto error;
2276 }
2277
2278 if ((err = substream->ops->open(substream)) < 0)
2279 goto error;
2280
2281 substream->hw_opened = 1;
2282
2283 err = snd_pcm_hw_constraints_complete(substream);
2284 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002285 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002286 goto error;
2287 }
2288
2289 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002291
2292 error:
2293 snd_pcm_release_substream(substream);
2294 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002297EXPORT_SYMBOL(snd_pcm_open_substream);
2298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002300 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002301 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Takashi Iwai877211f2005-11-17 13:59:38 +01002303 struct snd_pcm_file *pcm_file;
2304 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002305 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002307 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2308 if (err < 0)
2309 return err;
2310
Takashi Iwai548a6482006-07-31 16:51:51 +02002311 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2312 if (pcm_file == NULL) {
2313 snd_pcm_release_substream(substream);
2314 return -ENOMEM;
2315 }
2316 pcm_file->substream = substream;
2317 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002318 substream->file = pcm_file;
2319 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return 0;
2324}
2325
Clemens Ladischf87135f2005-11-20 14:06:59 +01002326static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Takashi Iwai877211f2005-11-17 13:59:38 +01002328 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002329 int err = nonseekable_open(inode, file);
2330 if (err < 0)
2331 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002332 pcm = snd_lookup_minor_data(iminor(inode),
2333 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002334 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002335 if (pcm)
2336 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002337 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002338}
2339
2340static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2341{
2342 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002343 int err = nonseekable_open(inode, file);
2344 if (err < 0)
2345 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002346 pcm = snd_lookup_minor_data(iminor(inode),
2347 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002348 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002349 if (pcm)
2350 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002351 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002352}
2353
2354static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2355{
2356 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 wait_queue_t wait;
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (pcm == NULL) {
2360 err = -ENODEV;
2361 goto __error1;
2362 }
2363 err = snd_card_file_add(pcm->card, file);
2364 if (err < 0)
2365 goto __error1;
2366 if (!try_module_get(pcm->card->module)) {
2367 err = -EFAULT;
2368 goto __error2;
2369 }
2370 init_waitqueue_entry(&wait, current);
2371 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002372 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002374 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (err >= 0)
2376 break;
2377 if (err == -EAGAIN) {
2378 if (file->f_flags & O_NONBLOCK) {
2379 err = -EBUSY;
2380 break;
2381 }
2382 } else
2383 break;
2384 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002385 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002387 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002388 if (pcm->card->shutdown) {
2389 err = -ENODEV;
2390 break;
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (signal_pending(current)) {
2393 err = -ERESTARTSYS;
2394 break;
2395 }
2396 }
2397 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002398 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (err < 0)
2400 goto __error;
2401 return err;
2402
2403 __error:
2404 module_put(pcm->card->module);
2405 __error2:
2406 snd_card_file_remove(pcm->card, file);
2407 __error1:
2408 return err;
2409}
2410
2411static int snd_pcm_release(struct inode *inode, struct file *file)
2412{
Takashi Iwai877211f2005-11-17 13:59:38 +01002413 struct snd_pcm *pcm;
2414 struct snd_pcm_substream *substream;
2415 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 pcm_file = file->private_data;
2418 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002419 if (snd_BUG_ON(!substream))
2420 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002422 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002423 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002424 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002425 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 wake_up(&pcm->open_wait);
2427 module_put(pcm->card->module);
2428 snd_card_file_remove(pcm->card, file);
2429 return 0;
2430}
2431
Takashi Iwai877211f2005-11-17 13:59:38 +01002432static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2433 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Takashi Iwai877211f2005-11-17 13:59:38 +01002435 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 snd_pcm_sframes_t appl_ptr;
2437 snd_pcm_sframes_t ret;
2438 snd_pcm_sframes_t hw_avail;
2439
2440 if (frames == 0)
2441 return 0;
2442
2443 snd_pcm_stream_lock_irq(substream);
2444 switch (runtime->status->state) {
2445 case SNDRV_PCM_STATE_PREPARED:
2446 break;
2447 case SNDRV_PCM_STATE_DRAINING:
2448 case SNDRV_PCM_STATE_RUNNING:
2449 if (snd_pcm_update_hw_ptr(substream) >= 0)
2450 break;
2451 /* Fall through */
2452 case SNDRV_PCM_STATE_XRUN:
2453 ret = -EPIPE;
2454 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002455 case SNDRV_PCM_STATE_SUSPENDED:
2456 ret = -ESTRPIPE;
2457 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 default:
2459 ret = -EBADFD;
2460 goto __end;
2461 }
2462
2463 hw_avail = snd_pcm_playback_hw_avail(runtime);
2464 if (hw_avail <= 0) {
2465 ret = 0;
2466 goto __end;
2467 }
2468 if (frames > (snd_pcm_uframes_t)hw_avail)
2469 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 appl_ptr = runtime->control->appl_ptr - frames;
2471 if (appl_ptr < 0)
2472 appl_ptr += runtime->boundary;
2473 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 ret = frames;
2475 __end:
2476 snd_pcm_stream_unlock_irq(substream);
2477 return ret;
2478}
2479
Takashi Iwai877211f2005-11-17 13:59:38 +01002480static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2481 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
Takashi Iwai877211f2005-11-17 13:59:38 +01002483 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 snd_pcm_sframes_t appl_ptr;
2485 snd_pcm_sframes_t ret;
2486 snd_pcm_sframes_t hw_avail;
2487
2488 if (frames == 0)
2489 return 0;
2490
2491 snd_pcm_stream_lock_irq(substream);
2492 switch (runtime->status->state) {
2493 case SNDRV_PCM_STATE_PREPARED:
2494 case SNDRV_PCM_STATE_DRAINING:
2495 break;
2496 case SNDRV_PCM_STATE_RUNNING:
2497 if (snd_pcm_update_hw_ptr(substream) >= 0)
2498 break;
2499 /* Fall through */
2500 case SNDRV_PCM_STATE_XRUN:
2501 ret = -EPIPE;
2502 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002503 case SNDRV_PCM_STATE_SUSPENDED:
2504 ret = -ESTRPIPE;
2505 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 default:
2507 ret = -EBADFD;
2508 goto __end;
2509 }
2510
2511 hw_avail = snd_pcm_capture_hw_avail(runtime);
2512 if (hw_avail <= 0) {
2513 ret = 0;
2514 goto __end;
2515 }
2516 if (frames > (snd_pcm_uframes_t)hw_avail)
2517 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 appl_ptr = runtime->control->appl_ptr - frames;
2519 if (appl_ptr < 0)
2520 appl_ptr += runtime->boundary;
2521 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 ret = frames;
2523 __end:
2524 snd_pcm_stream_unlock_irq(substream);
2525 return ret;
2526}
2527
Takashi Iwai877211f2005-11-17 13:59:38 +01002528static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2529 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Takashi Iwai877211f2005-11-17 13:59:38 +01002531 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 snd_pcm_sframes_t appl_ptr;
2533 snd_pcm_sframes_t ret;
2534 snd_pcm_sframes_t avail;
2535
2536 if (frames == 0)
2537 return 0;
2538
2539 snd_pcm_stream_lock_irq(substream);
2540 switch (runtime->status->state) {
2541 case SNDRV_PCM_STATE_PREPARED:
2542 case SNDRV_PCM_STATE_PAUSED:
2543 break;
2544 case SNDRV_PCM_STATE_DRAINING:
2545 case SNDRV_PCM_STATE_RUNNING:
2546 if (snd_pcm_update_hw_ptr(substream) >= 0)
2547 break;
2548 /* Fall through */
2549 case SNDRV_PCM_STATE_XRUN:
2550 ret = -EPIPE;
2551 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002552 case SNDRV_PCM_STATE_SUSPENDED:
2553 ret = -ESTRPIPE;
2554 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 default:
2556 ret = -EBADFD;
2557 goto __end;
2558 }
2559
2560 avail = snd_pcm_playback_avail(runtime);
2561 if (avail <= 0) {
2562 ret = 0;
2563 goto __end;
2564 }
2565 if (frames > (snd_pcm_uframes_t)avail)
2566 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 appl_ptr = runtime->control->appl_ptr + frames;
2568 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2569 appl_ptr -= runtime->boundary;
2570 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 ret = frames;
2572 __end:
2573 snd_pcm_stream_unlock_irq(substream);
2574 return ret;
2575}
2576
Takashi Iwai877211f2005-11-17 13:59:38 +01002577static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2578 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Takashi Iwai877211f2005-11-17 13:59:38 +01002580 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 snd_pcm_sframes_t appl_ptr;
2582 snd_pcm_sframes_t ret;
2583 snd_pcm_sframes_t avail;
2584
2585 if (frames == 0)
2586 return 0;
2587
2588 snd_pcm_stream_lock_irq(substream);
2589 switch (runtime->status->state) {
2590 case SNDRV_PCM_STATE_PREPARED:
2591 case SNDRV_PCM_STATE_DRAINING:
2592 case SNDRV_PCM_STATE_PAUSED:
2593 break;
2594 case SNDRV_PCM_STATE_RUNNING:
2595 if (snd_pcm_update_hw_ptr(substream) >= 0)
2596 break;
2597 /* Fall through */
2598 case SNDRV_PCM_STATE_XRUN:
2599 ret = -EPIPE;
2600 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002601 case SNDRV_PCM_STATE_SUSPENDED:
2602 ret = -ESTRPIPE;
2603 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 default:
2605 ret = -EBADFD;
2606 goto __end;
2607 }
2608
2609 avail = snd_pcm_capture_avail(runtime);
2610 if (avail <= 0) {
2611 ret = 0;
2612 goto __end;
2613 }
2614 if (frames > (snd_pcm_uframes_t)avail)
2615 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 appl_ptr = runtime->control->appl_ptr + frames;
2617 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2618 appl_ptr -= runtime->boundary;
2619 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 ret = frames;
2621 __end:
2622 snd_pcm_stream_unlock_irq(substream);
2623 return ret;
2624}
2625
Takashi Iwai877211f2005-11-17 13:59:38 +01002626static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Takashi Iwai877211f2005-11-17 13:59:38 +01002628 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 int err;
2630
2631 snd_pcm_stream_lock_irq(substream);
2632 switch (runtime->status->state) {
2633 case SNDRV_PCM_STATE_DRAINING:
2634 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2635 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002636 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 case SNDRV_PCM_STATE_RUNNING:
2638 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2639 break;
2640 /* Fall through */
2641 case SNDRV_PCM_STATE_PREPARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 err = 0;
2643 break;
Jeeja KPf3f6c612016-09-02 21:49:44 +05302644 case SNDRV_PCM_STATE_SUSPENDED:
2645 err = -ESTRPIPE;
2646 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 case SNDRV_PCM_STATE_XRUN:
2648 err = -EPIPE;
2649 break;
2650 default:
2651 __badfd:
2652 err = -EBADFD;
2653 break;
2654 }
2655 snd_pcm_stream_unlock_irq(substream);
2656 return err;
2657}
2658
Takashi Iwai877211f2005-11-17 13:59:38 +01002659static int snd_pcm_delay(struct snd_pcm_substream *substream,
2660 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Takashi Iwai877211f2005-11-17 13:59:38 +01002662 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 int err;
2664 snd_pcm_sframes_t n = 0;
2665
2666 snd_pcm_stream_lock_irq(substream);
2667 switch (runtime->status->state) {
2668 case SNDRV_PCM_STATE_DRAINING:
2669 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2670 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002671 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 case SNDRV_PCM_STATE_RUNNING:
2673 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2674 break;
2675 /* Fall through */
2676 case SNDRV_PCM_STATE_PREPARED:
2677 case SNDRV_PCM_STATE_SUSPENDED:
2678 err = 0;
2679 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2680 n = snd_pcm_playback_hw_avail(runtime);
2681 else
2682 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002683 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 break;
2685 case SNDRV_PCM_STATE_XRUN:
2686 err = -EPIPE;
2687 break;
2688 default:
2689 __badfd:
2690 err = -EBADFD;
2691 break;
2692 }
2693 snd_pcm_stream_unlock_irq(substream);
2694 if (!err)
2695 if (put_user(n, res))
2696 err = -EFAULT;
2697 return err;
2698}
2699
Takashi Iwai877211f2005-11-17 13:59:38 +01002700static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2701 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Takashi Iwai877211f2005-11-17 13:59:38 +01002703 struct snd_pcm_runtime *runtime = substream->runtime;
2704 struct snd_pcm_sync_ptr sync_ptr;
2705 volatile struct snd_pcm_mmap_status *status;
2706 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 int err;
Santosh Mardie68908a2012-10-09 10:32:42 +05302708 snd_pcm_uframes_t hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 memset(&sync_ptr, 0, sizeof(sync_ptr));
2711 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2712 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002713 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 -07002714 return -EFAULT;
2715 status = runtime->status;
2716 control = runtime->control;
2717 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2718 err = snd_pcm_hwsync(substream);
2719 if (err < 0)
2720 return err;
2721 }
2722 snd_pcm_stream_lock_irq(substream);
2723 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2724 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2725 else
2726 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2727 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2728 control->avail_min = sync_ptr.c.control.avail_min;
2729 else
2730 sync_ptr.c.control.avail_min = control->avail_min;
Santosh Mardie68908a2012-10-09 10:32:42 +05302731
2732 if (runtime->render_flag & SNDRV_NON_DMA_MODE) {
2733 hw_avail = snd_pcm_playback_hw_avail(runtime);
2734 if ((hw_avail >= runtime->start_threshold)
2735 && (runtime->render_flag &
2736 SNDRV_RENDER_STOPPED)) {
2737 if (substream->ops->restart)
2738 substream->ops->restart(substream);
2739 }
2740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 sync_ptr.s.status.state = status->state;
2742 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2743 sync_ptr.s.status.tstamp = status->tstamp;
2744 sync_ptr.s.status.suspended_state = status->suspended_state;
2745 snd_pcm_stream_unlock_irq(substream);
2746 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2747 return -EFAULT;
2748 return 0;
2749}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002750
2751static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2752{
2753 struct snd_pcm_runtime *runtime = substream->runtime;
2754 int arg;
2755
2756 if (get_user(arg, _arg))
2757 return -EFAULT;
2758 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2759 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002760 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002761 return 0;
2762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Takashi Iwai0df63e42006-04-28 15:13:41 +02002764static int snd_pcm_common_ioctl1(struct file *file,
2765 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 unsigned int cmd, void __user *arg)
2767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 switch (cmd) {
2769 case SNDRV_PCM_IOCTL_PVERSION:
2770 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2771 case SNDRV_PCM_IOCTL_INFO:
2772 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002773 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2774 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002775 case SNDRV_PCM_IOCTL_TTSTAMP:
2776 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 case SNDRV_PCM_IOCTL_HW_REFINE:
2778 return snd_pcm_hw_refine_user(substream, arg);
2779 case SNDRV_PCM_IOCTL_HW_PARAMS:
2780 return snd_pcm_hw_params_user(substream, arg);
2781 case SNDRV_PCM_IOCTL_HW_FREE:
2782 return snd_pcm_hw_free(substream);
2783 case SNDRV_PCM_IOCTL_SW_PARAMS:
2784 return snd_pcm_sw_params_user(substream, arg);
2785 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002786 return snd_pcm_status_user(substream, arg, false);
2787 case SNDRV_PCM_IOCTL_STATUS_EXT:
2788 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2790 return snd_pcm_channel_info_user(substream, arg);
2791 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002792 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 case SNDRV_PCM_IOCTL_RESET:
2794 return snd_pcm_reset(substream);
2795 case SNDRV_PCM_IOCTL_START:
2796 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2797 case SNDRV_PCM_IOCTL_LINK:
2798 return snd_pcm_link(substream, (int)(unsigned long) arg);
2799 case SNDRV_PCM_IOCTL_UNLINK:
2800 return snd_pcm_unlink(substream);
2801 case SNDRV_PCM_IOCTL_RESUME:
2802 return snd_pcm_resume(substream);
2803 case SNDRV_PCM_IOCTL_XRUN:
2804 return snd_pcm_xrun(substream);
2805 case SNDRV_PCM_IOCTL_HWSYNC:
2806 return snd_pcm_hwsync(substream);
2807 case SNDRV_PCM_IOCTL_DELAY:
2808 return snd_pcm_delay(substream, arg);
2809 case SNDRV_PCM_IOCTL_SYNC_PTR:
2810 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002811#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2813 return snd_pcm_hw_refine_old_user(substream, arg);
2814 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2815 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002818 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 case SNDRV_PCM_IOCTL_DROP:
2820 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002821 case SNDRV_PCM_IOCTL_PAUSE:
2822 {
2823 int res;
2824 snd_pcm_stream_lock_irq(substream);
2825 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2826 snd_pcm_stream_unlock_irq(substream);
2827 return res;
2828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002830 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return -ENOTTY;
2832}
2833
Takashi Iwai0df63e42006-04-28 15:13:41 +02002834static int snd_pcm_playback_ioctl1(struct file *file,
2835 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 unsigned int cmd, void __user *arg)
2837{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002838 if (snd_BUG_ON(!substream))
2839 return -ENXIO;
2840 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 switch (cmd) {
2843 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2844 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002845 struct snd_xferi xferi;
2846 struct snd_xferi __user *_xferi = arg;
2847 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 snd_pcm_sframes_t result;
2849 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2850 return -EBADFD;
2851 if (put_user(0, &_xferi->result))
2852 return -EFAULT;
2853 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2854 return -EFAULT;
2855 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2856 __put_user(result, &_xferi->result);
2857 return result < 0 ? result : 0;
2858 }
2859 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2860 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002861 struct snd_xfern xfern;
2862 struct snd_xfern __user *_xfern = arg;
2863 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 void __user **bufs;
2865 snd_pcm_sframes_t result;
2866 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2867 return -EBADFD;
2868 if (runtime->channels > 128)
2869 return -EINVAL;
2870 if (put_user(0, &_xfern->result))
2871 return -EFAULT;
2872 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2873 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002874
2875 bufs = memdup_user(xfern.bufs,
2876 sizeof(void *) * runtime->channels);
2877 if (IS_ERR(bufs))
2878 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2880 kfree(bufs);
2881 __put_user(result, &_xfern->result);
2882 return result < 0 ? result : 0;
2883 }
2884 case SNDRV_PCM_IOCTL_REWIND:
2885 {
2886 snd_pcm_uframes_t frames;
2887 snd_pcm_uframes_t __user *_frames = arg;
2888 snd_pcm_sframes_t result;
2889 if (get_user(frames, _frames))
2890 return -EFAULT;
2891 if (put_user(0, _frames))
2892 return -EFAULT;
2893 result = snd_pcm_playback_rewind(substream, frames);
2894 __put_user(result, _frames);
2895 return result < 0 ? result : 0;
2896 }
2897 case SNDRV_PCM_IOCTL_FORWARD:
2898 {
2899 snd_pcm_uframes_t frames;
2900 snd_pcm_uframes_t __user *_frames = arg;
2901 snd_pcm_sframes_t result;
2902 if (get_user(frames, _frames))
2903 return -EFAULT;
2904 if (put_user(0, _frames))
2905 return -EFAULT;
2906 result = snd_pcm_playback_forward(substream, frames);
2907 __put_user(result, _frames);
2908 return result < 0 ? result : 0;
2909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002911 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
Takashi Iwai0df63e42006-04-28 15:13:41 +02002914static int snd_pcm_capture_ioctl1(struct file *file,
2915 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 unsigned int cmd, void __user *arg)
2917{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002918 if (snd_BUG_ON(!substream))
2919 return -ENXIO;
2920 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2921 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 switch (cmd) {
2923 case SNDRV_PCM_IOCTL_READI_FRAMES:
2924 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002925 struct snd_xferi xferi;
2926 struct snd_xferi __user *_xferi = arg;
2927 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 snd_pcm_sframes_t result;
2929 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2930 return -EBADFD;
2931 if (put_user(0, &_xferi->result))
2932 return -EFAULT;
2933 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2934 return -EFAULT;
2935 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2936 __put_user(result, &_xferi->result);
2937 return result < 0 ? result : 0;
2938 }
2939 case SNDRV_PCM_IOCTL_READN_FRAMES:
2940 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002941 struct snd_xfern xfern;
2942 struct snd_xfern __user *_xfern = arg;
2943 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 void *bufs;
2945 snd_pcm_sframes_t result;
2946 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2947 return -EBADFD;
2948 if (runtime->channels > 128)
2949 return -EINVAL;
2950 if (put_user(0, &_xfern->result))
2951 return -EFAULT;
2952 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2953 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002954
2955 bufs = memdup_user(xfern.bufs,
2956 sizeof(void *) * runtime->channels);
2957 if (IS_ERR(bufs))
2958 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2960 kfree(bufs);
2961 __put_user(result, &_xfern->result);
2962 return result < 0 ? result : 0;
2963 }
2964 case SNDRV_PCM_IOCTL_REWIND:
2965 {
2966 snd_pcm_uframes_t frames;
2967 snd_pcm_uframes_t __user *_frames = arg;
2968 snd_pcm_sframes_t result;
2969 if (get_user(frames, _frames))
2970 return -EFAULT;
2971 if (put_user(0, _frames))
2972 return -EFAULT;
2973 result = snd_pcm_capture_rewind(substream, frames);
2974 __put_user(result, _frames);
2975 return result < 0 ? result : 0;
2976 }
2977 case SNDRV_PCM_IOCTL_FORWARD:
2978 {
2979 snd_pcm_uframes_t frames;
2980 snd_pcm_uframes_t __user *_frames = arg;
2981 snd_pcm_sframes_t result;
2982 if (get_user(frames, _frames))
2983 return -EFAULT;
2984 if (put_user(0, _frames))
2985 return -EFAULT;
2986 result = snd_pcm_capture_forward(substream, frames);
2987 __put_user(result, _frames);
2988 return result < 0 ? result : 0;
2989 }
2990 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002991 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
2993
Takashi Iwai877211f2005-11-17 13:59:38 +01002994static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2995 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Takashi Iwai877211f2005-11-17 13:59:38 +01002997 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 pcm_file = file->private_data;
3000
3001 if (((cmd >> 8) & 0xff) != 'A')
3002 return -ENOTTY;
3003
Takashi Iwai0df63e42006-04-28 15:13:41 +02003004 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3005 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Takashi Iwai877211f2005-11-17 13:59:38 +01003008static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3009 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
Takashi Iwai877211f2005-11-17 13:59:38 +01003011 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 pcm_file = file->private_data;
3014
3015 if (((cmd >> 8) & 0xff) != 'A')
3016 return -ENOTTY;
3017
Takashi Iwai0df63e42006-04-28 15:13:41 +02003018 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3019 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
Takashi Iwai877211f2005-11-17 13:59:38 +01003022int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 unsigned int cmd, void *arg)
3024{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003025 mm_segment_t fs;
3026 int result;
3027
3028 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 switch (substream->stream) {
3030 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003031 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3032 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003035 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3036 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003039 result = -EINVAL;
3040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003042 snd_leave_user(fs);
3043 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003046EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3047
Takashi Iwai877211f2005-11-17 13:59:38 +01003048static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3049 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Takashi Iwai877211f2005-11-17 13:59:38 +01003051 struct snd_pcm_file *pcm_file;
3052 struct snd_pcm_substream *substream;
3053 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 snd_pcm_sframes_t result;
3055
3056 pcm_file = file->private_data;
3057 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003058 if (PCM_RUNTIME_CHECK(substream))
3059 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 runtime = substream->runtime;
3061 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3062 return -EBADFD;
3063 if (!frame_aligned(runtime, count))
3064 return -EINVAL;
3065 count = bytes_to_frames(runtime, count);
3066 result = snd_pcm_lib_read(substream, buf, count);
3067 if (result > 0)
3068 result = frames_to_bytes(runtime, result);
3069 return result;
3070}
3071
Takashi Iwai877211f2005-11-17 13:59:38 +01003072static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3073 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Takashi Iwai877211f2005-11-17 13:59:38 +01003075 struct snd_pcm_file *pcm_file;
3076 struct snd_pcm_substream *substream;
3077 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 snd_pcm_sframes_t result;
3079
3080 pcm_file = file->private_data;
3081 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003082 if (PCM_RUNTIME_CHECK(substream))
3083 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003085 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3086 return -EBADFD;
3087 if (!frame_aligned(runtime, count))
3088 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 count = bytes_to_frames(runtime, count);
3090 result = snd_pcm_lib_write(substream, buf, count);
3091 if (result > 0)
3092 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return result;
3094}
3095
Al Viro1c65d982015-04-04 00:19:32 -04003096static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Takashi Iwai877211f2005-11-17 13:59:38 +01003098 struct snd_pcm_file *pcm_file;
3099 struct snd_pcm_substream *substream;
3100 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 snd_pcm_sframes_t result;
3102 unsigned long i;
3103 void __user **bufs;
3104 snd_pcm_uframes_t frames;
3105
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003106 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003108 if (PCM_RUNTIME_CHECK(substream))
3109 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 runtime = substream->runtime;
3111 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3112 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003113 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003115 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003117 if (!frame_aligned(runtime, to->iov->iov_len))
3118 return -EINVAL;
3119 frames = bytes_to_samples(runtime, to->iov->iov_len);
3120 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (bufs == NULL)
3122 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003123 for (i = 0; i < to->nr_segs; ++i)
3124 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 result = snd_pcm_lib_readv(substream, bufs, frames);
3126 if (result > 0)
3127 result = frames_to_bytes(runtime, result);
3128 kfree(bufs);
3129 return result;
3130}
3131
Al Viro1c65d982015-04-04 00:19:32 -04003132static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Takashi Iwai877211f2005-11-17 13:59:38 +01003134 struct snd_pcm_file *pcm_file;
3135 struct snd_pcm_substream *substream;
3136 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 snd_pcm_sframes_t result;
3138 unsigned long i;
3139 void __user **bufs;
3140 snd_pcm_uframes_t frames;
3141
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003142 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003144 if (PCM_RUNTIME_CHECK(substream))
3145 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003147 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3148 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003149 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003150 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003151 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3152 !frame_aligned(runtime, from->iov->iov_len))
3153 return -EINVAL;
3154 frames = bytes_to_samples(runtime, from->iov->iov_len);
3155 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (bufs == NULL)
3157 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003158 for (i = 0; i < from->nr_segs; ++i)
3159 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 result = snd_pcm_lib_writev(substream, bufs, frames);
3161 if (result > 0)
3162 result = frames_to_bytes(runtime, result);
3163 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return result;
3165}
3166
3167static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3168{
Takashi Iwai877211f2005-11-17 13:59:38 +01003169 struct snd_pcm_file *pcm_file;
3170 struct snd_pcm_substream *substream;
3171 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 unsigned int mask;
3173 snd_pcm_uframes_t avail;
3174
3175 pcm_file = file->private_data;
3176
3177 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003178 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003179 return POLLOUT | POLLWRNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 runtime = substream->runtime;
3181
3182 poll_wait(file, &runtime->sleep, wait);
3183
3184 snd_pcm_stream_lock_irq(substream);
3185 avail = snd_pcm_playback_avail(runtime);
3186 switch (runtime->status->state) {
3187 case SNDRV_PCM_STATE_RUNNING:
3188 case SNDRV_PCM_STATE_PREPARED:
3189 case SNDRV_PCM_STATE_PAUSED:
3190 if (avail >= runtime->control->avail_min) {
3191 mask = POLLOUT | POLLWRNORM;
3192 break;
3193 }
3194 /* Fall through */
3195 case SNDRV_PCM_STATE_DRAINING:
3196 mask = 0;
3197 break;
3198 default:
3199 mask = POLLOUT | POLLWRNORM | POLLERR;
3200 break;
3201 }
3202 snd_pcm_stream_unlock_irq(substream);
3203 return mask;
3204}
3205
3206static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3207{
Takashi Iwai877211f2005-11-17 13:59:38 +01003208 struct snd_pcm_file *pcm_file;
3209 struct snd_pcm_substream *substream;
3210 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 unsigned int mask;
3212 snd_pcm_uframes_t avail;
3213
3214 pcm_file = file->private_data;
3215
3216 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003217 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003218 return POLLIN | POLLRDNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 runtime = substream->runtime;
3220
3221 poll_wait(file, &runtime->sleep, wait);
3222
3223 snd_pcm_stream_lock_irq(substream);
3224 avail = snd_pcm_capture_avail(runtime);
3225 switch (runtime->status->state) {
3226 case SNDRV_PCM_STATE_RUNNING:
3227 case SNDRV_PCM_STATE_PREPARED:
3228 case SNDRV_PCM_STATE_PAUSED:
3229 if (avail >= runtime->control->avail_min) {
3230 mask = POLLIN | POLLRDNORM;
3231 break;
3232 }
3233 mask = 0;
3234 break;
3235 case SNDRV_PCM_STATE_DRAINING:
3236 if (avail > 0) {
3237 mask = POLLIN | POLLRDNORM;
3238 break;
3239 }
3240 /* Fall through */
3241 default:
3242 mask = POLLIN | POLLRDNORM | POLLERR;
3243 break;
3244 }
3245 snd_pcm_stream_unlock_irq(substream);
3246 return mask;
3247}
3248
3249/*
3250 * mmap support
3251 */
3252
3253/*
3254 * Only on coherent architectures, we can mmap the status and the control records
3255 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3256 */
3257#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3258/*
3259 * mmap status record
3260 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003261static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3262 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Takashi Iwai877211f2005-11-17 13:59:38 +01003264 struct snd_pcm_substream *substream = area->vm_private_data;
3265 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
3267 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003268 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003270 vmf->page = virt_to_page(runtime->status);
3271 get_page(vmf->page);
3272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003275static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003277 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278};
3279
Takashi Iwai877211f2005-11-17 13:59:38 +01003280static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 struct vm_area_struct *area)
3282{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 long size;
3284 if (!(area->vm_flags & VM_READ))
3285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003287 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return -EINVAL;
3289 area->vm_ops = &snd_pcm_vm_ops_status;
3290 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003291 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return 0;
3293}
3294
3295/*
3296 * mmap control record
3297 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003298static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3299 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Takashi Iwai877211f2005-11-17 13:59:38 +01003301 struct snd_pcm_substream *substream = area->vm_private_data;
3302 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
3304 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003305 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003307 vmf->page = virt_to_page(runtime->control);
3308 get_page(vmf->page);
3309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003312static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003314 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315};
3316
Takashi Iwai877211f2005-11-17 13:59:38 +01003317static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 struct vm_area_struct *area)
3319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 long size;
3321 if (!(area->vm_flags & VM_READ))
3322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003324 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return -EINVAL;
3326 area->vm_ops = &snd_pcm_vm_ops_control;
3327 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003328 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return 0;
3330}
3331#else /* ! coherent mmap */
3332/*
3333 * don't support mmap for status and control records.
3334 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003335static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 struct vm_area_struct *area)
3337{
3338 return -ENXIO;
3339}
Takashi Iwai877211f2005-11-17 13:59:38 +01003340static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 struct vm_area_struct *area)
3342{
3343 return -ENXIO;
3344}
3345#endif /* coherent mmap */
3346
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003347static inline struct page *
3348snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3349{
3350 void *vaddr = substream->runtime->dma_area + ofs;
3351 return virt_to_page(vaddr);
3352}
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003355 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003357static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3358 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
Takashi Iwai877211f2005-11-17 13:59:38 +01003360 struct snd_pcm_substream *substream = area->vm_private_data;
3361 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 unsigned long offset;
3363 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 size_t dma_bytes;
3365
3366 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003367 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003369 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3371 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003372 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003373 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003375 else
3376 page = snd_pcm_default_page_ops(substream, offset);
3377 if (!page)
3378 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003379 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003380 vmf->page = page;
3381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382}
3383
Takashi Iwai657b1982009-11-26 12:40:21 +01003384static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3385 .open = snd_pcm_mmap_data_open,
3386 .close = snd_pcm_mmap_data_close,
3387};
3388
3389static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 .open = snd_pcm_mmap_data_open,
3391 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003392 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393};
3394
3395/*
3396 * mmap the DMA buffer on RAM
3397 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003398
3399/**
3400 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3401 * @substream: PCM substream
3402 * @area: VMA
3403 *
3404 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3405 * this function is invoked implicitly.
3406 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003407int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3408 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003410 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003411#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003412 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3413 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3414 return remap_pfn_range(area, area->vm_start,
3415 substream->dma_buffer.addr >> PAGE_SHIFT,
3416 area->vm_end - area->vm_start, area->vm_page_prot);
3417 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003418#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003419#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003420 if (!substream->ops->page &&
3421 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3422 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3423 area,
3424 substream->runtime->dma_area,
3425 substream->runtime->dma_addr,
3426 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003427#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003428 /* mmap with fault handler */
3429 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return 0;
3431}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003432EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
3434/*
3435 * mmap the DMA buffer on I/O memory area
3436 */
3437#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003438/**
3439 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3440 * @substream: PCM substream
3441 * @area: VMA
3442 *
3443 * When your hardware uses the iomapped pages as the hardware buffer and
3444 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3445 * is supposed to work only on limited architectures.
3446 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003447int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3448 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003450 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003453 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003455
3456EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457#endif /* SNDRV_PCM_INFO_MMAP */
3458
3459/*
3460 * mmap DMA buffer
3461 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003462int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 struct vm_area_struct *area)
3464{
Takashi Iwai877211f2005-11-17 13:59:38 +01003465 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 long size;
3467 unsigned long offset;
3468 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003469 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3472 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3473 return -EINVAL;
3474 } else {
3475 if (!(area->vm_flags & VM_READ))
3476 return -EINVAL;
3477 }
3478 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3480 return -EBADFD;
3481 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3482 return -ENXIO;
3483 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3484 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3485 return -EINVAL;
3486 size = area->vm_end - area->vm_start;
3487 offset = area->vm_pgoff << PAGE_SHIFT;
3488 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3489 if ((size_t)size > dma_bytes)
3490 return -EINVAL;
3491 if (offset > dma_bytes - size)
3492 return -EINVAL;
3493
Takashi Iwai657b1982009-11-26 12:40:21 +01003494 area->vm_ops = &snd_pcm_vm_ops_data;
3495 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003497 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003499 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003500 if (!err)
3501 atomic_inc(&substream->mmap_count);
3502 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003505EXPORT_SYMBOL(snd_pcm_mmap_data);
3506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3508{
Takashi Iwai877211f2005-11-17 13:59:38 +01003509 struct snd_pcm_file * pcm_file;
3510 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 unsigned long offset;
3512
3513 pcm_file = file->private_data;
3514 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003515 if (PCM_RUNTIME_CHECK(substream))
3516 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
3518 offset = area->vm_pgoff << PAGE_SHIFT;
3519 switch (offset) {
3520 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003521 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return -ENXIO;
3523 return snd_pcm_mmap_status(substream, file, area);
3524 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003525 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 return -ENXIO;
3527 return snd_pcm_mmap_control(substream, file, area);
3528 default:
3529 return snd_pcm_mmap_data(substream, file, area);
3530 }
3531 return 0;
3532}
3533
3534static int snd_pcm_fasync(int fd, struct file * file, int on)
3535{
Takashi Iwai877211f2005-11-17 13:59:38 +01003536 struct snd_pcm_file * pcm_file;
3537 struct snd_pcm_substream *substream;
3538 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540 pcm_file = file->private_data;
3541 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003542 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003543 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003545 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
3548/*
3549 * ioctl32 compat
3550 */
3551#ifdef CONFIG_COMPAT
3552#include "pcm_compat.c"
3553#else
3554#define snd_pcm_ioctl_compat NULL
3555#endif
3556
3557/*
3558 * To be removed helpers to keep binary compatibility
3559 */
3560
Takashi Iwai59d48582005-12-01 10:51:58 +01003561#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3563#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3564
Takashi Iwai877211f2005-11-17 13:59:38 +01003565static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3566 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 unsigned int i;
3569
3570 memset(params, 0, sizeof(*params));
3571 params->flags = oparams->flags;
3572 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3573 params->masks[i].bits[0] = oparams->masks[i];
3574 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3575 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3576 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3577 params->info = oparams->info;
3578 params->msbits = oparams->msbits;
3579 params->rate_num = oparams->rate_num;
3580 params->rate_den = oparams->rate_den;
3581 params->fifo_size = oparams->fifo_size;
3582}
3583
Takashi Iwai877211f2005-11-17 13:59:38 +01003584static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3585 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
3587 unsigned int i;
3588
3589 memset(oparams, 0, sizeof(*oparams));
3590 oparams->flags = params->flags;
3591 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3592 oparams->masks[i] = params->masks[i].bits[0];
3593 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3594 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3595 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3596 oparams->info = params->info;
3597 oparams->msbits = params->msbits;
3598 oparams->rate_num = params->rate_num;
3599 oparams->rate_den = params->rate_den;
3600 oparams->fifo_size = params->fifo_size;
3601}
3602
Takashi Iwai877211f2005-11-17 13:59:38 +01003603static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3604 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Takashi Iwai877211f2005-11-17 13:59:38 +01003606 struct snd_pcm_hw_params *params;
3607 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 int err;
3609
3610 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003611 if (!params)
3612 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Li Zefanef44a1e2009-04-10 09:43:08 +08003614 oparams = memdup_user(_oparams, sizeof(*oparams));
3615 if (IS_ERR(oparams)) {
3616 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 goto out;
3618 }
3619 snd_pcm_hw_convert_from_old_params(params, oparams);
3620 err = snd_pcm_hw_refine(substream, params);
3621 snd_pcm_hw_convert_to_old_params(oparams, params);
3622 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3623 if (!err)
3624 err = -EFAULT;
3625 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003626
3627 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628out:
3629 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return err;
3631}
3632
Takashi Iwai877211f2005-11-17 13:59:38 +01003633static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3634 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
Takashi Iwai877211f2005-11-17 13:59:38 +01003636 struct snd_pcm_hw_params *params;
3637 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 int err;
3639
3640 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003641 if (!params)
3642 return -ENOMEM;
3643
3644 oparams = memdup_user(_oparams, sizeof(*oparams));
3645 if (IS_ERR(oparams)) {
3646 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 goto out;
3648 }
3649 snd_pcm_hw_convert_from_old_params(params, oparams);
3650 err = snd_pcm_hw_params(substream, params);
3651 snd_pcm_hw_convert_to_old_params(oparams, params);
3652 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3653 if (!err)
3654 err = -EFAULT;
3655 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003656
3657 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658out:
3659 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return err;
3661}
Takashi Iwai59d48582005-12-01 10:51:58 +01003662#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
Cliff Cai70036092008-09-03 10:54:36 +02003664#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003665static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3666 unsigned long addr,
3667 unsigned long len,
3668 unsigned long pgoff,
3669 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003670{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003671 struct snd_pcm_file *pcm_file = file->private_data;
3672 struct snd_pcm_substream *substream = pcm_file->substream;
3673 struct snd_pcm_runtime *runtime = substream->runtime;
3674 unsigned long offset = pgoff << PAGE_SHIFT;
3675
3676 switch (offset) {
3677 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3678 return (unsigned long)runtime->status;
3679 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3680 return (unsigned long)runtime->control;
3681 default:
3682 return (unsigned long)runtime->dma_area + offset;
3683 }
Cliff Cai70036092008-09-03 10:54:36 +02003684}
3685#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003686# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003687#endif
3688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689/*
3690 * Register section
3691 */
3692
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003693const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003695 .owner = THIS_MODULE,
3696 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003697 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003698 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003699 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003700 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003701 .poll = snd_pcm_playback_poll,
3702 .unlocked_ioctl = snd_pcm_playback_ioctl,
3703 .compat_ioctl = snd_pcm_ioctl_compat,
3704 .mmap = snd_pcm_mmap,
3705 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003706 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 },
3708 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003709 .owner = THIS_MODULE,
3710 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003711 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003712 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003713 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003714 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003715 .poll = snd_pcm_capture_poll,
3716 .unlocked_ioctl = snd_pcm_capture_ioctl,
3717 .compat_ioctl = snd_pcm_ioctl_compat,
3718 .mmap = snd_pcm_mmap,
3719 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003720 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
3722};