blob: 0179ef50b6b6554e68a47088f2488eb561ceb8a8 [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>
Chanho Minee8dce22018-11-26 14:36:37 +090039#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Compatibility
43 */
44
Takashi Iwai877211f2005-11-17 13:59:38 +010045struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 unsigned int flags;
47 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
48 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010049 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
51 unsigned int rmask;
52 unsigned int cmask;
53 unsigned int info;
54 unsigned int msbits;
55 unsigned int rate_num;
56 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010057 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned char reserved[64];
59};
60
Takashi Iwai59d48582005-12-01 10:51:58 +010061#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010062#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
63#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Takashi Iwai877211f2005-11-17 13:59:38 +010065static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
67static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
68 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010069#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010070static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
73 *
74 */
75
Takashi Iwai7af142f2014-09-01 11:19:37 +020076static DEFINE_RWLOCK(snd_pcm_link_rwlock);
77static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Takashi Iwai67ec1072016-02-17 14:30:26 +010079/* Writer in rwsem may block readers even during its waiting in queue,
80 * and this may lead to a deadlock when the code path takes read sem
81 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
82 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
Chanho Minee8dce22018-11-26 14:36:37 +090083 * sleep until all the readers are completed without blocking by writer.
Takashi Iwai67ec1072016-02-17 14:30:26 +010084 */
Chanho Minee8dce22018-11-26 14:36:37 +090085static inline void down_write_nonfifo(struct rw_semaphore *lock)
Takashi Iwai67ec1072016-02-17 14:30:26 +010086{
87 while (!down_write_trylock(lock))
Chanho Minee8dce22018-11-26 14:36:37 +090088 msleep(1);
Takashi Iwai67ec1072016-02-17 14:30:26 +010089}
90
Takashi Iwai30b771c2014-10-30 15:02:50 +010091/**
92 * snd_pcm_stream_lock - Lock the PCM stream
93 * @substream: PCM substream
94 *
95 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
96 * flag of the given substream. This also takes the global link rw lock
97 * (or rw sem), too, for avoiding the race with linked streams.
98 */
Takashi Iwai7af142f2014-09-01 11:19:37 +020099void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
100{
101 if (substream->pcm->nonatomic) {
Takashi Iwai67756e32015-07-17 15:22:33 +0200102 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
Takashi Iwai7af142f2014-09-01 11:19:37 +0200103 mutex_lock(&substream->self_group.mutex);
104 } else {
105 read_lock(&snd_pcm_link_rwlock);
106 spin_lock(&substream->self_group.lock);
107 }
108}
109EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
110
Takashi Iwai30b771c2014-10-30 15:02:50 +0100111/**
112 * snd_pcm_stream_lock - Unlock the PCM stream
113 * @substream: PCM substream
114 *
115 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
116 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200117void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
118{
119 if (substream->pcm->nonatomic) {
120 mutex_unlock(&substream->self_group.mutex);
121 up_read(&snd_pcm_link_rwsem);
122 } else {
123 spin_unlock(&substream->self_group.lock);
124 read_unlock(&snd_pcm_link_rwlock);
125 }
126}
127EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
128
Takashi Iwai30b771c2014-10-30 15:02:50 +0100129/**
130 * snd_pcm_stream_lock_irq - Lock the PCM stream
131 * @substream: PCM substream
132 *
133 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
134 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
135 * as snd_pcm_stream_lock().
136 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200137void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
138{
139 if (!substream->pcm->nonatomic)
140 local_irq_disable();
141 snd_pcm_stream_lock(substream);
142}
143EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
144
Takashi Iwai30b771c2014-10-30 15:02:50 +0100145/**
146 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
147 * @substream: PCM substream
148 *
149 * This is a counter-part of snd_pcm_stream_lock_irq().
150 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200151void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
152{
153 snd_pcm_stream_unlock(substream);
154 if (!substream->pcm->nonatomic)
155 local_irq_enable();
156}
157EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
158
159unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
160{
161 unsigned long flags = 0;
162 if (!substream->pcm->nonatomic)
163 local_irq_save(flags);
164 snd_pcm_stream_lock(substream);
165 return flags;
166}
167EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
168
Takashi Iwai30b771c2014-10-30 15:02:50 +0100169/**
170 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
171 * @substream: PCM substream
172 * @flags: irq flags
173 *
174 * This is a counter-part of snd_pcm_stream_lock_irqsave().
175 */
Takashi Iwai7af142f2014-09-01 11:19:37 +0200176void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
177 unsigned long flags)
178{
179 snd_pcm_stream_unlock(substream);
180 if (!substream->pcm->nonatomic)
181 local_irq_restore(flags);
182}
183EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185static inline mm_segment_t snd_enter_user(void)
186{
187 mm_segment_t fs = get_fs();
188 set_fs(get_ds());
189 return fs;
190}
191
192static inline void snd_leave_user(mm_segment_t fs)
193{
194 set_fs(fs);
195}
196
197
198
Takashi Iwai877211f2005-11-17 13:59:38 +0100199int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Takashi Iwai877211f2005-11-17 13:59:38 +0100201 struct snd_pcm *pcm = substream->pcm;
202 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 memset(info, 0, sizeof(*info));
205 info->card = pcm->card->number;
206 info->device = pcm->device;
207 info->stream = substream->stream;
208 info->subdevice = substream->number;
209 strlcpy(info->id, pcm->id, sizeof(info->id));
210 strlcpy(info->name, pcm->name, sizeof(info->name));
211 info->dev_class = pcm->dev_class;
212 info->dev_subclass = pcm->dev_subclass;
213 info->subdevices_count = pstr->substream_count;
214 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
215 strlcpy(info->subname, substream->name, sizeof(info->subname));
Karthikeyan Mani078c7b332017-10-02 16:56:55 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218}
219
Takashi Iwai877211f2005-11-17 13:59:38 +0100220int snd_pcm_info_user(struct snd_pcm_substream *substream,
221 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Takashi Iwai877211f2005-11-17 13:59:38 +0100223 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int err;
225
226 info = kmalloc(sizeof(*info), GFP_KERNEL);
227 if (! info)
228 return -ENOMEM;
229 err = snd_pcm_info(substream, info);
230 if (err >= 0) {
231 if (copy_to_user(_info, info, sizeof(*info)))
232 err = -EFAULT;
233 }
234 kfree(info);
235 return err;
236}
237
Takashi Iwai63825f32014-10-22 12:04:46 +0200238static bool hw_support_mmap(struct snd_pcm_substream *substream)
239{
240 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
241 return false;
242 /* check architectures that return -EINVAL from dma_mmap_coherent() */
243 /* FIXME: this should be some global flag */
244#if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
245 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
246 if (!substream->ops->mmap &&
247 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
248 return false;
249#endif
250 return true;
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#undef RULES_DEBUG
254
255#ifdef RULES_DEBUG
256#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
Joe Perches47023ec2010-09-13 21:24:02 -0700257static const char * const snd_pcm_hw_param_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 HW_PARAM(ACCESS),
259 HW_PARAM(FORMAT),
260 HW_PARAM(SUBFORMAT),
261 HW_PARAM(SAMPLE_BITS),
262 HW_PARAM(FRAME_BITS),
263 HW_PARAM(CHANNELS),
264 HW_PARAM(RATE),
265 HW_PARAM(PERIOD_TIME),
266 HW_PARAM(PERIOD_SIZE),
267 HW_PARAM(PERIOD_BYTES),
268 HW_PARAM(PERIODS),
269 HW_PARAM(BUFFER_TIME),
270 HW_PARAM(BUFFER_SIZE),
271 HW_PARAM(BUFFER_BYTES),
272 HW_PARAM(TICK_TIME),
273};
274#endif
275
Takashi Iwai877211f2005-11-17 13:59:38 +0100276int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
277 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100280 struct snd_pcm_hardware *hw;
281 struct snd_interval *i = NULL;
282 struct snd_mask *m = NULL;
283 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 unsigned int rstamps[constrs->rules_num];
285 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
286 unsigned int stamp = 2;
287 int changed, again;
288
289 params->info = 0;
290 params->fifo_size = 0;
291 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
292 params->msbits = 0;
293 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
294 params->rate_num = 0;
295 params->rate_den = 0;
296 }
297
298 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
299 m = hw_param_mask(params, k);
300 if (snd_mask_empty(m))
301 return -EINVAL;
302 if (!(params->rmask & (1 << k)))
303 continue;
304#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100305 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
306 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 -0700307#endif
308 changed = snd_mask_refine(m, constrs_mask(constrs, k));
309#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100310 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 -0700311#endif
312 if (changed)
313 params->cmask |= 1 << k;
314 if (changed < 0)
315 return changed;
316 }
317
318 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
319 i = hw_param_interval(params, k);
320 if (snd_interval_empty(i))
321 return -EINVAL;
322 if (!(params->rmask & (1 << k)))
323 continue;
324#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100325 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100327 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100329 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 i->openmin ? '(' : '[', i->min,
331 i->max, i->openmax ? ')' : ']');
Takashi Iwai09e56df2014-02-04 18:19:48 +0100332 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#endif
334 changed = snd_interval_refine(i, constrs_interval(constrs, k));
335#ifdef RULES_DEBUG
336 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100337 pr_cont("empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100339 pr_cont("%c%u %u%c\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 i->openmin ? '(' : '[', i->min,
341 i->max, i->openmax ? ')' : ']');
342#endif
343 if (changed)
344 params->cmask |= 1 << k;
345 if (changed < 0)
346 return changed;
347 }
348
349 for (k = 0; k < constrs->rules_num; k++)
350 rstamps[k] = 0;
351 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
352 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
353 do {
354 again = 0;
355 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100356 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 unsigned int d;
358 int doit = 0;
359 if (r->cond && !(r->cond & params->flags))
360 continue;
361 for (d = 0; r->deps[d] >= 0; d++) {
362 if (vstamps[r->deps[d]] > rstamps[k]) {
363 doit = 1;
364 break;
365 }
366 }
367 if (!doit)
368 continue;
369#ifdef RULES_DEBUG
Takashi Iwai09e56df2014-02-04 18:19:48 +0100370 pr_debug("Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100372 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (hw_is_mask(r->var)) {
374 m = hw_param_mask(params, r->var);
Takashi Iwai09e56df2014-02-04 18:19:48 +0100375 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else {
377 i = hw_param_interval(params, r->var);
378 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100379 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100381 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 i->openmin ? '(' : '[', i->min,
383 i->max, i->openmax ? ')' : ']');
384 }
385 }
386#endif
387 changed = r->func(params, r);
388#ifdef RULES_DEBUG
389 if (r->var >= 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +0100390 pr_cont(" -> ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (hw_is_mask(r->var))
Takashi Iwai09e56df2014-02-04 18:19:48 +0100392 pr_cont("%x", *m->bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 else {
394 if (i->empty)
Takashi Iwai09e56df2014-02-04 18:19:48 +0100395 pr_cont("empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 else
Takashi Iwai09e56df2014-02-04 18:19:48 +0100397 pr_cont("%c%u %u%c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 i->openmin ? '(' : '[', i->min,
399 i->max, i->openmax ? ')' : ']');
400 }
401 }
Takashi Iwai09e56df2014-02-04 18:19:48 +0100402 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#endif
404 rstamps[k] = stamp;
405 if (changed && r->var >= 0) {
406 params->cmask |= (1 << r->var);
407 vstamps[r->var] = stamp;
408 again = 1;
409 }
410 if (changed < 0)
411 return changed;
412 stamp++;
413 }
414 } while (again);
415 if (!params->msbits) {
416 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
417 if (snd_interval_single(i))
418 params->msbits = snd_interval_value(i);
419 }
420
421 if (!params->rate_den) {
422 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
423 if (snd_interval_single(i)) {
424 params->rate_num = snd_interval_value(i);
425 params->rate_den = 1;
426 }
427 }
428
429 hw = &substream->runtime->hw;
Takashi Iwai63825f32014-10-22 12:04:46 +0200430 if (!params->info) {
Libin Yang48d88292014-12-31 22:09:54 +0800431 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
432 SNDRV_PCM_INFO_DRAIN_TRIGGER);
Takashi Iwai63825f32014-10-22 12:04:46 +0200433 if (!hw_support_mmap(substream))
434 params->info &= ~(SNDRV_PCM_INFO_MMAP |
435 SNDRV_PCM_INFO_MMAP_VALID);
436 }
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200437 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100438 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
439 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
440 if (snd_mask_min(m) == snd_mask_max(m) &&
441 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200442 changed = substream->ops->ioctl(substream,
443 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200444 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200445 return changed;
446 }
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 params->rmask = 0;
449 return 0;
450}
451
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200452EXPORT_SYMBOL(snd_pcm_hw_refine);
453
Takashi Iwai877211f2005-11-17 13:59:38 +0100454static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
455 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Takashi Iwai877211f2005-11-17 13:59:38 +0100457 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int err;
459
Li Zefanef44a1e2009-04-10 09:43:08 +0800460 params = memdup_user(_params, sizeof(*params));
461 if (IS_ERR(params))
462 return PTR_ERR(params);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 err = snd_pcm_hw_refine(substream, params);
465 if (copy_to_user(_params, params, sizeof(*params))) {
466 if (!err)
467 err = -EFAULT;
468 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 kfree(params);
471 return err;
472}
473
Takashi Iwai9442e692006-09-30 23:27:19 -0700474static int period_to_usecs(struct snd_pcm_runtime *runtime)
475{
476 int usecs;
477
478 if (! runtime->rate)
479 return -1; /* invalid */
480
481 /* take 75% of period time as the deadline */
482 usecs = (750000 / runtime->rate) * runtime->period_size;
483 usecs += ((750000 % runtime->rate) * runtime->period_size) /
484 runtime->rate;
485
486 return usecs;
487}
488
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200489static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
490{
491 snd_pcm_stream_lock_irq(substream);
492 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
493 substream->runtime->status->state = state;
494 snd_pcm_stream_unlock_irq(substream);
495}
496
Jie Yang90bbaf62015-10-16 17:57:46 +0800497static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
498 int event)
499{
500#ifdef CONFIG_SND_PCM_TIMER
501 if (substream->timer)
502 snd_timer_notify(substream->timer, event,
503 &substream->runtime->trigger_tstamp);
504#endif
505}
506
Takashi Iwai877211f2005-11-17 13:59:38 +0100507static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
508 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Takashi Iwai877211f2005-11-17 13:59:38 +0100510 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700511 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 unsigned int bits;
513 snd_pcm_uframes_t frames;
514
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200515 if (PCM_RUNTIME_CHECK(substream))
516 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 snd_pcm_stream_lock_irq(substream);
519 switch (runtime->status->state) {
520 case SNDRV_PCM_STATE_OPEN:
521 case SNDRV_PCM_STATE_SETUP:
522 case SNDRV_PCM_STATE_PREPARED:
523 break;
524 default:
525 snd_pcm_stream_unlock_irq(substream);
526 return -EBADFD;
527 }
528 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100529#if IS_ENABLED(CONFIG_SND_PCM_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (!substream->oss.oss)
531#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200532 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return -EBADFD;
534
535 params->rmask = ~0U;
536 err = snd_pcm_hw_refine(substream, params);
537 if (err < 0)
538 goto _error;
539
540 err = snd_pcm_hw_params_choose(substream, params);
541 if (err < 0)
542 goto _error;
543
544 if (substream->ops->hw_params != NULL) {
545 err = substream->ops->hw_params(substream, params);
546 if (err < 0)
547 goto _error;
548 }
549
550 runtime->access = params_access(params);
551 runtime->format = params_format(params);
552 runtime->subformat = params_subformat(params);
553 runtime->channels = params_channels(params);
554 runtime->rate = params_rate(params);
555 runtime->period_size = params_period_size(params);
556 runtime->periods = params_periods(params);
557 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 runtime->info = params->info;
559 runtime->rate_num = params->rate_num;
560 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100561 runtime->no_period_wakeup =
562 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
563 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 bits = snd_pcm_format_physical_width(runtime->format);
566 runtime->sample_bits = bits;
567 bits *= runtime->channels;
568 runtime->frame_bits = bits;
569 frames = 1;
570 while (bits % 8 != 0) {
571 bits *= 2;
572 frames *= 2;
573 }
574 runtime->byte_align = bits / 8;
575 runtime->min_align = frames;
576
577 /* Default sw params */
578 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
579 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 runtime->start_threshold = 1;
582 runtime->stop_threshold = runtime->buffer_size;
583 runtime->silence_threshold = 0;
584 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200585 runtime->boundary = runtime->buffer_size;
Laxminath Kasam8428b712013-07-11 10:55:26 +0530586 while (runtime->boundary * 2 * runtime->channels <=
587 LONG_MAX - runtime->buffer_size)
Clemens Ladischead40462010-05-21 09:15:59 +0200588 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Takashi Iwai360440c2019-12-11 16:57:42 +0100590 /* clear the buffer for avoiding possible kernel info leaks */
Takashi Iwaiea4eb1a2020-01-29 10:40:41 +0100591 if (runtime->dma_area && !substream->ops->copy)
Takashi Iwai360440c2019-12-11 16:57:42 +0100592 memset(runtime->dma_area, 0, runtime->dma_bytes);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 snd_pcm_timer_resolution_change(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200595 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai9442e692006-09-30 23:27:19 -0700596
James Bottomley82f68252010-07-05 22:53:06 +0200597 if (pm_qos_request_active(&substream->latency_pm_qos_req))
598 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700599 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200600 pm_qos_add_request(&substream->latency_pm_qos_req,
601 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300604 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 so we force application to retry to set
606 the correct hardware parameter settings */
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200607 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (substream->ops->hw_free != NULL)
609 substream->ops->hw_free(substream);
610 return err;
611}
612
Takashi Iwai877211f2005-11-17 13:59:38 +0100613static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
614 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwai877211f2005-11-17 13:59:38 +0100616 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int err;
618
Li Zefanef44a1e2009-04-10 09:43:08 +0800619 params = memdup_user(_params, sizeof(*params));
620 if (IS_ERR(params))
621 return PTR_ERR(params);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 err = snd_pcm_hw_params(substream, params);
624 if (copy_to_user(_params, params, sizeof(*params))) {
625 if (!err)
626 err = -EFAULT;
627 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 kfree(params);
630 return err;
631}
632
Takashi Iwai877211f2005-11-17 13:59:38 +0100633static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwai877211f2005-11-17 13:59:38 +0100635 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int result = 0;
637
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200638 if (PCM_RUNTIME_CHECK(substream))
639 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 snd_pcm_stream_lock_irq(substream);
642 switch (runtime->status->state) {
643 case SNDRV_PCM_STATE_SETUP:
644 case SNDRV_PCM_STATE_PREPARED:
645 break;
646 default:
647 snd_pcm_stream_unlock_irq(substream);
648 return -EBADFD;
649 }
650 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200651 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -EBADFD;
653 if (substream->ops->hw_free)
654 result = substream->ops->hw_free(substream);
Takashi Iwai9b0573c2012-10-12 15:07:34 +0200655 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
James Bottomley82f68252010-07-05 22:53:06 +0200656 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return result;
658}
659
Takashi Iwai877211f2005-11-17 13:59:38 +0100660static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
661 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Takashi Iwai877211f2005-11-17 13:59:38 +0100663 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100664 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200666 if (PCM_RUNTIME_CHECK(substream))
667 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 snd_pcm_stream_lock_irq(substream);
670 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
671 snd_pcm_stream_unlock_irq(substream);
672 return -EBADFD;
673 }
674 snd_pcm_stream_unlock_irq(substream);
675
Dan Carpenter145d92e2015-09-23 12:42:28 +0300676 if (params->tstamp_mode < 0 ||
677 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EINVAL;
Takashi Iwai58900812014-07-16 17:45:27 +0200679 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
680 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
Takashi Iwai5646eda2014-07-10 09:50:19 +0200681 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (params->avail_min == 0)
683 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (params->silence_size >= runtime->boundary) {
685 if (params->silence_threshold != 0)
686 return -EINVAL;
687 } else {
688 if (params->silence_size > params->silence_threshold)
689 return -EINVAL;
690 if (params->silence_threshold > runtime->buffer_size)
691 return -EINVAL;
692 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100693 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 snd_pcm_stream_lock_irq(substream);
695 runtime->tstamp_mode = params->tstamp_mode;
Takashi Iwai58900812014-07-16 17:45:27 +0200696 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
697 runtime->tstamp_type = params->tstamp_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 runtime->period_step = params->period_step;
699 runtime->control->avail_min = params->avail_min;
700 runtime->start_threshold = params->start_threshold;
701 runtime->stop_threshold = params->stop_threshold;
702 runtime->silence_threshold = params->silence_threshold;
703 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 params->boundary = runtime->boundary;
705 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
707 runtime->silence_size > 0)
708 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100709 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Takashi Iwai877211f2005-11-17 13:59:38 +0100715static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
716 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Takashi Iwai877211f2005-11-17 13:59:38 +0100718 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int err;
720 if (copy_from_user(&params, _params, sizeof(params)))
721 return -EFAULT;
722 err = snd_pcm_sw_params(substream, &params);
723 if (copy_to_user(_params, &params, sizeof(params)))
724 return -EFAULT;
725 return err;
726}
727
Takashi Iwai877211f2005-11-17 13:59:38 +0100728int snd_pcm_status(struct snd_pcm_substream *substream,
729 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai877211f2005-11-17 13:59:38 +0100731 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 snd_pcm_stream_lock_irq(substream);
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600734
735 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
736 &runtime->audio_tstamp_config);
737
738 /* backwards compatible behavior */
739 if (runtime->audio_tstamp_config.type_requested ==
740 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
741 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
742 runtime->audio_tstamp_config.type_requested =
743 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
744 else
745 runtime->audio_tstamp_config.type_requested =
746 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
747 runtime->audio_tstamp_report.valid = 0;
748 } else
749 runtime->audio_tstamp_report.valid = 1;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 status->state = runtime->status->state;
752 status->suspended_state = runtime->status->suspended_state;
753 if (status->state == SNDRV_PCM_STATE_OPEN)
754 goto _end;
755 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100756 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100758 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
759 status->tstamp = runtime->status->tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600760 status->driver_tstamp = runtime->driver_tstamp;
Pierre-Louis Bossart4eeaaea2012-10-22 16:42:15 -0500761 status->audio_tstamp =
762 runtime->status->audio_tstamp;
Pierre-Louis Bossart3179f622015-02-13 15:14:06 -0600763 if (runtime->audio_tstamp_report.valid == 1)
764 /* backwards compatibility, no report provided in COMPAT mode */
765 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
766 &status->audio_tstamp_accuracy,
767 &runtime->audio_tstamp_report);
768
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100769 goto _tstamp_end;
770 }
Pierre-Louis Bossart0d59b812015-02-06 15:55:50 -0600771 } else {
772 /* get tstamp only in fallback mode and only if enabled */
773 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
774 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100775 }
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100776 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 status->appl_ptr = runtime->control->appl_ptr;
778 status->hw_ptr = runtime->status->hw_ptr;
779 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
780 status->avail = snd_pcm_playback_avail(runtime);
781 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200782 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200784 status->delay += runtime->delay;
785 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 status->delay = 0;
787 } else {
788 status->avail = snd_pcm_capture_avail(runtime);
789 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200790 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 else
792 status->delay = 0;
793 }
794 status->avail_max = runtime->avail_max;
795 status->overrange = runtime->overrange;
796 runtime->avail_max = 0;
797 runtime->overrange = 0;
798 _end:
799 snd_pcm_stream_unlock_irq(substream);
800 return 0;
801}
802
Takashi Iwai877211f2005-11-17 13:59:38 +0100803static int snd_pcm_status_user(struct snd_pcm_substream *substream,
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600804 struct snd_pcm_status __user * _status,
805 bool ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Takashi Iwai877211f2005-11-17 13:59:38 +0100807 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int res;
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 memset(&status, 0, sizeof(status));
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -0600811 /*
812 * with extension, parameters are read/write,
813 * get audio_tstamp_data from user,
814 * ignore rest of status structure
815 */
816 if (ext && get_user(status.audio_tstamp_data,
817 (u32 __user *)(&_status->audio_tstamp_data)))
818 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 res = snd_pcm_status(substream, &status);
820 if (res < 0)
821 return res;
822 if (copy_to_user(_status, &status, sizeof(status)))
823 return -EFAULT;
824 return 0;
825}
826
Takashi Iwai877211f2005-11-17 13:59:38 +0100827static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
828 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai877211f2005-11-17 13:59:38 +0100830 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 unsigned int channel;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 channel = info->channel;
834 runtime = substream->runtime;
835 snd_pcm_stream_lock_irq(substream);
836 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
837 snd_pcm_stream_unlock_irq(substream);
838 return -EBADFD;
839 }
840 snd_pcm_stream_unlock_irq(substream);
841 if (channel >= runtime->channels)
842 return -EINVAL;
843 memset(info, 0, sizeof(*info));
844 info->channel = channel;
845 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
846}
847
Takashi Iwai877211f2005-11-17 13:59:38 +0100848static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
849 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Takashi Iwai877211f2005-11-17 13:59:38 +0100851 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int res;
853
854 if (copy_from_user(&info, _info, sizeof(info)))
855 return -EFAULT;
856 res = snd_pcm_channel_info(substream, &info);
857 if (res < 0)
858 return res;
859 if (copy_to_user(_info, &info, sizeof(info)))
860 return -EFAULT;
861 return 0;
862}
863
Takashi Iwai877211f2005-11-17 13:59:38 +0100864static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Takashi Iwai877211f2005-11-17 13:59:38 +0100866 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (runtime->trigger_master == NULL)
868 return;
869 if (runtime->trigger_master == substream) {
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -0600870 if (!runtime->trigger_tstamp_latched)
871 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 } else {
873 snd_pcm_trigger_tstamp(runtime->trigger_master);
874 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
875 }
876 runtime->trigger_master = NULL;
877}
878
879struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100880 int (*pre_action)(struct snd_pcm_substream *substream, int state);
881 int (*do_action)(struct snd_pcm_substream *substream, int state);
882 void (*undo_action)(struct snd_pcm_substream *substream, int state);
883 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884};
885
886/*
887 * this functions is core for handling of linked stream
888 * Note: the stream state might be changed also on failure
889 * Note2: call with calling stream lock + link lock
890 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100891static int snd_pcm_action_group(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100892 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int state, int do_lock)
894{
Takashi Iwai877211f2005-11-17 13:59:38 +0100895 struct snd_pcm_substream *s = NULL;
896 struct snd_pcm_substream *s1;
Takashi Iwaidde1c652014-10-21 15:32:13 +0200897 int res = 0, depth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Takashi Iwaief991b92007-02-22 12:52:53 +0100899 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200900 if (do_lock && s != substream) {
901 if (s->pcm->nonatomic)
Takashi Iwaidde1c652014-10-21 15:32:13 +0200902 mutex_lock_nested(&s->self_group.mutex, depth);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200903 else
Takashi Iwaidde1c652014-10-21 15:32:13 +0200904 spin_lock_nested(&s->self_group.lock, depth);
905 depth++;
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 res = ops->pre_action(s, state);
908 if (res < 0)
909 goto _unlock;
910 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100911 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 res = ops->do_action(s, state);
913 if (res < 0) {
914 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100915 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (s1 == s) /* failed stream */
917 break;
918 ops->undo_action(s1, state);
919 }
920 }
921 s = NULL; /* unlock all */
922 goto _unlock;
923 }
924 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100925 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ops->post_action(s, state);
927 }
928 _unlock:
929 if (do_lock) {
930 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100931 snd_pcm_group_for_each_entry(s1, substream) {
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200932 if (s1 != substream) {
Takashi Iwai811deed2014-10-13 23:14:46 +0200933 if (s1->pcm->nonatomic)
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200934 mutex_unlock(&s1->self_group.mutex);
935 else
936 spin_unlock(&s1->self_group.lock);
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (s1 == s) /* end */
939 break;
940 }
941 }
942 return res;
943}
944
945/*
946 * Note: call with stream lock
947 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100948static int snd_pcm_action_single(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100949 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int state)
951{
952 int res;
953
954 res = ops->pre_action(substream, state);
955 if (res < 0)
956 return res;
957 res = ops->do_action(substream, state);
958 if (res == 0)
959 ops->post_action(substream, state);
960 else if (ops->undo_action)
961 ops->undo_action(substream, state);
962 return res;
963}
964
965/*
966 * Note: call with stream lock
967 */
Julia Lawallb17154c2015-11-29 16:36:40 +0100968static int snd_pcm_action(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100969 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int state)
971{
972 int res;
973
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100974 if (!snd_pcm_stream_linked(substream))
975 return snd_pcm_action_single(ops, substream, state);
Takashi Iwai257f8cc2014-08-29 15:32:29 +0200976
Takashi Iwaiaa8edd82014-10-31 15:19:36 +0100977 if (substream->pcm->nonatomic) {
978 if (!mutex_trylock(&substream->group->mutex)) {
979 mutex_unlock(&substream->self_group.mutex);
980 mutex_lock(&substream->group->mutex);
981 mutex_lock(&substream->self_group.mutex);
982 }
983 res = snd_pcm_action_group(ops, substream, state, 1);
984 mutex_unlock(&substream->group->mutex);
985 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!spin_trylock(&substream->group->lock)) {
987 spin_unlock(&substream->self_group.lock);
988 spin_lock(&substream->group->lock);
989 spin_lock(&substream->self_group.lock);
990 }
991 res = snd_pcm_action_group(ops, substream, state, 1);
992 spin_unlock(&substream->group->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 return res;
995}
996
997/*
998 * Note: don't use any locks before
999 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001000static int snd_pcm_action_lock_irq(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001001 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int state)
1003{
1004 int res;
1005
Takashi Iwaie3a4bd52014-10-31 14:45:04 +01001006 snd_pcm_stream_lock_irq(substream);
1007 res = snd_pcm_action(ops, substream, state);
1008 snd_pcm_stream_unlock_irq(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return res;
1010}
1011
1012/*
1013 */
Julia Lawallb17154c2015-11-29 16:36:40 +01001014static int snd_pcm_action_nonatomic(const struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +01001015 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int state)
1017{
1018 int res;
1019
1020 down_read(&snd_pcm_link_rwsem);
1021 if (snd_pcm_stream_linked(substream))
1022 res = snd_pcm_action_group(ops, substream, state, 0);
1023 else
1024 res = snd_pcm_action_single(ops, substream, state);
1025 up_read(&snd_pcm_link_rwsem);
1026 return res;
1027}
1028
1029/*
1030 * start callbacks
1031 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001032static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwai877211f2005-11-17 13:59:38 +01001034 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1036 return -EBADFD;
1037 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Liam Girdwoodf2727332016-01-29 02:27:17 +05301038 !substream->hw_no_buffer &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 !snd_pcm_playback_data(substream))
1040 return -EPIPE;
Pierre-Louis Bossart2b79d7a2015-02-06 15:55:51 -06001041 runtime->trigger_tstamp_latched = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 runtime->trigger_master = substream;
1043 return 0;
1044}
1045
Takashi Iwai877211f2005-11-17 13:59:38 +01001046static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 if (substream->runtime->trigger_master != substream)
1049 return 0;
1050 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1051}
1052
Takashi Iwai877211f2005-11-17 13:59:38 +01001053static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 if (substream->runtime->trigger_master == substream)
1056 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1057}
1058
Takashi Iwai877211f2005-11-17 13:59:38 +01001059static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Takashi Iwai877211f2005-11-17 13:59:38 +01001061 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001063 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +02001064 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1065 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 runtime->status->state = state;
1067 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1068 runtime->silence_size > 0)
1069 snd_pcm_playback_silence(substream, ULONG_MAX);
Jie Yang90bbaf62015-10-16 17:57:46 +08001070 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Julia Lawallb17154c2015-11-29 16:36:40 +01001073static const struct action_ops snd_pcm_action_start = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 .pre_action = snd_pcm_pre_start,
1075 .do_action = snd_pcm_do_start,
1076 .undo_action = snd_pcm_undo_start,
1077 .post_action = snd_pcm_post_start
1078};
1079
1080/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001081 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001082 * @substream: the PCM substream instance
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001083 *
1084 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001086int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Takashi Iwai877211f2005-11-17 13:59:38 +01001088 return snd_pcm_action(&snd_pcm_action_start, substream,
1089 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Sudheer Papothib40e9682016-01-29 02:19:04 +05301092static int snd_compressed_ioctl(struct snd_pcm_substream *substream,
1093 unsigned int cmd, void __user *arg)
1094{
1095 struct snd_pcm_runtime *runtime;
1096 int err = 0;
1097
1098 if (PCM_RUNTIME_CHECK(substream))
1099 return -ENXIO;
1100 runtime = substream->runtime;
1101 pr_debug("%s called with cmd = %d\n", __func__, cmd);
1102 err = substream->ops->ioctl(substream, cmd, arg);
1103 return err;
1104}
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05301105
1106static int snd_user_ioctl(struct snd_pcm_substream *substream,
1107 unsigned int cmd, void __user *arg)
1108{
1109 struct snd_pcm_runtime *runtime;
1110 int err = 0;
1111
1112 if (PCM_RUNTIME_CHECK(substream))
1113 return -ENXIO;
1114 runtime = substream->runtime;
1115 err = substream->ops->ioctl(substream, cmd, arg);
1116 return err;
1117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119/*
1120 * stop callbacks
1121 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001122static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Takashi Iwai877211f2005-11-17 13:59:38 +01001124 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1126 return -EBADFD;
1127 runtime->trigger_master = substream;
1128 return 0;
1129}
1130
Takashi Iwai877211f2005-11-17 13:59:38 +01001131static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 if (substream->runtime->trigger_master == substream &&
1134 snd_pcm_running(substream))
1135 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1136 return 0; /* unconditonally stop all substreams */
1137}
1138
Takashi Iwai877211f2005-11-17 13:59:38 +01001139static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Takashi Iwai877211f2005-11-17 13:59:38 +01001141 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (runtime->status->state != state) {
1143 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001144 runtime->status->state = state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001145 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001148 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Julia Lawallb17154c2015-11-29 16:36:40 +01001151static const struct action_ops snd_pcm_action_stop = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 .pre_action = snd_pcm_pre_stop,
1153 .do_action = snd_pcm_do_stop,
1154 .post_action = snd_pcm_post_stop
1155};
1156
1157/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001158 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +02001159 * @substream: the PCM substream instance
1160 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001162 * The state of each stream is then changed to the given state unconditionally.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001163 *
Masanari Iida0a114582014-02-09 00:47:36 +09001164 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Clemens Ladischfea952e2011-02-14 11:00:47 +01001166int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1169}
1170
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001171EXPORT_SYMBOL(snd_pcm_stop);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001174 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +02001175 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001177 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 * Unlike snd_pcm_stop(), this affects only the given stream.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001179 *
1180 * Return: Zero if succesful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001182int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Takashi Iwai877211f2005-11-17 13:59:38 +01001184 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1185 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
Takashi Iwai1fb85102014-11-07 17:08:28 +01001188/**
1189 * snd_pcm_stop_xrun - stop the running streams as XRUN
1190 * @substream: the PCM substream instance
Takashi Iwai1fb85102014-11-07 17:08:28 +01001191 *
1192 * This stops the given running substream (and all linked substreams) as XRUN.
1193 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1194 *
1195 * Return: Zero if successful, or a negative error code.
1196 */
1197int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1198{
1199 unsigned long flags;
1200 int ret = 0;
1201
1202 snd_pcm_stream_lock_irqsave(substream, flags);
1203 if (snd_pcm_running(substream))
1204 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1205 snd_pcm_stream_unlock_irqrestore(substream, flags);
1206 return ret;
1207}
1208EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/*
1211 * pause callbacks
1212 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001213static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwai877211f2005-11-17 13:59:38 +01001215 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1217 return -ENOSYS;
1218 if (push) {
1219 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1220 return -EBADFD;
1221 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1222 return -EBADFD;
1223 runtime->trigger_master = substream;
1224 return 0;
1225}
1226
Takashi Iwai877211f2005-11-17 13:59:38 +01001227static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 if (substream->runtime->trigger_master != substream)
1230 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +02001231 /* some drivers might use hw_ptr to recover from the pause -
1232 update the hw_ptr now */
1233 if (push)
1234 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001235 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001236 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001237 * delta, effectively to skip the check once.
1238 */
1239 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return substream->ops->trigger(substream,
1241 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1242 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1243}
1244
Takashi Iwai877211f2005-11-17 13:59:38 +01001245static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 if (substream->runtime->trigger_master == substream)
1248 substream->ops->trigger(substream,
1249 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1250 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1251}
1252
Takashi Iwai877211f2005-11-17 13:59:38 +01001253static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Takashi Iwai877211f2005-11-17 13:59:38 +01001255 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 snd_pcm_trigger_tstamp(substream);
1257 if (push) {
1258 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001259 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001261 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 } else {
1263 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Jie Yang90bbaf62015-10-16 17:57:46 +08001264 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266}
1267
Julia Lawallb17154c2015-11-29 16:36:40 +01001268static const struct action_ops snd_pcm_action_pause = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 .pre_action = snd_pcm_pre_pause,
1270 .do_action = snd_pcm_do_pause,
1271 .undo_action = snd_pcm_undo_pause,
1272 .post_action = snd_pcm_post_pause
1273};
1274
1275/*
1276 * Push/release the pause for all linked streams.
1277 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001278static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1281}
1282
1283#ifdef CONFIG_PM
1284/* suspend */
1285
Takashi Iwai877211f2005-11-17 13:59:38 +01001286static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Takashi Iwai877211f2005-11-17 13:59:38 +01001288 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaibe0c2682019-03-25 10:38:58 +01001289 switch (runtime->status->state) {
1290 case SNDRV_PCM_STATE_SUSPENDED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return -EBUSY;
Takashi Iwaibe0c2682019-03-25 10:38:58 +01001292 /* unresumable PCM state; return -EBUSY for skipping suspend */
1293 case SNDRV_PCM_STATE_OPEN:
1294 case SNDRV_PCM_STATE_SETUP:
1295 case SNDRV_PCM_STATE_DISCONNECTED:
1296 return -EBUSY;
1297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 runtime->trigger_master = substream;
1299 return 0;
1300}
1301
Takashi Iwai877211f2005-11-17 13:59:38 +01001302static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Takashi Iwai877211f2005-11-17 13:59:38 +01001304 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (runtime->trigger_master != substream)
1306 return 0;
1307 if (! snd_pcm_running(substream))
1308 return 0;
1309 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1310 return 0; /* suspend unconditionally */
1311}
1312
Takashi Iwai877211f2005-11-17 13:59:38 +01001313static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Takashi Iwai877211f2005-11-17 13:59:38 +01001315 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001317 runtime->status->suspended_state = runtime->status->state;
1318 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Jie Yang90bbaf62015-10-16 17:57:46 +08001319 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001321 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Julia Lawallb17154c2015-11-29 16:36:40 +01001324static const struct action_ops snd_pcm_action_suspend = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 .pre_action = snd_pcm_pre_suspend,
1326 .do_action = snd_pcm_do_suspend,
1327 .post_action = snd_pcm_post_suspend
1328};
1329
1330/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001331 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001332 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001335 *
1336 * Return: Zero if successful (or @substream is %NULL), or a negative error
1337 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001339int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 int err;
1342 unsigned long flags;
1343
Takashi Iwai603bf522005-11-17 15:59:14 +01001344 if (! substream)
1345 return 0;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 snd_pcm_stream_lock_irqsave(substream, flags);
1348 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1349 snd_pcm_stream_unlock_irqrestore(substream, flags);
1350 return err;
1351}
1352
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001353EXPORT_SYMBOL(snd_pcm_suspend);
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001356 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001357 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * After this call, all streams are changed to SUSPENDED state.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001360 *
1361 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001363int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Takashi Iwai877211f2005-11-17 13:59:38 +01001365 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int stream, err = 0;
1367
Takashi Iwai603bf522005-11-17 15:59:14 +01001368 if (! pcm)
1369 return 0;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001372 for (substream = pcm->streams[stream].substream;
1373 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* FIXME: the open/close code should lock this as well */
1375 if (substream->runtime == NULL)
1376 continue;
Ranjani Sridharan9b5a3412019-02-08 17:29:53 -06001377
1378 /*
1379 * Skip BE dai link PCM's that are internal and may
1380 * not have their substream ops set.
1381 */
1382 if (!substream->ops)
1383 continue;
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 err = snd_pcm_suspend(substream);
1386 if (err < 0 && err != -EBUSY)
1387 return err;
1388 }
1389 }
1390 return 0;
1391}
1392
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001393EXPORT_SYMBOL(snd_pcm_suspend_all);
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395/* resume */
1396
Takashi Iwai877211f2005-11-17 13:59:38 +01001397static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Takashi Iwai877211f2005-11-17 13:59:38 +01001399 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1401 return -ENOSYS;
1402 runtime->trigger_master = substream;
1403 return 0;
1404}
1405
Takashi Iwai877211f2005-11-17 13:59:38 +01001406static int snd_pcm_do_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 if (runtime->trigger_master != substream)
1410 return 0;
1411 /* DMA not running previously? */
1412 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1413 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1414 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1415 return 0;
1416 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1417}
1418
Takashi Iwai877211f2005-11-17 13:59:38 +01001419static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 if (substream->runtime->trigger_master == substream &&
1422 snd_pcm_running(substream))
1423 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1424}
1425
Takashi Iwai877211f2005-11-17 13:59:38 +01001426static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Takashi Iwai877211f2005-11-17 13:59:38 +01001428 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 snd_pcm_trigger_tstamp(substream);
Takashi Iwai9bc889b2014-11-06 12:15:25 +01001430 runtime->status->state = runtime->status->suspended_state;
Jie Yang90bbaf62015-10-16 17:57:46 +08001431 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Julia Lawallb17154c2015-11-29 16:36:40 +01001434static const struct action_ops snd_pcm_action_resume = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 .pre_action = snd_pcm_pre_resume,
1436 .do_action = snd_pcm_do_resume,
1437 .undo_action = snd_pcm_undo_resume,
1438 .post_action = snd_pcm_post_resume
1439};
1440
Takashi Iwai877211f2005-11-17 13:59:38 +01001441static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Takashi Iwai877211f2005-11-17 13:59:38 +01001443 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int res;
1445
1446 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001447 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1449 snd_power_unlock(card);
1450 return res;
1451}
1452
1453#else
1454
Takashi Iwai877211f2005-11-17 13:59:38 +01001455static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 return -ENOSYS;
1458}
1459
1460#endif /* CONFIG_PM */
1461
1462/*
1463 * xrun ioctl
1464 *
1465 * Change the RUNNING stream(s) to XRUN state.
1466 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001467static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Takashi Iwai877211f2005-11-17 13:59:38 +01001469 struct snd_card *card = substream->pcm->card;
1470 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 int result;
1472
1473 snd_power_lock(card);
1474 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001475 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (result < 0)
1477 goto _unlock;
1478 }
1479
1480 snd_pcm_stream_lock_irq(substream);
1481 switch (runtime->status->state) {
1482 case SNDRV_PCM_STATE_XRUN:
1483 result = 0; /* already there */
1484 break;
1485 case SNDRV_PCM_STATE_RUNNING:
1486 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1487 break;
1488 default:
1489 result = -EBADFD;
1490 }
1491 snd_pcm_stream_unlock_irq(substream);
1492 _unlock:
1493 snd_power_unlock(card);
1494 return result;
1495}
1496
1497/*
1498 * reset ioctl
1499 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001500static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Takashi Iwai877211f2005-11-17 13:59:38 +01001502 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 switch (runtime->status->state) {
1504 case SNDRV_PCM_STATE_RUNNING:
1505 case SNDRV_PCM_STATE_PREPARED:
1506 case SNDRV_PCM_STATE_PAUSED:
1507 case SNDRV_PCM_STATE_SUSPENDED:
1508 return 0;
1509 default:
1510 return -EBADFD;
1511 }
1512}
1513
Takashi Iwai877211f2005-11-17 13:59:38 +01001514static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Takashi Iwai877211f2005-11-17 13:59:38 +01001516 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1518 if (err < 0)
1519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001521 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1522 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 runtime->silence_start = runtime->status->hw_ptr;
1524 runtime->silence_filled = 0;
1525 return 0;
1526}
1527
Takashi Iwai877211f2005-11-17 13:59:38 +01001528static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Takashi Iwai877211f2005-11-17 13:59:38 +01001530 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 runtime->control->appl_ptr = runtime->status->hw_ptr;
1532 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1533 runtime->silence_size > 0)
1534 snd_pcm_playback_silence(substream, ULONG_MAX);
1535}
1536
Julia Lawallb17154c2015-11-29 16:36:40 +01001537static const struct action_ops snd_pcm_action_reset = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 .pre_action = snd_pcm_pre_reset,
1539 .do_action = snd_pcm_do_reset,
1540 .post_action = snd_pcm_post_reset
1541};
1542
Takashi Iwai877211f2005-11-17 13:59:38 +01001543static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1546}
1547
1548/*
1549 * prepare ioctl
1550 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001551/* we use the second argument for updating f_flags */
1552static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1553 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Takashi Iwai877211f2005-11-17 13:59:38 +01001555 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001556 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1557 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return -EBADFD;
1559 if (snd_pcm_running(substream))
1560 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001561 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return 0;
1563}
1564
Takashi Iwai877211f2005-11-17 13:59:38 +01001565static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 int err;
1568 err = substream->ops->prepare(substream);
1569 if (err < 0)
1570 return err;
1571 return snd_pcm_do_reset(substream, 0);
1572}
1573
Takashi Iwai877211f2005-11-17 13:59:38 +01001574static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Takashi Iwai877211f2005-11-17 13:59:38 +01001576 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 runtime->control->appl_ptr = runtime->status->hw_ptr;
Takashi Iwai9b0573c2012-10-12 15:07:34 +02001578 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Julia Lawallb17154c2015-11-29 16:36:40 +01001581static const struct action_ops snd_pcm_action_prepare = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 .pre_action = snd_pcm_pre_prepare,
1583 .do_action = snd_pcm_do_prepare,
1584 .post_action = snd_pcm_post_prepare
1585};
1586
1587/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001588 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001589 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001590 * @file: file to refer f_flags
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001591 *
1592 * Return: Zero if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001594static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1595 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001598 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001599 int f_flags;
1600
1601 if (file)
1602 f_flags = file->f_flags;
1603 else
1604 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001607 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001608 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1609 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 snd_power_unlock(card);
1611 return res;
1612}
1613
1614/*
1615 * drain ioctl
1616 */
1617
Takashi Iwai877211f2005-11-17 13:59:38 +01001618static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001620 struct snd_pcm_runtime *runtime = substream->runtime;
1621 switch (runtime->status->state) {
1622 case SNDRV_PCM_STATE_OPEN:
1623 case SNDRV_PCM_STATE_DISCONNECTED:
1624 case SNDRV_PCM_STATE_SUSPENDED:
1625 return -EBADFD;
1626 }
1627 runtime->trigger_master = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
1629}
1630
Takashi Iwai877211f2005-11-17 13:59:38 +01001631static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwai877211f2005-11-17 13:59:38 +01001633 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1635 switch (runtime->status->state) {
1636 case SNDRV_PCM_STATE_PREPARED:
1637 /* start playback stream if possible */
1638 if (! snd_pcm_playback_empty(substream)) {
1639 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1640 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
Takashi Iwai70372a72014-12-18 10:02:41 +01001641 } else {
1642 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 break;
1645 case SNDRV_PCM_STATE_RUNNING:
1646 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1647 break;
Takashi Iwai4f7c39d2012-05-21 11:59:57 +02001648 case SNDRV_PCM_STATE_XRUN:
1649 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 default:
1652 break;
1653 }
1654 } else {
1655 /* stop running stream */
1656 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001657 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001659 snd_pcm_do_stop(substream, new_state);
1660 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662 }
Libin Yang48d88292014-12-31 22:09:54 +08001663
1664 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1665 runtime->trigger_master == substream &&
1666 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1667 return substream->ops->trigger(substream,
1668 SNDRV_PCM_TRIGGER_DRAIN);
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 0;
1671}
1672
Takashi Iwai877211f2005-11-17 13:59:38 +01001673static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675}
1676
Julia Lawallb17154c2015-11-29 16:36:40 +01001677static const struct action_ops snd_pcm_action_drain_init = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 .pre_action = snd_pcm_pre_drain_init,
1679 .do_action = snd_pcm_do_drain_init,
1680 .post_action = snd_pcm_post_drain_init
1681};
1682
Takashi Iwai877211f2005-11-17 13:59:38 +01001683static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685/*
1686 * Drain the stream(s).
1687 * When the substream is linked, sync until the draining of all playback streams
1688 * is finished.
1689 * After this call, all streams are supposed to be either SETUP or DRAINING
1690 * (capture only) state.
1691 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001692static int snd_pcm_drain(struct snd_pcm_substream *substream,
1693 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Takashi Iwai877211f2005-11-17 13:59:38 +01001695 struct snd_card *card;
1696 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001697 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001698 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001700 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 card = substream->pcm->card;
1703 runtime = substream->runtime;
1704
1705 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1706 return -EBADFD;
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 snd_power_lock(card);
1709 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001710 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001711 if (result < 0) {
1712 snd_power_unlock(card);
1713 return result;
1714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001717 if (file) {
1718 if (file->f_flags & O_NONBLOCK)
1719 nonblock = 1;
1720 } else if (substream->f_flags & O_NONBLOCK)
1721 nonblock = 1;
1722
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001723 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001724 snd_pcm_stream_lock_irq(substream);
1725 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001726 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001727 snd_pcm_pause(substream, 0);
1728
1729 /* pre-start/stop - all running streams are changed to DRAINING state */
1730 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001731 if (result < 0)
1732 goto unlock;
1733 /* in non-blocking, we don't wait in ioctl but let caller poll */
1734 if (nonblock) {
1735 result = -EAGAIN;
1736 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 for (;;) {
1740 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001741 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (signal_pending(current)) {
1743 result = -ERESTARTSYS;
1744 break;
1745 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001746 /* find a substream to drain */
1747 to_check = NULL;
1748 snd_pcm_group_for_each_entry(s, substream) {
1749 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1750 continue;
1751 runtime = s->runtime;
1752 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1753 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001754 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001755 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001756 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001757 if (!to_check)
1758 break; /* all drained */
1759 init_waitqueue_entry(&wait, current);
1760 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001762 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001764 if (runtime->no_period_wakeup)
1765 tout = MAX_SCHEDULE_TIMEOUT;
1766 else {
1767 tout = 10;
1768 if (runtime->rate) {
1769 long t = runtime->period_size * 2 / runtime->rate;
1770 tout = max(t, tout);
1771 }
1772 tout = msecs_to_jiffies(tout * 1000);
1773 }
1774 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001776 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001778 remove_wait_queue(&to_check->sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001779 if (card->shutdown) {
1780 result = -ENODEV;
1781 break;
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (tout == 0) {
1784 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1785 result = -ESTRPIPE;
1786 else {
Takashi Iwai09e56df2014-02-04 18:19:48 +01001787 dev_dbg(substream->pcm->card->dev,
1788 "playback drain error (DMA or IRQ trouble?)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1790 result = -EIO;
1791 }
1792 break;
1793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001795
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001796 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001797 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001798 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 return result;
1802}
1803
1804/*
1805 * drop ioctl
1806 *
1807 * Immediately put all linked substreams into SETUP state.
1808 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001809static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Takashi Iwai877211f2005-11-17 13:59:38 +01001811 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 int result = 0;
1813
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001814 if (PCM_RUNTIME_CHECK(substream))
1815 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001818 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001819 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1820 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return -EBADFD;
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 snd_pcm_stream_lock_irq(substream);
1824 /* resume pause */
1825 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1826 snd_pcm_pause(substream, 0);
1827
1828 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1829 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1830 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return result;
1833}
1834
1835
Al Viro0888c322013-06-05 14:09:55 -04001836static bool is_pcm_file(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Al Viro0888c322013-06-05 14:09:55 -04001838 struct inode *inode = file_inode(file);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001839 unsigned int minor;
1840
Al Viro0888c322013-06-05 14:09:55 -04001841 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1842 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 minor = iminor(inode);
Al Viro0888c322013-06-05 14:09:55 -04001844 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1845 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848/*
1849 * PCM link handling
1850 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001851static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 int res = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001854 struct snd_pcm_file *pcm_file;
1855 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001856 struct snd_pcm_group *group;
Al Viro0888c322013-06-05 14:09:55 -04001857 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Al Viro0888c322013-06-05 14:09:55 -04001859 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return -EBADFD;
Al Viro0888c322013-06-05 14:09:55 -04001861 if (!is_pcm_file(f.file)) {
1862 res = -EBADFD;
1863 goto _badf;
1864 }
1865 pcm_file = f.file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001867 group = kmalloc(sizeof(*group), GFP_KERNEL);
1868 if (!group) {
1869 res = -ENOMEM;
1870 goto _nolock;
1871 }
Chanho Minee8dce22018-11-26 14:36:37 +09001872 down_write_nonfifo(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 write_lock_irq(&snd_pcm_link_rwlock);
1874 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001875 substream->runtime->status->state != substream1->runtime->status->state ||
1876 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 res = -EBADFD;
1878 goto _end;
1879 }
1880 if (snd_pcm_stream_linked(substream1)) {
1881 res = -EALREADY;
1882 goto _end;
1883 }
1884 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001885 substream->group = group;
Al Virodd6c5cd2013-06-05 14:07:08 -04001886 group = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 spin_lock_init(&substream->group->lock);
Takashi Iwai257f8cc2014-08-29 15:32:29 +02001888 mutex_init(&substream->group->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 INIT_LIST_HEAD(&substream->group->substreams);
1890 list_add_tail(&substream->link_list, &substream->group->substreams);
1891 substream->group->count = 1;
1892 }
1893 list_add_tail(&substream1->link_list, &substream->group->substreams);
1894 substream->group->count++;
1895 substream1->group = substream->group;
1896 _end:
1897 write_unlock_irq(&snd_pcm_link_rwlock);
1898 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001899 _nolock:
Takashi Iwaia0830db2012-10-16 13:05:59 +02001900 snd_card_unref(substream1->pcm->card);
Al Virodd6c5cd2013-06-05 14:07:08 -04001901 kfree(group);
Al Viro0888c322013-06-05 14:09:55 -04001902 _badf:
1903 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return res;
1905}
1906
Takashi Iwai877211f2005-11-17 13:59:38 +01001907static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 substream->group = &substream->self_group;
1910 INIT_LIST_HEAD(&substream->self_group.substreams);
1911 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1912}
1913
Takashi Iwai877211f2005-11-17 13:59:38 +01001914static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Takashi Iwaief991b92007-02-22 12:52:53 +01001916 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 int res = 0;
1918
Chanho Minee8dce22018-11-26 14:36:37 +09001919 down_write_nonfifo(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 write_lock_irq(&snd_pcm_link_rwlock);
1921 if (!snd_pcm_stream_linked(substream)) {
1922 res = -EALREADY;
1923 goto _end;
1924 }
1925 list_del(&substream->link_list);
1926 substream->group->count--;
1927 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001928 snd_pcm_group_for_each_entry(s, substream) {
1929 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931 }
1932 kfree(substream->group);
1933 }
1934 relink_to_local(substream);
1935 _end:
1936 write_unlock_irq(&snd_pcm_link_rwlock);
1937 up_write(&snd_pcm_link_rwsem);
1938 return res;
1939}
1940
1941/*
1942 * hw configurator
1943 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001944static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1945 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Takashi Iwai877211f2005-11-17 13:59:38 +01001947 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1949 hw_param_interval_c(params, rule->deps[1]), &t);
1950 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1951}
1952
Takashi Iwai877211f2005-11-17 13:59:38 +01001953static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1954 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Takashi Iwai877211f2005-11-17 13:59:38 +01001956 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
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_muldivk(struct snd_pcm_hw_params *params,
1963 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Takashi Iwai877211f2005-11-17 13:59:38 +01001965 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1967 hw_param_interval_c(params, rule->deps[1]),
1968 (unsigned long) rule->private, &t);
1969 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1970}
1971
Takashi Iwai877211f2005-11-17 13:59:38 +01001972static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1973 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Takashi Iwai877211f2005-11-17 13:59:38 +01001975 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1977 (unsigned long) rule->private,
1978 hw_param_interval_c(params, rule->deps[1]), &t);
1979 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1980}
1981
Takashi Iwai877211f2005-11-17 13:59:38 +01001982static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1983 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001986 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1987 struct snd_mask m;
1988 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 snd_mask_any(&m);
1990 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1991 int bits;
1992 if (! snd_mask_test(mask, k))
1993 continue;
1994 bits = snd_pcm_format_physical_width(k);
1995 if (bits <= 0)
1996 continue; /* ignore invalid formats */
1997 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1998 snd_mask_reset(&m, k);
1999 }
2000 return snd_mask_refine(mask, &m);
2001}
2002
Takashi Iwai877211f2005-11-17 13:59:38 +01002003static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2004 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Takashi Iwai877211f2005-11-17 13:59:38 +01002006 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 unsigned int k;
2008 t.min = UINT_MAX;
2009 t.max = 0;
2010 t.openmin = 0;
2011 t.openmax = 0;
2012 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2013 int bits;
2014 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2015 continue;
2016 bits = snd_pcm_format_physical_width(k);
2017 if (bits <= 0)
2018 continue; /* ignore invalid formats */
2019 if (t.min > (unsigned)bits)
2020 t.min = bits;
2021 if (t.max < (unsigned)bits)
2022 t.max = bits;
2023 }
2024 t.integer = 1;
2025 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2026}
2027
2028#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2029#error "Change this table"
2030#endif
2031
2032static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
Vidyakumar Athotafdcbc7f2015-10-26 12:52:58 -07002033 48000, 64000, 88200, 96000, 176400, 192000,
Walter Yang2404d12e2016-11-04 14:21:01 +08002034 352800, 384000 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Clemens Ladisch7653d552007-08-13 17:38:54 +02002036const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2037 .count = ARRAY_SIZE(rates),
2038 .list = rates,
2039};
2040
Takashi Iwai877211f2005-11-17 13:59:38 +01002041static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2042 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Takashi Iwai877211f2005-11-17 13:59:38 +01002044 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02002046 snd_pcm_known_rates.count,
2047 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Takashi Iwai877211f2005-11-17 13:59:38 +01002050static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2051 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Takashi Iwai877211f2005-11-17 13:59:38 +01002053 struct snd_interval t;
2054 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 t.min = 0;
2056 t.max = substream->buffer_bytes_max;
2057 t.openmin = 0;
2058 t.openmax = 0;
2059 t.integer = 1;
2060 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2061}
2062
Takashi Iwai877211f2005-11-17 13:59:38 +01002063int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Takashi Iwai877211f2005-11-17 13:59:38 +01002065 struct snd_pcm_runtime *runtime = substream->runtime;
2066 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 int k, err;
2068
2069 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2070 snd_mask_any(constrs_mask(constrs, k));
2071 }
2072
2073 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2074 snd_interval_any(constrs_interval(constrs, k));
2075 }
2076
2077 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2078 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2079 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2080 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2081 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2082
2083 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2084 snd_pcm_hw_rule_format, NULL,
2085 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2086 if (err < 0)
2087 return err;
2088 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2089 snd_pcm_hw_rule_sample_bits, NULL,
2090 SNDRV_PCM_HW_PARAM_FORMAT,
2091 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2092 if (err < 0)
2093 return err;
2094 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2095 snd_pcm_hw_rule_div, NULL,
2096 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2097 if (err < 0)
2098 return err;
2099 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2100 snd_pcm_hw_rule_mul, NULL,
2101 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2102 if (err < 0)
2103 return err;
2104 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2105 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2106 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2107 if (err < 0)
2108 return err;
2109 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2110 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2111 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2112 if (err < 0)
2113 return err;
2114 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2115 snd_pcm_hw_rule_div, NULL,
2116 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2117 if (err < 0)
2118 return err;
2119 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2120 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2121 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2122 if (err < 0)
2123 return err;
2124 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2125 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2126 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2127 if (err < 0)
2128 return err;
2129 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2130 snd_pcm_hw_rule_div, NULL,
2131 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2132 if (err < 0)
2133 return err;
2134 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2135 snd_pcm_hw_rule_div, NULL,
2136 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2137 if (err < 0)
2138 return err;
2139 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2140 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2141 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2142 if (err < 0)
2143 return err;
2144 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2145 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2146 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2147 if (err < 0)
2148 return err;
2149 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2150 snd_pcm_hw_rule_mul, NULL,
2151 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2152 if (err < 0)
2153 return err;
2154 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2155 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2156 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2157 if (err < 0)
2158 return err;
2159 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2160 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2161 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2162 if (err < 0)
2163 return err;
2164 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2165 snd_pcm_hw_rule_muldivk, (void*) 8,
2166 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2167 if (err < 0)
2168 return err;
2169 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2170 snd_pcm_hw_rule_muldivk, (void*) 8,
2171 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2172 if (err < 0)
2173 return err;
2174 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2175 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2176 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2177 if (err < 0)
2178 return err;
2179 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2180 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2181 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2182 if (err < 0)
2183 return err;
2184 return 0;
2185}
2186
Takashi Iwai877211f2005-11-17 13:59:38 +01002187int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
Takashi Iwai877211f2005-11-17 13:59:38 +01002189 struct snd_pcm_runtime *runtime = substream->runtime;
2190 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 int err;
2192 unsigned int mask = 0;
2193
2194 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2195 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2196 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2197 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
Takashi Iwai63825f32014-10-22 12:04:46 +02002198 if (hw_support_mmap(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2200 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2201 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2202 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2203 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2204 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2205 }
2206 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002207 if (err < 0)
2208 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002211 if (err < 0)
2212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
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_CHANNELS,
2219 hw->channels_min, hw->channels_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_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2224 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002225 if (err < 0)
2226 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2229 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01002230 if (err < 0)
2231 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2234 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002235 if (err < 0)
2236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2239 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002240 if (err < 0)
2241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2244 snd_pcm_hw_rule_buffer_bytes_max, substream,
2245 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2246 if (err < 0)
2247 return err;
2248
2249 /* FIXME: remove */
2250 if (runtime->dma_bytes) {
2251 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002252 if (err < 0)
Sachin Kamat6cf95152012-11-21 14:36:55 +05302253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255
2256 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2257 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2258 snd_pcm_hw_rule_rate, hw,
2259 SNDRV_PCM_HW_PARAM_RATE, -1);
2260 if (err < 0)
2261 return err;
2262 }
2263
2264 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2266
2267 return 0;
2268}
2269
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002270static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Takashi Iwaiba9890a2018-11-29 08:02:49 +01002272 if (snd_pcm_stream_linked(substream))
2273 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002274}
2275
2276void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2277{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002278 substream->ref_count--;
2279 if (substream->ref_count > 0)
2280 return;
2281
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002282 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002283 if (substream->hw_opened) {
Takashi Iwai094435d2015-09-29 12:57:42 +02002284 if (substream->ops->hw_free &&
2285 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 substream->ops->hw_free(substream);
2287 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002288 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002290 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2291 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002292 if (substream->pcm_release) {
2293 substream->pcm_release(substream);
2294 substream->pcm_release = NULL;
2295 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002296 snd_pcm_detach_substream(substream);
2297}
2298
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002299EXPORT_SYMBOL(snd_pcm_release_substream);
2300
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002301int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2302 struct file *file,
2303 struct snd_pcm_substream **rsubstream)
2304{
2305 struct snd_pcm_substream *substream;
2306 int err;
2307
2308 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2309 if (err < 0)
2310 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002311 if (substream->ref_count > 1) {
2312 *rsubstream = substream;
2313 return 0;
2314 }
2315
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002316 err = snd_pcm_hw_constraints_init(substream);
2317 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002318 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002319 goto error;
2320 }
2321
2322 if ((err = substream->ops->open(substream)) < 0)
2323 goto error;
2324
2325 substream->hw_opened = 1;
2326
2327 err = snd_pcm_hw_constraints_complete(substream);
2328 if (err < 0) {
Takashi Iwai09e56df2014-02-04 18:19:48 +01002329 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002330 goto error;
2331 }
2332
2333 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002335
2336 error:
2337 snd_pcm_release_substream(substream);
2338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002341EXPORT_SYMBOL(snd_pcm_open_substream);
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002344 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002345 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Takashi Iwai877211f2005-11-17 13:59:38 +01002347 struct snd_pcm_file *pcm_file;
2348 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002349 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002351 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2352 if (err < 0)
2353 return err;
2354
Takashi Iwai548a6482006-07-31 16:51:51 +02002355 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2356 if (pcm_file == NULL) {
2357 snd_pcm_release_substream(substream);
2358 return -ENOMEM;
2359 }
2360 pcm_file->substream = substream;
2361 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002362 substream->file = pcm_file;
2363 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return 0;
2368}
2369
Clemens Ladischf87135f2005-11-20 14:06:59 +01002370static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Takashi Iwai877211f2005-11-17 13:59:38 +01002372 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002373 int err = nonseekable_open(inode, file);
2374 if (err < 0)
2375 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002376 pcm = snd_lookup_minor_data(iminor(inode),
2377 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002378 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002379 if (pcm)
2380 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002381 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002382}
2383
2384static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2385{
2386 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002387 int err = nonseekable_open(inode, file);
2388 if (err < 0)
2389 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002390 pcm = snd_lookup_minor_data(iminor(inode),
2391 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002392 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
Takashi Iwai8bb4d9c2012-11-08 14:36:18 +01002393 if (pcm)
2394 snd_card_unref(pcm->card);
Takashi Iwaia0830db2012-10-16 13:05:59 +02002395 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002396}
2397
2398static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2399{
2400 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 wait_queue_t wait;
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (pcm == NULL) {
2404 err = -ENODEV;
2405 goto __error1;
2406 }
2407 err = snd_card_file_add(pcm->card, file);
2408 if (err < 0)
2409 goto __error1;
2410 if (!try_module_get(pcm->card->module)) {
2411 err = -EFAULT;
2412 goto __error2;
2413 }
2414 init_waitqueue_entry(&wait, current);
2415 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002416 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002418 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (err >= 0)
2420 break;
2421 if (err == -EAGAIN) {
2422 if (file->f_flags & O_NONBLOCK) {
2423 err = -EBUSY;
2424 break;
2425 }
2426 } else
2427 break;
2428 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002429 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002431 mutex_lock(&pcm->open_mutex);
Takashi Iwai0914f792012-10-16 16:43:39 +02002432 if (pcm->card->shutdown) {
2433 err = -ENODEV;
2434 break;
2435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (signal_pending(current)) {
2437 err = -ERESTARTSYS;
2438 break;
2439 }
2440 }
2441 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002442 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (err < 0)
2444 goto __error;
2445 return err;
2446
2447 __error:
2448 module_put(pcm->card->module);
2449 __error2:
2450 snd_card_file_remove(pcm->card, file);
2451 __error1:
2452 return err;
2453}
2454
2455static int snd_pcm_release(struct inode *inode, struct file *file)
2456{
Takashi Iwai877211f2005-11-17 13:59:38 +01002457 struct snd_pcm *pcm;
2458 struct snd_pcm_substream *substream;
2459 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 pcm_file = file->private_data;
2462 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002463 if (snd_BUG_ON(!substream))
2464 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002466 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002467 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002468 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002469 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 wake_up(&pcm->open_wait);
2471 module_put(pcm->card->module);
2472 snd_card_file_remove(pcm->card, file);
2473 return 0;
2474}
2475
Takashi Iwai877211f2005-11-17 13:59:38 +01002476static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2477 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Takashi Iwai877211f2005-11-17 13:59:38 +01002479 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 snd_pcm_sframes_t appl_ptr;
2481 snd_pcm_sframes_t ret;
2482 snd_pcm_sframes_t hw_avail;
2483
2484 if (frames == 0)
2485 return 0;
2486
2487 snd_pcm_stream_lock_irq(substream);
2488 switch (runtime->status->state) {
2489 case SNDRV_PCM_STATE_PREPARED:
2490 break;
2491 case SNDRV_PCM_STATE_DRAINING:
2492 case SNDRV_PCM_STATE_RUNNING:
2493 if (snd_pcm_update_hw_ptr(substream) >= 0)
2494 break;
2495 /* Fall through */
2496 case SNDRV_PCM_STATE_XRUN:
2497 ret = -EPIPE;
2498 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002499 case SNDRV_PCM_STATE_SUSPENDED:
2500 ret = -ESTRPIPE;
2501 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 default:
2503 ret = -EBADFD;
2504 goto __end;
2505 }
2506
2507 hw_avail = snd_pcm_playback_hw_avail(runtime);
2508 if (hw_avail <= 0) {
2509 ret = 0;
2510 goto __end;
2511 }
2512 if (frames > (snd_pcm_uframes_t)hw_avail)
2513 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 appl_ptr = runtime->control->appl_ptr - frames;
2515 if (appl_ptr < 0)
2516 appl_ptr += runtime->boundary;
2517 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 ret = frames;
2519 __end:
2520 snd_pcm_stream_unlock_irq(substream);
2521 return ret;
2522}
2523
Takashi Iwai877211f2005-11-17 13:59:38 +01002524static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2525 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Takashi Iwai877211f2005-11-17 13:59:38 +01002527 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 snd_pcm_sframes_t appl_ptr;
2529 snd_pcm_sframes_t ret;
2530 snd_pcm_sframes_t hw_avail;
2531
2532 if (frames == 0)
2533 return 0;
2534
2535 snd_pcm_stream_lock_irq(substream);
2536 switch (runtime->status->state) {
2537 case SNDRV_PCM_STATE_PREPARED:
2538 case SNDRV_PCM_STATE_DRAINING:
2539 break;
2540 case SNDRV_PCM_STATE_RUNNING:
2541 if (snd_pcm_update_hw_ptr(substream) >= 0)
2542 break;
2543 /* Fall through */
2544 case SNDRV_PCM_STATE_XRUN:
2545 ret = -EPIPE;
2546 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002547 case SNDRV_PCM_STATE_SUSPENDED:
2548 ret = -ESTRPIPE;
2549 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 default:
2551 ret = -EBADFD;
2552 goto __end;
2553 }
2554
2555 hw_avail = snd_pcm_capture_hw_avail(runtime);
2556 if (hw_avail <= 0) {
2557 ret = 0;
2558 goto __end;
2559 }
2560 if (frames > (snd_pcm_uframes_t)hw_avail)
2561 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 appl_ptr = runtime->control->appl_ptr - frames;
2563 if (appl_ptr < 0)
2564 appl_ptr += runtime->boundary;
2565 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 ret = frames;
2567 __end:
2568 snd_pcm_stream_unlock_irq(substream);
2569 return ret;
2570}
2571
Takashi Iwai877211f2005-11-17 13:59:38 +01002572static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2573 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
Takashi Iwai877211f2005-11-17 13:59:38 +01002575 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 snd_pcm_sframes_t appl_ptr;
2577 snd_pcm_sframes_t ret;
2578 snd_pcm_sframes_t avail;
2579
2580 if (frames == 0)
2581 return 0;
2582
2583 snd_pcm_stream_lock_irq(substream);
2584 switch (runtime->status->state) {
2585 case SNDRV_PCM_STATE_PREPARED:
2586 case SNDRV_PCM_STATE_PAUSED:
2587 break;
2588 case SNDRV_PCM_STATE_DRAINING:
2589 case SNDRV_PCM_STATE_RUNNING:
2590 if (snd_pcm_update_hw_ptr(substream) >= 0)
2591 break;
2592 /* Fall through */
2593 case SNDRV_PCM_STATE_XRUN:
2594 ret = -EPIPE;
2595 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002596 case SNDRV_PCM_STATE_SUSPENDED:
2597 ret = -ESTRPIPE;
2598 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 default:
2600 ret = -EBADFD;
2601 goto __end;
2602 }
2603
2604 avail = snd_pcm_playback_avail(runtime);
2605 if (avail <= 0) {
2606 ret = 0;
2607 goto __end;
2608 }
2609 if (frames > (snd_pcm_uframes_t)avail)
2610 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 appl_ptr = runtime->control->appl_ptr + frames;
2612 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2613 appl_ptr -= runtime->boundary;
2614 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 ret = frames;
2616 __end:
2617 snd_pcm_stream_unlock_irq(substream);
2618 return ret;
2619}
2620
Takashi Iwai877211f2005-11-17 13:59:38 +01002621static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2622 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
Takashi Iwai877211f2005-11-17 13:59:38 +01002624 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 snd_pcm_sframes_t appl_ptr;
2626 snd_pcm_sframes_t ret;
2627 snd_pcm_sframes_t avail;
2628
2629 if (frames == 0)
2630 return 0;
2631
2632 snd_pcm_stream_lock_irq(substream);
2633 switch (runtime->status->state) {
2634 case SNDRV_PCM_STATE_PREPARED:
2635 case SNDRV_PCM_STATE_DRAINING:
2636 case SNDRV_PCM_STATE_PAUSED:
2637 break;
2638 case SNDRV_PCM_STATE_RUNNING:
2639 if (snd_pcm_update_hw_ptr(substream) >= 0)
2640 break;
2641 /* Fall through */
2642 case SNDRV_PCM_STATE_XRUN:
2643 ret = -EPIPE;
2644 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002645 case SNDRV_PCM_STATE_SUSPENDED:
2646 ret = -ESTRPIPE;
2647 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 default:
2649 ret = -EBADFD;
2650 goto __end;
2651 }
2652
2653 avail = snd_pcm_capture_avail(runtime);
2654 if (avail <= 0) {
2655 ret = 0;
2656 goto __end;
2657 }
2658 if (frames > (snd_pcm_uframes_t)avail)
2659 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 appl_ptr = runtime->control->appl_ptr + frames;
2661 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2662 appl_ptr -= runtime->boundary;
2663 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 ret = frames;
2665 __end:
2666 snd_pcm_stream_unlock_irq(substream);
2667 return ret;
2668}
2669
Takashi Iwai877211f2005-11-17 13:59:38 +01002670static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Takashi Iwai877211f2005-11-17 13:59:38 +01002672 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 int err;
2674
2675 snd_pcm_stream_lock_irq(substream);
2676 switch (runtime->status->state) {
2677 case SNDRV_PCM_STATE_DRAINING:
2678 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2679 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002680 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 case SNDRV_PCM_STATE_RUNNING:
2682 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2683 break;
2684 /* Fall through */
2685 case SNDRV_PCM_STATE_PREPARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 err = 0;
2687 break;
Jeeja KPf3f6c612016-09-02 21:49:44 +05302688 case SNDRV_PCM_STATE_SUSPENDED:
2689 err = -ESTRPIPE;
2690 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 case SNDRV_PCM_STATE_XRUN:
2692 err = -EPIPE;
2693 break;
2694 default:
2695 __badfd:
2696 err = -EBADFD;
2697 break;
2698 }
2699 snd_pcm_stream_unlock_irq(substream);
2700 return err;
2701}
2702
Takashi Iwai877211f2005-11-17 13:59:38 +01002703static int snd_pcm_delay(struct snd_pcm_substream *substream,
2704 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
Takashi Iwai877211f2005-11-17 13:59:38 +01002706 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 int err;
2708 snd_pcm_sframes_t n = 0;
2709
2710 snd_pcm_stream_lock_irq(substream);
2711 switch (runtime->status->state) {
2712 case SNDRV_PCM_STATE_DRAINING:
2713 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2714 goto __badfd;
Takashi Iwai1f961532013-10-28 12:40:46 +01002715 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 case SNDRV_PCM_STATE_RUNNING:
2717 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2718 break;
2719 /* Fall through */
2720 case SNDRV_PCM_STATE_PREPARED:
2721 case SNDRV_PCM_STATE_SUSPENDED:
2722 err = 0;
2723 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2724 n = snd_pcm_playback_hw_avail(runtime);
2725 else
2726 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002727 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 break;
2729 case SNDRV_PCM_STATE_XRUN:
2730 err = -EPIPE;
2731 break;
2732 default:
2733 __badfd:
2734 err = -EBADFD;
2735 break;
2736 }
2737 snd_pcm_stream_unlock_irq(substream);
2738 if (!err)
2739 if (put_user(n, res))
2740 err = -EFAULT;
2741 return err;
2742}
2743
Takashi Iwai877211f2005-11-17 13:59:38 +01002744static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2745 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
Takashi Iwai877211f2005-11-17 13:59:38 +01002747 struct snd_pcm_runtime *runtime = substream->runtime;
2748 struct snd_pcm_sync_ptr sync_ptr;
2749 volatile struct snd_pcm_mmap_status *status;
2750 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 int err;
Santosh Mardie68908a2012-10-09 10:32:42 +05302752 snd_pcm_uframes_t hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 memset(&sync_ptr, 0, sizeof(sync_ptr));
2755 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2756 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002757 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 -07002758 return -EFAULT;
2759 status = runtime->status;
2760 control = runtime->control;
2761 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2762 err = snd_pcm_hwsync(substream);
2763 if (err < 0)
2764 return err;
2765 }
2766 snd_pcm_stream_lock_irq(substream);
2767 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2768 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2769 else
2770 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2771 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2772 control->avail_min = sync_ptr.c.control.avail_min;
2773 else
2774 sync_ptr.c.control.avail_min = control->avail_min;
Santosh Mardie68908a2012-10-09 10:32:42 +05302775
2776 if (runtime->render_flag & SNDRV_NON_DMA_MODE) {
2777 hw_avail = snd_pcm_playback_hw_avail(runtime);
2778 if ((hw_avail >= runtime->start_threshold)
2779 && (runtime->render_flag &
2780 SNDRV_RENDER_STOPPED)) {
2781 if (substream->ops->restart)
2782 substream->ops->restart(substream);
2783 }
2784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 sync_ptr.s.status.state = status->state;
2786 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2787 sync_ptr.s.status.tstamp = status->tstamp;
2788 sync_ptr.s.status.suspended_state = status->suspended_state;
David Henningsson1704aa12018-04-21 14:57:40 +02002789 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 snd_pcm_stream_unlock_irq(substream);
2791 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2792 return -EFAULT;
2793 return 0;
2794}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002795
2796static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2797{
2798 struct snd_pcm_runtime *runtime = substream->runtime;
2799 int arg;
2800
2801 if (get_user(arg, _arg))
2802 return -EFAULT;
2803 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2804 return -EINVAL;
Takashi Iwai2408c212014-07-10 09:40:38 +02002805 runtime->tstamp_type = arg;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002806 return 0;
2807}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Takashi Iwai0df63e42006-04-28 15:13:41 +02002809static int snd_pcm_common_ioctl1(struct file *file,
2810 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 unsigned int cmd, void __user *arg)
2812{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 switch (cmd) {
2814 case SNDRV_PCM_IOCTL_PVERSION:
2815 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2816 case SNDRV_PCM_IOCTL_INFO:
2817 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002818 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2819 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002820 case SNDRV_PCM_IOCTL_TTSTAMP:
2821 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 case SNDRV_PCM_IOCTL_HW_REFINE:
2823 return snd_pcm_hw_refine_user(substream, arg);
2824 case SNDRV_PCM_IOCTL_HW_PARAMS:
2825 return snd_pcm_hw_params_user(substream, arg);
2826 case SNDRV_PCM_IOCTL_HW_FREE:
2827 return snd_pcm_hw_free(substream);
2828 case SNDRV_PCM_IOCTL_SW_PARAMS:
2829 return snd_pcm_sw_params_user(substream, arg);
2830 case SNDRV_PCM_IOCTL_STATUS:
Pierre-Louis Bossart38ca2a42015-02-13 15:14:04 -06002831 return snd_pcm_status_user(substream, arg, false);
2832 case SNDRV_PCM_IOCTL_STATUS_EXT:
2833 return snd_pcm_status_user(substream, arg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2835 return snd_pcm_channel_info_user(substream, arg);
2836 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002837 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 case SNDRV_PCM_IOCTL_RESET:
2839 return snd_pcm_reset(substream);
2840 case SNDRV_PCM_IOCTL_START:
2841 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2842 case SNDRV_PCM_IOCTL_LINK:
2843 return snd_pcm_link(substream, (int)(unsigned long) arg);
2844 case SNDRV_PCM_IOCTL_UNLINK:
2845 return snd_pcm_unlink(substream);
2846 case SNDRV_PCM_IOCTL_RESUME:
2847 return snd_pcm_resume(substream);
2848 case SNDRV_PCM_IOCTL_XRUN:
2849 return snd_pcm_xrun(substream);
2850 case SNDRV_PCM_IOCTL_HWSYNC:
2851 return snd_pcm_hwsync(substream);
2852 case SNDRV_PCM_IOCTL_DELAY:
2853 return snd_pcm_delay(substream, arg);
2854 case SNDRV_PCM_IOCTL_SYNC_PTR:
2855 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002856#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2858 return snd_pcm_hw_refine_old_user(substream, arg);
2859 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2860 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002863 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 case SNDRV_PCM_IOCTL_DROP:
2865 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002866 case SNDRV_PCM_IOCTL_PAUSE:
2867 {
2868 int res;
2869 snd_pcm_stream_lock_irq(substream);
2870 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2871 snd_pcm_stream_unlock_irq(substream);
2872 return res;
2873 }
Sudheer Papothib40e9682016-01-29 02:19:04 +05302874 case SNDRV_COMPRESS_GET_CAPS:
2875 case SNDRV_COMPRESS_GET_CODEC_CAPS:
2876 case SNDRV_COMPRESS_SET_PARAMS:
2877 case SNDRV_COMPRESS_GET_PARAMS:
2878 case SNDRV_COMPRESS_TSTAMP:
2879 case SNDRV_COMPRESS_DRAIN:
2880 return snd_compressed_ioctl(substream, cmd, arg);
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05302881 default:
2882 if (((cmd >> 8) & 0xff) == 'U')
2883 return snd_user_ioctl(substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
Takashi Iwai09e56df2014-02-04 18:19:48 +01002885 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 return -ENOTTY;
2887}
2888
Takashi Iwai0df63e42006-04-28 15:13:41 +02002889static int snd_pcm_playback_ioctl1(struct file *file,
2890 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 unsigned int cmd, void __user *arg)
2892{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002893 if (snd_BUG_ON(!substream))
2894 return -ENXIO;
2895 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 switch (cmd) {
2898 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2899 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002900 struct snd_xferi xferi;
2901 struct snd_xferi __user *_xferi = arg;
2902 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 snd_pcm_sframes_t result;
2904 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2905 return -EBADFD;
2906 if (put_user(0, &_xferi->result))
2907 return -EFAULT;
2908 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2909 return -EFAULT;
2910 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2911 __put_user(result, &_xferi->result);
2912 return result < 0 ? result : 0;
2913 }
2914 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2915 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002916 struct snd_xfern xfern;
2917 struct snd_xfern __user *_xfern = arg;
2918 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 void __user **bufs;
2920 snd_pcm_sframes_t result;
2921 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2922 return -EBADFD;
2923 if (runtime->channels > 128)
2924 return -EINVAL;
2925 if (put_user(0, &_xfern->result))
2926 return -EFAULT;
2927 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2928 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002929
2930 bufs = memdup_user(xfern.bufs,
2931 sizeof(void *) * runtime->channels);
2932 if (IS_ERR(bufs))
2933 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2935 kfree(bufs);
2936 __put_user(result, &_xfern->result);
2937 return result < 0 ? result : 0;
2938 }
2939 case SNDRV_PCM_IOCTL_REWIND:
2940 {
2941 snd_pcm_uframes_t frames;
2942 snd_pcm_uframes_t __user *_frames = arg;
2943 snd_pcm_sframes_t result;
2944 if (get_user(frames, _frames))
2945 return -EFAULT;
2946 if (put_user(0, _frames))
2947 return -EFAULT;
2948 result = snd_pcm_playback_rewind(substream, frames);
2949 __put_user(result, _frames);
2950 return result < 0 ? result : 0;
2951 }
2952 case SNDRV_PCM_IOCTL_FORWARD:
2953 {
2954 snd_pcm_uframes_t frames;
2955 snd_pcm_uframes_t __user *_frames = arg;
2956 snd_pcm_sframes_t result;
2957 if (get_user(frames, _frames))
2958 return -EFAULT;
2959 if (put_user(0, _frames))
2960 return -EFAULT;
2961 result = snd_pcm_playback_forward(substream, frames);
2962 __put_user(result, _frames);
2963 return result < 0 ? result : 0;
2964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002966 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
2968
Takashi Iwai0df63e42006-04-28 15:13:41 +02002969static int snd_pcm_capture_ioctl1(struct file *file,
2970 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 unsigned int cmd, void __user *arg)
2972{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002973 if (snd_BUG_ON(!substream))
2974 return -ENXIO;
2975 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2976 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 switch (cmd) {
2978 case SNDRV_PCM_IOCTL_READI_FRAMES:
2979 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002980 struct snd_xferi xferi;
2981 struct snd_xferi __user *_xferi = arg;
2982 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 snd_pcm_sframes_t result;
2984 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2985 return -EBADFD;
2986 if (put_user(0, &_xferi->result))
2987 return -EFAULT;
2988 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2989 return -EFAULT;
2990 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2991 __put_user(result, &_xferi->result);
2992 return result < 0 ? result : 0;
2993 }
2994 case SNDRV_PCM_IOCTL_READN_FRAMES:
2995 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002996 struct snd_xfern xfern;
2997 struct snd_xfern __user *_xfern = arg;
2998 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 void *bufs;
3000 snd_pcm_sframes_t result;
3001 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3002 return -EBADFD;
3003 if (runtime->channels > 128)
3004 return -EINVAL;
3005 if (put_user(0, &_xfern->result))
3006 return -EFAULT;
3007 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3008 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08003009
3010 bufs = memdup_user(xfern.bufs,
3011 sizeof(void *) * runtime->channels);
3012 if (IS_ERR(bufs))
3013 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3015 kfree(bufs);
3016 __put_user(result, &_xfern->result);
3017 return result < 0 ? result : 0;
3018 }
3019 case SNDRV_PCM_IOCTL_REWIND:
3020 {
3021 snd_pcm_uframes_t frames;
3022 snd_pcm_uframes_t __user *_frames = arg;
3023 snd_pcm_sframes_t result;
3024 if (get_user(frames, _frames))
3025 return -EFAULT;
3026 if (put_user(0, _frames))
3027 return -EFAULT;
3028 result = snd_pcm_capture_rewind(substream, frames);
3029 __put_user(result, _frames);
3030 return result < 0 ? result : 0;
3031 }
3032 case SNDRV_PCM_IOCTL_FORWARD:
3033 {
3034 snd_pcm_uframes_t frames;
3035 snd_pcm_uframes_t __user *_frames = arg;
3036 snd_pcm_sframes_t result;
3037 if (get_user(frames, _frames))
3038 return -EFAULT;
3039 if (put_user(0, _frames))
3040 return -EFAULT;
3041 result = snd_pcm_capture_forward(substream, frames);
3042 __put_user(result, _frames);
3043 return result < 0 ? result : 0;
3044 }
3045 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02003046 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Takashi Iwai877211f2005-11-17 13:59:38 +01003049static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
3050 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Takashi Iwai877211f2005-11-17 13:59:38 +01003052 struct snd_pcm_file *pcm_file;
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303053 unsigned char ioctl_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
3055 pcm_file = file->private_data;
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303056 ioctl_magic = ((cmd >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303058 if (ioctl_magic != 'A' && ioctl_magic != 'C' && ioctl_magic != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return -ENOTTY;
3060
Takashi Iwai0df63e42006-04-28 15:13:41 +02003061 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3062 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Takashi Iwai877211f2005-11-17 13:59:38 +01003065static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3066 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Takashi Iwai877211f2005-11-17 13:59:38 +01003068 struct snd_pcm_file *pcm_file;
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303069 unsigned char ioctl_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071 pcm_file = file->private_data;
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303072 ioctl_magic = ((cmd >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Sudheer Papothi04e0fcd2016-02-24 00:32:16 +05303074 if (ioctl_magic != 'A' && ioctl_magic != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return -ENOTTY;
3076
Takashi Iwai0df63e42006-04-28 15:13:41 +02003077 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3078 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Takashi Iwai877211f2005-11-17 13:59:38 +01003081int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 unsigned int cmd, void *arg)
3083{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003084 mm_segment_t fs;
3085 int result;
3086
3087 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 switch (substream->stream) {
3089 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003090 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3091 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02003094 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3095 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003098 result = -EINVAL;
3099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02003101 snd_leave_user(fs);
3102 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003105EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3106
Takashi Iwai877211f2005-11-17 13:59:38 +01003107static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3108 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Takashi Iwai877211f2005-11-17 13:59:38 +01003110 struct snd_pcm_file *pcm_file;
3111 struct snd_pcm_substream *substream;
3112 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 snd_pcm_sframes_t result;
3114
3115 pcm_file = file->private_data;
3116 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003117 if (PCM_RUNTIME_CHECK(substream))
3118 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 runtime = substream->runtime;
3120 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3121 return -EBADFD;
3122 if (!frame_aligned(runtime, count))
3123 return -EINVAL;
3124 count = bytes_to_frames(runtime, count);
3125 result = snd_pcm_lib_read(substream, buf, count);
3126 if (result > 0)
3127 result = frames_to_bytes(runtime, result);
3128 return result;
3129}
3130
Takashi Iwai877211f2005-11-17 13:59:38 +01003131static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3132 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Takashi Iwai877211f2005-11-17 13:59:38 +01003134 struct snd_pcm_file *pcm_file;
3135 struct snd_pcm_substream *substream;
3136 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 snd_pcm_sframes_t result;
3138
3139 pcm_file = file->private_data;
3140 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003141 if (PCM_RUNTIME_CHECK(substream))
3142 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003144 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3145 return -EBADFD;
3146 if (!frame_aligned(runtime, count))
3147 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 count = bytes_to_frames(runtime, count);
3149 result = snd_pcm_lib_write(substream, buf, count);
3150 if (result > 0)
3151 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return result;
3153}
3154
Al Viro1c65d982015-04-04 00:19:32 -04003155static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Takashi Iwai877211f2005-11-17 13:59:38 +01003157 struct snd_pcm_file *pcm_file;
3158 struct snd_pcm_substream *substream;
3159 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 snd_pcm_sframes_t result;
3161 unsigned long i;
3162 void __user **bufs;
3163 snd_pcm_uframes_t frames;
3164
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003165 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003167 if (PCM_RUNTIME_CHECK(substream))
3168 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 runtime = substream->runtime;
3170 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3171 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003172 if (!iter_is_iovec(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003174 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003176 if (!frame_aligned(runtime, to->iov->iov_len))
3177 return -EINVAL;
3178 frames = bytes_to_samples(runtime, to->iov->iov_len);
3179 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 if (bufs == NULL)
3181 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003182 for (i = 0; i < to->nr_segs; ++i)
3183 bufs[i] = to->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 result = snd_pcm_lib_readv(substream, bufs, frames);
3185 if (result > 0)
3186 result = frames_to_bytes(runtime, result);
3187 kfree(bufs);
3188 return result;
3189}
3190
Al Viro1c65d982015-04-04 00:19:32 -04003191static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
Takashi Iwai877211f2005-11-17 13:59:38 +01003193 struct snd_pcm_file *pcm_file;
3194 struct snd_pcm_substream *substream;
3195 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 snd_pcm_sframes_t result;
3197 unsigned long i;
3198 void __user **bufs;
3199 snd_pcm_uframes_t frames;
3200
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003201 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003203 if (PCM_RUNTIME_CHECK(substream))
3204 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003206 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3207 return -EBADFD;
Al Viro1c65d982015-04-04 00:19:32 -04003208 if (!iter_is_iovec(from))
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003209 return -EINVAL;
Al Viro1c65d982015-04-04 00:19:32 -04003210 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3211 !frame_aligned(runtime, from->iov->iov_len))
3212 return -EINVAL;
3213 frames = bytes_to_samples(runtime, from->iov->iov_len);
3214 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (bufs == NULL)
3216 return -ENOMEM;
Al Viro1c65d982015-04-04 00:19:32 -04003217 for (i = 0; i < from->nr_segs; ++i)
3218 bufs[i] = from->iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 result = snd_pcm_lib_writev(substream, bufs, frames);
3220 if (result > 0)
3221 result = frames_to_bytes(runtime, result);
3222 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return result;
3224}
3225
3226static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3227{
Takashi Iwai877211f2005-11-17 13:59:38 +01003228 struct snd_pcm_file *pcm_file;
3229 struct snd_pcm_substream *substream;
3230 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 unsigned int mask;
3232 snd_pcm_uframes_t avail;
3233
3234 pcm_file = file->private_data;
3235
3236 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003237 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003238 return POLLOUT | POLLWRNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 runtime = substream->runtime;
3240
3241 poll_wait(file, &runtime->sleep, wait);
3242
3243 snd_pcm_stream_lock_irq(substream);
3244 avail = snd_pcm_playback_avail(runtime);
3245 switch (runtime->status->state) {
3246 case SNDRV_PCM_STATE_RUNNING:
3247 case SNDRV_PCM_STATE_PREPARED:
3248 case SNDRV_PCM_STATE_PAUSED:
3249 if (avail >= runtime->control->avail_min) {
3250 mask = POLLOUT | POLLWRNORM;
3251 break;
3252 }
3253 /* Fall through */
3254 case SNDRV_PCM_STATE_DRAINING:
3255 mask = 0;
3256 break;
3257 default:
3258 mask = POLLOUT | POLLWRNORM | POLLERR;
3259 break;
3260 }
3261 snd_pcm_stream_unlock_irq(substream);
3262 return mask;
3263}
3264
3265static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3266{
Takashi Iwai877211f2005-11-17 13:59:38 +01003267 struct snd_pcm_file *pcm_file;
3268 struct snd_pcm_substream *substream;
3269 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 unsigned int mask;
3271 snd_pcm_uframes_t avail;
3272
3273 pcm_file = file->private_data;
3274
3275 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003276 if (PCM_RUNTIME_CHECK(substream))
Charles Keepaxe099aee2016-05-04 14:59:07 +01003277 return POLLIN | POLLRDNORM | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 runtime = substream->runtime;
3279
3280 poll_wait(file, &runtime->sleep, wait);
3281
3282 snd_pcm_stream_lock_irq(substream);
3283 avail = snd_pcm_capture_avail(runtime);
3284 switch (runtime->status->state) {
3285 case SNDRV_PCM_STATE_RUNNING:
3286 case SNDRV_PCM_STATE_PREPARED:
3287 case SNDRV_PCM_STATE_PAUSED:
3288 if (avail >= runtime->control->avail_min) {
3289 mask = POLLIN | POLLRDNORM;
3290 break;
3291 }
3292 mask = 0;
3293 break;
3294 case SNDRV_PCM_STATE_DRAINING:
3295 if (avail > 0) {
3296 mask = POLLIN | POLLRDNORM;
3297 break;
3298 }
3299 /* Fall through */
3300 default:
3301 mask = POLLIN | POLLRDNORM | POLLERR;
3302 break;
3303 }
3304 snd_pcm_stream_unlock_irq(substream);
3305 return mask;
3306}
3307
3308/*
3309 * mmap support
3310 */
3311
3312/*
3313 * Only on coherent architectures, we can mmap the status and the control records
3314 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3315 */
3316#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3317/*
3318 * mmap status record
3319 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003320static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3321 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
Takashi Iwai877211f2005-11-17 13:59:38 +01003323 struct snd_pcm_substream *substream = area->vm_private_data;
3324 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003327 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003329 vmf->page = virt_to_page(runtime->status);
3330 get_page(vmf->page);
3331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003334static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003336 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337};
3338
Takashi Iwai877211f2005-11-17 13:59:38 +01003339static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 struct vm_area_struct *area)
3341{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 long size;
3343 if (!(area->vm_flags & VM_READ))
3344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003346 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 return -EINVAL;
3348 area->vm_ops = &snd_pcm_vm_ops_status;
3349 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003350 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return 0;
3352}
3353
3354/*
3355 * mmap control record
3356 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003357static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3358 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
Takashi Iwai877211f2005-11-17 13:59:38 +01003360 struct snd_pcm_substream *substream = area->vm_private_data;
3361 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003364 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003366 vmf->page = virt_to_page(runtime->control);
3367 get_page(vmf->page);
3368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003371static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003373 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374};
3375
Takashi Iwai877211f2005-11-17 13:59:38 +01003376static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 struct vm_area_struct *area)
3378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 long size;
3380 if (!(area->vm_flags & VM_READ))
3381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003383 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 return -EINVAL;
3385 area->vm_ops = &snd_pcm_vm_ops_control;
3386 area->vm_private_data = substream;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003387 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 return 0;
3389}
3390#else /* ! coherent mmap */
3391/*
3392 * don't support mmap for status and control records.
3393 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003394static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 struct vm_area_struct *area)
3396{
3397 return -ENXIO;
3398}
Takashi Iwai877211f2005-11-17 13:59:38 +01003399static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 struct vm_area_struct *area)
3401{
3402 return -ENXIO;
3403}
3404#endif /* coherent mmap */
3405
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003406static inline struct page *
3407snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3408{
3409 void *vaddr = substream->runtime->dma_area + ofs;
3410 return virt_to_page(vaddr);
3411}
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003414 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003416static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3417 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
Takashi Iwai877211f2005-11-17 13:59:38 +01003419 struct snd_pcm_substream *substream = area->vm_private_data;
3420 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 unsigned long offset;
3422 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 size_t dma_bytes;
3424
3425 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003426 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003428 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3430 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003431 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003432 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003434 else
3435 page = snd_pcm_default_page_ops(substream, offset);
3436 if (!page)
3437 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003438 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003439 vmf->page = page;
3440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441}
3442
Takashi Iwai657b1982009-11-26 12:40:21 +01003443static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3444 .open = snd_pcm_mmap_data_open,
3445 .close = snd_pcm_mmap_data_close,
3446};
3447
3448static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 .open = snd_pcm_mmap_data_open,
3450 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003451 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452};
3453
3454/*
3455 * mmap the DMA buffer on RAM
3456 */
Takashi Iwai30b771c2014-10-30 15:02:50 +01003457
3458/**
3459 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3460 * @substream: PCM substream
3461 * @area: VMA
3462 *
3463 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3464 * this function is invoked implicitly.
3465 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003466int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3467 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003469 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Takashi Iwaia5606f82013-10-24 14:25:32 +02003470#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +08003471 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3472 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3473 return remap_pfn_range(area, area->vm_start,
3474 substream->dma_buffer.addr >> PAGE_SHIFT,
3475 area->vm_end - area->vm_start, area->vm_page_prot);
3476 }
Takashi Iwaia5606f82013-10-24 14:25:32 +02003477#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai49d776f2014-10-24 12:36:23 +02003478#ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
Takashi Iwai657b1982009-11-26 12:40:21 +01003479 if (!substream->ops->page &&
3480 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3481 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3482 area,
3483 substream->runtime->dma_area,
3484 substream->runtime->dma_addr,
Stefan Roesed5a2bcb2018-03-26 16:10:21 +02003485 substream->runtime->dma_bytes);
Takashi Iwai49d776f2014-10-24 12:36:23 +02003486#endif /* CONFIG_X86 */
Takashi Iwai657b1982009-11-26 12:40:21 +01003487 /* mmap with fault handler */
3488 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return 0;
3490}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003491EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493/*
3494 * mmap the DMA buffer on I/O memory area
3495 */
3496#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai30b771c2014-10-30 15:02:50 +01003497/**
3498 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3499 * @substream: PCM substream
3500 * @area: VMA
3501 *
3502 * When your hardware uses the iomapped pages as the hardware buffer and
3503 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3504 * is supposed to work only on limited architectures.
3505 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003506int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3507 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003509 struct snd_pcm_runtime *runtime = substream->runtime;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds0fe09a42013-04-19 10:01:04 -07003512 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003514
3515EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516#endif /* SNDRV_PCM_INFO_MMAP */
3517
3518/*
3519 * mmap DMA buffer
3520 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003521int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 struct vm_area_struct *area)
3523{
Takashi Iwai877211f2005-11-17 13:59:38 +01003524 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 long size;
3526 unsigned long offset;
3527 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003528 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
3530 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3531 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3532 return -EINVAL;
3533 } else {
3534 if (!(area->vm_flags & VM_READ))
3535 return -EINVAL;
3536 }
3537 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3539 return -EBADFD;
3540 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3541 return -ENXIO;
3542 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3543 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3544 return -EINVAL;
3545 size = area->vm_end - area->vm_start;
3546 offset = area->vm_pgoff << PAGE_SHIFT;
3547 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3548 if ((size_t)size > dma_bytes)
3549 return -EINVAL;
3550 if (offset > dma_bytes - size)
3551 return -EINVAL;
3552
Takashi Iwai657b1982009-11-26 12:40:21 +01003553 area->vm_ops = &snd_pcm_vm_ops_data;
3554 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003556 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003558 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003559 if (!err)
3560 atomic_inc(&substream->mmap_count);
3561 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003564EXPORT_SYMBOL(snd_pcm_mmap_data);
3565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3567{
Takashi Iwai877211f2005-11-17 13:59:38 +01003568 struct snd_pcm_file * pcm_file;
3569 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 unsigned long offset;
3571
3572 pcm_file = file->private_data;
3573 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003574 if (PCM_RUNTIME_CHECK(substream))
3575 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 offset = area->vm_pgoff << PAGE_SHIFT;
3578 switch (offset) {
3579 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003580 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return -ENXIO;
3582 return snd_pcm_mmap_status(substream, file, area);
3583 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003584 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return -ENXIO;
3586 return snd_pcm_mmap_control(substream, file, area);
3587 default:
3588 return snd_pcm_mmap_data(substream, file, area);
3589 }
3590 return 0;
3591}
3592
3593static int snd_pcm_fasync(int fd, struct file * file, int on)
3594{
Takashi Iwai877211f2005-11-17 13:59:38 +01003595 struct snd_pcm_file * pcm_file;
3596 struct snd_pcm_substream *substream;
3597 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599 pcm_file = file->private_data;
3600 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003601 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003602 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003604 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
3607/*
3608 * ioctl32 compat
3609 */
3610#ifdef CONFIG_COMPAT
3611#include "pcm_compat.c"
3612#else
3613#define snd_pcm_ioctl_compat NULL
3614#endif
3615
3616/*
3617 * To be removed helpers to keep binary compatibility
3618 */
3619
Takashi Iwai59d48582005-12-01 10:51:58 +01003620#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3622#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3623
Takashi Iwai877211f2005-11-17 13:59:38 +01003624static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3625 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
3627 unsigned int i;
3628
3629 memset(params, 0, sizeof(*params));
3630 params->flags = oparams->flags;
3631 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3632 params->masks[i].bits[0] = oparams->masks[i];
3633 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3634 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3635 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3636 params->info = oparams->info;
3637 params->msbits = oparams->msbits;
3638 params->rate_num = oparams->rate_num;
3639 params->rate_den = oparams->rate_den;
3640 params->fifo_size = oparams->fifo_size;
3641}
3642
Takashi Iwai877211f2005-11-17 13:59:38 +01003643static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3644 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
3646 unsigned int i;
3647
3648 memset(oparams, 0, sizeof(*oparams));
3649 oparams->flags = params->flags;
3650 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3651 oparams->masks[i] = params->masks[i].bits[0];
3652 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3653 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3654 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3655 oparams->info = params->info;
3656 oparams->msbits = params->msbits;
3657 oparams->rate_num = params->rate_num;
3658 oparams->rate_den = params->rate_den;
3659 oparams->fifo_size = params->fifo_size;
3660}
3661
Takashi Iwai877211f2005-11-17 13:59:38 +01003662static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3663 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
Takashi Iwai877211f2005-11-17 13:59:38 +01003665 struct snd_pcm_hw_params *params;
3666 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 int err;
3668
3669 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003670 if (!params)
3671 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Li Zefanef44a1e2009-04-10 09:43:08 +08003673 oparams = memdup_user(_oparams, sizeof(*oparams));
3674 if (IS_ERR(oparams)) {
3675 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 goto out;
3677 }
3678 snd_pcm_hw_convert_from_old_params(params, oparams);
3679 err = snd_pcm_hw_refine(substream, params);
3680 snd_pcm_hw_convert_to_old_params(oparams, params);
3681 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3682 if (!err)
3683 err = -EFAULT;
3684 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003685
3686 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687out:
3688 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return err;
3690}
3691
Takashi Iwai877211f2005-11-17 13:59:38 +01003692static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3693 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
Takashi Iwai877211f2005-11-17 13:59:38 +01003695 struct snd_pcm_hw_params *params;
3696 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 int err;
3698
3699 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003700 if (!params)
3701 return -ENOMEM;
3702
3703 oparams = memdup_user(_oparams, sizeof(*oparams));
3704 if (IS_ERR(oparams)) {
3705 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 goto out;
3707 }
3708 snd_pcm_hw_convert_from_old_params(params, oparams);
3709 err = snd_pcm_hw_params(substream, params);
3710 snd_pcm_hw_convert_to_old_params(oparams, params);
3711 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3712 if (!err)
3713 err = -EFAULT;
3714 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003715
3716 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717out:
3718 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 return err;
3720}
Takashi Iwai59d48582005-12-01 10:51:58 +01003721#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Cliff Cai70036092008-09-03 10:54:36 +02003723#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003724static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3725 unsigned long addr,
3726 unsigned long len,
3727 unsigned long pgoff,
3728 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003729{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003730 struct snd_pcm_file *pcm_file = file->private_data;
3731 struct snd_pcm_substream *substream = pcm_file->substream;
3732 struct snd_pcm_runtime *runtime = substream->runtime;
3733 unsigned long offset = pgoff << PAGE_SHIFT;
3734
3735 switch (offset) {
3736 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3737 return (unsigned long)runtime->status;
3738 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3739 return (unsigned long)runtime->control;
3740 default:
3741 return (unsigned long)runtime->dma_area + offset;
3742 }
Cliff Cai70036092008-09-03 10:54:36 +02003743}
3744#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003745# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003746#endif
3747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748/*
3749 * Register section
3750 */
3751
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003752const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003754 .owner = THIS_MODULE,
3755 .write = snd_pcm_write,
Al Viro1c65d982015-04-04 00:19:32 -04003756 .write_iter = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003757 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003758 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003759 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003760 .poll = snd_pcm_playback_poll,
3761 .unlocked_ioctl = snd_pcm_playback_ioctl,
3762 .compat_ioctl = snd_pcm_ioctl_compat,
3763 .mmap = snd_pcm_mmap,
3764 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003765 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 },
3767 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003768 .owner = THIS_MODULE,
3769 .read = snd_pcm_read,
Al Viro1c65d982015-04-04 00:19:32 -04003770 .read_iter = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003771 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003772 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003773 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003774 .poll = snd_pcm_capture_poll,
3775 .unlocked_ioctl = snd_pcm_capture_ioctl,
3776 .compat_ioctl = snd_pcm_ioctl_compat,
3777 .mmap = snd_pcm_mmap,
3778 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003779 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
3781};