blob: 8e5649a1d2068732afd8590cc007be2a7baa56ca [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>
Sudheer Papothib40e9682016-01-29 02:19:04 +053032#include <sound/compress_offload.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/info.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/timer.h>
37#include <sound/minors.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080038#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Compatibility
42 */
43
Takashi Iwai877211f2005-11-17 13:59:38 +010044struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned int flags;
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010048 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
50 unsigned int rmask;
51 unsigned int cmask;
52 unsigned int info;
53 unsigned int msbits;
54 unsigned int rate_num;
55 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010056 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned char reserved[64];
58};
59
Takashi Iwai59d48582005-12-01 10:51:58 +010060#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010061#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Takashi Iwai877211f2005-11-17 13:59:38 +010064static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010068#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010069static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 *
73 */
74
Takashi Iwai7af142f2014-09-01 11:19:37 +020075static DEFINE_RWLOCK(snd_pcm_link_rwlock);
76static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Takashi 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
82 * spin until it gets the lock.
83 */
84static inline void down_write_nonblock(struct rw_semaphore *lock)
85{
86 while (!down_write_trylock(lock))
87 cond_resched();
88}
89
Takashi Iwai30b771c2014-10-30 15:02:50 +010090/**
91 * snd_pcm_stream_lock - Lock the PCM stream
92 * @substream: PCM substream
93 *
94 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
95 * flag of the given substream. This also takes the global link rw lock
96 * (or rw sem), too, for avoiding the race with linked streams.
97 */
Takashi Iwai7af142f2014-09-01 11:19:37 +020098void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
99{
100 if (substream->pcm->nonatomic) {
Takashi Iwai67756e32015-07-17 15:22:33 +0200101 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200102 mutex_lock(&substream->self_group.mutex);
103 } else {
104 read_lock(&snd_pcm_link_rwlock);
105 spin_lock(&substream->self_group.lock);
106 }
107}
108EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
109
Takashi Iwai30b771c2014-10-30 15:02:50 +0100110/**
111 * snd_pcm_stream_lock - Unlock the PCM stream
112 * @substream: PCM substream
113 *
114 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
115 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200116void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
117{
118 if (substream->pcm->nonatomic) {
119 mutex_unlock(&substream->self_group.mutex);
120 up_read(&snd_pcm_link_rwsem);
121 } else {
122 spin_unlock(&substream->self_group.lock);
123 read_unlock(&snd_pcm_link_rwlock);
124 }
125}
126EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
127
Takashi Iwai30b771c2014-10-30 15:02:50 +0100128/**
129 * snd_pcm_stream_lock_irq - Lock the PCM stream
130 * @substream: PCM substream
131 *
132 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
133 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
134 * as snd_pcm_stream_lock().
135 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200136void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
137{
138 if (!substream->pcm->nonatomic)
139 local_irq_disable();
140 snd_pcm_stream_lock(substream);
141}
142EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
143
Takashi Iwai30b771c2014-10-30 15:02:50 +0100144/**
145 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
146 * @substream: PCM substream
147 *
148 * This is a counter-part of snd_pcm_stream_lock_irq().
149 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200150void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
151{
152 snd_pcm_stream_unlock(substream);
153 if (!substream->pcm->nonatomic)
154 local_irq_enable();
155}
156EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
157
158unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
159{
160 unsigned long flags = 0;
161 if (!substream->pcm->nonatomic)
162 local_irq_save(flags);
163 snd_pcm_stream_lock(substream);
164 return flags;
165}
166EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
167
Takashi Iwai30b771c2014-10-30 15:02:50 +0100168/**
169 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
170 * @substream: PCM substream
171 * @flags: irq flags
172 *
173 * This is a counter-part of snd_pcm_stream_lock_irqsave().
174 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200175void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
176 unsigned long flags)
177{
178 snd_pcm_stream_unlock(substream);
179 if (!substream->pcm->nonatomic)
180 local_irq_restore(flags);
181}
182EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184static inline mm_segment_t snd_enter_user(void)
185{
186 mm_segment_t fs = get_fs();
187 set_fs(get_ds());
188 return fs;
189}
190
191static inline void snd_leave_user(mm_segment_t fs)
192{
193 set_fs(fs);
194}
195
196
197
Takashi Iwai877211f2005-11-17 13:59:38 +0100198int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Takashi Iwai877211f2005-11-17 13:59:38 +0100200 struct snd_pcm *pcm = substream->pcm;
201 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 memset(info, 0, sizeof(*info));
204 info->card = pcm->card->number;
205 info->device = pcm->device;
206 info->stream = substream->stream;
207 info->subdevice = substream->number;
208 strlcpy(info->id, pcm->id, sizeof(info->id));
209 strlcpy(info->name, pcm->name, sizeof(info->name));
210 info->dev_class = pcm->dev_class;
211 info->dev_subclass = pcm->dev_subclass;
212 info->subdevices_count = pstr->substream_count;
213 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
214 strlcpy(info->subname, substream->name, sizeof(info->subname));
Karthikeyan Mani078c7b332017-10-02 16:56:55 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217}
218
Takashi Iwai877211f2005-11-17 13:59:38 +0100219int snd_pcm_info_user(struct snd_pcm_substream *substream,
220 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Takashi Iwai877211f2005-11-17 13:59:38 +0100222 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int err;
224
225 info = kmalloc(sizeof(*info), GFP_KERNEL);
226 if (! info)
227 return -ENOMEM;
228 err = snd_pcm_info(substream, info);
229 if (err >= 0) {
230 if (copy_to_user(_info, info, sizeof(*info)))
231 err = -EFAULT;
232 }
233 kfree(info);
234 return err;
235}
236
Takashi Iwai63825f32014-10-22 12:04:46 +0200237static bool hw_support_mmap(struct snd_pcm_substream *substream)
238{
239 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
240 return false;
241 /* check architectures that return -EINVAL from dma_mmap_coherent() */
242 /* FIXME: this should be some global flag */
243#if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
244 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
245 if (!substream->ops->mmap &&
246 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
247 return false;
248#endif
249 return true;
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#undef RULES_DEBUG
253
254#ifdef RULES_DEBUG
255#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
Joe Perches47023ec2010-09-13 21:24:02 -0700256static const char * const snd_pcm_hw_param_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 HW_PARAM(ACCESS),
258 HW_PARAM(FORMAT),
259 HW_PARAM(SUBFORMAT),
260 HW_PARAM(SAMPLE_BITS),
261 HW_PARAM(FRAME_BITS),
262 HW_PARAM(CHANNELS),
263 HW_PARAM(RATE),
264 HW_PARAM(PERIOD_TIME),
265 HW_PARAM(PERIOD_SIZE),
266 HW_PARAM(PERIOD_BYTES),
267 HW_PARAM(PERIODS),
268 HW_PARAM(BUFFER_TIME),
269 HW_PARAM(BUFFER_SIZE),
270 HW_PARAM(BUFFER_BYTES),
271 HW_PARAM(TICK_TIME),
272};
273#endif
274
Takashi Iwai877211f2005-11-17 13:59:38 +0100275int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
276 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100279 struct snd_pcm_hardware *hw;
280 struct snd_interval *i = NULL;
281 struct snd_mask *m = NULL;
282 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unsigned int rstamps[constrs->rules_num];
284 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
285 unsigned int stamp = 2;
286 int changed, again;
287
288 params->info = 0;
289 params->fifo_size = 0;
290 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
291 params->msbits = 0;
292 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
293 params->rate_num = 0;
294 params->rate_den = 0;
295 }
296
297 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
298 m = hw_param_mask(params, k);
299 if (snd_mask_empty(m))
300 return -EINVAL;
301 if (!(params->rmask & (1 << k)))
302 continue;
303#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100304 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
305 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 -0700306#endif
307 changed = snd_mask_refine(m, constrs_mask(constrs, k));
308#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100309 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 -0700310#endif
311 if (changed)
312 params->cmask |= 1 << k;
313 if (changed < 0)
314 return changed;
315 }
316
317 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
318 i = hw_param_interval(params, k);
319 if (snd_interval_empty(i))
320 return -EINVAL;
321 if (!(params->rmask & (1 << k)))
322 continue;
323#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100324 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100326 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100328 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 i->openmin ? '(' : '[', i->min,
330 i->max, i->openmax ? ')' : ']');
Takashi Iwai09e56df2014-02-04 18:19:48 +0100331 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#endif
333 changed = snd_interval_refine(i, constrs_interval(constrs, k));
334#ifdef RULES_DEBUG
335 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100336 pr_cont("empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100338 pr_cont("%c%u %u%c\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 i->openmin ? '(' : '[', i->min,
340 i->max, i->openmax ? ')' : ']');
341#endif
342 if (changed)
343 params->cmask |= 1 << k;
344 if (changed < 0)
345 return changed;
346 }
347
348 for (k = 0; k < constrs->rules_num; k++)
349 rstamps[k] = 0;
350 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
351 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
352 do {
353 again = 0;
354 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100355 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unsigned int d;
357 int doit = 0;
358 if (r->cond && !(r->cond & params->flags))
359 continue;
360 for (d = 0; r->deps[d] >= 0; d++) {
361 if (vstamps[r->deps[d]] > rstamps[k]) {
362 doit = 1;
363 break;
364 }
365 }
366 if (!doit)
367 continue;
368#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100369 pr_debug("Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100371 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (hw_is_mask(r->var)) {
373 m = hw_param_mask(params, r->var);
Takashi Iwai09e56df2014-02-04 18:19:48 +0100374 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } else {
376 i = hw_param_interval(params, r->var);
377 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100378 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100380 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 i->openmin ? '(' : '[', i->min,
382 i->max, i->openmax ? ')' : ']');
383 }
384 }
385#endif
386 changed = r->func(params, r);
387#ifdef RULES_DEBUG
388 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100389 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (hw_is_mask(r->var))
Takashi Iwai09e56df2014-02-04 18:19:48 +0100391 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 else {
393 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100394 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100396 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 i->openmin ? '(' : '[', i->min,
398 i->max, i->openmax ? ')' : ']');
399 }
400 }
Takashi Iwai09e56df2014-02-04 18:19:48 +0100401 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#endif
403 rstamps[k] = stamp;
404 if (changed && r->var >= 0) {
405 params->cmask |= (1 << r->var);
406 vstamps[r->var] = stamp;
407 again = 1;
408 }
409 if (changed < 0)
410 return changed;
411 stamp++;
412 }
413 } while (again);
414 if (!params->msbits) {
415 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
416 if (snd_interval_single(i))
417 params->msbits = snd_interval_value(i);
418 }
419
420 if (!params->rate_den) {
421 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
422 if (snd_interval_single(i)) {
423 params->rate_num = snd_interval_value(i);
424 params->rate_den = 1;
425 }
426 }
427
428 hw = &substream->runtime->hw;
Takashi Iwai63825f32014-10-22 12:04:46 +0200429 if (!params->info) {
Libin Yang48d88292014-12-31 22:09:54 +0800430 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
431 SNDRV_PCM_INFO_DRAIN_TRIGGER);
Takashi Iwai63825f32014-10-22 12:04:46 +0200432 if (!hw_support_mmap(substream))
433 params->info &= ~(SNDRV_PCM_INFO_MMAP |
434 SNDRV_PCM_INFO_MMAP_VALID);
435 }
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200436 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100437 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
438 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
439 if (snd_mask_min(m) == snd_mask_max(m) &&
440 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200441 changed = substream->ops->ioctl(substream,
442 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200443 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200444 return changed;
445 }
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 params->rmask = 0;
448 return 0;
449}
450
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200451EXPORT_SYMBOL(snd_pcm_hw_refine);
452
Takashi Iwai877211f2005-11-17 13:59:38 +0100453static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
454 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Takashi Iwai877211f2005-11-17 13:59:38 +0100456 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int err;
458
Li Zefanef44a1e2009-04-10 09:43:08 +0800459 params = memdup_user(_params, sizeof(*params));
460 if (IS_ERR(params))
461 return PTR_ERR(params);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 err = snd_pcm_hw_refine(substream, params);
464 if (copy_to_user(_params, params, sizeof(*params))) {
465 if (!err)
466 err = -EFAULT;
467 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 kfree(params);
470 return err;
471}
472
Takashi Iwai9442e692006-09-30 23:27:19 -0700473static int period_to_usecs(struct snd_pcm_runtime *runtime)
474{
475 int usecs;
476
477 if (! runtime->rate)
478 return -1; /* invalid */
479
480 /* take 75% of period time as the deadline */
481 usecs = (750000 / runtime->rate) * runtime->period_size;
482 usecs += ((750000 % runtime->rate) * runtime->period_size) /
483 runtime->rate;
484
485 return usecs;
486}
487
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200488static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
489{
490 snd_pcm_stream_lock_irq(substream);
491 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
492 substream->runtime->status->state = state;
493 snd_pcm_stream_unlock_irq(substream);
494}
495
Jie Yang90bbaf62015-10-16 17:57:46 +0800496static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
497 int event)
498{
499#ifdef CONFIG_SND_PCM_TIMER
500 if (substream->timer)
501 snd_timer_notify(substream->timer, event,
502 &substream->runtime->trigger_tstamp);
503#endif
504}
505
Takashi Iwai877211f2005-11-17 13:59:38 +0100506static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
507 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Takashi Iwai877211f2005-11-17 13:59:38 +0100509 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700510 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 unsigned int bits;
512 snd_pcm_uframes_t frames;
513
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200514 if (PCM_RUNTIME_CHECK(substream))
515 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 snd_pcm_stream_lock_irq(substream);
518 switch (runtime->status->state) {
519 case SNDRV_PCM_STATE_OPEN:
520 case SNDRV_PCM_STATE_SETUP:
521 case SNDRV_PCM_STATE_PREPARED:
522 break;
523 default:
524 snd_pcm_stream_unlock_irq(substream);
525 return -EBADFD;
526 }
527 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100528#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (!substream->oss.oss)
530#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200531 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return -EBADFD;
533
534 params->rmask = ~0U;
535 err = snd_pcm_hw_refine(substream, params);
536 if (err < 0)
537 goto _error;
538
539 err = snd_pcm_hw_params_choose(substream, params);
540 if (err < 0)
541 goto _error;
542
543 if (substream->ops->hw_params != NULL) {
544 err = substream->ops->hw_params(substream, params);
545 if (err < 0)
546 goto _error;
547 }
548
549 runtime->access = params_access(params);
550 runtime->format = params_format(params);
551 runtime->subformat = params_subformat(params);
552 runtime->channels = params_channels(params);
553 runtime->rate = params_rate(params);
554 runtime->period_size = params_period_size(params);
555 runtime->periods = params_periods(params);
556 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 runtime->info = params->info;
558 runtime->rate_num = params->rate_num;
559 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100560 runtime->no_period_wakeup =
561 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
562 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 bits = snd_pcm_format_physical_width(runtime->format);
565 runtime->sample_bits = bits;
566 bits *= runtime->channels;
567 runtime->frame_bits = bits;
568 frames = 1;
569 while (bits % 8 != 0) {
570 bits *= 2;
571 frames *= 2;
572 }
573 runtime->byte_align = bits / 8;
574 runtime->min_align = frames;
575
576 /* Default sw params */
577 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
578 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 runtime->start_threshold = 1;
581 runtime->stop_threshold = runtime->buffer_size;
582 runtime->silence_threshold = 0;
583 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200584 runtime->boundary = runtime->buffer_size;
Laxminath Kasam8428b712013-07-11 10:55:26 +0530585 while (runtime->boundary * 2 * runtime->channels <=
586 LONG_MAX - runtime->buffer_size)
Clemens Ladischead40462010-05-21 09:15:59 +0200587 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200590 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700591
James Bottomley82f68252010-07-05 22:53:06 +0200592 if (pm_qos_request_active(&substream->latency_pm_qos_req))
593 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700594 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200595 pm_qos_add_request(&substream->latency_pm_qos_req,
596 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return 0;
598 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300599 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 so we force application to retry to set
601 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200602 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (substream->ops->hw_free != NULL)
604 substream->ops->hw_free(substream);
605 return err;
606}
607
Takashi Iwai877211f2005-11-17 13:59:38 +0100608static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
609 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Takashi Iwai877211f2005-11-17 13:59:38 +0100611 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int err;
613
Li Zefanef44a1e2009-04-10 09:43:08 +0800614 params = memdup_user(_params, sizeof(*params));
615 if (IS_ERR(params))
616 return PTR_ERR(params);
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 err = snd_pcm_hw_params(substream, params);
619 if (copy_to_user(_params, params, sizeof(*params))) {
620 if (!err)
621 err = -EFAULT;
622 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 kfree(params);
625 return err;
626}
627
Takashi Iwai877211f2005-11-17 13:59:38 +0100628static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Takashi Iwai877211f2005-11-17 13:59:38 +0100630 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int result = 0;
632
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200633 if (PCM_RUNTIME_CHECK(substream))
634 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 snd_pcm_stream_lock_irq(substream);
637 switch (runtime->status->state) {
638 case SNDRV_PCM_STATE_SETUP:
639 case SNDRV_PCM_STATE_PREPARED:
640 break;
641 default:
642 snd_pcm_stream_unlock_irq(substream);
643 return -EBADFD;
644 }
645 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200646 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return -EBADFD;
648 if (substream->ops->hw_free)
649 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200650 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200651 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return result;
653}
654
Takashi Iwai877211f2005-11-17 13:59:38 +0100655static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
656 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Takashi Iwai877211f2005-11-17 13:59:38 +0100658 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100659 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200661 if (PCM_RUNTIME_CHECK(substream))
662 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 snd_pcm_stream_lock_irq(substream);
665 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
666 snd_pcm_stream_unlock_irq(substream);
667 return -EBADFD;
668 }
669 snd_pcm_stream_unlock_irq(substream);
670
Dan Carpenter145d92e2015-09-23 12:42:28 +0300671 if (params->tstamp_mode < 0 ||
672 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -EINVAL;
Takashi Iwai58900812014-07-16 17:45:27 +0200674 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
675 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200676 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (params->avail_min == 0)
678 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (params->silence_size >= runtime->boundary) {
680 if (params->silence_threshold != 0)
681 return -EINVAL;
682 } else {
683 if (params->silence_size > params->silence_threshold)
684 return -EINVAL;
685 if (params->silence_threshold > runtime->buffer_size)
686 return -EINVAL;
687 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100688 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 snd_pcm_stream_lock_irq(substream);
690 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai58900812014-07-16 17:45:27 +0200691 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
692 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 runtime->period_step = params->period_step;
694 runtime->control->avail_min = params->avail_min;
695 runtime->start_threshold = params->start_threshold;
696 runtime->stop_threshold = params->stop_threshold;
697 runtime->silence_threshold = params->silence_threshold;
698 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 params->boundary = runtime->boundary;
700 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
702 runtime->silence_size > 0)
703 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100704 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100707 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Takashi Iwai877211f2005-11-17 13:59:38 +0100710static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
711 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Takashi Iwai877211f2005-11-17 13:59:38 +0100713 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int err;
715 if (copy_from_user(&params, _params, sizeof(params)))
716 return -EFAULT;
717 err = snd_pcm_sw_params(substream, &params);
718 if (copy_to_user(_params, &params, sizeof(params)))
719 return -EFAULT;
720 return err;
721}
722
Takashi Iwai877211f2005-11-17 13:59:38 +0100723int snd_pcm_status(struct snd_pcm_substream *substream,
724 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Takashi Iwai877211f2005-11-17 13:59:38 +0100726 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600729
730 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
731 &runtime->audio_tstamp_config);
732
733 /* backwards compatible behavior */
734 if (runtime->audio_tstamp_config.type_requested ==
735 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
736 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
737 runtime->audio_tstamp_config.type_requested =
738 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
739 else
740 runtime->audio_tstamp_config.type_requested =
741 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
742 runtime->audio_tstamp_report.valid = 0;
743 } else
744 runtime->audio_tstamp_report.valid = 1;
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 status->state = runtime->status->state;
747 status->suspended_state = runtime->status->suspended_state;
748 if (status->state == SNDRV_PCM_STATE_OPEN)
749 goto _end;
750 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100751 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100753 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
754 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600755 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500756 status->audio_tstamp =
757 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600758 if (runtime->audio_tstamp_report.valid == 1)
759 /* backwards compatibility, no report provided in COMPAT mode */
760 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
761 &status->audio_tstamp_accuracy,
762 &runtime->audio_tstamp_report);
763
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100764 goto _tstamp_end;
765 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600766 } else {
767 /* get tstamp only in fallback mode and only if enabled */
768 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
769 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100770 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100771 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 status->appl_ptr = runtime->control->appl_ptr;
773 status->hw_ptr = runtime->status->hw_ptr;
774 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
775 status->avail = snd_pcm_playback_avail(runtime);
776 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200777 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200779 status->delay += runtime->delay;
780 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 status->delay = 0;
782 } else {
783 status->avail = snd_pcm_capture_avail(runtime);
784 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200785 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 else
787 status->delay = 0;
788 }
789 status->avail_max = runtime->avail_max;
790 status->overrange = runtime->overrange;
791 runtime->avail_max = 0;
792 runtime->overrange = 0;
793 _end:
794 snd_pcm_stream_unlock_irq(substream);
795 return 0;
796}
797
Takashi Iwai877211f2005-11-17 13:59:38 +0100798static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600799 struct snd_pcm_status __user * _status,
800 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Takashi Iwai877211f2005-11-17 13:59:38 +0100802 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600806 /*
807 * with extension, parameters are read/write,
808 * get audio_tstamp_data from user,
809 * ignore rest of status structure
810 */
811 if (ext && get_user(status.audio_tstamp_data,
812 (u32 __user *)(&_status->audio_tstamp_data)))
813 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 res = snd_pcm_status(substream, &status);
815 if (res < 0)
816 return res;
817 if (copy_to_user(_status, &status, sizeof(status)))
818 return -EFAULT;
819 return 0;
820}
821
Takashi Iwai877211f2005-11-17 13:59:38 +0100822static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
823 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwai877211f2005-11-17 13:59:38 +0100825 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 unsigned int channel;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 channel = info->channel;
829 runtime = substream->runtime;
830 snd_pcm_stream_lock_irq(substream);
831 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
832 snd_pcm_stream_unlock_irq(substream);
833 return -EBADFD;
834 }
835 snd_pcm_stream_unlock_irq(substream);
836 if (channel >= runtime->channels)
837 return -EINVAL;
838 memset(info, 0, sizeof(*info));
839 info->channel = channel;
840 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
841}
842
Takashi Iwai877211f2005-11-17 13:59:38 +0100843static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
844 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Takashi Iwai877211f2005-11-17 13:59:38 +0100846 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int res;
848
849 if (copy_from_user(&info, _info, sizeof(info)))
850 return -EFAULT;
851 res = snd_pcm_channel_info(substream, &info);
852 if (res < 0)
853 return res;
854 if (copy_to_user(_info, &info, sizeof(info)))
855 return -EFAULT;
856 return 0;
857}
858
Takashi Iwai877211f2005-11-17 13:59:38 +0100859static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Takashi Iwai877211f2005-11-17 13:59:38 +0100861 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (runtime->trigger_master == NULL)
863 return;
864 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600865 if (!runtime->trigger_tstamp_latched)
866 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 } else {
868 snd_pcm_trigger_tstamp(runtime->trigger_master);
869 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
870 }
871 runtime->trigger_master = NULL;
872}
873
874struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100875 int (*pre_action)(struct snd_pcm_substream *substream, int state);
876 int (*do_action)(struct snd_pcm_substream *substream, int state);
877 void (*undo_action)(struct snd_pcm_substream *substream, int state);
878 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879};
880
881/*
882 * this functions is core for handling of linked stream
883 * Note: the stream state might be changed also on failure
884 * Note2: call with calling stream lock + link lock
885 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100886static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100887 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int state, int do_lock)
889{
Takashi Iwai877211f2005-11-17 13:59:38 +0100890 struct snd_pcm_substream *s = NULL;
891 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200892 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Takashi Iwaief991b92007-02-22 12:52:53 +0100894 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200895 if (do_lock && s != substream) {
896 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200897 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200898 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200899 spin_lock_nested(&s->self_group.lock, depth);
900 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 res = ops->pre_action(s, state);
903 if (res < 0)
904 goto _unlock;
905 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100906 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 res = ops->do_action(s, state);
908 if (res < 0) {
909 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100910 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (s1 == s) /* failed stream */
912 break;
913 ops->undo_action(s1, state);
914 }
915 }
916 s = NULL; /* unlock all */
917 goto _unlock;
918 }
919 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100920 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ops->post_action(s, state);
922 }
923 _unlock:
924 if (do_lock) {
925 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100926 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200927 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200928 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200929 mutex_unlock(&s1->self_group.mutex);
930 else
931 spin_unlock(&s1->self_group.lock);
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (s1 == s) /* end */
934 break;
935 }
936 }
937 return res;
938}
939
940/*
941 * Note: call with stream lock
942 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100943static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100944 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int state)
946{
947 int res;
948
949 res = ops->pre_action(substream, state);
950 if (res < 0)
951 return res;
952 res = ops->do_action(substream, state);
953 if (res == 0)
954 ops->post_action(substream, state);
955 else if (ops->undo_action)
956 ops->undo_action(substream, state);
957 return res;
958}
959
960/*
961 * Note: call with stream lock
962 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100963static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100964 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int state)
966{
967 int res;
968
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100969 if (!snd_pcm_stream_linked(substream))
970 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200971
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100972 if (substream->pcm->nonatomic) {
973 if (!mutex_trylock(&substream->group->mutex)) {
974 mutex_unlock(&substream->self_group.mutex);
975 mutex_lock(&substream->group->mutex);
976 mutex_lock(&substream->self_group.mutex);
977 }
978 res = snd_pcm_action_group(ops, substream, state, 1);
979 mutex_unlock(&substream->group->mutex);
980 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (!spin_trylock(&substream->group->lock)) {
982 spin_unlock(&substream->self_group.lock);
983 spin_lock(&substream->group->lock);
984 spin_lock(&substream->self_group.lock);
985 }
986 res = snd_pcm_action_group(ops, substream, state, 1);
987 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 return res;
990}
991
992/*
993 * Note: don't use any locks before
994 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100995static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100996 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int state)
998{
999 int res;
1000
Takashi Iwaie3a4bd52014-10-31 14:45:04 +01001001 snd_pcm_stream_lock_irq(substream);
1002 res = snd_pcm_action(ops, substream, state);
1003 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return res;
1005}
1006
1007/*
1008 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001009static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001010 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int state)
1012{
1013 int res;
1014
1015 down_read(&snd_pcm_link_rwsem);
1016 if (snd_pcm_stream_linked(substream))
1017 res = snd_pcm_action_group(ops, substream, state, 0);
1018 else
1019 res = snd_pcm_action_single(ops, substream, state);
1020 up_read(&snd_pcm_link_rwsem);
1021 return res;
1022}
1023
1024/*
1025 * start callbacks
1026 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001027static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Takashi Iwai877211f2005-11-17 13:59:38 +01001029 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1031 return -EBADFD;
1032 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Liam Girdwoodf2727332016-01-29 02:27:17 +05301033 !substream->hw_no_buffer &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 !snd_pcm_playback_data(substream))
1035 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001036 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 runtime->trigger_master = substream;
1038 return 0;
1039}
1040
Takashi Iwai877211f2005-11-17 13:59:38 +01001041static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 if (substream->runtime->trigger_master != substream)
1044 return 0;
1045 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1046}
1047
Takashi Iwai877211f2005-11-17 13:59:38 +01001048static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 if (substream->runtime->trigger_master == substream)
1051 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1052}
1053
Takashi Iwai877211f2005-11-17 13:59:38 +01001054static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Takashi Iwai877211f2005-11-17 13:59:38 +01001056 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001058 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001059 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1060 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 runtime->status->state = state;
1062 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1063 runtime->silence_size > 0)
1064 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001065 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Julia Lawallb17154c2015-11-29 16:36:40 +01001068static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 .pre_action = snd_pcm_pre_start,
1070 .do_action = snd_pcm_do_start,
1071 .undo_action = snd_pcm_undo_start,
1072 .post_action = snd_pcm_post_start
1073};
1074
1075/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001076 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001077 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001078 *
1079 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001081int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Takashi Iwai877211f2005-11-17 13:59:38 +01001083 return snd_pcm_action(&snd_pcm_action_start, substream,
1084 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Sudheer Papothib40e9682016-01-29 02:19:04 +05301087static int snd_compressed_ioctl(struct snd_pcm_substream *substream,
1088 unsigned int cmd, void __user *arg)
1089{
1090 struct snd_pcm_runtime *runtime;
1091 int err = 0;
1092
1093 if (PCM_RUNTIME_CHECK(substream))
1094 return -ENXIO;
1095 runtime = substream->runtime;
1096 pr_debug("%s called with cmd = %d\n", __func__, cmd);
1097 err = substream->ops->ioctl(substream, cmd, arg);
1098 return err;
1099}
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05301100
1101static int snd_user_ioctl(struct snd_pcm_substream *substream,
1102 unsigned int cmd, void __user *arg)
1103{
1104 struct snd_pcm_runtime *runtime;
1105 int err = 0;
1106
1107 if (PCM_RUNTIME_CHECK(substream))
1108 return -ENXIO;
1109 runtime = substream->runtime;
1110 err = substream->ops->ioctl(substream, cmd, arg);
1111 return err;
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/*
1115 * stop callbacks
1116 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001117static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Takashi Iwai877211f2005-11-17 13:59:38 +01001119 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1121 return -EBADFD;
1122 runtime->trigger_master = substream;
1123 return 0;
1124}
1125
Takashi Iwai877211f2005-11-17 13:59:38 +01001126static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 if (substream->runtime->trigger_master == substream &&
1129 snd_pcm_running(substream))
1130 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1131 return 0; /* unconditonally stop all substreams */
1132}
1133
Takashi Iwai877211f2005-11-17 13:59:38 +01001134static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Takashi Iwai877211f2005-11-17 13:59:38 +01001136 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (runtime->status->state != state) {
1138 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001139 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001140 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001143 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Julia Lawallb17154c2015-11-29 16:36:40 +01001146static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 .pre_action = snd_pcm_pre_stop,
1148 .do_action = snd_pcm_do_stop,
1149 .post_action = snd_pcm_post_stop
1150};
1151
1152/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001153 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001154 * @substream: the PCM substream instance
1155 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001157 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001158 *
Masanari Iida0a114582014-02-09 00:47:36 +09001159 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001161int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1164}
1165
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001166EXPORT_SYMBOL(snd_pcm_stop);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001169 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001170 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001172 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001174 *
1175 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001177int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Takashi Iwai877211f2005-11-17 13:59:38 +01001179 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1180 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Takashi Iwai1fb85102014-11-07 17:08:28 +01001183/**
1184 * snd_pcm_stop_xrun - stop the running streams as XRUN
1185 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001186 *
1187 * This stops the given running substream (and all linked substreams) as XRUN.
1188 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1189 *
1190 * Return: Zero if successful, or a negative error code.
1191 */
1192int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1193{
1194 unsigned long flags;
1195 int ret = 0;
1196
1197 snd_pcm_stream_lock_irqsave(substream, flags);
1198 if (snd_pcm_running(substream))
1199 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1200 snd_pcm_stream_unlock_irqrestore(substream, flags);
1201 return ret;
1202}
1203EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205/*
1206 * pause callbacks
1207 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001208static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Takashi Iwai877211f2005-11-17 13:59:38 +01001210 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1212 return -ENOSYS;
1213 if (push) {
1214 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1215 return -EBADFD;
1216 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1217 return -EBADFD;
1218 runtime->trigger_master = substream;
1219 return 0;
1220}
1221
Takashi Iwai877211f2005-11-17 13:59:38 +01001222static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 if (substream->runtime->trigger_master != substream)
1225 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001226 /* some drivers might use hw_ptr to recover from the pause -
1227 update the hw_ptr now */
1228 if (push)
1229 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001230 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001231 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001232 * delta, effectively to skip the check once.
1233 */
1234 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return substream->ops->trigger(substream,
1236 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1237 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1238}
1239
Takashi Iwai877211f2005-11-17 13:59:38 +01001240static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 if (substream->runtime->trigger_master == substream)
1243 substream->ops->trigger(substream,
1244 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1245 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1246}
1247
Takashi Iwai877211f2005-11-17 13:59:38 +01001248static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Takashi Iwai877211f2005-11-17 13:59:38 +01001250 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 snd_pcm_trigger_tstamp(substream);
1252 if (push) {
1253 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001254 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001256 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 } else {
1258 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001259 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261}
1262
Julia Lawallb17154c2015-11-29 16:36:40 +01001263static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 .pre_action = snd_pcm_pre_pause,
1265 .do_action = snd_pcm_do_pause,
1266 .undo_action = snd_pcm_undo_pause,
1267 .post_action = snd_pcm_post_pause
1268};
1269
1270/*
1271 * Push/release the pause for all linked streams.
1272 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001273static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1276}
1277
1278#ifdef CONFIG_PM
1279/* suspend */
1280
Takashi Iwai877211f2005-11-17 13:59:38 +01001281static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Takashi Iwai877211f2005-11-17 13:59:38 +01001283 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1285 return -EBUSY;
1286 runtime->trigger_master = substream;
1287 return 0;
1288}
1289
Takashi Iwai877211f2005-11-17 13:59:38 +01001290static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Takashi Iwai877211f2005-11-17 13:59:38 +01001292 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (runtime->trigger_master != substream)
1294 return 0;
1295 if (! snd_pcm_running(substream))
1296 return 0;
1297 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1298 return 0; /* suspend unconditionally */
1299}
1300
Takashi Iwai877211f2005-11-17 13:59:38 +01001301static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Takashi Iwai877211f2005-11-17 13:59:38 +01001303 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001305 runtime->status->suspended_state = runtime->status->state;
1306 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001307 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001309 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Julia Lawallb17154c2015-11-29 16:36:40 +01001312static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 .pre_action = snd_pcm_pre_suspend,
1314 .do_action = snd_pcm_do_suspend,
1315 .post_action = snd_pcm_post_suspend
1316};
1317
1318/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001319 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001320 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001323 *
1324 * Return: Zero if successful (or @substream is %NULL), or a negative error
1325 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001327int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 int err;
1330 unsigned long flags;
1331
Takashi Iwai603bf522005-11-17 15:59:14 +01001332 if (! substream)
1333 return 0;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 snd_pcm_stream_lock_irqsave(substream, flags);
1336 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1337 snd_pcm_stream_unlock_irqrestore(substream, flags);
1338 return err;
1339}
1340
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001341EXPORT_SYMBOL(snd_pcm_suspend);
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001344 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001345 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001348 *
1349 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001351int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Takashi Iwai877211f2005-11-17 13:59:38 +01001353 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int stream, err = 0;
1355
Takashi Iwai603bf522005-11-17 15:59:14 +01001356 if (! pcm)
1357 return 0;
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001360 for (substream = pcm->streams[stream].substream;
1361 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* FIXME: the open/close code should lock this as well */
1363 if (substream->runtime == NULL)
1364 continue;
1365 err = snd_pcm_suspend(substream);
1366 if (err < 0 && err != -EBUSY)
1367 return err;
1368 }
1369 }
1370 return 0;
1371}
1372
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001373EXPORT_SYMBOL(snd_pcm_suspend_all);
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375/* resume */
1376
Takashi Iwai877211f2005-11-17 13:59:38 +01001377static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Takashi Iwai877211f2005-11-17 13:59:38 +01001379 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1381 return -ENOSYS;
1382 runtime->trigger_master = substream;
1383 return 0;
1384}
1385
Takashi Iwai877211f2005-11-17 13:59:38 +01001386static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Takashi Iwai877211f2005-11-17 13:59:38 +01001388 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (runtime->trigger_master != substream)
1390 return 0;
1391 /* DMA not running previously? */
1392 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1393 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1394 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1395 return 0;
1396 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1397}
1398
Takashi Iwai877211f2005-11-17 13:59:38 +01001399static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 if (substream->runtime->trigger_master == substream &&
1402 snd_pcm_running(substream))
1403 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1404}
1405
Takashi Iwai877211f2005-11-17 13:59:38 +01001406static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Takashi Iwai877211f2005-11-17 13:59:38 +01001408 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001410 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001411 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Julia Lawallb17154c2015-11-29 16:36:40 +01001414static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 .pre_action = snd_pcm_pre_resume,
1416 .do_action = snd_pcm_do_resume,
1417 .undo_action = snd_pcm_undo_resume,
1418 .post_action = snd_pcm_post_resume
1419};
1420
Takashi Iwai877211f2005-11-17 13:59:38 +01001421static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Takashi Iwai877211f2005-11-17 13:59:38 +01001423 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int res;
1425
1426 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001427 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1429 snd_power_unlock(card);
1430 return res;
1431}
1432
1433#else
1434
Takashi Iwai877211f2005-11-17 13:59:38 +01001435static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 return -ENOSYS;
1438}
1439
1440#endif /* CONFIG_PM */
1441
1442/*
1443 * xrun ioctl
1444 *
1445 * Change the RUNNING stream(s) to XRUN state.
1446 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001447static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Takashi Iwai877211f2005-11-17 13:59:38 +01001449 struct snd_card *card = substream->pcm->card;
1450 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int result;
1452
1453 snd_power_lock(card);
1454 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001455 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (result < 0)
1457 goto _unlock;
1458 }
1459
1460 snd_pcm_stream_lock_irq(substream);
1461 switch (runtime->status->state) {
1462 case SNDRV_PCM_STATE_XRUN:
1463 result = 0; /* already there */
1464 break;
1465 case SNDRV_PCM_STATE_RUNNING:
1466 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1467 break;
1468 default:
1469 result = -EBADFD;
1470 }
1471 snd_pcm_stream_unlock_irq(substream);
1472 _unlock:
1473 snd_power_unlock(card);
1474 return result;
1475}
1476
1477/*
1478 * reset ioctl
1479 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001480static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Takashi Iwai877211f2005-11-17 13:59:38 +01001482 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 switch (runtime->status->state) {
1484 case SNDRV_PCM_STATE_RUNNING:
1485 case SNDRV_PCM_STATE_PREPARED:
1486 case SNDRV_PCM_STATE_PAUSED:
1487 case SNDRV_PCM_STATE_SUSPENDED:
1488 return 0;
1489 default:
1490 return -EBADFD;
1491 }
1492}
1493
Takashi Iwai877211f2005-11-17 13:59:38 +01001494static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwai877211f2005-11-17 13:59:38 +01001496 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1498 if (err < 0)
1499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001501 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1502 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 runtime->silence_start = runtime->status->hw_ptr;
1504 runtime->silence_filled = 0;
1505 return 0;
1506}
1507
Takashi Iwai877211f2005-11-17 13:59:38 +01001508static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Takashi Iwai877211f2005-11-17 13:59:38 +01001510 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 runtime->control->appl_ptr = runtime->status->hw_ptr;
1512 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1513 runtime->silence_size > 0)
1514 snd_pcm_playback_silence(substream, ULONG_MAX);
1515}
1516
Julia Lawallb17154c2015-11-29 16:36:40 +01001517static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 .pre_action = snd_pcm_pre_reset,
1519 .do_action = snd_pcm_do_reset,
1520 .post_action = snd_pcm_post_reset
1521};
1522
Takashi Iwai877211f2005-11-17 13:59:38 +01001523static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1526}
1527
1528/*
1529 * prepare ioctl
1530 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001531/* we use the second argument for updating f_flags */
1532static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1533 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Takashi Iwai877211f2005-11-17 13:59:38 +01001535 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001536 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1537 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return -EBADFD;
1539 if (snd_pcm_running(substream))
1540 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001541 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return 0;
1543}
1544
Takashi Iwai877211f2005-11-17 13:59:38 +01001545static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 int err;
1548 err = substream->ops->prepare(substream);
1549 if (err < 0)
1550 return err;
1551 return snd_pcm_do_reset(substream, 0);
1552}
1553
Takashi Iwai877211f2005-11-17 13:59:38 +01001554static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Takashi Iwai877211f2005-11-17 13:59:38 +01001556 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001558 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Julia Lawallb17154c2015-11-29 16:36:40 +01001561static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 .pre_action = snd_pcm_pre_prepare,
1563 .do_action = snd_pcm_do_prepare,
1564 .post_action = snd_pcm_post_prepare
1565};
1566
1567/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001568 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001569 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001570 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001571 *
1572 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001574static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1575 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001578 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001579 int f_flags;
1580
1581 if (file)
1582 f_flags = file->f_flags;
1583 else
1584 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001587 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001588 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1589 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 snd_power_unlock(card);
1591 return res;
1592}
1593
1594/*
1595 * drain ioctl
1596 */
1597
Takashi Iwai877211f2005-11-17 13:59:38 +01001598static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001600 struct snd_pcm_runtime *runtime = substream->runtime;
1601 switch (runtime->status->state) {
1602 case SNDRV_PCM_STATE_OPEN:
1603 case SNDRV_PCM_STATE_DISCONNECTED:
1604 case SNDRV_PCM_STATE_SUSPENDED:
1605 return -EBADFD;
1606 }
1607 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return 0;
1609}
1610
Takashi Iwai877211f2005-11-17 13:59:38 +01001611static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Takashi Iwai877211f2005-11-17 13:59:38 +01001613 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1615 switch (runtime->status->state) {
1616 case SNDRV_PCM_STATE_PREPARED:
1617 /* start playback stream if possible */
1618 if (! snd_pcm_playback_empty(substream)) {
1619 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1620 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001621 } else {
1622 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624 break;
1625 case SNDRV_PCM_STATE_RUNNING:
1626 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1627 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001628 case SNDRV_PCM_STATE_XRUN:
1629 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1630 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 default:
1632 break;
1633 }
1634 } else {
1635 /* stop running stream */
1636 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001637 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001639 snd_pcm_do_stop(substream, new_state);
1640 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642 }
Libin Yang48d88292014-12-31 22:09:54 +08001643
1644 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1645 runtime->trigger_master == substream &&
1646 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1647 return substream->ops->trigger(substream,
1648 SNDRV_PCM_TRIGGER_DRAIN);
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return 0;
1651}
1652
Takashi Iwai877211f2005-11-17 13:59:38 +01001653static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655}
1656
Julia Lawallb17154c2015-11-29 16:36:40 +01001657static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 .pre_action = snd_pcm_pre_drain_init,
1659 .do_action = snd_pcm_do_drain_init,
1660 .post_action = snd_pcm_post_drain_init
1661};
1662
Takashi Iwai877211f2005-11-17 13:59:38 +01001663static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665/*
1666 * Drain the stream(s).
1667 * When the substream is linked, sync until the draining of all playback streams
1668 * is finished.
1669 * After this call, all streams are supposed to be either SETUP or DRAINING
1670 * (capture only) state.
1671 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001672static int snd_pcm_drain(struct snd_pcm_substream *substream,
1673 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Takashi Iwai877211f2005-11-17 13:59:38 +01001675 struct snd_card *card;
1676 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001677 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001678 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001680 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 card = substream->pcm->card;
1683 runtime = substream->runtime;
1684
1685 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1686 return -EBADFD;
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 snd_power_lock(card);
1689 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001690 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001691 if (result < 0) {
1692 snd_power_unlock(card);
1693 return result;
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001697 if (file) {
1698 if (file->f_flags & O_NONBLOCK)
1699 nonblock = 1;
1700 } else if (substream->f_flags & O_NONBLOCK)
1701 nonblock = 1;
1702
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001703 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001704 snd_pcm_stream_lock_irq(substream);
1705 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001706 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001707 snd_pcm_pause(substream, 0);
1708
1709 /* pre-start/stop - all running streams are changed to DRAINING state */
1710 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001711 if (result < 0)
1712 goto unlock;
1713 /* in non-blocking, we don't wait in ioctl but let caller poll */
1714 if (nonblock) {
1715 result = -EAGAIN;
1716 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 for (;;) {
1720 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001721 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (signal_pending(current)) {
1723 result = -ERESTARTSYS;
1724 break;
1725 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001726 /* find a substream to drain */
1727 to_check = NULL;
1728 snd_pcm_group_for_each_entry(s, substream) {
1729 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1730 continue;
1731 runtime = s->runtime;
1732 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1733 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001734 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001735 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001736 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001737 if (!to_check)
1738 break; /* all drained */
1739 init_waitqueue_entry(&wait, current);
1740 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001742 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001744 if (runtime->no_period_wakeup)
1745 tout = MAX_SCHEDULE_TIMEOUT;
1746 else {
1747 tout = 10;
1748 if (runtime->rate) {
1749 long t = runtime->period_size * 2 / runtime->rate;
1750 tout = max(t, tout);
1751 }
1752 tout = msecs_to_jiffies(tout * 1000);
1753 }
1754 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001756 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001758 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001759 if (card->shutdown) {
1760 result = -ENODEV;
1761 break;
1762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (tout == 0) {
1764 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1765 result = -ESTRPIPE;
1766 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001767 dev_dbg(substream->pcm->card->dev,
1768 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1770 result = -EIO;
1771 }
1772 break;
1773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001775
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001776 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001777 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001778 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 return result;
1782}
1783
1784/*
1785 * drop ioctl
1786 *
1787 * Immediately put all linked substreams into SETUP state.
1788 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001789static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Takashi Iwai877211f2005-11-17 13:59:38 +01001791 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 int result = 0;
1793
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001794 if (PCM_RUNTIME_CHECK(substream))
1795 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001798 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001799 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1800 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return -EBADFD;
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 snd_pcm_stream_lock_irq(substream);
1804 /* resume pause */
1805 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1806 snd_pcm_pause(substream, 0);
1807
1808 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1809 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1810 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return result;
1813}
1814
1815
Al Viro0888c322013-06-05 14:09:55 -04001816static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Al Viro0888c322013-06-05 14:09:55 -04001818 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001819 unsigned int minor;
1820
Al Viro0888c322013-06-05 14:09:55 -04001821 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1822 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001824 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1825 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
1828/*
1829 * PCM link handling
1830 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001831static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001834 struct snd_pcm_file *pcm_file;
1835 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001836 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001837 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Al Viro0888c322013-06-05 14:09:55 -04001839 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001841 if (!is_pcm_file(f.file)) {
1842 res = -EBADFD;
1843 goto _badf;
1844 }
1845 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001847 group = kmalloc(sizeof(*group), GFP_KERNEL);
1848 if (!group) {
1849 res = -ENOMEM;
1850 goto _nolock;
1851 }
Takashi Iwai67ec1072016-02-17 14:30:26 +01001852 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 write_lock_irq(&snd_pcm_link_rwlock);
1854 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001855 substream->runtime->status->state != substream1->runtime->status->state ||
1856 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 res = -EBADFD;
1858 goto _end;
1859 }
1860 if (snd_pcm_stream_linked(substream1)) {
1861 res = -EALREADY;
1862 goto _end;
1863 }
1864 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001865 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001866 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001868 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 INIT_LIST_HEAD(&substream->group->substreams);
1870 list_add_tail(&substream->link_list, &substream->group->substreams);
1871 substream->group->count = 1;
1872 }
1873 list_add_tail(&substream1->link_list, &substream->group->substreams);
1874 substream->group->count++;
1875 substream1->group = substream->group;
1876 _end:
1877 write_unlock_irq(&snd_pcm_link_rwlock);
1878 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001879 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001880 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001881 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001882 _badf:
1883 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return res;
1885}
1886
Takashi Iwai877211f2005-11-17 13:59:38 +01001887static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 substream->group = &substream->self_group;
1890 INIT_LIST_HEAD(&substream->self_group.substreams);
1891 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1892}
1893
Takashi Iwai877211f2005-11-17 13:59:38 +01001894static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Takashi Iwaief991b92007-02-22 12:52:53 +01001896 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int res = 0;
1898
Takashi Iwai67ec1072016-02-17 14:30:26 +01001899 down_write_nonblock(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 write_lock_irq(&snd_pcm_link_rwlock);
1901 if (!snd_pcm_stream_linked(substream)) {
1902 res = -EALREADY;
1903 goto _end;
1904 }
1905 list_del(&substream->link_list);
1906 substream->group->count--;
1907 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001908 snd_pcm_group_for_each_entry(s, substream) {
1909 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 break;
1911 }
1912 kfree(substream->group);
1913 }
1914 relink_to_local(substream);
1915 _end:
1916 write_unlock_irq(&snd_pcm_link_rwlock);
1917 up_write(&snd_pcm_link_rwsem);
1918 return res;
1919}
1920
1921/*
1922 * hw configurator
1923 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001924static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1925 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Takashi Iwai877211f2005-11-17 13:59:38 +01001927 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1929 hw_param_interval_c(params, rule->deps[1]), &t);
1930 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1931}
1932
Takashi Iwai877211f2005-11-17 13:59:38 +01001933static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1934 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Takashi Iwai877211f2005-11-17 13:59:38 +01001936 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1938 hw_param_interval_c(params, rule->deps[1]), &t);
1939 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1940}
1941
Takashi Iwai877211f2005-11-17 13:59:38 +01001942static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1943 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Takashi Iwai877211f2005-11-17 13:59:38 +01001945 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1947 hw_param_interval_c(params, rule->deps[1]),
1948 (unsigned long) rule->private, &t);
1949 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1950}
1951
Takashi Iwai877211f2005-11-17 13:59:38 +01001952static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1953 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Takashi Iwai877211f2005-11-17 13:59:38 +01001955 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1957 (unsigned long) rule->private,
1958 hw_param_interval_c(params, rule->deps[1]), &t);
1959 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1960}
1961
Takashi Iwai877211f2005-11-17 13:59:38 +01001962static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1963 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001966 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1967 struct snd_mask m;
1968 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 snd_mask_any(&m);
1970 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1971 int bits;
1972 if (! snd_mask_test(mask, k))
1973 continue;
1974 bits = snd_pcm_format_physical_width(k);
1975 if (bits <= 0)
1976 continue; /* ignore invalid formats */
1977 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1978 snd_mask_reset(&m, k);
1979 }
1980 return snd_mask_refine(mask, &m);
1981}
1982
Takashi Iwai877211f2005-11-17 13:59:38 +01001983static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1984 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Takashi Iwai877211f2005-11-17 13:59:38 +01001986 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 unsigned int k;
1988 t.min = UINT_MAX;
1989 t.max = 0;
1990 t.openmin = 0;
1991 t.openmax = 0;
1992 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1993 int bits;
1994 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1995 continue;
1996 bits = snd_pcm_format_physical_width(k);
1997 if (bits <= 0)
1998 continue; /* ignore invalid formats */
1999 if (t.min > (unsigned)bits)
2000 t.min = bits;
2001 if (t.max < (unsigned)bits)
2002 t.max = bits;
2003 }
2004 t.integer = 1;
2005 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2006}
2007
2008#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2009#error "Change this table"
2010#endif
2011
2012static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
Vidyakumar Athotafdcbc7f2015-10-26 12:52:58 -07002013 48000, 64000, 88200, 96000, 176400, 192000,
Walter Yang2404d12e2016-11-04 14:21:01 +08002014 352800, 384000 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Clemens Ladisch7653d552007-08-13 17:38:54 +02002016const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2017 .count = ARRAY_SIZE(rates),
2018 .list = rates,
2019};
2020
Takashi Iwai877211f2005-11-17 13:59:38 +01002021static int snd_pcm_hw_rule_rate(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_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002026 snd_pcm_known_rates.count,
2027 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Takashi Iwai877211f2005-11-17 13:59:38 +01002030static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2031 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Takashi Iwai877211f2005-11-17 13:59:38 +01002033 struct snd_interval t;
2034 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 t.min = 0;
2036 t.max = substream->buffer_bytes_max;
2037 t.openmin = 0;
2038 t.openmax = 0;
2039 t.integer = 1;
2040 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2041}
2042
Takashi Iwai877211f2005-11-17 13:59:38 +01002043int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Takashi Iwai877211f2005-11-17 13:59:38 +01002045 struct snd_pcm_runtime *runtime = substream->runtime;
2046 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 int k, err;
2048
2049 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2050 snd_mask_any(constrs_mask(constrs, k));
2051 }
2052
2053 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2054 snd_interval_any(constrs_interval(constrs, k));
2055 }
2056
2057 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2058 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2059 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2060 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2061 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2062
2063 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2064 snd_pcm_hw_rule_format, NULL,
2065 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2066 if (err < 0)
2067 return err;
2068 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2069 snd_pcm_hw_rule_sample_bits, NULL,
2070 SNDRV_PCM_HW_PARAM_FORMAT,
2071 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2072 if (err < 0)
2073 return err;
2074 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2075 snd_pcm_hw_rule_div, NULL,
2076 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2077 if (err < 0)
2078 return err;
2079 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2080 snd_pcm_hw_rule_mul, NULL,
2081 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2082 if (err < 0)
2083 return err;
2084 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2085 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2086 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2087 if (err < 0)
2088 return err;
2089 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2090 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2091 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2092 if (err < 0)
2093 return err;
2094 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2095 snd_pcm_hw_rule_div, NULL,
2096 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2097 if (err < 0)
2098 return err;
2099 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2100 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2101 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2102 if (err < 0)
2103 return err;
2104 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2105 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2106 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2107 if (err < 0)
2108 return err;
2109 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2110 snd_pcm_hw_rule_div, NULL,
2111 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2112 if (err < 0)
2113 return err;
2114 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2115 snd_pcm_hw_rule_div, NULL,
2116 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2117 if (err < 0)
2118 return err;
2119 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2120 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2121 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2122 if (err < 0)
2123 return err;
2124 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2125 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2126 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2127 if (err < 0)
2128 return err;
2129 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2130 snd_pcm_hw_rule_mul, NULL,
2131 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2132 if (err < 0)
2133 return err;
2134 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2135 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2136 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2137 if (err < 0)
2138 return err;
2139 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2140 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2141 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2142 if (err < 0)
2143 return err;
2144 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2145 snd_pcm_hw_rule_muldivk, (void*) 8,
2146 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2147 if (err < 0)
2148 return err;
2149 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2150 snd_pcm_hw_rule_muldivk, (void*) 8,
2151 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2152 if (err < 0)
2153 return err;
2154 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2155 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2156 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2157 if (err < 0)
2158 return err;
2159 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2160 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2161 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2162 if (err < 0)
2163 return err;
2164 return 0;
2165}
2166
Takashi Iwai877211f2005-11-17 13:59:38 +01002167int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Takashi Iwai877211f2005-11-17 13:59:38 +01002169 struct snd_pcm_runtime *runtime = substream->runtime;
2170 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 int err;
2172 unsigned int mask = 0;
2173
2174 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2175 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2176 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2177 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002178 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2180 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2181 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2182 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2183 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2184 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2185 }
2186 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002187 if (err < 0)
2188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
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_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002195 if (err < 0)
2196 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2199 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002200 if (err < 0)
2201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2204 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002205 if (err < 0)
2206 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2209 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002210 if (err < 0)
2211 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2214 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002215 if (err < 0)
2216 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2219 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002220 if (err < 0)
2221 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2224 snd_pcm_hw_rule_buffer_bytes_max, substream,
2225 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2226 if (err < 0)
2227 return err;
2228
2229 /* FIXME: remove */
2230 if (runtime->dma_bytes) {
2231 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002232 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
2235
2236 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2237 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2238 snd_pcm_hw_rule_rate, hw,
2239 SNDRV_PCM_HW_PARAM_RATE, -1);
2240 if (err < 0)
2241 return err;
2242 }
2243
2244 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2246
2247 return 0;
2248}
2249
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002250static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002253}
2254
2255void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2256{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002257 substream->ref_count--;
2258 if (substream->ref_count > 0)
2259 return;
2260
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002261 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002262 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002263 if (substream->ops->hw_free &&
2264 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 substream->ops->hw_free(substream);
2266 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002267 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002269 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2270 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002271 if (substream->pcm_release) {
2272 substream->pcm_release(substream);
2273 substream->pcm_release = NULL;
2274 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002275 snd_pcm_detach_substream(substream);
2276}
2277
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002278EXPORT_SYMBOL(snd_pcm_release_substream);
2279
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002280int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2281 struct file *file,
2282 struct snd_pcm_substream **rsubstream)
2283{
2284 struct snd_pcm_substream *substream;
2285 int err;
2286
2287 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2288 if (err < 0)
2289 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002290 if (substream->ref_count > 1) {
2291 *rsubstream = substream;
2292 return 0;
2293 }
2294
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002295 err = snd_pcm_hw_constraints_init(substream);
2296 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002297 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002298 goto error;
2299 }
2300
2301 if ((err = substream->ops->open(substream)) < 0)
2302 goto error;
2303
2304 substream->hw_opened = 1;
2305
2306 err = snd_pcm_hw_constraints_complete(substream);
2307 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002308 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002309 goto error;
2310 }
2311
2312 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002314
2315 error:
2316 snd_pcm_release_substream(substream);
2317 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002320EXPORT_SYMBOL(snd_pcm_open_substream);
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002323 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002324 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Takashi Iwai877211f2005-11-17 13:59:38 +01002326 struct snd_pcm_file *pcm_file;
2327 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002328 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002330 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2331 if (err < 0)
2332 return err;
2333
Takashi Iwai548a6482006-07-31 16:51:51 +02002334 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2335 if (pcm_file == NULL) {
2336 snd_pcm_release_substream(substream);
2337 return -ENOMEM;
2338 }
2339 pcm_file->substream = substream;
2340 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002341 substream->file = pcm_file;
2342 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return 0;
2347}
2348
Clemens Ladischf87135f2005-11-20 14:06:59 +01002349static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Takashi Iwai877211f2005-11-17 13:59:38 +01002351 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002352 int err = nonseekable_open(inode, file);
2353 if (err < 0)
2354 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002355 pcm = snd_lookup_minor_data(iminor(inode),
2356 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002357 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002358 if (pcm)
2359 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002360 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002361}
2362
2363static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2364{
2365 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002366 int err = nonseekable_open(inode, file);
2367 if (err < 0)
2368 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002369 pcm = snd_lookup_minor_data(iminor(inode),
2370 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002371 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002372 if (pcm)
2373 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002374 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002375}
2376
2377static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2378{
2379 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 wait_queue_t wait;
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (pcm == NULL) {
2383 err = -ENODEV;
2384 goto __error1;
2385 }
2386 err = snd_card_file_add(pcm->card, file);
2387 if (err < 0)
2388 goto __error1;
2389 if (!try_module_get(pcm->card->module)) {
2390 err = -EFAULT;
2391 goto __error2;
2392 }
2393 init_waitqueue_entry(&wait, current);
2394 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002395 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002397 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (err >= 0)
2399 break;
2400 if (err == -EAGAIN) {
2401 if (file->f_flags & O_NONBLOCK) {
2402 err = -EBUSY;
2403 break;
2404 }
2405 } else
2406 break;
2407 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002408 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002410 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002411 if (pcm->card->shutdown) {
2412 err = -ENODEV;
2413 break;
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (signal_pending(current)) {
2416 err = -ERESTARTSYS;
2417 break;
2418 }
2419 }
2420 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002421 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (err < 0)
2423 goto __error;
2424 return err;
2425
2426 __error:
2427 module_put(pcm->card->module);
2428 __error2:
2429 snd_card_file_remove(pcm->card, file);
2430 __error1:
2431 return err;
2432}
2433
2434static int snd_pcm_release(struct inode *inode, struct file *file)
2435{
Takashi Iwai877211f2005-11-17 13:59:38 +01002436 struct snd_pcm *pcm;
2437 struct snd_pcm_substream *substream;
2438 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 pcm_file = file->private_data;
2441 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002442 if (snd_BUG_ON(!substream))
2443 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002445 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002446 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002447 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002448 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 wake_up(&pcm->open_wait);
2450 module_put(pcm->card->module);
2451 snd_card_file_remove(pcm->card, file);
2452 return 0;
2453}
2454
Takashi Iwai877211f2005-11-17 13:59:38 +01002455static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2456 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Takashi Iwai877211f2005-11-17 13:59:38 +01002458 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 snd_pcm_sframes_t appl_ptr;
2460 snd_pcm_sframes_t ret;
2461 snd_pcm_sframes_t hw_avail;
2462
2463 if (frames == 0)
2464 return 0;
2465
2466 snd_pcm_stream_lock_irq(substream);
2467 switch (runtime->status->state) {
2468 case SNDRV_PCM_STATE_PREPARED:
2469 break;
2470 case SNDRV_PCM_STATE_DRAINING:
2471 case SNDRV_PCM_STATE_RUNNING:
2472 if (snd_pcm_update_hw_ptr(substream) >= 0)
2473 break;
2474 /* Fall through */
2475 case SNDRV_PCM_STATE_XRUN:
2476 ret = -EPIPE;
2477 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002478 case SNDRV_PCM_STATE_SUSPENDED:
2479 ret = -ESTRPIPE;
2480 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 default:
2482 ret = -EBADFD;
2483 goto __end;
2484 }
2485
2486 hw_avail = snd_pcm_playback_hw_avail(runtime);
2487 if (hw_avail <= 0) {
2488 ret = 0;
2489 goto __end;
2490 }
2491 if (frames > (snd_pcm_uframes_t)hw_avail)
2492 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 appl_ptr = runtime->control->appl_ptr - frames;
2494 if (appl_ptr < 0)
2495 appl_ptr += runtime->boundary;
2496 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 ret = frames;
2498 __end:
2499 snd_pcm_stream_unlock_irq(substream);
2500 return ret;
2501}
2502
Takashi Iwai877211f2005-11-17 13:59:38 +01002503static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2504 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Takashi Iwai877211f2005-11-17 13:59:38 +01002506 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 snd_pcm_sframes_t appl_ptr;
2508 snd_pcm_sframes_t ret;
2509 snd_pcm_sframes_t hw_avail;
2510
2511 if (frames == 0)
2512 return 0;
2513
2514 snd_pcm_stream_lock_irq(substream);
2515 switch (runtime->status->state) {
2516 case SNDRV_PCM_STATE_PREPARED:
2517 case SNDRV_PCM_STATE_DRAINING:
2518 break;
2519 case SNDRV_PCM_STATE_RUNNING:
2520 if (snd_pcm_update_hw_ptr(substream) >= 0)
2521 break;
2522 /* Fall through */
2523 case SNDRV_PCM_STATE_XRUN:
2524 ret = -EPIPE;
2525 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002526 case SNDRV_PCM_STATE_SUSPENDED:
2527 ret = -ESTRPIPE;
2528 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 default:
2530 ret = -EBADFD;
2531 goto __end;
2532 }
2533
2534 hw_avail = snd_pcm_capture_hw_avail(runtime);
2535 if (hw_avail <= 0) {
2536 ret = 0;
2537 goto __end;
2538 }
2539 if (frames > (snd_pcm_uframes_t)hw_avail)
2540 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 appl_ptr = runtime->control->appl_ptr - frames;
2542 if (appl_ptr < 0)
2543 appl_ptr += runtime->boundary;
2544 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 ret = frames;
2546 __end:
2547 snd_pcm_stream_unlock_irq(substream);
2548 return ret;
2549}
2550
Takashi Iwai877211f2005-11-17 13:59:38 +01002551static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2552 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Takashi Iwai877211f2005-11-17 13:59:38 +01002554 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 snd_pcm_sframes_t appl_ptr;
2556 snd_pcm_sframes_t ret;
2557 snd_pcm_sframes_t avail;
2558
2559 if (frames == 0)
2560 return 0;
2561
2562 snd_pcm_stream_lock_irq(substream);
2563 switch (runtime->status->state) {
2564 case SNDRV_PCM_STATE_PREPARED:
2565 case SNDRV_PCM_STATE_PAUSED:
2566 break;
2567 case SNDRV_PCM_STATE_DRAINING:
2568 case SNDRV_PCM_STATE_RUNNING:
2569 if (snd_pcm_update_hw_ptr(substream) >= 0)
2570 break;
2571 /* Fall through */
2572 case SNDRV_PCM_STATE_XRUN:
2573 ret = -EPIPE;
2574 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002575 case SNDRV_PCM_STATE_SUSPENDED:
2576 ret = -ESTRPIPE;
2577 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 default:
2579 ret = -EBADFD;
2580 goto __end;
2581 }
2582
2583 avail = snd_pcm_playback_avail(runtime);
2584 if (avail <= 0) {
2585 ret = 0;
2586 goto __end;
2587 }
2588 if (frames > (snd_pcm_uframes_t)avail)
2589 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 appl_ptr = runtime->control->appl_ptr + frames;
2591 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2592 appl_ptr -= runtime->boundary;
2593 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 ret = frames;
2595 __end:
2596 snd_pcm_stream_unlock_irq(substream);
2597 return ret;
2598}
2599
Takashi Iwai877211f2005-11-17 13:59:38 +01002600static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2601 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Takashi Iwai877211f2005-11-17 13:59:38 +01002603 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 snd_pcm_sframes_t appl_ptr;
2605 snd_pcm_sframes_t ret;
2606 snd_pcm_sframes_t avail;
2607
2608 if (frames == 0)
2609 return 0;
2610
2611 snd_pcm_stream_lock_irq(substream);
2612 switch (runtime->status->state) {
2613 case SNDRV_PCM_STATE_PREPARED:
2614 case SNDRV_PCM_STATE_DRAINING:
2615 case SNDRV_PCM_STATE_PAUSED:
2616 break;
2617 case SNDRV_PCM_STATE_RUNNING:
2618 if (snd_pcm_update_hw_ptr(substream) >= 0)
2619 break;
2620 /* Fall through */
2621 case SNDRV_PCM_STATE_XRUN:
2622 ret = -EPIPE;
2623 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002624 case SNDRV_PCM_STATE_SUSPENDED:
2625 ret = -ESTRPIPE;
2626 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 default:
2628 ret = -EBADFD;
2629 goto __end;
2630 }
2631
2632 avail = snd_pcm_capture_avail(runtime);
2633 if (avail <= 0) {
2634 ret = 0;
2635 goto __end;
2636 }
2637 if (frames > (snd_pcm_uframes_t)avail)
2638 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 appl_ptr = runtime->control->appl_ptr + frames;
2640 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2641 appl_ptr -= runtime->boundary;
2642 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 ret = frames;
2644 __end:
2645 snd_pcm_stream_unlock_irq(substream);
2646 return ret;
2647}
2648
Takashi Iwai877211f2005-11-17 13:59:38 +01002649static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
Takashi Iwai877211f2005-11-17 13:59:38 +01002651 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 int err;
2653
2654 snd_pcm_stream_lock_irq(substream);
2655 switch (runtime->status->state) {
2656 case SNDRV_PCM_STATE_DRAINING:
2657 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2658 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002659 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 case SNDRV_PCM_STATE_RUNNING:
2661 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2662 break;
2663 /* Fall through */
2664 case SNDRV_PCM_STATE_PREPARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 err = 0;
2666 break;
Jeeja KPf3f6c612016-09-02 21:49:44 +05302667 case SNDRV_PCM_STATE_SUSPENDED:
2668 err = -ESTRPIPE;
2669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 case SNDRV_PCM_STATE_XRUN:
2671 err = -EPIPE;
2672 break;
2673 default:
2674 __badfd:
2675 err = -EBADFD;
2676 break;
2677 }
2678 snd_pcm_stream_unlock_irq(substream);
2679 return err;
2680}
2681
Takashi Iwai877211f2005-11-17 13:59:38 +01002682static int snd_pcm_delay(struct snd_pcm_substream *substream,
2683 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Takashi Iwai877211f2005-11-17 13:59:38 +01002685 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 int err;
2687 snd_pcm_sframes_t n = 0;
2688
2689 snd_pcm_stream_lock_irq(substream);
2690 switch (runtime->status->state) {
2691 case SNDRV_PCM_STATE_DRAINING:
2692 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2693 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002694 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 case SNDRV_PCM_STATE_RUNNING:
2696 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2697 break;
2698 /* Fall through */
2699 case SNDRV_PCM_STATE_PREPARED:
2700 case SNDRV_PCM_STATE_SUSPENDED:
2701 err = 0;
2702 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2703 n = snd_pcm_playback_hw_avail(runtime);
2704 else
2705 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002706 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
2708 case SNDRV_PCM_STATE_XRUN:
2709 err = -EPIPE;
2710 break;
2711 default:
2712 __badfd:
2713 err = -EBADFD;
2714 break;
2715 }
2716 snd_pcm_stream_unlock_irq(substream);
2717 if (!err)
2718 if (put_user(n, res))
2719 err = -EFAULT;
2720 return err;
2721}
2722
Takashi Iwai877211f2005-11-17 13:59:38 +01002723static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2724 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Takashi Iwai877211f2005-11-17 13:59:38 +01002726 struct snd_pcm_runtime *runtime = substream->runtime;
2727 struct snd_pcm_sync_ptr sync_ptr;
2728 volatile struct snd_pcm_mmap_status *status;
2729 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 int err;
Santosh Mardie68908a2012-10-09 10:32:42 +05302731 snd_pcm_uframes_t hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 memset(&sync_ptr, 0, sizeof(sync_ptr));
2734 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2735 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002736 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 -07002737 return -EFAULT;
2738 status = runtime->status;
2739 control = runtime->control;
2740 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2741 err = snd_pcm_hwsync(substream);
2742 if (err < 0)
2743 return err;
2744 }
2745 snd_pcm_stream_lock_irq(substream);
2746 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2747 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2748 else
2749 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2750 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2751 control->avail_min = sync_ptr.c.control.avail_min;
2752 else
2753 sync_ptr.c.control.avail_min = control->avail_min;
Santosh Mardie68908a2012-10-09 10:32:42 +05302754
2755 if (runtime->render_flag & SNDRV_NON_DMA_MODE) {
2756 hw_avail = snd_pcm_playback_hw_avail(runtime);
2757 if ((hw_avail >= runtime->start_threshold)
2758 && (runtime->render_flag &
2759 SNDRV_RENDER_STOPPED)) {
2760 if (substream->ops->restart)
2761 substream->ops->restart(substream);
2762 }
2763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 sync_ptr.s.status.state = status->state;
2765 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2766 sync_ptr.s.status.tstamp = status->tstamp;
2767 sync_ptr.s.status.suspended_state = status->suspended_state;
2768 snd_pcm_stream_unlock_irq(substream);
2769 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2770 return -EFAULT;
2771 return 0;
2772}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002773
2774static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2775{
2776 struct snd_pcm_runtime *runtime = substream->runtime;
2777 int arg;
2778
2779 if (get_user(arg, _arg))
2780 return -EFAULT;
2781 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2782 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002783 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002784 return 0;
2785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Takashi Iwai0df63e42006-04-28 15:13:41 +02002787static int snd_pcm_common_ioctl1(struct file *file,
2788 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 unsigned int cmd, void __user *arg)
2790{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 switch (cmd) {
2792 case SNDRV_PCM_IOCTL_PVERSION:
2793 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2794 case SNDRV_PCM_IOCTL_INFO:
2795 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002796 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2797 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002798 case SNDRV_PCM_IOCTL_TTSTAMP:
2799 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 case SNDRV_PCM_IOCTL_HW_REFINE:
2801 return snd_pcm_hw_refine_user(substream, arg);
2802 case SNDRV_PCM_IOCTL_HW_PARAMS:
2803 return snd_pcm_hw_params_user(substream, arg);
2804 case SNDRV_PCM_IOCTL_HW_FREE:
2805 return snd_pcm_hw_free(substream);
2806 case SNDRV_PCM_IOCTL_SW_PARAMS:
2807 return snd_pcm_sw_params_user(substream, arg);
2808 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002809 return snd_pcm_status_user(substream, arg, false);
2810 case SNDRV_PCM_IOCTL_STATUS_EXT:
2811 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2813 return snd_pcm_channel_info_user(substream, arg);
2814 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002815 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 case SNDRV_PCM_IOCTL_RESET:
2817 return snd_pcm_reset(substream);
2818 case SNDRV_PCM_IOCTL_START:
2819 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2820 case SNDRV_PCM_IOCTL_LINK:
2821 return snd_pcm_link(substream, (int)(unsigned long) arg);
2822 case SNDRV_PCM_IOCTL_UNLINK:
2823 return snd_pcm_unlink(substream);
2824 case SNDRV_PCM_IOCTL_RESUME:
2825 return snd_pcm_resume(substream);
2826 case SNDRV_PCM_IOCTL_XRUN:
2827 return snd_pcm_xrun(substream);
2828 case SNDRV_PCM_IOCTL_HWSYNC:
2829 return snd_pcm_hwsync(substream);
2830 case SNDRV_PCM_IOCTL_DELAY:
2831 return snd_pcm_delay(substream, arg);
2832 case SNDRV_PCM_IOCTL_SYNC_PTR:
2833 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002834#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2836 return snd_pcm_hw_refine_old_user(substream, arg);
2837 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2838 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002839#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002841 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 case SNDRV_PCM_IOCTL_DROP:
2843 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002844 case SNDRV_PCM_IOCTL_PAUSE:
2845 {
2846 int res;
2847 snd_pcm_stream_lock_irq(substream);
2848 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2849 snd_pcm_stream_unlock_irq(substream);
2850 return res;
2851 }
Sudheer Papothib40e9682016-01-29 02:19:04 +05302852 case SNDRV_COMPRESS_GET_CAPS:
2853 case SNDRV_COMPRESS_GET_CODEC_CAPS:
2854 case SNDRV_COMPRESS_SET_PARAMS:
2855 case SNDRV_COMPRESS_GET_PARAMS:
2856 case SNDRV_COMPRESS_TSTAMP:
2857 case SNDRV_COMPRESS_DRAIN:
2858 return snd_compressed_ioctl(substream, cmd, arg);
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05302859 default:
2860 if (((cmd >> 8) & 0xff) == 'U')
2861 return snd_user_ioctl(substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002863 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return -ENOTTY;
2865}
2866
Takashi Iwai0df63e42006-04-28 15:13:41 +02002867static int snd_pcm_playback_ioctl1(struct file *file,
2868 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 unsigned int cmd, void __user *arg)
2870{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002871 if (snd_BUG_ON(!substream))
2872 return -ENXIO;
2873 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 switch (cmd) {
2876 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2877 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002878 struct snd_xferi xferi;
2879 struct snd_xferi __user *_xferi = arg;
2880 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 snd_pcm_sframes_t result;
2882 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2883 return -EBADFD;
2884 if (put_user(0, &_xferi->result))
2885 return -EFAULT;
2886 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2887 return -EFAULT;
2888 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2889 __put_user(result, &_xferi->result);
2890 return result < 0 ? result : 0;
2891 }
2892 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2893 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002894 struct snd_xfern xfern;
2895 struct snd_xfern __user *_xfern = arg;
2896 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 void __user **bufs;
2898 snd_pcm_sframes_t result;
2899 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2900 return -EBADFD;
2901 if (runtime->channels > 128)
2902 return -EINVAL;
2903 if (put_user(0, &_xfern->result))
2904 return -EFAULT;
2905 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2906 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002907
2908 bufs = memdup_user(xfern.bufs,
2909 sizeof(void *) * runtime->channels);
2910 if (IS_ERR(bufs))
2911 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2913 kfree(bufs);
2914 __put_user(result, &_xfern->result);
2915 return result < 0 ? result : 0;
2916 }
2917 case SNDRV_PCM_IOCTL_REWIND:
2918 {
2919 snd_pcm_uframes_t frames;
2920 snd_pcm_uframes_t __user *_frames = arg;
2921 snd_pcm_sframes_t result;
2922 if (get_user(frames, _frames))
2923 return -EFAULT;
2924 if (put_user(0, _frames))
2925 return -EFAULT;
2926 result = snd_pcm_playback_rewind(substream, frames);
2927 __put_user(result, _frames);
2928 return result < 0 ? result : 0;
2929 }
2930 case SNDRV_PCM_IOCTL_FORWARD:
2931 {
2932 snd_pcm_uframes_t frames;
2933 snd_pcm_uframes_t __user *_frames = arg;
2934 snd_pcm_sframes_t result;
2935 if (get_user(frames, _frames))
2936 return -EFAULT;
2937 if (put_user(0, _frames))
2938 return -EFAULT;
2939 result = snd_pcm_playback_forward(substream, frames);
2940 __put_user(result, _frames);
2941 return result < 0 ? result : 0;
2942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002944 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
Takashi Iwai0df63e42006-04-28 15:13:41 +02002947static int snd_pcm_capture_ioctl1(struct file *file,
2948 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 unsigned int cmd, void __user *arg)
2950{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002951 if (snd_BUG_ON(!substream))
2952 return -ENXIO;
2953 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2954 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 switch (cmd) {
2956 case SNDRV_PCM_IOCTL_READI_FRAMES:
2957 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002958 struct snd_xferi xferi;
2959 struct snd_xferi __user *_xferi = arg;
2960 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 snd_pcm_sframes_t result;
2962 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2963 return -EBADFD;
2964 if (put_user(0, &_xferi->result))
2965 return -EFAULT;
2966 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2967 return -EFAULT;
2968 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2969 __put_user(result, &_xferi->result);
2970 return result < 0 ? result : 0;
2971 }
2972 case SNDRV_PCM_IOCTL_READN_FRAMES:
2973 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002974 struct snd_xfern xfern;
2975 struct snd_xfern __user *_xfern = arg;
2976 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 void *bufs;
2978 snd_pcm_sframes_t result;
2979 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2980 return -EBADFD;
2981 if (runtime->channels > 128)
2982 return -EINVAL;
2983 if (put_user(0, &_xfern->result))
2984 return -EFAULT;
2985 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2986 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002987
2988 bufs = memdup_user(xfern.bufs,
2989 sizeof(void *) * runtime->channels);
2990 if (IS_ERR(bufs))
2991 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2993 kfree(bufs);
2994 __put_user(result, &_xfern->result);
2995 return result < 0 ? result : 0;
2996 }
2997 case SNDRV_PCM_IOCTL_REWIND:
2998 {
2999 snd_pcm_uframes_t frames;
3000 snd_pcm_uframes_t __user *_frames = arg;
3001 snd_pcm_sframes_t result;
3002 if (get_user(frames, _frames))
3003 return -EFAULT;
3004 if (put_user(0, _frames))
3005 return -EFAULT;
3006 result = snd_pcm_capture_rewind(substream, frames);
3007 __put_user(result, _frames);
3008 return result < 0 ? result : 0;
3009 }
3010 case SNDRV_PCM_IOCTL_FORWARD:
3011 {
3012 snd_pcm_uframes_t frames;
3013 snd_pcm_uframes_t __user *_frames = arg;
3014 snd_pcm_sframes_t result;
3015 if (get_user(frames, _frames))
3016 return -EFAULT;
3017 if (put_user(0, _frames))
3018 return -EFAULT;
3019 result = snd_pcm_capture_forward(substream, frames);
3020 __put_user(result, _frames);
3021 return result < 0 ? result : 0;
3022 }
3023 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02003024 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
Takashi Iwai877211f2005-11-17 13:59:38 +01003027static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
3028 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Takashi Iwai877211f2005-11-17 13:59:38 +01003030 struct snd_pcm_file *pcm_file;
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303031 unsigned char ioctl_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033 pcm_file = file->private_data;
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303034 ioctl_magic = ((cmd >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303036 if (ioctl_magic != 'A' && ioctl_magic != 'C' && ioctl_magic != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return -ENOTTY;
3038
Takashi Iwai0df63e42006-04-28 15:13:41 +02003039 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3040 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Takashi Iwai877211f2005-11-17 13:59:38 +01003043static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3044 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Takashi Iwai877211f2005-11-17 13:59:38 +01003046 struct snd_pcm_file *pcm_file;
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303047 unsigned char ioctl_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 pcm_file = file->private_data;
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303050 ioctl_magic = ((cmd >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303052 if (ioctl_magic != 'A' && ioctl_magic != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return -ENOTTY;
3054
Takashi Iwai0df63e42006-04-28 15:13:41 +02003055 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3056 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
Takashi Iwai877211f2005-11-17 13:59:38 +01003059int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 unsigned int cmd, void *arg)
3061{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003062 mm_segment_t fs;
3063 int result;
3064
3065 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 switch (substream->stream) {
3067 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003068 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3069 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003070 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003072 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3073 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003076 result = -EINVAL;
3077 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003079 snd_leave_user(fs);
3080 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003083EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3084
Takashi Iwai877211f2005-11-17 13:59:38 +01003085static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3086 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Takashi Iwai877211f2005-11-17 13:59:38 +01003088 struct snd_pcm_file *pcm_file;
3089 struct snd_pcm_substream *substream;
3090 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 snd_pcm_sframes_t result;
3092
3093 pcm_file = file->private_data;
3094 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003095 if (PCM_RUNTIME_CHECK(substream))
3096 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 runtime = substream->runtime;
3098 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3099 return -EBADFD;
3100 if (!frame_aligned(runtime, count))
3101 return -EINVAL;
3102 count = bytes_to_frames(runtime, count);
3103 result = snd_pcm_lib_read(substream, buf, count);
3104 if (result > 0)
3105 result = frames_to_bytes(runtime, result);
3106 return result;
3107}
3108
Takashi Iwai877211f2005-11-17 13:59:38 +01003109static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3110 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Takashi Iwai877211f2005-11-17 13:59:38 +01003112 struct snd_pcm_file *pcm_file;
3113 struct snd_pcm_substream *substream;
3114 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 snd_pcm_sframes_t result;
3116
3117 pcm_file = file->private_data;
3118 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003119 if (PCM_RUNTIME_CHECK(substream))
3120 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003122 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3123 return -EBADFD;
3124 if (!frame_aligned(runtime, count))
3125 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 count = bytes_to_frames(runtime, count);
3127 result = snd_pcm_lib_write(substream, buf, count);
3128 if (result > 0)
3129 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return result;
3131}
3132
Al Viro1c65d982015-04-04 00:19:32 -04003133static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Takashi Iwai877211f2005-11-17 13:59:38 +01003135 struct snd_pcm_file *pcm_file;
3136 struct snd_pcm_substream *substream;
3137 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 snd_pcm_sframes_t result;
3139 unsigned long i;
3140 void __user **bufs;
3141 snd_pcm_uframes_t frames;
3142
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003143 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003145 if (PCM_RUNTIME_CHECK(substream))
3146 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 runtime = substream->runtime;
3148 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3149 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003150 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003152 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003154 if (!frame_aligned(runtime, to->iov->iov_len))
3155 return -EINVAL;
3156 frames = bytes_to_samples(runtime, to->iov->iov_len);
3157 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if (bufs == NULL)
3159 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003160 for (i = 0; i < to->nr_segs; ++i)
3161 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 result = snd_pcm_lib_readv(substream, bufs, frames);
3163 if (result > 0)
3164 result = frames_to_bytes(runtime, result);
3165 kfree(bufs);
3166 return result;
3167}
3168
Al Viro1c65d982015-04-04 00:19:32 -04003169static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
Takashi Iwai877211f2005-11-17 13:59:38 +01003171 struct snd_pcm_file *pcm_file;
3172 struct snd_pcm_substream *substream;
3173 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 snd_pcm_sframes_t result;
3175 unsigned long i;
3176 void __user **bufs;
3177 snd_pcm_uframes_t frames;
3178
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003179 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003181 if (PCM_RUNTIME_CHECK(substream))
3182 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003184 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3185 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003186 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003187 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003188 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3189 !frame_aligned(runtime, from->iov->iov_len))
3190 return -EINVAL;
3191 frames = bytes_to_samples(runtime, from->iov->iov_len);
3192 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (bufs == NULL)
3194 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003195 for (i = 0; i < from->nr_segs; ++i)
3196 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 result = snd_pcm_lib_writev(substream, bufs, frames);
3198 if (result > 0)
3199 result = frames_to_bytes(runtime, result);
3200 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return result;
3202}
3203
3204static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3205{
Takashi Iwai877211f2005-11-17 13:59:38 +01003206 struct snd_pcm_file *pcm_file;
3207 struct snd_pcm_substream *substream;
3208 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 unsigned int mask;
3210 snd_pcm_uframes_t avail;
3211
3212 pcm_file = file->private_data;
3213
3214 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003215 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003216 return POLLOUT | POLLWRNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 runtime = substream->runtime;
3218
3219 poll_wait(file, &runtime->sleep, wait);
3220
3221 snd_pcm_stream_lock_irq(substream);
3222 avail = snd_pcm_playback_avail(runtime);
3223 switch (runtime->status->state) {
3224 case SNDRV_PCM_STATE_RUNNING:
3225 case SNDRV_PCM_STATE_PREPARED:
3226 case SNDRV_PCM_STATE_PAUSED:
3227 if (avail >= runtime->control->avail_min) {
3228 mask = POLLOUT | POLLWRNORM;
3229 break;
3230 }
3231 /* Fall through */
3232 case SNDRV_PCM_STATE_DRAINING:
3233 mask = 0;
3234 break;
3235 default:
3236 mask = POLLOUT | POLLWRNORM | POLLERR;
3237 break;
3238 }
3239 snd_pcm_stream_unlock_irq(substream);
3240 return mask;
3241}
3242
3243static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3244{
Takashi Iwai877211f2005-11-17 13:59:38 +01003245 struct snd_pcm_file *pcm_file;
3246 struct snd_pcm_substream *substream;
3247 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 unsigned int mask;
3249 snd_pcm_uframes_t avail;
3250
3251 pcm_file = file->private_data;
3252
3253 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003254 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003255 return POLLIN | POLLRDNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 runtime = substream->runtime;
3257
3258 poll_wait(file, &runtime->sleep, wait);
3259
3260 snd_pcm_stream_lock_irq(substream);
3261 avail = snd_pcm_capture_avail(runtime);
3262 switch (runtime->status->state) {
3263 case SNDRV_PCM_STATE_RUNNING:
3264 case SNDRV_PCM_STATE_PREPARED:
3265 case SNDRV_PCM_STATE_PAUSED:
3266 if (avail >= runtime->control->avail_min) {
3267 mask = POLLIN | POLLRDNORM;
3268 break;
3269 }
3270 mask = 0;
3271 break;
3272 case SNDRV_PCM_STATE_DRAINING:
3273 if (avail > 0) {
3274 mask = POLLIN | POLLRDNORM;
3275 break;
3276 }
3277 /* Fall through */
3278 default:
3279 mask = POLLIN | POLLRDNORM | POLLERR;
3280 break;
3281 }
3282 snd_pcm_stream_unlock_irq(substream);
3283 return mask;
3284}
3285
3286/*
3287 * mmap support
3288 */
3289
3290/*
3291 * Only on coherent architectures, we can mmap the status and the control records
3292 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3293 */
3294#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3295/*
3296 * mmap status record
3297 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003298static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3299 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Takashi Iwai877211f2005-11-17 13:59:38 +01003301 struct snd_pcm_substream *substream = area->vm_private_data;
3302 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
3304 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003305 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003307 vmf->page = virt_to_page(runtime->status);
3308 get_page(vmf->page);
3309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003312static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003314 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315};
3316
Takashi Iwai877211f2005-11-17 13:59:38 +01003317static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 struct vm_area_struct *area)
3319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 long size;
3321 if (!(area->vm_flags & VM_READ))
3322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003324 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return -EINVAL;
3326 area->vm_ops = &snd_pcm_vm_ops_status;
3327 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003328 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return 0;
3330}
3331
3332/*
3333 * mmap control record
3334 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003335static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3336 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
Takashi Iwai877211f2005-11-17 13:59:38 +01003338 struct snd_pcm_substream *substream = area->vm_private_data;
3339 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003342 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003344 vmf->page = virt_to_page(runtime->control);
3345 get_page(vmf->page);
3346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003349static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003351 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352};
3353
Takashi Iwai877211f2005-11-17 13:59:38 +01003354static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 struct vm_area_struct *area)
3356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 long size;
3358 if (!(area->vm_flags & VM_READ))
3359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003361 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 return -EINVAL;
3363 area->vm_ops = &snd_pcm_vm_ops_control;
3364 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003365 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return 0;
3367}
3368#else /* ! coherent mmap */
3369/*
3370 * don't support mmap for status and control records.
3371 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003372static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 struct vm_area_struct *area)
3374{
3375 return -ENXIO;
3376}
Takashi Iwai877211f2005-11-17 13:59:38 +01003377static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 struct vm_area_struct *area)
3379{
3380 return -ENXIO;
3381}
3382#endif /* coherent mmap */
3383
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003384static inline struct page *
3385snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3386{
3387 void *vaddr = substream->runtime->dma_area + ofs;
3388 return virt_to_page(vaddr);
3389}
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003392 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003394static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3395 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
Takashi Iwai877211f2005-11-17 13:59:38 +01003397 struct snd_pcm_substream *substream = area->vm_private_data;
3398 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 unsigned long offset;
3400 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 size_t dma_bytes;
3402
3403 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003404 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003406 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3408 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003409 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003410 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003412 else
3413 page = snd_pcm_default_page_ops(substream, offset);
3414 if (!page)
3415 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003416 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003417 vmf->page = page;
3418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419}
3420
Takashi Iwai657b1982009-11-26 12:40:21 +01003421static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3422 .open = snd_pcm_mmap_data_open,
3423 .close = snd_pcm_mmap_data_close,
3424};
3425
3426static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 .open = snd_pcm_mmap_data_open,
3428 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003429 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430};
3431
3432/*
3433 * mmap the DMA buffer on RAM
3434 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003435
3436/**
3437 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3438 * @substream: PCM substream
3439 * @area: VMA
3440 *
3441 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3442 * this function is invoked implicitly.
3443 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003444int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3445 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003447 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003448#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003449 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3450 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3451 return remap_pfn_range(area, area->vm_start,
3452 substream->dma_buffer.addr >> PAGE_SHIFT,
3453 area->vm_end - area->vm_start, area->vm_page_prot);
3454 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003455#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003456#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003457 if (!substream->ops->page &&
3458 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3459 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3460 area,
3461 substream->runtime->dma_area,
3462 substream->runtime->dma_addr,
3463 area->vm_end - area->vm_start);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003464#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003465 /* mmap with fault handler */
3466 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 return 0;
3468}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003469EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471/*
3472 * mmap the DMA buffer on I/O memory area
3473 */
3474#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003475/**
3476 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3477 * @substream: PCM substream
3478 * @area: VMA
3479 *
3480 * When your hardware uses the iomapped pages as the hardware buffer and
3481 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3482 * is supposed to work only on limited architectures.
3483 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003484int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3485 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003487 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003490 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003492
3493EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494#endif /* SNDRV_PCM_INFO_MMAP */
3495
3496/*
3497 * mmap DMA buffer
3498 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003499int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 struct vm_area_struct *area)
3501{
Takashi Iwai877211f2005-11-17 13:59:38 +01003502 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 long size;
3504 unsigned long offset;
3505 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003506 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3509 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3510 return -EINVAL;
3511 } else {
3512 if (!(area->vm_flags & VM_READ))
3513 return -EINVAL;
3514 }
3515 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3517 return -EBADFD;
3518 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3519 return -ENXIO;
3520 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3521 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3522 return -EINVAL;
3523 size = area->vm_end - area->vm_start;
3524 offset = area->vm_pgoff << PAGE_SHIFT;
3525 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3526 if ((size_t)size > dma_bytes)
3527 return -EINVAL;
3528 if (offset > dma_bytes - size)
3529 return -EINVAL;
3530
Takashi Iwai657b1982009-11-26 12:40:21 +01003531 area->vm_ops = &snd_pcm_vm_ops_data;
3532 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003534 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003536 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003537 if (!err)
3538 atomic_inc(&substream->mmap_count);
3539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003542EXPORT_SYMBOL(snd_pcm_mmap_data);
3543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3545{
Takashi Iwai877211f2005-11-17 13:59:38 +01003546 struct snd_pcm_file * pcm_file;
3547 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 unsigned long offset;
3549
3550 pcm_file = file->private_data;
3551 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003552 if (PCM_RUNTIME_CHECK(substream))
3553 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
3555 offset = area->vm_pgoff << PAGE_SHIFT;
3556 switch (offset) {
3557 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003558 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return -ENXIO;
3560 return snd_pcm_mmap_status(substream, file, area);
3561 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003562 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 return -ENXIO;
3564 return snd_pcm_mmap_control(substream, file, area);
3565 default:
3566 return snd_pcm_mmap_data(substream, file, area);
3567 }
3568 return 0;
3569}
3570
3571static int snd_pcm_fasync(int fd, struct file * file, int on)
3572{
Takashi Iwai877211f2005-11-17 13:59:38 +01003573 struct snd_pcm_file * pcm_file;
3574 struct snd_pcm_substream *substream;
3575 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 pcm_file = file->private_data;
3578 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003579 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003580 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003582 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
3585/*
3586 * ioctl32 compat
3587 */
3588#ifdef CONFIG_COMPAT
3589#include "pcm_compat.c"
3590#else
3591#define snd_pcm_ioctl_compat NULL
3592#endif
3593
3594/*
3595 * To be removed helpers to keep binary compatibility
3596 */
3597
Takashi Iwai59d48582005-12-01 10:51:58 +01003598#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3600#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3601
Takashi Iwai877211f2005-11-17 13:59:38 +01003602static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3603 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
3605 unsigned int i;
3606
3607 memset(params, 0, sizeof(*params));
3608 params->flags = oparams->flags;
3609 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3610 params->masks[i].bits[0] = oparams->masks[i];
3611 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3612 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3613 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3614 params->info = oparams->info;
3615 params->msbits = oparams->msbits;
3616 params->rate_num = oparams->rate_num;
3617 params->rate_den = oparams->rate_den;
3618 params->fifo_size = oparams->fifo_size;
3619}
3620
Takashi Iwai877211f2005-11-17 13:59:38 +01003621static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3622 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 unsigned int i;
3625
3626 memset(oparams, 0, sizeof(*oparams));
3627 oparams->flags = params->flags;
3628 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3629 oparams->masks[i] = params->masks[i].bits[0];
3630 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3631 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3632 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3633 oparams->info = params->info;
3634 oparams->msbits = params->msbits;
3635 oparams->rate_num = params->rate_num;
3636 oparams->rate_den = params->rate_den;
3637 oparams->fifo_size = params->fifo_size;
3638}
3639
Takashi Iwai877211f2005-11-17 13:59:38 +01003640static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3641 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
Takashi Iwai877211f2005-11-17 13:59:38 +01003643 struct snd_pcm_hw_params *params;
3644 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 int err;
3646
3647 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003648 if (!params)
3649 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Li Zefanef44a1e2009-04-10 09:43:08 +08003651 oparams = memdup_user(_oparams, sizeof(*oparams));
3652 if (IS_ERR(oparams)) {
3653 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 goto out;
3655 }
3656 snd_pcm_hw_convert_from_old_params(params, oparams);
3657 err = snd_pcm_hw_refine(substream, params);
3658 snd_pcm_hw_convert_to_old_params(oparams, params);
3659 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3660 if (!err)
3661 err = -EFAULT;
3662 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003663
3664 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665out:
3666 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 return err;
3668}
3669
Takashi Iwai877211f2005-11-17 13:59:38 +01003670static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3671 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Takashi Iwai877211f2005-11-17 13:59:38 +01003673 struct snd_pcm_hw_params *params;
3674 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 int err;
3676
3677 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003678 if (!params)
3679 return -ENOMEM;
3680
3681 oparams = memdup_user(_oparams, sizeof(*oparams));
3682 if (IS_ERR(oparams)) {
3683 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 goto out;
3685 }
3686 snd_pcm_hw_convert_from_old_params(params, oparams);
3687 err = snd_pcm_hw_params(substream, params);
3688 snd_pcm_hw_convert_to_old_params(oparams, params);
3689 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3690 if (!err)
3691 err = -EFAULT;
3692 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003693
3694 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695out:
3696 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return err;
3698}
Takashi Iwai59d48582005-12-01 10:51:58 +01003699#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Cliff Cai70036092008-09-03 10:54:36 +02003701#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003702static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3703 unsigned long addr,
3704 unsigned long len,
3705 unsigned long pgoff,
3706 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003707{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003708 struct snd_pcm_file *pcm_file = file->private_data;
3709 struct snd_pcm_substream *substream = pcm_file->substream;
3710 struct snd_pcm_runtime *runtime = substream->runtime;
3711 unsigned long offset = pgoff << PAGE_SHIFT;
3712
3713 switch (offset) {
3714 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3715 return (unsigned long)runtime->status;
3716 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3717 return (unsigned long)runtime->control;
3718 default:
3719 return (unsigned long)runtime->dma_area + offset;
3720 }
Cliff Cai70036092008-09-03 10:54:36 +02003721}
3722#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003723# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003724#endif
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726/*
3727 * Register section
3728 */
3729
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003730const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003732 .owner = THIS_MODULE,
3733 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003734 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003735 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003736 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003737 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003738 .poll = snd_pcm_playback_poll,
3739 .unlocked_ioctl = snd_pcm_playback_ioctl,
3740 .compat_ioctl = snd_pcm_ioctl_compat,
3741 .mmap = snd_pcm_mmap,
3742 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003743 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 },
3745 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003746 .owner = THIS_MODULE,
3747 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003748 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003749 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003750 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003751 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003752 .poll = snd_pcm_capture_poll,
3753 .unlocked_ioctl = snd_pcm_capture_ioctl,
3754 .compat_ioctl = snd_pcm_ioctl_compat,
3755 .mmap = snd_pcm_mmap,
3756 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003757 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 }
3759};