blob: 23e17a58651b30cb56bd1545d00b5b82c1a49f24 [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>
Chanho Minee8dce22018-11-26 14:36:37 +090038#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Compatibility
42 */
43
Takashi Iwai877211f2005-11-17 13:59:38 +010044struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned int flags;
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010048 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
50 unsigned int rmask;
51 unsigned int cmask;
52 unsigned int info;
53 unsigned int msbits;
54 unsigned int rate_num;
55 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010056 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned char reserved[64];
58};
59
Takashi Iwai59d48582005-12-01 10:51:58 +010060#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010061#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Takashi Iwai877211f2005-11-17 13:59:38 +010064static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010068#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010069static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 *
73 */
74
Takashi Iwai7af142f2014-09-01 11:19:37 +020075static DEFINE_RWLOCK(snd_pcm_link_rwlock);
76static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Takashi Iwai67ec1072016-02-17 14:30:26 +010078/* Writer in rwsem may block readers even during its waiting in queue,
79 * and this may lead to a deadlock when the code path takes read sem
80 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
81 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
Chanho Minee8dce22018-11-26 14:36:37 +090082 * sleep until all the readers are completed without blocking by writer.
Takashi Iwai67ec1072016-02-17 14:30:26 +010083 */
Chanho Minee8dce22018-11-26 14:36:37 +090084static inline void down_write_nonfifo(struct rw_semaphore *lock)
Takashi Iwai67ec1072016-02-17 14:30:26 +010085{
86 while (!down_write_trylock(lock))
Chanho Minee8dce22018-11-26 14:36:37 +090087 msleep(1);
Takashi Iwai67ec1072016-02-17 14:30:26 +010088}
89
Takashi Iwai30b771c2014-10-30 15:02:50 +010090/**
91 * snd_pcm_stream_lock - Lock the PCM stream
92 * @substream: PCM substream
93 *
94 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
95 * flag of the given substream. This also takes the global link rw lock
96 * (or rw sem), too, for avoiding the race with linked streams.
97 */
Takashi Iwai7af142f2014-09-01 11:19:37 +020098void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
99{
100 if (substream->pcm->nonatomic) {
Takashi Iwai67756e32015-07-17 15:22:33 +0200101 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200102 mutex_lock(&substream->self_group.mutex);
103 } else {
104 read_lock(&snd_pcm_link_rwlock);
105 spin_lock(&substream->self_group.lock);
106 }
107}
108EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
109
Takashi Iwai30b771c2014-10-30 15:02:50 +0100110/**
111 * snd_pcm_stream_lock - Unlock the PCM stream
112 * @substream: PCM substream
113 *
114 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
115 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200116void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
117{
118 if (substream->pcm->nonatomic) {
119 mutex_unlock(&substream->self_group.mutex);
120 up_read(&snd_pcm_link_rwsem);
121 } else {
122 spin_unlock(&substream->self_group.lock);
123 read_unlock(&snd_pcm_link_rwlock);
124 }
125}
126EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
127
Takashi Iwai30b771c2014-10-30 15:02:50 +0100128/**
129 * snd_pcm_stream_lock_irq - Lock the PCM stream
130 * @substream: PCM substream
131 *
132 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
133 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
134 * as snd_pcm_stream_lock().
135 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200136void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
137{
138 if (!substream->pcm->nonatomic)
139 local_irq_disable();
140 snd_pcm_stream_lock(substream);
141}
142EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
143
Takashi Iwai30b771c2014-10-30 15:02:50 +0100144/**
145 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
146 * @substream: PCM substream
147 *
148 * This is a counter-part of snd_pcm_stream_lock_irq().
149 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200150void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
151{
152 snd_pcm_stream_unlock(substream);
153 if (!substream->pcm->nonatomic)
154 local_irq_enable();
155}
156EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
157
158unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
159{
160 unsigned long flags = 0;
161 if (!substream->pcm->nonatomic)
162 local_irq_save(flags);
163 snd_pcm_stream_lock(substream);
164 return flags;
165}
166EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
167
Takashi Iwai30b771c2014-10-30 15:02:50 +0100168/**
169 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
170 * @substream: PCM substream
171 * @flags: irq flags
172 *
173 * This is a counter-part of snd_pcm_stream_lock_irqsave().
174 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200175void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
176 unsigned long flags)
177{
178 snd_pcm_stream_unlock(substream);
179 if (!substream->pcm->nonatomic)
180 local_irq_restore(flags);
181}
182EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184static inline mm_segment_t snd_enter_user(void)
185{
186 mm_segment_t fs = get_fs();
187 set_fs(get_ds());
188 return fs;
189}
190
191static inline void snd_leave_user(mm_segment_t fs)
192{
193 set_fs(fs);
194}
195
196
197
Takashi Iwai877211f2005-11-17 13:59:38 +0100198int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Takashi Iwai877211f2005-11-17 13:59:38 +0100200 struct snd_pcm_runtime *runtime;
201 struct snd_pcm *pcm = substream->pcm;
202 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 memset(info, 0, sizeof(*info));
205 info->card = pcm->card->number;
206 info->device = pcm->device;
207 info->stream = substream->stream;
208 info->subdevice = substream->number;
209 strlcpy(info->id, pcm->id, sizeof(info->id));
210 strlcpy(info->name, pcm->name, sizeof(info->name));
211 info->dev_class = pcm->dev_class;
212 info->dev_subclass = pcm->dev_subclass;
213 info->subdevices_count = pstr->substream_count;
214 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
215 strlcpy(info->subname, substream->name, sizeof(info->subname));
216 runtime = substream->runtime;
Takashi Sakamoto946023a2017-06-14 19:30:03 +0900217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219}
220
Takashi Iwai877211f2005-11-17 13:59:38 +0100221int snd_pcm_info_user(struct snd_pcm_substream *substream,
222 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Takashi Iwai877211f2005-11-17 13:59:38 +0100224 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int err;
226
227 info = kmalloc(sizeof(*info), GFP_KERNEL);
228 if (! info)
229 return -ENOMEM;
230 err = snd_pcm_info(substream, info);
231 if (err >= 0) {
232 if (copy_to_user(_info, info, sizeof(*info)))
233 err = -EFAULT;
234 }
235 kfree(info);
236 return err;
237}
238
Takashi Iwai63825f32014-10-22 12:04:46 +0200239static bool hw_support_mmap(struct snd_pcm_substream *substream)
240{
241 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
242 return false;
243 /* check architectures that return -EINVAL from dma_mmap_coherent() */
244 /* FIXME: this should be some global flag */
245#if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
246 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
247 if (!substream->ops->mmap &&
248 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
249 return false;
250#endif
251 return true;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#undef RULES_DEBUG
255
256#ifdef RULES_DEBUG
257#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
Joe Perches47023ec2010-09-13 21:24:02 -0700258static const char * const snd_pcm_hw_param_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 HW_PARAM(ACCESS),
260 HW_PARAM(FORMAT),
261 HW_PARAM(SUBFORMAT),
262 HW_PARAM(SAMPLE_BITS),
263 HW_PARAM(FRAME_BITS),
264 HW_PARAM(CHANNELS),
265 HW_PARAM(RATE),
266 HW_PARAM(PERIOD_TIME),
267 HW_PARAM(PERIOD_SIZE),
268 HW_PARAM(PERIOD_BYTES),
269 HW_PARAM(PERIODS),
270 HW_PARAM(BUFFER_TIME),
271 HW_PARAM(BUFFER_SIZE),
272 HW_PARAM(BUFFER_BYTES),
273 HW_PARAM(TICK_TIME),
274};
275#endif
276
Takashi Iwai877211f2005-11-17 13:59:38 +0100277int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
278 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100281 struct snd_pcm_hardware *hw;
282 struct snd_interval *i = NULL;
283 struct snd_mask *m = NULL;
284 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 unsigned int rstamps[constrs->rules_num];
286 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
287 unsigned int stamp = 2;
288 int changed, again;
289
290 params->info = 0;
291 params->fifo_size = 0;
292 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
293 params->msbits = 0;
294 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
295 params->rate_num = 0;
296 params->rate_den = 0;
297 }
298
299 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
300 m = hw_param_mask(params, k);
301 if (snd_mask_empty(m))
302 return -EINVAL;
303 if (!(params->rmask & (1 << k)))
304 continue;
305#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100306 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
307 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 -0700308#endif
309 changed = snd_mask_refine(m, constrs_mask(constrs, k));
310#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100311 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 -0700312#endif
313 if (changed)
314 params->cmask |= 1 << k;
315 if (changed < 0)
316 return changed;
317 }
318
319 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
320 i = hw_param_interval(params, k);
321 if (snd_interval_empty(i))
322 return -EINVAL;
323 if (!(params->rmask & (1 << k)))
324 continue;
325#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100326 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100328 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100330 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 i->openmin ? '(' : '[', i->min,
332 i->max, i->openmax ? ')' : ']');
Takashi Iwai09e56df2014-02-04 18:19:48 +0100333 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#endif
335 changed = snd_interval_refine(i, constrs_interval(constrs, k));
336#ifdef RULES_DEBUG
337 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100338 pr_cont("empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100340 pr_cont("%c%u %u%c\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 i->openmin ? '(' : '[', i->min,
342 i->max, i->openmax ? ')' : ']');
343#endif
344 if (changed)
345 params->cmask |= 1 << k;
346 if (changed < 0)
347 return changed;
348 }
349
350 for (k = 0; k < constrs->rules_num; k++)
351 rstamps[k] = 0;
352 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
353 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
354 do {
355 again = 0;
356 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100357 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 unsigned int d;
359 int doit = 0;
360 if (r->cond && !(r->cond & params->flags))
361 continue;
362 for (d = 0; r->deps[d] >= 0; d++) {
363 if (vstamps[r->deps[d]] > rstamps[k]) {
364 doit = 1;
365 break;
366 }
367 }
368 if (!doit)
369 continue;
370#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100371 pr_debug("Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100373 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (hw_is_mask(r->var)) {
375 m = hw_param_mask(params, r->var);
Takashi Iwai09e56df2014-02-04 18:19:48 +0100376 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 } else {
378 i = hw_param_interval(params, r->var);
379 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100380 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100382 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 i->openmin ? '(' : '[', i->min,
384 i->max, i->openmax ? ')' : ']');
385 }
386 }
387#endif
388 changed = r->func(params, r);
389#ifdef RULES_DEBUG
390 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100391 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (hw_is_mask(r->var))
Takashi Iwai09e56df2014-02-04 18:19:48 +0100393 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 else {
395 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100396 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100398 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 i->openmin ? '(' : '[', i->min,
400 i->max, i->openmax ? ')' : ']');
401 }
402 }
Takashi Iwai09e56df2014-02-04 18:19:48 +0100403 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#endif
405 rstamps[k] = stamp;
406 if (changed && r->var >= 0) {
407 params->cmask |= (1 << r->var);
408 vstamps[r->var] = stamp;
409 again = 1;
410 }
411 if (changed < 0)
412 return changed;
413 stamp++;
414 }
415 } while (again);
416 if (!params->msbits) {
417 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
418 if (snd_interval_single(i))
419 params->msbits = snd_interval_value(i);
420 }
421
422 if (!params->rate_den) {
423 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
424 if (snd_interval_single(i)) {
425 params->rate_num = snd_interval_value(i);
426 params->rate_den = 1;
427 }
428 }
429
430 hw = &substream->runtime->hw;
Takashi Iwai63825f32014-10-22 12:04:46 +0200431 if (!params->info) {
Libin Yang48d88292014-12-31 22:09:54 +0800432 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
433 SNDRV_PCM_INFO_DRAIN_TRIGGER);
Takashi Iwai63825f32014-10-22 12:04:46 +0200434 if (!hw_support_mmap(substream))
435 params->info &= ~(SNDRV_PCM_INFO_MMAP |
436 SNDRV_PCM_INFO_MMAP_VALID);
437 }
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200438 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100439 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
440 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
441 if (snd_mask_min(m) == snd_mask_max(m) &&
442 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200443 changed = substream->ops->ioctl(substream,
444 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200445 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200446 return changed;
447 }
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 params->rmask = 0;
450 return 0;
451}
452
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200453EXPORT_SYMBOL(snd_pcm_hw_refine);
454
Takashi Iwai877211f2005-11-17 13:59:38 +0100455static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
456 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Takashi Iwai877211f2005-11-17 13:59:38 +0100458 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int err;
460
Li Zefanef44a1e2009-04-10 09:43:08 +0800461 params = memdup_user(_params, sizeof(*params));
462 if (IS_ERR(params))
463 return PTR_ERR(params);
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 err = snd_pcm_hw_refine(substream, params);
466 if (copy_to_user(_params, params, sizeof(*params))) {
467 if (!err)
468 err = -EFAULT;
469 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 kfree(params);
472 return err;
473}
474
Takashi Iwai9442e692006-09-30 23:27:19 -0700475static int period_to_usecs(struct snd_pcm_runtime *runtime)
476{
477 int usecs;
478
479 if (! runtime->rate)
480 return -1; /* invalid */
481
482 /* take 75% of period time as the deadline */
483 usecs = (750000 / runtime->rate) * runtime->period_size;
484 usecs += ((750000 % runtime->rate) * runtime->period_size) /
485 runtime->rate;
486
487 return usecs;
488}
489
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200490static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
491{
492 snd_pcm_stream_lock_irq(substream);
493 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
494 substream->runtime->status->state = state;
495 snd_pcm_stream_unlock_irq(substream);
496}
497
Jie Yang90bbaf62015-10-16 17:57:46 +0800498static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
499 int event)
500{
501#ifdef CONFIG_SND_PCM_TIMER
502 if (substream->timer)
503 snd_timer_notify(substream->timer, event,
504 &substream->runtime->trigger_tstamp);
505#endif
506}
507
Takashi Iwai877211f2005-11-17 13:59:38 +0100508static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
509 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Takashi Iwai877211f2005-11-17 13:59:38 +0100511 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700512 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 unsigned int bits;
514 snd_pcm_uframes_t frames;
515
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200516 if (PCM_RUNTIME_CHECK(substream))
517 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 snd_pcm_stream_lock_irq(substream);
520 switch (runtime->status->state) {
521 case SNDRV_PCM_STATE_OPEN:
522 case SNDRV_PCM_STATE_SETUP:
523 case SNDRV_PCM_STATE_PREPARED:
524 break;
525 default:
526 snd_pcm_stream_unlock_irq(substream);
527 return -EBADFD;
528 }
529 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100530#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!substream->oss.oss)
532#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200533 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return -EBADFD;
535
536 params->rmask = ~0U;
537 err = snd_pcm_hw_refine(substream, params);
538 if (err < 0)
539 goto _error;
540
541 err = snd_pcm_hw_params_choose(substream, params);
542 if (err < 0)
543 goto _error;
544
545 if (substream->ops->hw_params != NULL) {
546 err = substream->ops->hw_params(substream, params);
547 if (err < 0)
548 goto _error;
549 }
550
551 runtime->access = params_access(params);
552 runtime->format = params_format(params);
553 runtime->subformat = params_subformat(params);
554 runtime->channels = params_channels(params);
555 runtime->rate = params_rate(params);
556 runtime->period_size = params_period_size(params);
557 runtime->periods = params_periods(params);
558 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 runtime->info = params->info;
560 runtime->rate_num = params->rate_num;
561 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100562 runtime->no_period_wakeup =
563 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
564 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 bits = snd_pcm_format_physical_width(runtime->format);
567 runtime->sample_bits = bits;
568 bits *= runtime->channels;
569 runtime->frame_bits = bits;
570 frames = 1;
571 while (bits % 8 != 0) {
572 bits *= 2;
573 frames *= 2;
574 }
575 runtime->byte_align = bits / 8;
576 runtime->min_align = frames;
577
578 /* Default sw params */
579 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
580 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 runtime->start_threshold = 1;
583 runtime->stop_threshold = runtime->buffer_size;
584 runtime->silence_threshold = 0;
585 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200586 runtime->boundary = runtime->buffer_size;
587 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
588 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Takashi Iwai360440c2019-12-11 16:57:42 +0100590 /* clear the buffer for avoiding possible kernel info leaks */
Takashi Iwaiea4eb1a2020-01-29 10:40:41 +0100591 if (runtime->dma_area && !substream->ops->copy)
Takashi Iwai360440c2019-12-11 16:57:42 +0100592 memset(runtime->dma_area, 0, runtime->dma_bytes);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200595 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700596
James Bottomley82f68252010-07-05 22:53:06 +0200597 if (pm_qos_request_active(&substream->latency_pm_qos_req))
598 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700599 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200600 pm_qos_add_request(&substream->latency_pm_qos_req,
601 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300604 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 so we force application to retry to set
606 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200607 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (substream->ops->hw_free != NULL)
609 substream->ops->hw_free(substream);
610 return err;
611}
612
Takashi Iwai877211f2005-11-17 13:59:38 +0100613static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
614 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwai877211f2005-11-17 13:59:38 +0100616 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int err;
618
Li Zefanef44a1e2009-04-10 09:43:08 +0800619 params = memdup_user(_params, sizeof(*params));
620 if (IS_ERR(params))
621 return PTR_ERR(params);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 err = snd_pcm_hw_params(substream, params);
624 if (copy_to_user(_params, params, sizeof(*params))) {
625 if (!err)
626 err = -EFAULT;
627 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 kfree(params);
630 return err;
631}
632
Takashi Iwai877211f2005-11-17 13:59:38 +0100633static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwai877211f2005-11-17 13:59:38 +0100635 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int result = 0;
637
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200638 if (PCM_RUNTIME_CHECK(substream))
639 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 snd_pcm_stream_lock_irq(substream);
642 switch (runtime->status->state) {
643 case SNDRV_PCM_STATE_SETUP:
644 case SNDRV_PCM_STATE_PREPARED:
645 break;
646 default:
647 snd_pcm_stream_unlock_irq(substream);
648 return -EBADFD;
649 }
650 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200651 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -EBADFD;
653 if (substream->ops->hw_free)
654 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200655 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200656 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return result;
658}
659
Takashi Iwai877211f2005-11-17 13:59:38 +0100660static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
661 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Takashi Iwai877211f2005-11-17 13:59:38 +0100663 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100664 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200666 if (PCM_RUNTIME_CHECK(substream))
667 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 snd_pcm_stream_lock_irq(substream);
670 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
671 snd_pcm_stream_unlock_irq(substream);
672 return -EBADFD;
673 }
674 snd_pcm_stream_unlock_irq(substream);
675
Dan Carpenter145d92e2015-09-23 12:42:28 +0300676 if (params->tstamp_mode < 0 ||
677 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EINVAL;
Takashi Iwai58900812014-07-16 17:45:27 +0200679 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
680 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200681 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (params->avail_min == 0)
683 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (params->silence_size >= runtime->boundary) {
685 if (params->silence_threshold != 0)
686 return -EINVAL;
687 } else {
688 if (params->silence_size > params->silence_threshold)
689 return -EINVAL;
690 if (params->silence_threshold > runtime->buffer_size)
691 return -EINVAL;
692 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100693 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 snd_pcm_stream_lock_irq(substream);
695 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai58900812014-07-16 17:45:27 +0200696 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
697 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 runtime->period_step = params->period_step;
699 runtime->control->avail_min = params->avail_min;
700 runtime->start_threshold = params->start_threshold;
701 runtime->stop_threshold = params->stop_threshold;
702 runtime->silence_threshold = params->silence_threshold;
703 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 params->boundary = runtime->boundary;
705 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
707 runtime->silence_size > 0)
708 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100709 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Takashi Iwai877211f2005-11-17 13:59:38 +0100715static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
716 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Takashi Iwai877211f2005-11-17 13:59:38 +0100718 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int err;
720 if (copy_from_user(&params, _params, sizeof(params)))
721 return -EFAULT;
722 err = snd_pcm_sw_params(substream, &params);
723 if (copy_to_user(_params, &params, sizeof(params)))
724 return -EFAULT;
725 return err;
726}
727
Takashi Iwai877211f2005-11-17 13:59:38 +0100728int snd_pcm_status(struct snd_pcm_substream *substream,
729 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai877211f2005-11-17 13:59:38 +0100731 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600734
735 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
736 &runtime->audio_tstamp_config);
737
738 /* backwards compatible behavior */
739 if (runtime->audio_tstamp_config.type_requested ==
740 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
741 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
742 runtime->audio_tstamp_config.type_requested =
743 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
744 else
745 runtime->audio_tstamp_config.type_requested =
746 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
747 runtime->audio_tstamp_report.valid = 0;
748 } else
749 runtime->audio_tstamp_report.valid = 1;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 status->state = runtime->status->state;
752 status->suspended_state = runtime->status->suspended_state;
753 if (status->state == SNDRV_PCM_STATE_OPEN)
754 goto _end;
755 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100756 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100758 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
759 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600760 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500761 status->audio_tstamp =
762 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600763 if (runtime->audio_tstamp_report.valid == 1)
764 /* backwards compatibility, no report provided in COMPAT mode */
765 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
766 &status->audio_tstamp_accuracy,
767 &runtime->audio_tstamp_report);
768
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100769 goto _tstamp_end;
770 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600771 } else {
772 /* get tstamp only in fallback mode and only if enabled */
773 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
774 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100775 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100776 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 status->appl_ptr = runtime->control->appl_ptr;
778 status->hw_ptr = runtime->status->hw_ptr;
779 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
780 status->avail = snd_pcm_playback_avail(runtime);
781 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200782 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200784 status->delay += runtime->delay;
785 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 status->delay = 0;
787 } else {
788 status->avail = snd_pcm_capture_avail(runtime);
789 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200790 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 else
792 status->delay = 0;
793 }
794 status->avail_max = runtime->avail_max;
795 status->overrange = runtime->overrange;
796 runtime->avail_max = 0;
797 runtime->overrange = 0;
798 _end:
799 snd_pcm_stream_unlock_irq(substream);
800 return 0;
801}
802
Takashi Iwai877211f2005-11-17 13:59:38 +0100803static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600804 struct snd_pcm_status __user * _status,
805 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Takashi Iwai877211f2005-11-17 13:59:38 +0100807 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600811 /*
812 * with extension, parameters are read/write,
813 * get audio_tstamp_data from user,
814 * ignore rest of status structure
815 */
816 if (ext && get_user(status.audio_tstamp_data,
817 (u32 __user *)(&_status->audio_tstamp_data)))
818 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 res = snd_pcm_status(substream, &status);
820 if (res < 0)
821 return res;
822 if (copy_to_user(_status, &status, sizeof(status)))
823 return -EFAULT;
824 return 0;
825}
826
Takashi Iwai877211f2005-11-17 13:59:38 +0100827static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
828 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai877211f2005-11-17 13:59:38 +0100830 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 unsigned int channel;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 channel = info->channel;
834 runtime = substream->runtime;
835 snd_pcm_stream_lock_irq(substream);
836 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
837 snd_pcm_stream_unlock_irq(substream);
838 return -EBADFD;
839 }
840 snd_pcm_stream_unlock_irq(substream);
841 if (channel >= runtime->channels)
842 return -EINVAL;
843 memset(info, 0, sizeof(*info));
844 info->channel = channel;
845 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
846}
847
Takashi Iwai877211f2005-11-17 13:59:38 +0100848static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
849 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Takashi Iwai877211f2005-11-17 13:59:38 +0100851 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int res;
853
854 if (copy_from_user(&info, _info, sizeof(info)))
855 return -EFAULT;
856 res = snd_pcm_channel_info(substream, &info);
857 if (res < 0)
858 return res;
859 if (copy_to_user(_info, &info, sizeof(info)))
860 return -EFAULT;
861 return 0;
862}
863
Takashi Iwai877211f2005-11-17 13:59:38 +0100864static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Takashi Iwai877211f2005-11-17 13:59:38 +0100866 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (runtime->trigger_master == NULL)
868 return;
869 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600870 if (!runtime->trigger_tstamp_latched)
871 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 } else {
873 snd_pcm_trigger_tstamp(runtime->trigger_master);
874 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
875 }
876 runtime->trigger_master = NULL;
877}
878
879struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100880 int (*pre_action)(struct snd_pcm_substream *substream, int state);
881 int (*do_action)(struct snd_pcm_substream *substream, int state);
882 void (*undo_action)(struct snd_pcm_substream *substream, int state);
883 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884};
885
886/*
887 * this functions is core for handling of linked stream
888 * Note: the stream state might be changed also on failure
889 * Note2: call with calling stream lock + link lock
890 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100891static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100892 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int state, int do_lock)
894{
Takashi Iwai877211f2005-11-17 13:59:38 +0100895 struct snd_pcm_substream *s = NULL;
896 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200897 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Takashi Iwaief991b92007-02-22 12:52:53 +0100899 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200900 if (do_lock && s != substream) {
901 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200902 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200903 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200904 spin_lock_nested(&s->self_group.lock, depth);
905 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 res = ops->pre_action(s, state);
908 if (res < 0)
909 goto _unlock;
910 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100911 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 res = ops->do_action(s, state);
913 if (res < 0) {
914 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100915 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (s1 == s) /* failed stream */
917 break;
918 ops->undo_action(s1, state);
919 }
920 }
921 s = NULL; /* unlock all */
922 goto _unlock;
923 }
924 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100925 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ops->post_action(s, state);
927 }
928 _unlock:
929 if (do_lock) {
930 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100931 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200932 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200933 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200934 mutex_unlock(&s1->self_group.mutex);
935 else
936 spin_unlock(&s1->self_group.lock);
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (s1 == s) /* end */
939 break;
940 }
941 }
942 return res;
943}
944
945/*
946 * Note: call with stream lock
947 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100948static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100949 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int state)
951{
952 int res;
953
954 res = ops->pre_action(substream, state);
955 if (res < 0)
956 return res;
957 res = ops->do_action(substream, state);
958 if (res == 0)
959 ops->post_action(substream, state);
960 else if (ops->undo_action)
961 ops->undo_action(substream, state);
962 return res;
963}
964
965/*
966 * Note: call with stream lock
967 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100968static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100969 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int state)
971{
972 int res;
973
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100974 if (!snd_pcm_stream_linked(substream))
975 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200976
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100977 if (substream->pcm->nonatomic) {
978 if (!mutex_trylock(&substream->group->mutex)) {
979 mutex_unlock(&substream->self_group.mutex);
980 mutex_lock(&substream->group->mutex);
981 mutex_lock(&substream->self_group.mutex);
982 }
983 res = snd_pcm_action_group(ops, substream, state, 1);
984 mutex_unlock(&substream->group->mutex);
985 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!spin_trylock(&substream->group->lock)) {
987 spin_unlock(&substream->self_group.lock);
988 spin_lock(&substream->group->lock);
989 spin_lock(&substream->self_group.lock);
990 }
991 res = snd_pcm_action_group(ops, substream, state, 1);
992 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 return res;
995}
996
997/*
998 * Note: don't use any locks before
999 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001000static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001001 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int state)
1003{
1004 int res;
1005
Takashi Iwaie3a4bd52014-10-31 14:45:04 +01001006 snd_pcm_stream_lock_irq(substream);
1007 res = snd_pcm_action(ops, substream, state);
1008 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return res;
1010}
1011
1012/*
1013 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001014static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001015 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int state)
1017{
1018 int res;
1019
1020 down_read(&snd_pcm_link_rwsem);
1021 if (snd_pcm_stream_linked(substream))
1022 res = snd_pcm_action_group(ops, substream, state, 0);
1023 else
1024 res = snd_pcm_action_single(ops, substream, state);
1025 up_read(&snd_pcm_link_rwsem);
1026 return res;
1027}
1028
1029/*
1030 * start callbacks
1031 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001032static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwai877211f2005-11-17 13:59:38 +01001034 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1036 return -EBADFD;
1037 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1038 !snd_pcm_playback_data(substream))
1039 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001040 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 runtime->trigger_master = substream;
1042 return 0;
1043}
1044
Takashi Iwai877211f2005-11-17 13:59:38 +01001045static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 if (substream->runtime->trigger_master != substream)
1048 return 0;
1049 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1050}
1051
Takashi Iwai877211f2005-11-17 13:59:38 +01001052static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 if (substream->runtime->trigger_master == substream)
1055 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1056}
1057
Takashi Iwai877211f2005-11-17 13:59:38 +01001058static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Takashi Iwai877211f2005-11-17 13:59:38 +01001060 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001062 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001063 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1064 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 runtime->status->state = state;
1066 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1067 runtime->silence_size > 0)
1068 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001069 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
Julia Lawallb17154c2015-11-29 16:36:40 +01001072static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 .pre_action = snd_pcm_pre_start,
1074 .do_action = snd_pcm_do_start,
1075 .undo_action = snd_pcm_undo_start,
1076 .post_action = snd_pcm_post_start
1077};
1078
1079/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001080 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001081 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001082 *
1083 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001085int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Takashi Iwai877211f2005-11-17 13:59:38 +01001087 return snd_pcm_action(&snd_pcm_action_start, substream,
1088 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091/*
1092 * stop callbacks
1093 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001094static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Takashi Iwai877211f2005-11-17 13:59:38 +01001096 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1098 return -EBADFD;
1099 runtime->trigger_master = substream;
1100 return 0;
1101}
1102
Takashi Iwai877211f2005-11-17 13:59:38 +01001103static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 if (substream->runtime->trigger_master == substream &&
1106 snd_pcm_running(substream))
1107 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1108 return 0; /* unconditonally stop all substreams */
1109}
1110
Takashi Iwai877211f2005-11-17 13:59:38 +01001111static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Takashi Iwai877211f2005-11-17 13:59:38 +01001113 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (runtime->status->state != state) {
1115 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001116 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001117 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001120 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Julia Lawallb17154c2015-11-29 16:36:40 +01001123static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 .pre_action = snd_pcm_pre_stop,
1125 .do_action = snd_pcm_do_stop,
1126 .post_action = snd_pcm_post_stop
1127};
1128
1129/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001130 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001131 * @substream: the PCM substream instance
1132 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001134 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001135 *
Masanari Iida0a114582014-02-09 00:47:36 +09001136 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001138int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1141}
1142
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001143EXPORT_SYMBOL(snd_pcm_stop);
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001146 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001147 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001149 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001151 *
1152 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001154int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Takashi Iwai877211f2005-11-17 13:59:38 +01001156 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1157 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
Takashi Iwai1fb85102014-11-07 17:08:28 +01001160/**
1161 * snd_pcm_stop_xrun - stop the running streams as XRUN
1162 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001163 *
1164 * This stops the given running substream (and all linked substreams) as XRUN.
1165 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1166 *
1167 * Return: Zero if successful, or a negative error code.
1168 */
1169int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1170{
1171 unsigned long flags;
1172 int ret = 0;
1173
1174 snd_pcm_stream_lock_irqsave(substream, flags);
1175 if (snd_pcm_running(substream))
1176 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1177 snd_pcm_stream_unlock_irqrestore(substream, flags);
1178 return ret;
1179}
1180EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/*
1183 * pause callbacks
1184 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001185static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Takashi Iwai877211f2005-11-17 13:59:38 +01001187 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1189 return -ENOSYS;
1190 if (push) {
1191 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1192 return -EBADFD;
1193 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1194 return -EBADFD;
1195 runtime->trigger_master = substream;
1196 return 0;
1197}
1198
Takashi Iwai877211f2005-11-17 13:59:38 +01001199static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 if (substream->runtime->trigger_master != substream)
1202 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001203 /* some drivers might use hw_ptr to recover from the pause -
1204 update the hw_ptr now */
1205 if (push)
1206 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001207 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001208 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001209 * delta, effectively to skip the check once.
1210 */
1211 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return substream->ops->trigger(substream,
1213 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1214 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1215}
1216
Takashi Iwai877211f2005-11-17 13:59:38 +01001217static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 if (substream->runtime->trigger_master == substream)
1220 substream->ops->trigger(substream,
1221 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1222 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1223}
1224
Takashi Iwai877211f2005-11-17 13:59:38 +01001225static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Takashi Iwai877211f2005-11-17 13:59:38 +01001227 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 snd_pcm_trigger_tstamp(substream);
1229 if (push) {
1230 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001231 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001233 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 } else {
1235 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001236 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238}
1239
Julia Lawallb17154c2015-11-29 16:36:40 +01001240static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 .pre_action = snd_pcm_pre_pause,
1242 .do_action = snd_pcm_do_pause,
1243 .undo_action = snd_pcm_undo_pause,
1244 .post_action = snd_pcm_post_pause
1245};
1246
1247/*
1248 * Push/release the pause for all linked streams.
1249 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001250static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1253}
1254
1255#ifdef CONFIG_PM
1256/* suspend */
1257
Takashi Iwai877211f2005-11-17 13:59:38 +01001258static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Takashi Iwai877211f2005-11-17 13:59:38 +01001260 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaibe0c2682019-03-25 10:38:58 +01001261 switch (runtime->status->state) {
1262 case SNDRV_PCM_STATE_SUSPENDED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -EBUSY;
Takashi Iwaibe0c2682019-03-25 10:38:58 +01001264 /* unresumable PCM state; return -EBUSY for skipping suspend */
1265 case SNDRV_PCM_STATE_OPEN:
1266 case SNDRV_PCM_STATE_SETUP:
1267 case SNDRV_PCM_STATE_DISCONNECTED:
1268 return -EBUSY;
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 runtime->trigger_master = substream;
1271 return 0;
1272}
1273
Takashi Iwai877211f2005-11-17 13:59:38 +01001274static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Takashi Iwai877211f2005-11-17 13:59:38 +01001276 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (runtime->trigger_master != substream)
1278 return 0;
1279 if (! snd_pcm_running(substream))
1280 return 0;
1281 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1282 return 0; /* suspend unconditionally */
1283}
1284
Takashi Iwai877211f2005-11-17 13:59:38 +01001285static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Takashi Iwai877211f2005-11-17 13:59:38 +01001287 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001289 runtime->status->suspended_state = runtime->status->state;
1290 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001291 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001293 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Julia Lawallb17154c2015-11-29 16:36:40 +01001296static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 .pre_action = snd_pcm_pre_suspend,
1298 .do_action = snd_pcm_do_suspend,
1299 .post_action = snd_pcm_post_suspend
1300};
1301
1302/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001303 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001304 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001307 *
1308 * Return: Zero if successful (or @substream is %NULL), or a negative error
1309 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001311int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 int err;
1314 unsigned long flags;
1315
Takashi Iwai603bf522005-11-17 15:59:14 +01001316 if (! substream)
1317 return 0;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 snd_pcm_stream_lock_irqsave(substream, flags);
1320 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1321 snd_pcm_stream_unlock_irqrestore(substream, flags);
1322 return err;
1323}
1324
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001325EXPORT_SYMBOL(snd_pcm_suspend);
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001328 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001329 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001332 *
1333 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001335int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Takashi Iwai877211f2005-11-17 13:59:38 +01001337 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int stream, err = 0;
1339
Takashi Iwai603bf522005-11-17 15:59:14 +01001340 if (! pcm)
1341 return 0;
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001344 for (substream = pcm->streams[stream].substream;
1345 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 /* FIXME: the open/close code should lock this as well */
1347 if (substream->runtime == NULL)
1348 continue;
Ranjani Sridharan9b5a3412019-02-08 17:29:53 -06001349
1350 /*
1351 * Skip BE dai link PCM's that are internal and may
1352 * not have their substream ops set.
1353 */
1354 if (!substream->ops)
1355 continue;
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 err = snd_pcm_suspend(substream);
1358 if (err < 0 && err != -EBUSY)
1359 return err;
1360 }
1361 }
1362 return 0;
1363}
1364
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001365EXPORT_SYMBOL(snd_pcm_suspend_all);
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367/* resume */
1368
Takashi Iwai877211f2005-11-17 13:59:38 +01001369static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Takashi Iwai877211f2005-11-17 13:59:38 +01001371 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1373 return -ENOSYS;
1374 runtime->trigger_master = substream;
1375 return 0;
1376}
1377
Takashi Iwai877211f2005-11-17 13:59:38 +01001378static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Takashi Iwai877211f2005-11-17 13:59:38 +01001380 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (runtime->trigger_master != substream)
1382 return 0;
1383 /* DMA not running previously? */
1384 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1385 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1386 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1387 return 0;
1388 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1389}
1390
Takashi Iwai877211f2005-11-17 13:59:38 +01001391static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 if (substream->runtime->trigger_master == substream &&
1394 snd_pcm_running(substream))
1395 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1396}
1397
Takashi Iwai877211f2005-11-17 13:59:38 +01001398static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Takashi Iwai877211f2005-11-17 13:59:38 +01001400 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001402 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001403 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Julia Lawallb17154c2015-11-29 16:36:40 +01001406static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 .pre_action = snd_pcm_pre_resume,
1408 .do_action = snd_pcm_do_resume,
1409 .undo_action = snd_pcm_undo_resume,
1410 .post_action = snd_pcm_post_resume
1411};
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{
Takashi Iwai877211f2005-11-17 13:59:38 +01001415 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int res;
1417
1418 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001419 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1421 snd_power_unlock(card);
1422 return res;
1423}
1424
1425#else
1426
Takashi Iwai877211f2005-11-17 13:59:38 +01001427static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 return -ENOSYS;
1430}
1431
1432#endif /* CONFIG_PM */
1433
1434/*
1435 * xrun ioctl
1436 *
1437 * Change the RUNNING stream(s) to XRUN state.
1438 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001439static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Takashi Iwai877211f2005-11-17 13:59:38 +01001441 struct snd_card *card = substream->pcm->card;
1442 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int result;
1444
1445 snd_power_lock(card);
1446 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001447 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (result < 0)
1449 goto _unlock;
1450 }
1451
1452 snd_pcm_stream_lock_irq(substream);
1453 switch (runtime->status->state) {
1454 case SNDRV_PCM_STATE_XRUN:
1455 result = 0; /* already there */
1456 break;
1457 case SNDRV_PCM_STATE_RUNNING:
1458 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1459 break;
1460 default:
1461 result = -EBADFD;
1462 }
1463 snd_pcm_stream_unlock_irq(substream);
1464 _unlock:
1465 snd_power_unlock(card);
1466 return result;
1467}
1468
1469/*
1470 * reset ioctl
1471 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001472static int snd_pcm_pre_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 switch (runtime->status->state) {
1476 case SNDRV_PCM_STATE_RUNNING:
1477 case SNDRV_PCM_STATE_PREPARED:
1478 case SNDRV_PCM_STATE_PAUSED:
1479 case SNDRV_PCM_STATE_SUSPENDED:
1480 return 0;
1481 default:
1482 return -EBADFD;
1483 }
1484}
1485
Takashi Iwai877211f2005-11-17 13:59:38 +01001486static int snd_pcm_do_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 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1490 if (err < 0)
1491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001493 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1494 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 runtime->silence_start = runtime->status->hw_ptr;
1496 runtime->silence_filled = 0;
1497 return 0;
1498}
1499
Takashi Iwai877211f2005-11-17 13:59:38 +01001500static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Takashi Iwai877211f2005-11-17 13:59:38 +01001502 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 runtime->control->appl_ptr = runtime->status->hw_ptr;
1504 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1505 runtime->silence_size > 0)
1506 snd_pcm_playback_silence(substream, ULONG_MAX);
1507}
1508
Julia Lawallb17154c2015-11-29 16:36:40 +01001509static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 .pre_action = snd_pcm_pre_reset,
1511 .do_action = snd_pcm_do_reset,
1512 .post_action = snd_pcm_post_reset
1513};
1514
Takashi Iwai877211f2005-11-17 13:59:38 +01001515static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1518}
1519
1520/*
1521 * prepare ioctl
1522 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001523/* we use the second argument for updating f_flags */
1524static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1525 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Takashi Iwai877211f2005-11-17 13:59:38 +01001527 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001528 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1529 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return -EBADFD;
1531 if (snd_pcm_running(substream))
1532 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001533 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return 0;
1535}
1536
Takashi Iwai877211f2005-11-17 13:59:38 +01001537static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
1539 int err;
1540 err = substream->ops->prepare(substream);
1541 if (err < 0)
1542 return err;
1543 return snd_pcm_do_reset(substream, 0);
1544}
1545
Takashi Iwai877211f2005-11-17 13:59:38 +01001546static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Takashi Iwai877211f2005-11-17 13:59:38 +01001548 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001550 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Julia Lawallb17154c2015-11-29 16:36:40 +01001553static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 .pre_action = snd_pcm_pre_prepare,
1555 .do_action = snd_pcm_do_prepare,
1556 .post_action = snd_pcm_post_prepare
1557};
1558
1559/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001560 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001561 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001562 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001563 *
1564 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001566static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1567 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001570 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001571 int f_flags;
1572
1573 if (file)
1574 f_flags = file->f_flags;
1575 else
1576 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001579 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001580 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1581 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 snd_power_unlock(card);
1583 return res;
1584}
1585
1586/*
1587 * drain ioctl
1588 */
1589
Takashi Iwai877211f2005-11-17 13:59:38 +01001590static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001592 struct snd_pcm_runtime *runtime = substream->runtime;
1593 switch (runtime->status->state) {
1594 case SNDRV_PCM_STATE_OPEN:
1595 case SNDRV_PCM_STATE_DISCONNECTED:
1596 case SNDRV_PCM_STATE_SUSPENDED:
1597 return -EBADFD;
1598 }
1599 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return 0;
1601}
1602
Takashi Iwai877211f2005-11-17 13:59:38 +01001603static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Takashi Iwai877211f2005-11-17 13:59:38 +01001605 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1607 switch (runtime->status->state) {
1608 case SNDRV_PCM_STATE_PREPARED:
1609 /* start playback stream if possible */
1610 if (! snd_pcm_playback_empty(substream)) {
1611 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1612 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001613 } else {
1614 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616 break;
1617 case SNDRV_PCM_STATE_RUNNING:
1618 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1619 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001620 case SNDRV_PCM_STATE_XRUN:
1621 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1622 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 default:
1624 break;
1625 }
1626 } else {
1627 /* stop running stream */
1628 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001629 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001631 snd_pcm_do_stop(substream, new_state);
1632 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634 }
Libin Yang48d88292014-12-31 22:09:54 +08001635
1636 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1637 runtime->trigger_master == substream &&
1638 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1639 return substream->ops->trigger(substream,
1640 SNDRV_PCM_TRIGGER_DRAIN);
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return 0;
1643}
1644
Takashi Iwai877211f2005-11-17 13:59:38 +01001645static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647}
1648
Julia Lawallb17154c2015-11-29 16:36:40 +01001649static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 .pre_action = snd_pcm_pre_drain_init,
1651 .do_action = snd_pcm_do_drain_init,
1652 .post_action = snd_pcm_post_drain_init
1653};
1654
Takashi Iwai877211f2005-11-17 13:59:38 +01001655static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657/*
1658 * Drain the stream(s).
1659 * When the substream is linked, sync until the draining of all playback streams
1660 * is finished.
1661 * After this call, all streams are supposed to be either SETUP or DRAINING
1662 * (capture only) state.
1663 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001664static int snd_pcm_drain(struct snd_pcm_substream *substream,
1665 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Takashi Iwai877211f2005-11-17 13:59:38 +01001667 struct snd_card *card;
1668 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001669 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001670 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001672 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 card = substream->pcm->card;
1675 runtime = substream->runtime;
1676
1677 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1678 return -EBADFD;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 snd_power_lock(card);
1681 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001682 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001683 if (result < 0) {
1684 snd_power_unlock(card);
1685 return result;
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001689 if (file) {
1690 if (file->f_flags & O_NONBLOCK)
1691 nonblock = 1;
1692 } else if (substream->f_flags & O_NONBLOCK)
1693 nonblock = 1;
1694
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001695 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001696 snd_pcm_stream_lock_irq(substream);
1697 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001698 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001699 snd_pcm_pause(substream, 0);
1700
1701 /* pre-start/stop - all running streams are changed to DRAINING state */
1702 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001703 if (result < 0)
1704 goto unlock;
1705 /* in non-blocking, we don't wait in ioctl but let caller poll */
1706 if (nonblock) {
1707 result = -EAGAIN;
1708 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 for (;;) {
1712 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001713 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (signal_pending(current)) {
1715 result = -ERESTARTSYS;
1716 break;
1717 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001718 /* find a substream to drain */
1719 to_check = NULL;
1720 snd_pcm_group_for_each_entry(s, substream) {
1721 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1722 continue;
1723 runtime = s->runtime;
1724 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1725 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001726 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001727 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001728 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001729 if (!to_check)
1730 break; /* all drained */
1731 init_waitqueue_entry(&wait, current);
1732 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001734 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001736 if (runtime->no_period_wakeup)
1737 tout = MAX_SCHEDULE_TIMEOUT;
1738 else {
1739 tout = 10;
1740 if (runtime->rate) {
1741 long t = runtime->period_size * 2 / runtime->rate;
1742 tout = max(t, tout);
1743 }
1744 tout = msecs_to_jiffies(tout * 1000);
1745 }
1746 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001748 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001750 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001751 if (card->shutdown) {
1752 result = -ENODEV;
1753 break;
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (tout == 0) {
1756 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1757 result = -ESTRPIPE;
1758 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001759 dev_dbg(substream->pcm->card->dev,
1760 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1762 result = -EIO;
1763 }
1764 break;
1765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001767
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001768 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001769 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001770 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 return result;
1774}
1775
1776/*
1777 * drop ioctl
1778 *
1779 * Immediately put all linked substreams into SETUP state.
1780 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001781static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Takashi Iwai877211f2005-11-17 13:59:38 +01001783 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 int result = 0;
1785
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001786 if (PCM_RUNTIME_CHECK(substream))
1787 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001790 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001791 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1792 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return -EBADFD;
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 snd_pcm_stream_lock_irq(substream);
1796 /* resume pause */
1797 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1798 snd_pcm_pause(substream, 0);
1799
1800 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1801 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1802 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return result;
1805}
1806
1807
Al Viro0888c322013-06-05 14:09:55 -04001808static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Al Viro0888c322013-06-05 14:09:55 -04001810 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001811 unsigned int minor;
1812
Al Viro0888c322013-06-05 14:09:55 -04001813 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1814 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001816 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1817 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
1820/*
1821 * PCM link handling
1822 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001823static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001826 struct snd_pcm_file *pcm_file;
1827 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001828 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001829 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Al Viro0888c322013-06-05 14:09:55 -04001831 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001833 if (!is_pcm_file(f.file)) {
1834 res = -EBADFD;
1835 goto _badf;
1836 }
1837 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001839 group = kmalloc(sizeof(*group), GFP_KERNEL);
1840 if (!group) {
1841 res = -ENOMEM;
1842 goto _nolock;
1843 }
Chanho Minee8dce22018-11-26 14:36:37 +09001844 down_write_nonfifo(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 write_lock_irq(&snd_pcm_link_rwlock);
1846 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001847 substream->runtime->status->state != substream1->runtime->status->state ||
1848 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 res = -EBADFD;
1850 goto _end;
1851 }
1852 if (snd_pcm_stream_linked(substream1)) {
1853 res = -EALREADY;
1854 goto _end;
1855 }
1856 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001857 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001858 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001860 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 INIT_LIST_HEAD(&substream->group->substreams);
1862 list_add_tail(&substream->link_list, &substream->group->substreams);
1863 substream->group->count = 1;
1864 }
1865 list_add_tail(&substream1->link_list, &substream->group->substreams);
1866 substream->group->count++;
1867 substream1->group = substream->group;
1868 _end:
1869 write_unlock_irq(&snd_pcm_link_rwlock);
1870 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001871 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001872 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001873 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001874 _badf:
1875 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return res;
1877}
1878
Takashi Iwai877211f2005-11-17 13:59:38 +01001879static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 substream->group = &substream->self_group;
1882 INIT_LIST_HEAD(&substream->self_group.substreams);
1883 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1884}
1885
Takashi Iwai877211f2005-11-17 13:59:38 +01001886static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Takashi Iwaief991b92007-02-22 12:52:53 +01001888 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 int res = 0;
1890
Chanho Minee8dce22018-11-26 14:36:37 +09001891 down_write_nonfifo(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 write_lock_irq(&snd_pcm_link_rwlock);
1893 if (!snd_pcm_stream_linked(substream)) {
1894 res = -EALREADY;
1895 goto _end;
1896 }
1897 list_del(&substream->link_list);
1898 substream->group->count--;
1899 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001900 snd_pcm_group_for_each_entry(s, substream) {
1901 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 break;
1903 }
1904 kfree(substream->group);
1905 }
1906 relink_to_local(substream);
1907 _end:
1908 write_unlock_irq(&snd_pcm_link_rwlock);
1909 up_write(&snd_pcm_link_rwsem);
1910 return res;
1911}
1912
1913/*
1914 * hw configurator
1915 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001916static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1917 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Takashi Iwai877211f2005-11-17 13:59:38 +01001919 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1921 hw_param_interval_c(params, rule->deps[1]), &t);
1922 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1923}
1924
Takashi Iwai877211f2005-11-17 13:59:38 +01001925static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1926 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Takashi Iwai877211f2005-11-17 13:59:38 +01001928 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1930 hw_param_interval_c(params, rule->deps[1]), &t);
1931 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1932}
1933
Takashi Iwai877211f2005-11-17 13:59:38 +01001934static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1935 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Takashi Iwai877211f2005-11-17 13:59:38 +01001937 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1939 hw_param_interval_c(params, rule->deps[1]),
1940 (unsigned long) rule->private, &t);
1941 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1942}
1943
Takashi Iwai877211f2005-11-17 13:59:38 +01001944static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1945 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Takashi Iwai877211f2005-11-17 13:59:38 +01001947 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1949 (unsigned long) rule->private,
1950 hw_param_interval_c(params, rule->deps[1]), &t);
1951 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1952}
1953
Takashi Iwai877211f2005-11-17 13:59:38 +01001954static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1955 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001958 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1959 struct snd_mask m;
1960 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 snd_mask_any(&m);
1962 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1963 int bits;
1964 if (! snd_mask_test(mask, k))
1965 continue;
1966 bits = snd_pcm_format_physical_width(k);
1967 if (bits <= 0)
1968 continue; /* ignore invalid formats */
1969 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1970 snd_mask_reset(&m, k);
1971 }
1972 return snd_mask_refine(mask, &m);
1973}
1974
Takashi Iwai877211f2005-11-17 13:59:38 +01001975static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1976 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Takashi Iwai877211f2005-11-17 13:59:38 +01001978 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 unsigned int k;
1980 t.min = UINT_MAX;
1981 t.max = 0;
1982 t.openmin = 0;
1983 t.openmax = 0;
1984 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1985 int bits;
1986 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1987 continue;
1988 bits = snd_pcm_format_physical_width(k);
1989 if (bits <= 0)
1990 continue; /* ignore invalid formats */
1991 if (t.min > (unsigned)bits)
1992 t.min = bits;
1993 if (t.max < (unsigned)bits)
1994 t.max = bits;
1995 }
1996 t.integer = 1;
1997 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1998}
1999
2000#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2001#error "Change this table"
2002#endif
2003
2004static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2005 48000, 64000, 88200, 96000, 176400, 192000 };
2006
Clemens Ladisch7653d552007-08-13 17:38:54 +02002007const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2008 .count = ARRAY_SIZE(rates),
2009 .list = rates,
2010};
2011
Takashi Iwai877211f2005-11-17 13:59:38 +01002012static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2013 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Takashi Iwai877211f2005-11-17 13:59:38 +01002015 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002017 snd_pcm_known_rates.count,
2018 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Takashi Iwai877211f2005-11-17 13:59:38 +01002021static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2022 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Takashi Iwai877211f2005-11-17 13:59:38 +01002024 struct snd_interval t;
2025 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 t.min = 0;
2027 t.max = substream->buffer_bytes_max;
2028 t.openmin = 0;
2029 t.openmax = 0;
2030 t.integer = 1;
2031 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2032}
2033
Takashi Iwai877211f2005-11-17 13:59:38 +01002034int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Takashi Iwai877211f2005-11-17 13:59:38 +01002036 struct snd_pcm_runtime *runtime = substream->runtime;
2037 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 int k, err;
2039
2040 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2041 snd_mask_any(constrs_mask(constrs, k));
2042 }
2043
2044 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2045 snd_interval_any(constrs_interval(constrs, k));
2046 }
2047
2048 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2049 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2050 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2051 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2052 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2053
2054 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2055 snd_pcm_hw_rule_format, NULL,
2056 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2057 if (err < 0)
2058 return err;
2059 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2060 snd_pcm_hw_rule_sample_bits, NULL,
2061 SNDRV_PCM_HW_PARAM_FORMAT,
2062 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2063 if (err < 0)
2064 return err;
2065 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2066 snd_pcm_hw_rule_div, NULL,
2067 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2068 if (err < 0)
2069 return err;
2070 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2071 snd_pcm_hw_rule_mul, NULL,
2072 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2073 if (err < 0)
2074 return err;
2075 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2076 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2077 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2078 if (err < 0)
2079 return err;
2080 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2081 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2082 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2083 if (err < 0)
2084 return err;
2085 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2086 snd_pcm_hw_rule_div, NULL,
2087 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2088 if (err < 0)
2089 return err;
2090 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2091 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2092 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2093 if (err < 0)
2094 return err;
2095 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2096 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2097 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2098 if (err < 0)
2099 return err;
2100 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2101 snd_pcm_hw_rule_div, NULL,
2102 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2103 if (err < 0)
2104 return err;
2105 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2106 snd_pcm_hw_rule_div, NULL,
2107 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2108 if (err < 0)
2109 return err;
2110 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2111 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2112 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2113 if (err < 0)
2114 return err;
2115 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2116 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2117 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2118 if (err < 0)
2119 return err;
2120 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2121 snd_pcm_hw_rule_mul, NULL,
2122 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2123 if (err < 0)
2124 return err;
2125 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2126 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2127 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2128 if (err < 0)
2129 return err;
2130 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2131 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2132 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2133 if (err < 0)
2134 return err;
2135 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2136 snd_pcm_hw_rule_muldivk, (void*) 8,
2137 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2138 if (err < 0)
2139 return err;
2140 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2141 snd_pcm_hw_rule_muldivk, (void*) 8,
2142 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2143 if (err < 0)
2144 return err;
2145 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2146 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2147 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2148 if (err < 0)
2149 return err;
2150 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2151 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2152 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2153 if (err < 0)
2154 return err;
2155 return 0;
2156}
2157
Takashi Iwai877211f2005-11-17 13:59:38 +01002158int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Takashi Iwai877211f2005-11-17 13:59:38 +01002160 struct snd_pcm_runtime *runtime = substream->runtime;
2161 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 int err;
2163 unsigned int mask = 0;
2164
2165 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2166 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2167 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2168 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002169 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2171 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2172 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2173 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2174 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2175 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2176 }
2177 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002178 if (err < 0)
2179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002182 if (err < 0)
2183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002186 if (err < 0)
2187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2190 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002191 if (err < 0)
2192 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2195 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002196 if (err < 0)
2197 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2200 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002201 if (err < 0)
2202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2205 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002206 if (err < 0)
2207 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2210 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002211 if (err < 0)
2212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2215 snd_pcm_hw_rule_buffer_bytes_max, substream,
2216 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2217 if (err < 0)
2218 return err;
2219
2220 /* FIXME: remove */
2221 if (runtime->dma_bytes) {
2222 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002223 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
2226
2227 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2228 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2229 snd_pcm_hw_rule_rate, hw,
2230 SNDRV_PCM_HW_PARAM_RATE, -1);
2231 if (err < 0)
2232 return err;
2233 }
2234
2235 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2237
2238 return 0;
2239}
2240
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002241static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Takashi Iwaiba9890a2018-11-29 08:02:49 +01002243 if (snd_pcm_stream_linked(substream))
2244 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002245}
2246
2247void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2248{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002249 substream->ref_count--;
2250 if (substream->ref_count > 0)
2251 return;
2252
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002253 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002254 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002255 if (substream->ops->hw_free &&
2256 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 substream->ops->hw_free(substream);
2258 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002259 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002261 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2262 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002263 if (substream->pcm_release) {
2264 substream->pcm_release(substream);
2265 substream->pcm_release = NULL;
2266 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002267 snd_pcm_detach_substream(substream);
2268}
2269
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002270EXPORT_SYMBOL(snd_pcm_release_substream);
2271
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002272int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2273 struct file *file,
2274 struct snd_pcm_substream **rsubstream)
2275{
2276 struct snd_pcm_substream *substream;
2277 int err;
2278
2279 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2280 if (err < 0)
2281 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002282 if (substream->ref_count > 1) {
2283 *rsubstream = substream;
2284 return 0;
2285 }
2286
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002287 err = snd_pcm_hw_constraints_init(substream);
2288 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002289 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002290 goto error;
2291 }
2292
2293 if ((err = substream->ops->open(substream)) < 0)
2294 goto error;
2295
2296 substream->hw_opened = 1;
2297
2298 err = snd_pcm_hw_constraints_complete(substream);
2299 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002300 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002301 goto error;
2302 }
2303
2304 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002306
2307 error:
2308 snd_pcm_release_substream(substream);
2309 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002312EXPORT_SYMBOL(snd_pcm_open_substream);
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002315 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002316 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Takashi Iwai877211f2005-11-17 13:59:38 +01002318 struct snd_pcm_file *pcm_file;
2319 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002320 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002322 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2323 if (err < 0)
2324 return err;
2325
Takashi Iwai548a6482006-07-31 16:51:51 +02002326 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2327 if (pcm_file == NULL) {
2328 snd_pcm_release_substream(substream);
2329 return -ENOMEM;
2330 }
2331 pcm_file->substream = substream;
2332 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002333 substream->file = pcm_file;
2334 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return 0;
2339}
2340
Clemens Ladischf87135f2005-11-20 14:06:59 +01002341static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Takashi Iwai877211f2005-11-17 13:59:38 +01002343 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002344 int err = nonseekable_open(inode, file);
2345 if (err < 0)
2346 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002347 pcm = snd_lookup_minor_data(iminor(inode),
2348 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002349 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002350 if (pcm)
2351 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002352 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002353}
2354
2355static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2356{
2357 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002358 int err = nonseekable_open(inode, file);
2359 if (err < 0)
2360 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002361 pcm = snd_lookup_minor_data(iminor(inode),
2362 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002363 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002364 if (pcm)
2365 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002366 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002367}
2368
2369static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2370{
2371 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 wait_queue_t wait;
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (pcm == NULL) {
2375 err = -ENODEV;
2376 goto __error1;
2377 }
2378 err = snd_card_file_add(pcm->card, file);
2379 if (err < 0)
2380 goto __error1;
2381 if (!try_module_get(pcm->card->module)) {
2382 err = -EFAULT;
2383 goto __error2;
2384 }
2385 init_waitqueue_entry(&wait, current);
2386 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002387 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002389 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (err >= 0)
2391 break;
2392 if (err == -EAGAIN) {
2393 if (file->f_flags & O_NONBLOCK) {
2394 err = -EBUSY;
2395 break;
2396 }
2397 } else
2398 break;
2399 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002400 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002402 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002403 if (pcm->card->shutdown) {
2404 err = -ENODEV;
2405 break;
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (signal_pending(current)) {
2408 err = -ERESTARTSYS;
2409 break;
2410 }
2411 }
2412 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002413 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (err < 0)
2415 goto __error;
2416 return err;
2417
2418 __error:
2419 module_put(pcm->card->module);
2420 __error2:
2421 snd_card_file_remove(pcm->card, file);
2422 __error1:
2423 return err;
2424}
2425
2426static int snd_pcm_release(struct inode *inode, struct file *file)
2427{
Takashi Iwai877211f2005-11-17 13:59:38 +01002428 struct snd_pcm *pcm;
2429 struct snd_pcm_substream *substream;
2430 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 pcm_file = file->private_data;
2433 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002434 if (snd_BUG_ON(!substream))
2435 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002437 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002438 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002439 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002440 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 wake_up(&pcm->open_wait);
2442 module_put(pcm->card->module);
2443 snd_card_file_remove(pcm->card, file);
2444 return 0;
2445}
2446
Takashi Iwai877211f2005-11-17 13:59:38 +01002447static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2448 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Takashi Iwai877211f2005-11-17 13:59:38 +01002450 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 snd_pcm_sframes_t appl_ptr;
2452 snd_pcm_sframes_t ret;
2453 snd_pcm_sframes_t hw_avail;
2454
2455 if (frames == 0)
2456 return 0;
2457
2458 snd_pcm_stream_lock_irq(substream);
2459 switch (runtime->status->state) {
2460 case SNDRV_PCM_STATE_PREPARED:
2461 break;
2462 case SNDRV_PCM_STATE_DRAINING:
2463 case SNDRV_PCM_STATE_RUNNING:
2464 if (snd_pcm_update_hw_ptr(substream) >= 0)
2465 break;
2466 /* Fall through */
2467 case SNDRV_PCM_STATE_XRUN:
2468 ret = -EPIPE;
2469 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002470 case SNDRV_PCM_STATE_SUSPENDED:
2471 ret = -ESTRPIPE;
2472 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 default:
2474 ret = -EBADFD;
2475 goto __end;
2476 }
2477
2478 hw_avail = snd_pcm_playback_hw_avail(runtime);
2479 if (hw_avail <= 0) {
2480 ret = 0;
2481 goto __end;
2482 }
2483 if (frames > (snd_pcm_uframes_t)hw_avail)
2484 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 appl_ptr = runtime->control->appl_ptr - frames;
2486 if (appl_ptr < 0)
2487 appl_ptr += runtime->boundary;
2488 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 ret = frames;
2490 __end:
2491 snd_pcm_stream_unlock_irq(substream);
2492 return ret;
2493}
2494
Takashi Iwai877211f2005-11-17 13:59:38 +01002495static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2496 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Takashi Iwai877211f2005-11-17 13:59:38 +01002498 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 snd_pcm_sframes_t appl_ptr;
2500 snd_pcm_sframes_t ret;
2501 snd_pcm_sframes_t hw_avail;
2502
2503 if (frames == 0)
2504 return 0;
2505
2506 snd_pcm_stream_lock_irq(substream);
2507 switch (runtime->status->state) {
2508 case SNDRV_PCM_STATE_PREPARED:
2509 case SNDRV_PCM_STATE_DRAINING:
2510 break;
2511 case SNDRV_PCM_STATE_RUNNING:
2512 if (snd_pcm_update_hw_ptr(substream) >= 0)
2513 break;
2514 /* Fall through */
2515 case SNDRV_PCM_STATE_XRUN:
2516 ret = -EPIPE;
2517 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002518 case SNDRV_PCM_STATE_SUSPENDED:
2519 ret = -ESTRPIPE;
2520 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 default:
2522 ret = -EBADFD;
2523 goto __end;
2524 }
2525
2526 hw_avail = snd_pcm_capture_hw_avail(runtime);
2527 if (hw_avail <= 0) {
2528 ret = 0;
2529 goto __end;
2530 }
2531 if (frames > (snd_pcm_uframes_t)hw_avail)
2532 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 appl_ptr = runtime->control->appl_ptr - frames;
2534 if (appl_ptr < 0)
2535 appl_ptr += runtime->boundary;
2536 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 ret = frames;
2538 __end:
2539 snd_pcm_stream_unlock_irq(substream);
2540 return ret;
2541}
2542
Takashi Iwai877211f2005-11-17 13:59:38 +01002543static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2544 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
Takashi Iwai877211f2005-11-17 13:59:38 +01002546 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 snd_pcm_sframes_t appl_ptr;
2548 snd_pcm_sframes_t ret;
2549 snd_pcm_sframes_t avail;
2550
2551 if (frames == 0)
2552 return 0;
2553
2554 snd_pcm_stream_lock_irq(substream);
2555 switch (runtime->status->state) {
2556 case SNDRV_PCM_STATE_PREPARED:
2557 case SNDRV_PCM_STATE_PAUSED:
2558 break;
2559 case SNDRV_PCM_STATE_DRAINING:
2560 case SNDRV_PCM_STATE_RUNNING:
2561 if (snd_pcm_update_hw_ptr(substream) >= 0)
2562 break;
2563 /* Fall through */
2564 case SNDRV_PCM_STATE_XRUN:
2565 ret = -EPIPE;
2566 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002567 case SNDRV_PCM_STATE_SUSPENDED:
2568 ret = -ESTRPIPE;
2569 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 default:
2571 ret = -EBADFD;
2572 goto __end;
2573 }
2574
2575 avail = snd_pcm_playback_avail(runtime);
2576 if (avail <= 0) {
2577 ret = 0;
2578 goto __end;
2579 }
2580 if (frames > (snd_pcm_uframes_t)avail)
2581 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 appl_ptr = runtime->control->appl_ptr + frames;
2583 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2584 appl_ptr -= runtime->boundary;
2585 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 ret = frames;
2587 __end:
2588 snd_pcm_stream_unlock_irq(substream);
2589 return ret;
2590}
2591
Takashi Iwai877211f2005-11-17 13:59:38 +01002592static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2593 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Takashi Iwai877211f2005-11-17 13:59:38 +01002595 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 snd_pcm_sframes_t appl_ptr;
2597 snd_pcm_sframes_t ret;
2598 snd_pcm_sframes_t avail;
2599
2600 if (frames == 0)
2601 return 0;
2602
2603 snd_pcm_stream_lock_irq(substream);
2604 switch (runtime->status->state) {
2605 case SNDRV_PCM_STATE_PREPARED:
2606 case SNDRV_PCM_STATE_DRAINING:
2607 case SNDRV_PCM_STATE_PAUSED:
2608 break;
2609 case SNDRV_PCM_STATE_RUNNING:
2610 if (snd_pcm_update_hw_ptr(substream) >= 0)
2611 break;
2612 /* Fall through */
2613 case SNDRV_PCM_STATE_XRUN:
2614 ret = -EPIPE;
2615 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002616 case SNDRV_PCM_STATE_SUSPENDED:
2617 ret = -ESTRPIPE;
2618 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 default:
2620 ret = -EBADFD;
2621 goto __end;
2622 }
2623
2624 avail = snd_pcm_capture_avail(runtime);
2625 if (avail <= 0) {
2626 ret = 0;
2627 goto __end;
2628 }
2629 if (frames > (snd_pcm_uframes_t)avail)
2630 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 appl_ptr = runtime->control->appl_ptr + frames;
2632 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2633 appl_ptr -= runtime->boundary;
2634 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 ret = frames;
2636 __end:
2637 snd_pcm_stream_unlock_irq(substream);
2638 return ret;
2639}
2640
Takashi Iwai877211f2005-11-17 13:59:38 +01002641static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
Takashi Iwai877211f2005-11-17 13:59:38 +01002643 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 int err;
2645
2646 snd_pcm_stream_lock_irq(substream);
2647 switch (runtime->status->state) {
2648 case SNDRV_PCM_STATE_DRAINING:
2649 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2650 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002651 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 case SNDRV_PCM_STATE_RUNNING:
2653 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2654 break;
2655 /* Fall through */
2656 case SNDRV_PCM_STATE_PREPARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 err = 0;
2658 break;
Jeeja KPf3f6c612016-09-02 21:49:44 +05302659 case SNDRV_PCM_STATE_SUSPENDED:
2660 err = -ESTRPIPE;
2661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 case SNDRV_PCM_STATE_XRUN:
2663 err = -EPIPE;
2664 break;
2665 default:
2666 __badfd:
2667 err = -EBADFD;
2668 break;
2669 }
2670 snd_pcm_stream_unlock_irq(substream);
2671 return err;
2672}
2673
Takashi Iwai877211f2005-11-17 13:59:38 +01002674static int snd_pcm_delay(struct snd_pcm_substream *substream,
2675 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
Takashi Iwai877211f2005-11-17 13:59:38 +01002677 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 int err;
2679 snd_pcm_sframes_t n = 0;
2680
2681 snd_pcm_stream_lock_irq(substream);
2682 switch (runtime->status->state) {
2683 case SNDRV_PCM_STATE_DRAINING:
2684 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2685 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002686 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 case SNDRV_PCM_STATE_RUNNING:
2688 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2689 break;
2690 /* Fall through */
2691 case SNDRV_PCM_STATE_PREPARED:
2692 case SNDRV_PCM_STATE_SUSPENDED:
2693 err = 0;
2694 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2695 n = snd_pcm_playback_hw_avail(runtime);
2696 else
2697 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002698 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 break;
2700 case SNDRV_PCM_STATE_XRUN:
2701 err = -EPIPE;
2702 break;
2703 default:
2704 __badfd:
2705 err = -EBADFD;
2706 break;
2707 }
2708 snd_pcm_stream_unlock_irq(substream);
2709 if (!err)
2710 if (put_user(n, res))
2711 err = -EFAULT;
2712 return err;
2713}
2714
Takashi Iwai877211f2005-11-17 13:59:38 +01002715static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2716 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Takashi Iwai877211f2005-11-17 13:59:38 +01002718 struct snd_pcm_runtime *runtime = substream->runtime;
2719 struct snd_pcm_sync_ptr sync_ptr;
2720 volatile struct snd_pcm_mmap_status *status;
2721 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 int err;
2723
2724 memset(&sync_ptr, 0, sizeof(sync_ptr));
2725 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2726 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002727 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 -07002728 return -EFAULT;
2729 status = runtime->status;
2730 control = runtime->control;
2731 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2732 err = snd_pcm_hwsync(substream);
2733 if (err < 0)
2734 return err;
2735 }
2736 snd_pcm_stream_lock_irq(substream);
2737 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2738 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2739 else
2740 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2741 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2742 control->avail_min = sync_ptr.c.control.avail_min;
2743 else
2744 sync_ptr.c.control.avail_min = control->avail_min;
2745 sync_ptr.s.status.state = status->state;
2746 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2747 sync_ptr.s.status.tstamp = status->tstamp;
2748 sync_ptr.s.status.suspended_state = status->suspended_state;
David Henningsson1704aa12018-04-21 14:57:40 +02002749 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 snd_pcm_stream_unlock_irq(substream);
2751 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2752 return -EFAULT;
2753 return 0;
2754}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002755
2756static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2757{
2758 struct snd_pcm_runtime *runtime = substream->runtime;
2759 int arg;
2760
2761 if (get_user(arg, _arg))
2762 return -EFAULT;
2763 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2764 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002765 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002766 return 0;
2767}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Takashi Iwai0df63e42006-04-28 15:13:41 +02002769static int snd_pcm_common_ioctl1(struct file *file,
2770 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 unsigned int cmd, void __user *arg)
2772{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 switch (cmd) {
2774 case SNDRV_PCM_IOCTL_PVERSION:
2775 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2776 case SNDRV_PCM_IOCTL_INFO:
2777 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002778 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2779 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002780 case SNDRV_PCM_IOCTL_TTSTAMP:
2781 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 case SNDRV_PCM_IOCTL_HW_REFINE:
2783 return snd_pcm_hw_refine_user(substream, arg);
2784 case SNDRV_PCM_IOCTL_HW_PARAMS:
2785 return snd_pcm_hw_params_user(substream, arg);
2786 case SNDRV_PCM_IOCTL_HW_FREE:
2787 return snd_pcm_hw_free(substream);
2788 case SNDRV_PCM_IOCTL_SW_PARAMS:
2789 return snd_pcm_sw_params_user(substream, arg);
2790 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002791 return snd_pcm_status_user(substream, arg, false);
2792 case SNDRV_PCM_IOCTL_STATUS_EXT:
2793 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2795 return snd_pcm_channel_info_user(substream, arg);
2796 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002797 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 case SNDRV_PCM_IOCTL_RESET:
2799 return snd_pcm_reset(substream);
2800 case SNDRV_PCM_IOCTL_START:
2801 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2802 case SNDRV_PCM_IOCTL_LINK:
2803 return snd_pcm_link(substream, (int)(unsigned long) arg);
2804 case SNDRV_PCM_IOCTL_UNLINK:
2805 return snd_pcm_unlink(substream);
2806 case SNDRV_PCM_IOCTL_RESUME:
2807 return snd_pcm_resume(substream);
2808 case SNDRV_PCM_IOCTL_XRUN:
2809 return snd_pcm_xrun(substream);
2810 case SNDRV_PCM_IOCTL_HWSYNC:
2811 return snd_pcm_hwsync(substream);
2812 case SNDRV_PCM_IOCTL_DELAY:
2813 return snd_pcm_delay(substream, arg);
2814 case SNDRV_PCM_IOCTL_SYNC_PTR:
2815 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002816#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2818 return snd_pcm_hw_refine_old_user(substream, arg);
2819 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2820 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002823 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 case SNDRV_PCM_IOCTL_DROP:
2825 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002826 case SNDRV_PCM_IOCTL_PAUSE:
2827 {
2828 int res;
2829 snd_pcm_stream_lock_irq(substream);
2830 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2831 snd_pcm_stream_unlock_irq(substream);
2832 return res;
2833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002835 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return -ENOTTY;
2837}
2838
Takashi Iwai0df63e42006-04-28 15:13:41 +02002839static int snd_pcm_playback_ioctl1(struct file *file,
2840 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 unsigned int cmd, void __user *arg)
2842{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002843 if (snd_BUG_ON(!substream))
2844 return -ENXIO;
2845 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2846 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 switch (cmd) {
2848 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2849 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002850 struct snd_xferi xferi;
2851 struct snd_xferi __user *_xferi = arg;
2852 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 snd_pcm_sframes_t result;
2854 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2855 return -EBADFD;
2856 if (put_user(0, &_xferi->result))
2857 return -EFAULT;
2858 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2859 return -EFAULT;
2860 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2861 __put_user(result, &_xferi->result);
2862 return result < 0 ? result : 0;
2863 }
2864 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2865 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002866 struct snd_xfern xfern;
2867 struct snd_xfern __user *_xfern = arg;
2868 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 void __user **bufs;
2870 snd_pcm_sframes_t result;
2871 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2872 return -EBADFD;
2873 if (runtime->channels > 128)
2874 return -EINVAL;
2875 if (put_user(0, &_xfern->result))
2876 return -EFAULT;
2877 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2878 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002879
2880 bufs = memdup_user(xfern.bufs,
2881 sizeof(void *) * runtime->channels);
2882 if (IS_ERR(bufs))
2883 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2885 kfree(bufs);
2886 __put_user(result, &_xfern->result);
2887 return result < 0 ? result : 0;
2888 }
2889 case SNDRV_PCM_IOCTL_REWIND:
2890 {
2891 snd_pcm_uframes_t frames;
2892 snd_pcm_uframes_t __user *_frames = arg;
2893 snd_pcm_sframes_t result;
2894 if (get_user(frames, _frames))
2895 return -EFAULT;
2896 if (put_user(0, _frames))
2897 return -EFAULT;
2898 result = snd_pcm_playback_rewind(substream, frames);
2899 __put_user(result, _frames);
2900 return result < 0 ? result : 0;
2901 }
2902 case SNDRV_PCM_IOCTL_FORWARD:
2903 {
2904 snd_pcm_uframes_t frames;
2905 snd_pcm_uframes_t __user *_frames = arg;
2906 snd_pcm_sframes_t result;
2907 if (get_user(frames, _frames))
2908 return -EFAULT;
2909 if (put_user(0, _frames))
2910 return -EFAULT;
2911 result = snd_pcm_playback_forward(substream, frames);
2912 __put_user(result, _frames);
2913 return result < 0 ? result : 0;
2914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002916 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918
Takashi Iwai0df63e42006-04-28 15:13:41 +02002919static int snd_pcm_capture_ioctl1(struct file *file,
2920 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 unsigned int cmd, void __user *arg)
2922{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002923 if (snd_BUG_ON(!substream))
2924 return -ENXIO;
2925 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2926 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 switch (cmd) {
2928 case SNDRV_PCM_IOCTL_READI_FRAMES:
2929 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002930 struct snd_xferi xferi;
2931 struct snd_xferi __user *_xferi = arg;
2932 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 snd_pcm_sframes_t result;
2934 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2935 return -EBADFD;
2936 if (put_user(0, &_xferi->result))
2937 return -EFAULT;
2938 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2939 return -EFAULT;
2940 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2941 __put_user(result, &_xferi->result);
2942 return result < 0 ? result : 0;
2943 }
2944 case SNDRV_PCM_IOCTL_READN_FRAMES:
2945 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002946 struct snd_xfern xfern;
2947 struct snd_xfern __user *_xfern = arg;
2948 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 void *bufs;
2950 snd_pcm_sframes_t result;
2951 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2952 return -EBADFD;
2953 if (runtime->channels > 128)
2954 return -EINVAL;
2955 if (put_user(0, &_xfern->result))
2956 return -EFAULT;
2957 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2958 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002959
2960 bufs = memdup_user(xfern.bufs,
2961 sizeof(void *) * runtime->channels);
2962 if (IS_ERR(bufs))
2963 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2965 kfree(bufs);
2966 __put_user(result, &_xfern->result);
2967 return result < 0 ? result : 0;
2968 }
2969 case SNDRV_PCM_IOCTL_REWIND:
2970 {
2971 snd_pcm_uframes_t frames;
2972 snd_pcm_uframes_t __user *_frames = arg;
2973 snd_pcm_sframes_t result;
2974 if (get_user(frames, _frames))
2975 return -EFAULT;
2976 if (put_user(0, _frames))
2977 return -EFAULT;
2978 result = snd_pcm_capture_rewind(substream, frames);
2979 __put_user(result, _frames);
2980 return result < 0 ? result : 0;
2981 }
2982 case SNDRV_PCM_IOCTL_FORWARD:
2983 {
2984 snd_pcm_uframes_t frames;
2985 snd_pcm_uframes_t __user *_frames = arg;
2986 snd_pcm_sframes_t result;
2987 if (get_user(frames, _frames))
2988 return -EFAULT;
2989 if (put_user(0, _frames))
2990 return -EFAULT;
2991 result = snd_pcm_capture_forward(substream, frames);
2992 __put_user(result, _frames);
2993 return result < 0 ? result : 0;
2994 }
2995 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002996 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
Takashi Iwai877211f2005-11-17 13:59:38 +01002999static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
3000 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Takashi Iwai877211f2005-11-17 13:59:38 +01003002 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 pcm_file = file->private_data;
3005
3006 if (((cmd >> 8) & 0xff) != 'A')
3007 return -ENOTTY;
3008
Takashi Iwai0df63e42006-04-28 15:13:41 +02003009 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3010 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
Takashi Iwai877211f2005-11-17 13:59:38 +01003013static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3014 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Takashi Iwai877211f2005-11-17 13:59:38 +01003016 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 pcm_file = file->private_data;
3019
3020 if (((cmd >> 8) & 0xff) != 'A')
3021 return -ENOTTY;
3022
Takashi Iwai0df63e42006-04-28 15:13:41 +02003023 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3024 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
Takashi Iwai877211f2005-11-17 13:59:38 +01003027int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 unsigned int cmd, void *arg)
3029{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003030 mm_segment_t fs;
3031 int result;
3032
3033 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 switch (substream->stream) {
3035 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003036 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3037 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003040 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3041 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003042 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003044 result = -EINVAL;
3045 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003047 snd_leave_user(fs);
3048 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003051EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3052
Takashi Iwai877211f2005-11-17 13:59:38 +01003053static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3054 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Takashi Iwai877211f2005-11-17 13:59:38 +01003056 struct snd_pcm_file *pcm_file;
3057 struct snd_pcm_substream *substream;
3058 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 snd_pcm_sframes_t result;
3060
3061 pcm_file = file->private_data;
3062 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003063 if (PCM_RUNTIME_CHECK(substream))
3064 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 runtime = substream->runtime;
3066 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3067 return -EBADFD;
3068 if (!frame_aligned(runtime, count))
3069 return -EINVAL;
3070 count = bytes_to_frames(runtime, count);
3071 result = snd_pcm_lib_read(substream, buf, count);
3072 if (result > 0)
3073 result = frames_to_bytes(runtime, result);
3074 return result;
3075}
3076
Takashi Iwai877211f2005-11-17 13:59:38 +01003077static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3078 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
Takashi Iwai877211f2005-11-17 13:59:38 +01003080 struct snd_pcm_file *pcm_file;
3081 struct snd_pcm_substream *substream;
3082 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 snd_pcm_sframes_t result;
3084
3085 pcm_file = file->private_data;
3086 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003087 if (PCM_RUNTIME_CHECK(substream))
3088 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003090 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3091 return -EBADFD;
3092 if (!frame_aligned(runtime, count))
3093 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 count = bytes_to_frames(runtime, count);
3095 result = snd_pcm_lib_write(substream, buf, count);
3096 if (result > 0)
3097 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return result;
3099}
3100
Al Viro1c65d982015-04-04 00:19:32 -04003101static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Takashi Iwai877211f2005-11-17 13:59:38 +01003103 struct snd_pcm_file *pcm_file;
3104 struct snd_pcm_substream *substream;
3105 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 snd_pcm_sframes_t result;
3107 unsigned long i;
3108 void __user **bufs;
3109 snd_pcm_uframes_t frames;
3110
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003111 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003113 if (PCM_RUNTIME_CHECK(substream))
3114 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 runtime = substream->runtime;
3116 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3117 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003118 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003120 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003122 if (!frame_aligned(runtime, to->iov->iov_len))
3123 return -EINVAL;
3124 frames = bytes_to_samples(runtime, to->iov->iov_len);
3125 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (bufs == NULL)
3127 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003128 for (i = 0; i < to->nr_segs; ++i)
3129 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 result = snd_pcm_lib_readv(substream, bufs, frames);
3131 if (result > 0)
3132 result = frames_to_bytes(runtime, result);
3133 kfree(bufs);
3134 return result;
3135}
3136
Al Viro1c65d982015-04-04 00:19:32 -04003137static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Takashi Iwai877211f2005-11-17 13:59:38 +01003139 struct snd_pcm_file *pcm_file;
3140 struct snd_pcm_substream *substream;
3141 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 snd_pcm_sframes_t result;
3143 unsigned long i;
3144 void __user **bufs;
3145 snd_pcm_uframes_t frames;
3146
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003147 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003149 if (PCM_RUNTIME_CHECK(substream))
3150 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003152 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3153 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003154 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003155 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003156 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3157 !frame_aligned(runtime, from->iov->iov_len))
3158 return -EINVAL;
3159 frames = bytes_to_samples(runtime, from->iov->iov_len);
3160 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (bufs == NULL)
3162 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003163 for (i = 0; i < from->nr_segs; ++i)
3164 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 result = snd_pcm_lib_writev(substream, bufs, frames);
3166 if (result > 0)
3167 result = frames_to_bytes(runtime, result);
3168 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return result;
3170}
3171
3172static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3173{
Takashi Iwai877211f2005-11-17 13:59:38 +01003174 struct snd_pcm_file *pcm_file;
3175 struct snd_pcm_substream *substream;
3176 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 unsigned int mask;
3178 snd_pcm_uframes_t avail;
3179
3180 pcm_file = file->private_data;
3181
3182 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003183 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003184 return POLLOUT | POLLWRNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 runtime = substream->runtime;
3186
3187 poll_wait(file, &runtime->sleep, wait);
3188
3189 snd_pcm_stream_lock_irq(substream);
3190 avail = snd_pcm_playback_avail(runtime);
3191 switch (runtime->status->state) {
3192 case SNDRV_PCM_STATE_RUNNING:
3193 case SNDRV_PCM_STATE_PREPARED:
3194 case SNDRV_PCM_STATE_PAUSED:
3195 if (avail >= runtime->control->avail_min) {
3196 mask = POLLOUT | POLLWRNORM;
3197 break;
3198 }
3199 /* Fall through */
3200 case SNDRV_PCM_STATE_DRAINING:
3201 mask = 0;
3202 break;
3203 default:
3204 mask = POLLOUT | POLLWRNORM | POLLERR;
3205 break;
3206 }
3207 snd_pcm_stream_unlock_irq(substream);
3208 return mask;
3209}
3210
3211static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3212{
Takashi Iwai877211f2005-11-17 13:59:38 +01003213 struct snd_pcm_file *pcm_file;
3214 struct snd_pcm_substream *substream;
3215 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 unsigned int mask;
3217 snd_pcm_uframes_t avail;
3218
3219 pcm_file = file->private_data;
3220
3221 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003222 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003223 return POLLIN | POLLRDNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 runtime = substream->runtime;
3225
3226 poll_wait(file, &runtime->sleep, wait);
3227
3228 snd_pcm_stream_lock_irq(substream);
3229 avail = snd_pcm_capture_avail(runtime);
3230 switch (runtime->status->state) {
3231 case SNDRV_PCM_STATE_RUNNING:
3232 case SNDRV_PCM_STATE_PREPARED:
3233 case SNDRV_PCM_STATE_PAUSED:
3234 if (avail >= runtime->control->avail_min) {
3235 mask = POLLIN | POLLRDNORM;
3236 break;
3237 }
3238 mask = 0;
3239 break;
3240 case SNDRV_PCM_STATE_DRAINING:
3241 if (avail > 0) {
3242 mask = POLLIN | POLLRDNORM;
3243 break;
3244 }
3245 /* Fall through */
3246 default:
3247 mask = POLLIN | POLLRDNORM | POLLERR;
3248 break;
3249 }
3250 snd_pcm_stream_unlock_irq(substream);
3251 return mask;
3252}
3253
3254/*
3255 * mmap support
3256 */
3257
3258/*
3259 * Only on coherent architectures, we can mmap the status and the control records
3260 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3261 */
3262#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3263/*
3264 * mmap status record
3265 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003266static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3267 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Takashi Iwai877211f2005-11-17 13:59:38 +01003269 struct snd_pcm_substream *substream = area->vm_private_data;
3270 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
3272 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003273 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003275 vmf->page = virt_to_page(runtime->status);
3276 get_page(vmf->page);
3277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003280static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003282 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283};
3284
Takashi Iwai877211f2005-11-17 13:59:38 +01003285static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 struct vm_area_struct *area)
3287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 long size;
3289 if (!(area->vm_flags & VM_READ))
3290 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003292 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return -EINVAL;
3294 area->vm_ops = &snd_pcm_vm_ops_status;
3295 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003296 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return 0;
3298}
3299
3300/*
3301 * mmap control record
3302 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003303static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3304 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
Takashi Iwai877211f2005-11-17 13:59:38 +01003306 struct snd_pcm_substream *substream = area->vm_private_data;
3307 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003310 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003312 vmf->page = virt_to_page(runtime->control);
3313 get_page(vmf->page);
3314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003317static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003319 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320};
3321
Takashi Iwai877211f2005-11-17 13:59:38 +01003322static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 struct vm_area_struct *area)
3324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 long size;
3326 if (!(area->vm_flags & VM_READ))
3327 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003329 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return -EINVAL;
3331 area->vm_ops = &snd_pcm_vm_ops_control;
3332 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003333 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 return 0;
3335}
3336#else /* ! coherent mmap */
3337/*
3338 * don't support mmap for status and control records.
3339 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003340static int snd_pcm_mmap_status(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}
Takashi Iwai877211f2005-11-17 13:59:38 +01003345static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 struct vm_area_struct *area)
3347{
3348 return -ENXIO;
3349}
3350#endif /* coherent mmap */
3351
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003352static inline struct page *
3353snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3354{
3355 void *vaddr = substream->runtime->dma_area + ofs;
3356 return virt_to_page(vaddr);
3357}
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003360 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003362static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3363 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
Takashi Iwai877211f2005-11-17 13:59:38 +01003365 struct snd_pcm_substream *substream = area->vm_private_data;
3366 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 unsigned long offset;
3368 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 size_t dma_bytes;
3370
3371 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003372 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003374 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3376 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003377 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003378 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003380 else
3381 page = snd_pcm_default_page_ops(substream, offset);
3382 if (!page)
3383 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003384 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003385 vmf->page = page;
3386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387}
3388
Takashi Iwai657b1982009-11-26 12:40:21 +01003389static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3390 .open = snd_pcm_mmap_data_open,
3391 .close = snd_pcm_mmap_data_close,
3392};
3393
3394static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 .open = snd_pcm_mmap_data_open,
3396 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003397 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398};
3399
3400/*
3401 * mmap the DMA buffer on RAM
3402 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003403
3404/**
3405 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3406 * @substream: PCM substream
3407 * @area: VMA
3408 *
3409 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3410 * this function is invoked implicitly.
3411 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003412int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3413 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003415 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003416#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003417 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3418 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3419 return remap_pfn_range(area, area->vm_start,
3420 substream->dma_buffer.addr >> PAGE_SHIFT,
3421 area->vm_end - area->vm_start, area->vm_page_prot);
3422 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003423#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003424#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003425 if (!substream->ops->page &&
3426 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3427 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3428 area,
3429 substream->runtime->dma_area,
3430 substream->runtime->dma_addr,
Stefan Roesed5a2bcb2018-03-26 16:10:21 +02003431 substream->runtime->dma_bytes);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003432#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003433 /* mmap with fault handler */
3434 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return 0;
3436}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003437EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439/*
3440 * mmap the DMA buffer on I/O memory area
3441 */
3442#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003443/**
3444 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3445 * @substream: PCM substream
3446 * @area: VMA
3447 *
3448 * When your hardware uses the iomapped pages as the hardware buffer and
3449 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3450 * is supposed to work only on limited architectures.
3451 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003452int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3453 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003455 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003458 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003460
3461EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462#endif /* SNDRV_PCM_INFO_MMAP */
3463
3464/*
3465 * mmap DMA buffer
3466 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003467int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 struct vm_area_struct *area)
3469{
Takashi Iwai877211f2005-11-17 13:59:38 +01003470 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 long size;
3472 unsigned long offset;
3473 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003474 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3477 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3478 return -EINVAL;
3479 } else {
3480 if (!(area->vm_flags & VM_READ))
3481 return -EINVAL;
3482 }
3483 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3485 return -EBADFD;
3486 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3487 return -ENXIO;
3488 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3489 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3490 return -EINVAL;
3491 size = area->vm_end - area->vm_start;
3492 offset = area->vm_pgoff << PAGE_SHIFT;
3493 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3494 if ((size_t)size > dma_bytes)
3495 return -EINVAL;
3496 if (offset > dma_bytes - size)
3497 return -EINVAL;
3498
Takashi Iwai657b1982009-11-26 12:40:21 +01003499 area->vm_ops = &snd_pcm_vm_ops_data;
3500 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003502 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003504 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003505 if (!err)
3506 atomic_inc(&substream->mmap_count);
3507 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508}
3509
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003510EXPORT_SYMBOL(snd_pcm_mmap_data);
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3513{
Takashi Iwai877211f2005-11-17 13:59:38 +01003514 struct snd_pcm_file * pcm_file;
3515 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 unsigned long offset;
3517
3518 pcm_file = file->private_data;
3519 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003520 if (PCM_RUNTIME_CHECK(substream))
3521 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
3523 offset = area->vm_pgoff << PAGE_SHIFT;
3524 switch (offset) {
3525 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003526 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return -ENXIO;
3528 return snd_pcm_mmap_status(substream, file, area);
3529 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003530 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return -ENXIO;
3532 return snd_pcm_mmap_control(substream, file, area);
3533 default:
3534 return snd_pcm_mmap_data(substream, file, area);
3535 }
3536 return 0;
3537}
3538
3539static int snd_pcm_fasync(int fd, struct file * file, int on)
3540{
Takashi Iwai877211f2005-11-17 13:59:38 +01003541 struct snd_pcm_file * pcm_file;
3542 struct snd_pcm_substream *substream;
3543 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 pcm_file = file->private_data;
3546 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003547 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003548 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003550 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551}
3552
3553/*
3554 * ioctl32 compat
3555 */
3556#ifdef CONFIG_COMPAT
3557#include "pcm_compat.c"
3558#else
3559#define snd_pcm_ioctl_compat NULL
3560#endif
3561
3562/*
3563 * To be removed helpers to keep binary compatibility
3564 */
3565
Takashi Iwai59d48582005-12-01 10:51:58 +01003566#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3568#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3569
Takashi Iwai877211f2005-11-17 13:59:38 +01003570static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3571 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
3573 unsigned int i;
3574
3575 memset(params, 0, sizeof(*params));
3576 params->flags = oparams->flags;
3577 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3578 params->masks[i].bits[0] = oparams->masks[i];
3579 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3580 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3581 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3582 params->info = oparams->info;
3583 params->msbits = oparams->msbits;
3584 params->rate_num = oparams->rate_num;
3585 params->rate_den = oparams->rate_den;
3586 params->fifo_size = oparams->fifo_size;
3587}
3588
Takashi Iwai877211f2005-11-17 13:59:38 +01003589static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3590 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
3592 unsigned int i;
3593
3594 memset(oparams, 0, sizeof(*oparams));
3595 oparams->flags = params->flags;
3596 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3597 oparams->masks[i] = params->masks[i].bits[0];
3598 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3599 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3600 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3601 oparams->info = params->info;
3602 oparams->msbits = params->msbits;
3603 oparams->rate_num = params->rate_num;
3604 oparams->rate_den = params->rate_den;
3605 oparams->fifo_size = params->fifo_size;
3606}
3607
Takashi Iwai877211f2005-11-17 13:59:38 +01003608static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3609 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
Takashi Iwai877211f2005-11-17 13:59:38 +01003611 struct snd_pcm_hw_params *params;
3612 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 int err;
3614
3615 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003616 if (!params)
3617 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Li Zefanef44a1e2009-04-10 09:43:08 +08003619 oparams = memdup_user(_oparams, sizeof(*oparams));
3620 if (IS_ERR(oparams)) {
3621 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 goto out;
3623 }
3624 snd_pcm_hw_convert_from_old_params(params, oparams);
3625 err = snd_pcm_hw_refine(substream, params);
3626 snd_pcm_hw_convert_to_old_params(oparams, params);
3627 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3628 if (!err)
3629 err = -EFAULT;
3630 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003631
3632 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633out:
3634 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 return err;
3636}
3637
Takashi Iwai877211f2005-11-17 13:59:38 +01003638static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3639 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
Takashi Iwai877211f2005-11-17 13:59:38 +01003641 struct snd_pcm_hw_params *params;
3642 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 int err;
3644
3645 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003646 if (!params)
3647 return -ENOMEM;
3648
3649 oparams = memdup_user(_oparams, sizeof(*oparams));
3650 if (IS_ERR(oparams)) {
3651 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 goto out;
3653 }
3654 snd_pcm_hw_convert_from_old_params(params, oparams);
3655 err = snd_pcm_hw_params(substream, params);
3656 snd_pcm_hw_convert_to_old_params(oparams, params);
3657 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3658 if (!err)
3659 err = -EFAULT;
3660 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003661
3662 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663out:
3664 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 return err;
3666}
Takashi Iwai59d48582005-12-01 10:51:58 +01003667#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Cliff Cai70036092008-09-03 10:54:36 +02003669#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003670static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3671 unsigned long addr,
3672 unsigned long len,
3673 unsigned long pgoff,
3674 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003675{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003676 struct snd_pcm_file *pcm_file = file->private_data;
3677 struct snd_pcm_substream *substream = pcm_file->substream;
3678 struct snd_pcm_runtime *runtime = substream->runtime;
3679 unsigned long offset = pgoff << PAGE_SHIFT;
3680
3681 switch (offset) {
3682 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3683 return (unsigned long)runtime->status;
3684 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3685 return (unsigned long)runtime->control;
3686 default:
3687 return (unsigned long)runtime->dma_area + offset;
3688 }
Cliff Cai70036092008-09-03 10:54:36 +02003689}
3690#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003691# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003692#endif
3693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694/*
3695 * Register section
3696 */
3697
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003698const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003700 .owner = THIS_MODULE,
3701 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003702 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003703 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003704 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003705 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003706 .poll = snd_pcm_playback_poll,
3707 .unlocked_ioctl = snd_pcm_playback_ioctl,
3708 .compat_ioctl = snd_pcm_ioctl_compat,
3709 .mmap = snd_pcm_mmap,
3710 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003711 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 },
3713 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003714 .owner = THIS_MODULE,
3715 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003716 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003717 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003718 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003719 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003720 .poll = snd_pcm_capture_poll,
3721 .unlocked_ioctl = snd_pcm_capture_ioctl,
3722 .compat_ioctl = snd_pcm_ioctl_compat,
3723 .mmap = snd_pcm_mmap,
3724 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003725 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 }
3727};