blob: 750457634a2f7c6ff91c0a687a15fae5d6b83615 [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07008 * the Free Software Foundation; only version 2 of the License.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/file.h>
24#include <linux/slab.h>
25#include <linux/time.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020026#include <linux/pm_qos.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/uio.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010028#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include <sound/control.h>
Asish Bhattacharya9048f752011-11-01 20:33:44 +053031#include <sound/compress_offload.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/info.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/timer.h>
36#include <sound/minors.h>
37#include <asm/io.h>
Takashi Iwai9fe17b52010-05-12 10:32:42 +020038#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
39#include <dma-coherence.h>
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Compatibility
44 */
45
Takashi Iwai877211f2005-11-17 13:59:38 +010046struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 unsigned int flags;
48 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
49 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010050 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
52 unsigned int rmask;
53 unsigned int cmask;
54 unsigned int info;
55 unsigned int msbits;
56 unsigned int rate_num;
57 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010058 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned char reserved[64];
60};
61
Takashi Iwai59d48582005-12-01 10:51:58 +010062#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010063#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
64#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Takashi Iwai877211f2005-11-17 13:59:38 +010066static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
68static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
69 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010070#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010071static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 *
75 */
76
77DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020078EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020080static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static inline mm_segment_t snd_enter_user(void)
83{
84 mm_segment_t fs = get_fs();
85 set_fs(get_ds());
86 return fs;
87}
88
89static inline void snd_leave_user(mm_segment_t fs)
90{
91 set_fs(fs);
92}
93
94
95
Takashi Iwai877211f2005-11-17 13:59:38 +010096int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Takashi Iwai877211f2005-11-17 13:59:38 +010098 struct snd_pcm_runtime *runtime;
99 struct snd_pcm *pcm = substream->pcm;
100 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 memset(info, 0, sizeof(*info));
103 info->card = pcm->card->number;
104 info->device = pcm->device;
105 info->stream = substream->stream;
106 info->subdevice = substream->number;
107 strlcpy(info->id, pcm->id, sizeof(info->id));
108 strlcpy(info->name, pcm->name, sizeof(info->name));
109 info->dev_class = pcm->dev_class;
110 info->dev_subclass = pcm->dev_subclass;
111 info->subdevices_count = pstr->substream_count;
112 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
113 strlcpy(info->subname, substream->name, sizeof(info->subname));
114 runtime = substream->runtime;
115 /* AB: FIXME!!! This is definitely nonsense */
116 if (runtime) {
117 info->sync = runtime->sync;
118 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
119 }
120 return 0;
121}
122
Takashi Iwai877211f2005-11-17 13:59:38 +0100123int snd_pcm_info_user(struct snd_pcm_substream *substream,
124 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Takashi Iwai877211f2005-11-17 13:59:38 +0100126 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int err;
128
129 info = kmalloc(sizeof(*info), GFP_KERNEL);
130 if (! info)
131 return -ENOMEM;
132 err = snd_pcm_info(substream, info);
133 if (err >= 0) {
134 if (copy_to_user(_info, info, sizeof(*info)))
135 err = -EFAULT;
136 }
137 kfree(info);
138 return err;
139}
140
141#undef RULES_DEBUG
142
143#ifdef RULES_DEBUG
144#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
Joe Perches47023ec2010-09-13 21:24:02 -0700145static const char * const snd_pcm_hw_param_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 HW_PARAM(ACCESS),
147 HW_PARAM(FORMAT),
148 HW_PARAM(SUBFORMAT),
149 HW_PARAM(SAMPLE_BITS),
150 HW_PARAM(FRAME_BITS),
151 HW_PARAM(CHANNELS),
152 HW_PARAM(RATE),
153 HW_PARAM(PERIOD_TIME),
154 HW_PARAM(PERIOD_SIZE),
155 HW_PARAM(PERIOD_BYTES),
156 HW_PARAM(PERIODS),
157 HW_PARAM(BUFFER_TIME),
158 HW_PARAM(BUFFER_SIZE),
159 HW_PARAM(BUFFER_BYTES),
160 HW_PARAM(TICK_TIME),
161};
162#endif
163
Takashi Iwai877211f2005-11-17 13:59:38 +0100164int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
165 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100168 struct snd_pcm_hardware *hw;
169 struct snd_interval *i = NULL;
170 struct snd_mask *m = NULL;
171 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int rstamps[constrs->rules_num];
173 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
174 unsigned int stamp = 2;
175 int changed, again;
176
177 params->info = 0;
178 params->fifo_size = 0;
179 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
180 params->msbits = 0;
181 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
182 params->rate_num = 0;
183 params->rate_den = 0;
184 }
185
186 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
187 m = hw_param_mask(params, k);
188 if (snd_mask_empty(m))
189 return -EINVAL;
190 if (!(params->rmask & (1 << k)))
191 continue;
192#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100193 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
195#endif
196 changed = snd_mask_refine(m, constrs_mask(constrs, k));
197#ifdef RULES_DEBUG
198 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
199#endif
200 if (changed)
201 params->cmask |= 1 << k;
202 if (changed < 0)
203 return changed;
204 }
205
206 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
207 i = hw_param_interval(params, k);
208 if (snd_interval_empty(i))
209 return -EINVAL;
210 if (!(params->rmask & (1 << k)))
211 continue;
212#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100213 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (i->empty)
215 printk("empty");
216 else
217 printk("%c%u %u%c",
218 i->openmin ? '(' : '[', i->min,
219 i->max, i->openmax ? ')' : ']');
220 printk(" -> ");
221#endif
222 changed = snd_interval_refine(i, constrs_interval(constrs, k));
223#ifdef RULES_DEBUG
224 if (i->empty)
225 printk("empty\n");
226 else
227 printk("%c%u %u%c\n",
228 i->openmin ? '(' : '[', i->min,
229 i->max, i->openmax ? ')' : ']');
230#endif
231 if (changed)
232 params->cmask |= 1 << k;
233 if (changed < 0)
234 return changed;
235 }
236
237 for (k = 0; k < constrs->rules_num; k++)
238 rstamps[k] = 0;
239 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
240 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
241 do {
242 again = 0;
243 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100244 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned int d;
246 int doit = 0;
247 if (r->cond && !(r->cond & params->flags))
248 continue;
249 for (d = 0; r->deps[d] >= 0; d++) {
250 if (vstamps[r->deps[d]] > rstamps[k]) {
251 doit = 1;
252 break;
253 }
254 }
255 if (!doit)
256 continue;
257#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100258 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (r->var >= 0) {
260 printk("%s = ", snd_pcm_hw_param_names[r->var]);
261 if (hw_is_mask(r->var)) {
262 m = hw_param_mask(params, r->var);
263 printk("%x", *m->bits);
264 } else {
265 i = hw_param_interval(params, r->var);
266 if (i->empty)
267 printk("empty");
268 else
269 printk("%c%u %u%c",
270 i->openmin ? '(' : '[', i->min,
271 i->max, i->openmax ? ')' : ']');
272 }
273 }
274#endif
275 changed = r->func(params, r);
276#ifdef RULES_DEBUG
277 if (r->var >= 0) {
278 printk(" -> ");
279 if (hw_is_mask(r->var))
280 printk("%x", *m->bits);
281 else {
282 if (i->empty)
283 printk("empty");
284 else
285 printk("%c%u %u%c",
286 i->openmin ? '(' : '[', i->min,
287 i->max, i->openmax ? ')' : ']');
288 }
289 }
290 printk("\n");
291#endif
292 rstamps[k] = stamp;
293 if (changed && r->var >= 0) {
294 params->cmask |= (1 << r->var);
295 vstamps[r->var] = stamp;
296 again = 1;
297 }
298 if (changed < 0)
299 return changed;
300 stamp++;
301 }
302 } while (again);
303 if (!params->msbits) {
304 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
305 if (snd_interval_single(i))
306 params->msbits = snd_interval_value(i);
307 }
308
309 if (!params->rate_den) {
310 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
311 if (snd_interval_single(i)) {
312 params->rate_num = snd_interval_value(i);
313 params->rate_den = 1;
314 }
315 }
316
317 hw = &substream->runtime->hw;
318 if (!params->info)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200319 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
320 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100321 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
322 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
323 if (snd_mask_min(m) == snd_mask_max(m) &&
324 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200325 changed = substream->ops->ioctl(substream,
326 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200327 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200328 return changed;
329 }
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 params->rmask = 0;
332 return 0;
333}
334
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200335EXPORT_SYMBOL(snd_pcm_hw_refine);
336
Takashi Iwai877211f2005-11-17 13:59:38 +0100337static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
338 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Takashi Iwai877211f2005-11-17 13:59:38 +0100340 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int err;
342
Li Zefanef44a1e2009-04-10 09:43:08 +0800343 params = memdup_user(_params, sizeof(*params));
344 if (IS_ERR(params))
345 return PTR_ERR(params);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 err = snd_pcm_hw_refine(substream, params);
348 if (copy_to_user(_params, params, sizeof(*params))) {
349 if (!err)
350 err = -EFAULT;
351 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 kfree(params);
354 return err;
355}
356
Takashi Iwai9442e692006-09-30 23:27:19 -0700357static int period_to_usecs(struct snd_pcm_runtime *runtime)
358{
359 int usecs;
360
361 if (! runtime->rate)
362 return -1; /* invalid */
363
364 /* take 75% of period time as the deadline */
365 usecs = (750000 / runtime->rate) * runtime->period_size;
366 usecs += ((750000 % runtime->rate) * runtime->period_size) /
367 runtime->rate;
368
369 return usecs;
370}
371
Takashi Iwai877211f2005-11-17 13:59:38 +0100372static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
373 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Takashi Iwai877211f2005-11-17 13:59:38 +0100375 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700376 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 unsigned int bits;
378 snd_pcm_uframes_t frames;
379
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200380 if (PCM_RUNTIME_CHECK(substream))
381 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 snd_pcm_stream_lock_irq(substream);
384 switch (runtime->status->state) {
385 case SNDRV_PCM_STATE_OPEN:
386 case SNDRV_PCM_STATE_SETUP:
387 case SNDRV_PCM_STATE_PREPARED:
388 break;
389 default:
390 snd_pcm_stream_unlock_irq(substream);
391 return -EBADFD;
392 }
393 snd_pcm_stream_unlock_irq(substream);
394#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
395 if (!substream->oss.oss)
396#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200397 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -EBADFD;
399
400 params->rmask = ~0U;
401 err = snd_pcm_hw_refine(substream, params);
402 if (err < 0)
403 goto _error;
404
405 err = snd_pcm_hw_params_choose(substream, params);
406 if (err < 0)
407 goto _error;
408
409 if (substream->ops->hw_params != NULL) {
410 err = substream->ops->hw_params(substream, params);
411 if (err < 0)
412 goto _error;
413 }
414
415 runtime->access = params_access(params);
416 runtime->format = params_format(params);
417 runtime->subformat = params_subformat(params);
418 runtime->channels = params_channels(params);
419 runtime->rate = params_rate(params);
420 runtime->period_size = params_period_size(params);
421 runtime->periods = params_periods(params);
422 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 runtime->info = params->info;
424 runtime->rate_num = params->rate_num;
425 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100426 runtime->no_period_wakeup =
427 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
428 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 bits = snd_pcm_format_physical_width(runtime->format);
431 runtime->sample_bits = bits;
432 bits *= runtime->channels;
433 runtime->frame_bits = bits;
434 frames = 1;
435 while (bits % 8 != 0) {
436 bits *= 2;
437 frames *= 2;
438 }
439 runtime->byte_align = bits / 8;
440 runtime->min_align = frames;
441
442 /* Default sw params */
443 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
444 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 runtime->start_threshold = 1;
447 runtime->stop_threshold = runtime->buffer_size;
448 runtime->silence_threshold = 0;
449 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200450 runtime->boundary = runtime->buffer_size;
Laxminath Kasam9c89b9b2013-07-11 10:55:26 +0530451 while (runtime->boundary * 2 * runtime->channels <=
452 LONG_MAX - runtime->buffer_size)
Clemens Ladischead40462010-05-21 09:15:59 +0200453 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 snd_pcm_timer_resolution_change(substream);
456 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700457
James Bottomley82f68252010-07-05 22:53:06 +0200458 if (pm_qos_request_active(&substream->latency_pm_qos_req))
459 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700460 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200461 pm_qos_add_request(&substream->latency_pm_qos_req,
462 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return 0;
464 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300465 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 so we force application to retry to set
467 the correct hardware parameter settings */
468 runtime->status->state = SNDRV_PCM_STATE_OPEN;
469 if (substream->ops->hw_free != NULL)
470 substream->ops->hw_free(substream);
471 return err;
472}
473
Takashi Iwai877211f2005-11-17 13:59:38 +0100474static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
475 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Takashi Iwai877211f2005-11-17 13:59:38 +0100477 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int err;
479
Li Zefanef44a1e2009-04-10 09:43:08 +0800480 params = memdup_user(_params, sizeof(*params));
481 if (IS_ERR(params))
482 return PTR_ERR(params);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 err = snd_pcm_hw_params(substream, params);
485 if (copy_to_user(_params, params, sizeof(*params))) {
486 if (!err)
487 err = -EFAULT;
488 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 kfree(params);
491 return err;
492}
493
Takashi Iwai877211f2005-11-17 13:59:38 +0100494static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Takashi Iwai877211f2005-11-17 13:59:38 +0100496 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int result = 0;
498
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200499 if (PCM_RUNTIME_CHECK(substream))
500 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 snd_pcm_stream_lock_irq(substream);
503 switch (runtime->status->state) {
504 case SNDRV_PCM_STATE_SETUP:
505 case SNDRV_PCM_STATE_PREPARED:
506 break;
507 default:
508 snd_pcm_stream_unlock_irq(substream);
509 return -EBADFD;
510 }
511 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200512 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -EBADFD;
514 if (substream->ops->hw_free)
515 result = substream->ops->hw_free(substream);
516 runtime->status->state = SNDRV_PCM_STATE_OPEN;
James Bottomley82f68252010-07-05 22:53:06 +0200517 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return result;
519}
520
Takashi Iwai877211f2005-11-17 13:59:38 +0100521static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
522 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Takashi Iwai877211f2005-11-17 13:59:38 +0100524 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100525 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200527 if (PCM_RUNTIME_CHECK(substream))
528 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 snd_pcm_stream_lock_irq(substream);
531 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
532 snd_pcm_stream_unlock_irq(substream);
533 return -EBADFD;
534 }
535 snd_pcm_stream_unlock_irq(substream);
536
537 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
538 return -EINVAL;
539 if (params->avail_min == 0)
540 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (params->silence_size >= runtime->boundary) {
542 if (params->silence_threshold != 0)
543 return -EINVAL;
544 } else {
545 if (params->silence_size > params->silence_threshold)
546 return -EINVAL;
547 if (params->silence_threshold > runtime->buffer_size)
548 return -EINVAL;
549 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100550 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 snd_pcm_stream_lock_irq(substream);
552 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 runtime->period_step = params->period_step;
554 runtime->control->avail_min = params->avail_min;
555 runtime->start_threshold = params->start_threshold;
556 runtime->stop_threshold = params->stop_threshold;
557 runtime->silence_threshold = params->silence_threshold;
558 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 params->boundary = runtime->boundary;
560 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
562 runtime->silence_size > 0)
563 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100564 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100567 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Takashi Iwai877211f2005-11-17 13:59:38 +0100570static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
571 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Takashi Iwai877211f2005-11-17 13:59:38 +0100573 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int err;
575 if (copy_from_user(&params, _params, sizeof(params)))
576 return -EFAULT;
577 err = snd_pcm_sw_params(substream, &params);
578 if (copy_to_user(_params, &params, sizeof(params)))
579 return -EFAULT;
580 return err;
581}
582
Takashi Iwai877211f2005-11-17 13:59:38 +0100583int snd_pcm_status(struct snd_pcm_substream *substream,
584 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Takashi Iwai877211f2005-11-17 13:59:38 +0100586 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 snd_pcm_stream_lock_irq(substream);
589 status->state = runtime->status->state;
590 status->suspended_state = runtime->status->suspended_state;
591 if (status->state == SNDRV_PCM_STATE_OPEN)
592 goto _end;
593 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100594 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100596 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
597 status->tstamp = runtime->status->tstamp;
598 goto _tstamp_end;
599 }
600 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100601 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100602 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 status->appl_ptr = runtime->control->appl_ptr;
604 status->hw_ptr = runtime->status->hw_ptr;
605 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
606 status->avail = snd_pcm_playback_avail(runtime);
607 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200608 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200610 status->delay += runtime->delay;
611 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 status->delay = 0;
613 } else {
614 status->avail = snd_pcm_capture_avail(runtime);
615 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200616 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 else
618 status->delay = 0;
619 }
620 status->avail_max = runtime->avail_max;
621 status->overrange = runtime->overrange;
622 runtime->avail_max = 0;
623 runtime->overrange = 0;
624 _end:
625 snd_pcm_stream_unlock_irq(substream);
626 return 0;
627}
628
Takashi Iwai877211f2005-11-17 13:59:38 +0100629static int snd_pcm_status_user(struct snd_pcm_substream *substream,
630 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Takashi Iwai877211f2005-11-17 13:59:38 +0100632 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 int res;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 memset(&status, 0, sizeof(status));
636 res = snd_pcm_status(substream, &status);
637 if (res < 0)
638 return res;
639 if (copy_to_user(_status, &status, sizeof(status)))
640 return -EFAULT;
641 return 0;
642}
643
Takashi Iwai877211f2005-11-17 13:59:38 +0100644static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
645 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Takashi Iwai877211f2005-11-17 13:59:38 +0100647 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 unsigned int channel;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 channel = info->channel;
651 runtime = substream->runtime;
652 snd_pcm_stream_lock_irq(substream);
653 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
654 snd_pcm_stream_unlock_irq(substream);
655 return -EBADFD;
656 }
657 snd_pcm_stream_unlock_irq(substream);
658 if (channel >= runtime->channels)
659 return -EINVAL;
660 memset(info, 0, sizeof(*info));
661 info->channel = channel;
662 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
663}
664
Takashi Iwai877211f2005-11-17 13:59:38 +0100665static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
666 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Takashi Iwai877211f2005-11-17 13:59:38 +0100668 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int res;
670
671 if (copy_from_user(&info, _info, sizeof(info)))
672 return -EFAULT;
673 res = snd_pcm_channel_info(substream, &info);
674 if (res < 0)
675 return res;
676 if (copy_to_user(_info, &info, sizeof(info)))
677 return -EFAULT;
678 return 0;
679}
680
Takashi Iwai877211f2005-11-17 13:59:38 +0100681static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Takashi Iwai877211f2005-11-17 13:59:38 +0100683 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (runtime->trigger_master == NULL)
685 return;
686 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100687 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 } else {
689 snd_pcm_trigger_tstamp(runtime->trigger_master);
690 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
691 }
692 runtime->trigger_master = NULL;
693}
694
695struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100696 int (*pre_action)(struct snd_pcm_substream *substream, int state);
697 int (*do_action)(struct snd_pcm_substream *substream, int state);
698 void (*undo_action)(struct snd_pcm_substream *substream, int state);
699 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700};
701
702/*
703 * this functions is core for handling of linked stream
704 * Note: the stream state might be changed also on failure
705 * Note2: call with calling stream lock + link lock
706 */
707static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100708 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int state, int do_lock)
710{
Takashi Iwai877211f2005-11-17 13:59:38 +0100711 struct snd_pcm_substream *s = NULL;
712 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int res = 0;
714
Takashi Iwaief991b92007-02-22 12:52:53 +0100715 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200717 spin_lock_nested(&s->self_group.lock,
718 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 res = ops->pre_action(s, state);
720 if (res < 0)
721 goto _unlock;
722 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100723 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 res = ops->do_action(s, state);
725 if (res < 0) {
726 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100727 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (s1 == s) /* failed stream */
729 break;
730 ops->undo_action(s1, state);
731 }
732 }
733 s = NULL; /* unlock all */
734 goto _unlock;
735 }
736 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100737 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 ops->post_action(s, state);
739 }
740 _unlock:
741 if (do_lock) {
742 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100743 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (s1 != substream)
745 spin_unlock(&s1->self_group.lock);
746 if (s1 == s) /* end */
747 break;
748 }
749 }
750 return res;
751}
752
753/*
754 * Note: call with stream lock
755 */
756static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100757 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int state)
759{
760 int res;
761
762 res = ops->pre_action(substream, state);
763 if (res < 0)
764 return res;
765 res = ops->do_action(substream, state);
766 if (res == 0)
767 ops->post_action(substream, state);
768 else if (ops->undo_action)
769 ops->undo_action(substream, state);
770 return res;
771}
772
773/*
774 * Note: call with stream lock
775 */
776static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100777 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int state)
779{
780 int res;
781
782 if (snd_pcm_stream_linked(substream)) {
783 if (!spin_trylock(&substream->group->lock)) {
784 spin_unlock(&substream->self_group.lock);
785 spin_lock(&substream->group->lock);
786 spin_lock(&substream->self_group.lock);
787 }
788 res = snd_pcm_action_group(ops, substream, state, 1);
789 spin_unlock(&substream->group->lock);
790 } else {
791 res = snd_pcm_action_single(ops, substream, state);
792 }
793 return res;
794}
795
796/*
797 * Note: don't use any locks before
798 */
799static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100800 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int state)
802{
803 int res;
804
805 read_lock_irq(&snd_pcm_link_rwlock);
806 if (snd_pcm_stream_linked(substream)) {
807 spin_lock(&substream->group->lock);
808 spin_lock(&substream->self_group.lock);
809 res = snd_pcm_action_group(ops, substream, state, 1);
810 spin_unlock(&substream->self_group.lock);
811 spin_unlock(&substream->group->lock);
812 } else {
813 spin_lock(&substream->self_group.lock);
814 res = snd_pcm_action_single(ops, substream, state);
815 spin_unlock(&substream->self_group.lock);
816 }
817 read_unlock_irq(&snd_pcm_link_rwlock);
818 return res;
819}
820
821/*
822 */
823static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100824 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 int state)
826{
827 int res;
828
829 down_read(&snd_pcm_link_rwsem);
830 if (snd_pcm_stream_linked(substream))
831 res = snd_pcm_action_group(ops, substream, state, 0);
832 else
833 res = snd_pcm_action_single(ops, substream, state);
834 up_read(&snd_pcm_link_rwsem);
835 return res;
836}
837
838/*
839 * start callbacks
840 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100841static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Takashi Iwai877211f2005-11-17 13:59:38 +0100843 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
845 return -EBADFD;
846 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Liam Girdwoodad2581e2011-02-01 21:31:31 +0000847 !substream->hw_no_buffer &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 !snd_pcm_playback_data(substream))
849 return -EPIPE;
850 runtime->trigger_master = substream;
851 return 0;
852}
853
Takashi Iwai877211f2005-11-17 13:59:38 +0100854static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 if (substream->runtime->trigger_master != substream)
857 return 0;
858 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
859}
860
Takashi Iwai877211f2005-11-17 13:59:38 +0100861static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 if (substream->runtime->trigger_master == substream)
864 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
865}
866
Takashi Iwai877211f2005-11-17 13:59:38 +0100867static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Takashi Iwai877211f2005-11-17 13:59:38 +0100869 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200871 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200872 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
873 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 runtime->status->state = state;
875 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
876 runtime->silence_size > 0)
877 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100879 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
880 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static struct action_ops snd_pcm_action_start = {
884 .pre_action = snd_pcm_pre_start,
885 .do_action = snd_pcm_do_start,
886 .undo_action = snd_pcm_undo_start,
887 .post_action = snd_pcm_post_start
888};
889
890/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700891 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200892 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100894int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Takashi Iwai877211f2005-11-17 13:59:38 +0100896 return snd_pcm_action(&snd_pcm_action_start, substream,
897 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
901 * stop callbacks
902 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100903static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Takashi Iwai877211f2005-11-17 13:59:38 +0100905 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
907 return -EBADFD;
908 runtime->trigger_master = substream;
909 return 0;
910}
911
Takashi Iwai877211f2005-11-17 13:59:38 +0100912static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 if (substream->runtime->trigger_master == substream &&
915 snd_pcm_running(substream))
916 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
917 return 0; /* unconditonally stop all substreams */
918}
919
Takashi Iwai877211f2005-11-17 13:59:38 +0100920static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Takashi Iwai877211f2005-11-17 13:59:38 +0100922 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (runtime->status->state != state) {
924 snd_pcm_trigger_tstamp(substream);
925 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100926 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
927 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100931 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934static struct action_ops snd_pcm_action_stop = {
935 .pre_action = snd_pcm_pre_stop,
936 .do_action = snd_pcm_do_stop,
937 .post_action = snd_pcm_post_stop
938};
939
940/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700941 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200942 * @substream: the PCM substream instance
943 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700945 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
Clemens Ladischfea952e2011-02-14 11:00:47 +0100947int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
950}
951
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200952EXPORT_SYMBOL(snd_pcm_stop);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700955 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200956 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700958 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * Unlike snd_pcm_stop(), this affects only the given stream.
960 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100961int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Takashi Iwai877211f2005-11-17 13:59:38 +0100963 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
964 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967/*
968 * pause callbacks
969 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100970static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Takashi Iwai877211f2005-11-17 13:59:38 +0100972 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
974 return -ENOSYS;
975 if (push) {
976 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
977 return -EBADFD;
978 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
979 return -EBADFD;
980 runtime->trigger_master = substream;
981 return 0;
982}
983
Takashi Iwai877211f2005-11-17 13:59:38 +0100984static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 if (substream->runtime->trigger_master != substream)
987 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200988 /* some drivers might use hw_ptr to recover from the pause -
989 update the hw_ptr now */
990 if (push)
991 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200992 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400993 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200994 * delta, effectively to skip the check once.
995 */
996 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return substream->ops->trigger(substream,
998 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
999 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1000}
1001
Takashi Iwai877211f2005-11-17 13:59:38 +01001002static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 if (substream->runtime->trigger_master == substream)
1005 substream->ops->trigger(substream,
1006 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1007 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1008}
1009
Takashi Iwai877211f2005-11-17 13:59:38 +01001010static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Takashi Iwai877211f2005-11-17 13:59:38 +01001012 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 snd_pcm_trigger_tstamp(substream);
1014 if (push) {
1015 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1016 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001017 snd_timer_notify(substream->timer,
1018 SNDRV_TIMER_EVENT_MPAUSE,
1019 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001021 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else {
1023 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001025 snd_timer_notify(substream->timer,
1026 SNDRV_TIMER_EVENT_MCONTINUE,
1027 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029}
1030
1031static struct action_ops snd_pcm_action_pause = {
1032 .pre_action = snd_pcm_pre_pause,
1033 .do_action = snd_pcm_do_pause,
1034 .undo_action = snd_pcm_undo_pause,
1035 .post_action = snd_pcm_post_pause
1036};
1037
1038/*
1039 * Push/release the pause for all linked streams.
1040 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001041static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1044}
1045
1046#ifdef CONFIG_PM
1047/* suspend */
1048
Takashi Iwai877211f2005-11-17 13:59:38 +01001049static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Takashi Iwai877211f2005-11-17 13:59:38 +01001051 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1053 return -EBUSY;
1054 runtime->trigger_master = substream;
1055 return 0;
1056}
1057
Takashi Iwai877211f2005-11-17 13:59:38 +01001058static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Takashi Iwai877211f2005-11-17 13:59:38 +01001060 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (runtime->trigger_master != substream)
1062 return 0;
1063 if (! snd_pcm_running(substream))
1064 return 0;
1065 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1066 return 0; /* suspend unconditionally */
1067}
1068
Takashi Iwai877211f2005-11-17 13:59:38 +01001069static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Takashi Iwai877211f2005-11-17 13:59:38 +01001071 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 snd_pcm_trigger_tstamp(substream);
1073 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001074 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1075 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 runtime->status->suspended_state = runtime->status->state;
1077 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001079 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082static struct action_ops snd_pcm_action_suspend = {
1083 .pre_action = snd_pcm_pre_suspend,
1084 .do_action = snd_pcm_do_suspend,
1085 .post_action = snd_pcm_post_suspend
1086};
1087
1088/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001089 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001090 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 * After this call, all streams are changed to SUSPENDED state.
1093 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001094int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 int err;
1097 unsigned long flags;
1098
Takashi Iwai603bf522005-11-17 15:59:14 +01001099 if (! substream)
1100 return 0;
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 snd_pcm_stream_lock_irqsave(substream, flags);
1103 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1104 snd_pcm_stream_unlock_irqrestore(substream, flags);
1105 return err;
1106}
1107
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001108EXPORT_SYMBOL(snd_pcm_suspend);
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001111 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001112 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 * After this call, all streams are changed to SUSPENDED state.
1115 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001116int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Takashi Iwai877211f2005-11-17 13:59:38 +01001118 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int stream, err = 0;
1120
Takashi Iwai603bf522005-11-17 15:59:14 +01001121 if (! pcm)
1122 return 0;
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001125 for (substream = pcm->streams[stream].substream;
1126 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /* FIXME: the open/close code should lock this as well */
1128 if (substream->runtime == NULL)
1129 continue;
1130 err = snd_pcm_suspend(substream);
1131 if (err < 0 && err != -EBUSY)
1132 return err;
1133 }
1134 }
1135 return 0;
1136}
1137
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001138EXPORT_SYMBOL(snd_pcm_suspend_all);
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/* resume */
1141
Takashi Iwai877211f2005-11-17 13:59:38 +01001142static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Takashi Iwai877211f2005-11-17 13:59:38 +01001144 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1146 return -ENOSYS;
1147 runtime->trigger_master = substream;
1148 return 0;
1149}
1150
Takashi Iwai877211f2005-11-17 13:59:38 +01001151static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Takashi Iwai877211f2005-11-17 13:59:38 +01001153 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (runtime->trigger_master != substream)
1155 return 0;
1156 /* DMA not running previously? */
1157 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1158 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1159 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1160 return 0;
1161 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1162}
1163
Takashi Iwai877211f2005-11-17 13:59:38 +01001164static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 if (substream->runtime->trigger_master == substream &&
1167 snd_pcm_running(substream))
1168 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1169}
1170
Takashi Iwai877211f2005-11-17 13:59:38 +01001171static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Takashi Iwai877211f2005-11-17 13:59:38 +01001173 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 snd_pcm_trigger_tstamp(substream);
1175 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001176 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1177 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181static struct action_ops snd_pcm_action_resume = {
1182 .pre_action = snd_pcm_pre_resume,
1183 .do_action = snd_pcm_do_resume,
1184 .undo_action = snd_pcm_undo_resume,
1185 .post_action = snd_pcm_post_resume
1186};
1187
Takashi Iwai877211f2005-11-17 13:59:38 +01001188static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Takashi Iwai877211f2005-11-17 13:59:38 +01001190 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int res;
1192
1193 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001194 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1196 snd_power_unlock(card);
1197 return res;
1198}
1199
1200#else
1201
Takashi Iwai877211f2005-11-17 13:59:38 +01001202static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 return -ENOSYS;
1205}
1206
1207#endif /* CONFIG_PM */
1208
1209/*
1210 * xrun ioctl
1211 *
1212 * Change the RUNNING stream(s) to XRUN state.
1213 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001214static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Takashi Iwai877211f2005-11-17 13:59:38 +01001216 struct snd_card *card = substream->pcm->card;
1217 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int result;
1219
1220 snd_power_lock(card);
1221 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001222 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (result < 0)
1224 goto _unlock;
1225 }
1226
1227 snd_pcm_stream_lock_irq(substream);
1228 switch (runtime->status->state) {
1229 case SNDRV_PCM_STATE_XRUN:
1230 result = 0; /* already there */
1231 break;
1232 case SNDRV_PCM_STATE_RUNNING:
1233 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1234 break;
1235 default:
1236 result = -EBADFD;
1237 }
1238 snd_pcm_stream_unlock_irq(substream);
1239 _unlock:
1240 snd_power_unlock(card);
1241 return result;
1242}
1243
1244/*
1245 * reset ioctl
1246 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001247static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Takashi Iwai877211f2005-11-17 13:59:38 +01001249 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 switch (runtime->status->state) {
1251 case SNDRV_PCM_STATE_RUNNING:
1252 case SNDRV_PCM_STATE_PREPARED:
1253 case SNDRV_PCM_STATE_PAUSED:
1254 case SNDRV_PCM_STATE_SUSPENDED:
1255 return 0;
1256 default:
1257 return -EBADFD;
1258 }
1259}
1260
Takashi Iwai877211f2005-11-17 13:59:38 +01001261static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwai877211f2005-11-17 13:59:38 +01001263 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1265 if (err < 0)
1266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001268 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1269 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 runtime->silence_start = runtime->status->hw_ptr;
1271 runtime->silence_filled = 0;
1272 return 0;
1273}
1274
Takashi Iwai877211f2005-11-17 13:59:38 +01001275static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Takashi Iwai877211f2005-11-17 13:59:38 +01001277 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 runtime->control->appl_ptr = runtime->status->hw_ptr;
1279 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1280 runtime->silence_size > 0)
1281 snd_pcm_playback_silence(substream, ULONG_MAX);
1282}
1283
1284static struct action_ops snd_pcm_action_reset = {
1285 .pre_action = snd_pcm_pre_reset,
1286 .do_action = snd_pcm_do_reset,
1287 .post_action = snd_pcm_post_reset
1288};
1289
Takashi Iwai877211f2005-11-17 13:59:38 +01001290static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1293}
1294
1295/*
1296 * prepare ioctl
1297 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001298/* we use the second argument for updating f_flags */
1299static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1300 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Takashi Iwai877211f2005-11-17 13:59:38 +01001302 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001303 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1304 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return -EBADFD;
1306 if (snd_pcm_running(substream))
1307 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001308 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return 0;
1310}
1311
Takashi Iwai877211f2005-11-17 13:59:38 +01001312static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 int err;
1315 err = substream->ops->prepare(substream);
1316 if (err < 0)
1317 return err;
1318 return snd_pcm_do_reset(substream, 0);
1319}
1320
Takashi Iwai877211f2005-11-17 13:59:38 +01001321static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Takashi Iwai877211f2005-11-17 13:59:38 +01001323 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 runtime->control->appl_ptr = runtime->status->hw_ptr;
1325 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1326}
1327
1328static struct action_ops snd_pcm_action_prepare = {
1329 .pre_action = snd_pcm_pre_prepare,
1330 .do_action = snd_pcm_do_prepare,
1331 .post_action = snd_pcm_post_prepare
1332};
1333
1334/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001335 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001336 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001337 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001339static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1340 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001343 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001344 int f_flags;
1345
1346 if (file)
1347 f_flags = file->f_flags;
1348 else
1349 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001352 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001353 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1354 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 snd_power_unlock(card);
1356 return res;
1357}
1358
1359/*
1360 * drain ioctl
1361 */
1362
Takashi Iwai877211f2005-11-17 13:59:38 +01001363static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 substream->runtime->trigger_master = substream;
1366 return 0;
1367}
1368
Takashi Iwai877211f2005-11-17 13:59:38 +01001369static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Takashi Iwai877211f2005-11-17 13:59:38 +01001371 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1373 switch (runtime->status->state) {
1374 case SNDRV_PCM_STATE_PREPARED:
1375 /* start playback stream if possible */
1376 if (! snd_pcm_playback_empty(substream)) {
1377 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1378 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1379 }
1380 break;
1381 case SNDRV_PCM_STATE_RUNNING:
1382 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1383 break;
1384 default:
1385 break;
1386 }
1387 } else {
1388 /* stop running stream */
1389 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001390 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001392 snd_pcm_do_stop(substream, new_state);
1393 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395 }
1396 return 0;
1397}
1398
Takashi Iwai877211f2005-11-17 13:59:38 +01001399static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401}
1402
1403static struct action_ops snd_pcm_action_drain_init = {
1404 .pre_action = snd_pcm_pre_drain_init,
1405 .do_action = snd_pcm_do_drain_init,
1406 .post_action = snd_pcm_post_drain_init
1407};
1408
Takashi Iwai877211f2005-11-17 13:59:38 +01001409static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411/*
1412 * Drain the stream(s).
1413 * When the substream is linked, sync until the draining of all playback streams
1414 * is finished.
1415 * After this call, all streams are supposed to be either SETUP or DRAINING
1416 * (capture only) state.
1417 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001418static int snd_pcm_drain(struct snd_pcm_substream *substream,
1419 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Takashi Iwai877211f2005-11-17 13:59:38 +01001421 struct snd_card *card;
1422 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001423 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001424 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001426 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 card = substream->pcm->card;
1429 runtime = substream->runtime;
1430
1431 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1432 return -EBADFD;
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 snd_power_lock(card);
1435 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001436 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001437 if (result < 0) {
1438 snd_power_unlock(card);
1439 return result;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001443 if (file) {
1444 if (file->f_flags & O_NONBLOCK)
1445 nonblock = 1;
1446 } else if (substream->f_flags & O_NONBLOCK)
1447 nonblock = 1;
1448
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001449 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001450 snd_pcm_stream_lock_irq(substream);
1451 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001452 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001453 snd_pcm_pause(substream, 0);
1454
1455 /* pre-start/stop - all running streams are changed to DRAINING state */
1456 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001457 if (result < 0)
1458 goto unlock;
1459 /* in non-blocking, we don't wait in ioctl but let caller poll */
1460 if (nonblock) {
1461 result = -EAGAIN;
1462 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 for (;;) {
1466 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001467 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (signal_pending(current)) {
1469 result = -ERESTARTSYS;
1470 break;
1471 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001472 /* find a substream to drain */
1473 to_check = NULL;
1474 snd_pcm_group_for_each_entry(s, substream) {
1475 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1476 continue;
1477 runtime = s->runtime;
1478 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1479 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001480 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001481 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001482 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001483 if (!to_check)
1484 break; /* all drained */
1485 init_waitqueue_entry(&wait, current);
1486 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001488 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001490 if (runtime->no_period_wakeup)
1491 tout = MAX_SCHEDULE_TIMEOUT;
1492 else {
1493 tout = 10;
1494 if (runtime->rate) {
1495 long t = runtime->period_size * 2 / runtime->rate;
1496 tout = max(t, tout);
1497 }
1498 tout = msecs_to_jiffies(tout * 1000);
1499 }
1500 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001502 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001504 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (tout == 0) {
1506 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1507 result = -ESTRPIPE;
1508 else {
1509 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1510 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1511 result = -EIO;
1512 }
1513 break;
1514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001516
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001517 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001518 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001519 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 return result;
1523}
1524
Asish Bhattacharya9048f752011-11-01 20:33:44 +05301525static int snd_compressed_ioctl(struct snd_pcm_substream *substream,
1526 unsigned int cmd, void __user *arg)
1527{
1528 struct snd_pcm_runtime *runtime;
1529 int err = 0;
1530
1531 if (PCM_RUNTIME_CHECK(substream))
1532 return -ENXIO;
1533 runtime = substream->runtime;
Swaminathan Sathappan362a17c2012-04-25 18:09:46 -07001534 pr_debug("%s called with cmd = %d\n", __func__, cmd);
Asish Bhattacharya9048f752011-11-01 20:33:44 +05301535 err = substream->ops->ioctl(substream, cmd, arg);
1536 return err;
1537}
Joonwoo Parked3bab02013-02-13 15:59:44 -08001538
1539static int snd_user_ioctl(struct snd_pcm_substream *substream,
1540 unsigned int cmd, void __user *arg)
1541{
1542 struct snd_pcm_runtime *runtime;
1543 int err = 0;
1544
1545 if (PCM_RUNTIME_CHECK(substream))
1546 return -ENXIO;
1547 runtime = substream->runtime;
1548 err = substream->ops->ioctl(substream, cmd, arg);
1549 return err;
1550}
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552/*
1553 * drop ioctl
1554 *
1555 * Immediately put all linked substreams into SETUP state.
1556 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001557static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Takashi Iwai877211f2005-11-17 13:59:38 +01001559 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int result = 0;
1561
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001562 if (PCM_RUNTIME_CHECK(substream))
1563 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001566 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001567 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1568 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return -EBADFD;
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 snd_pcm_stream_lock_irq(substream);
1572 /* resume pause */
1573 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1574 snd_pcm_pause(substream, 0);
1575
1576 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1577 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1578 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return result;
1581}
1582
1583
1584/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585static struct file *snd_pcm_file_fd(int fd)
1586{
1587 struct file *file;
1588 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001589 unsigned int minor;
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 file = fget(fd);
1592 if (!file)
1593 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001594 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (!S_ISCHR(inode->i_mode) ||
1596 imajor(inode) != snd_major) {
1597 fput(file);
1598 return NULL;
1599 }
1600 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001601 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1602 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 fput(file);
1604 return NULL;
1605 }
1606 return file;
1607}
1608
1609/*
1610 * PCM link handling
1611 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001612static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 int res = 0;
1615 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001616 struct snd_pcm_file *pcm_file;
1617 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001618 struct snd_pcm_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 file = snd_pcm_file_fd(fd);
1621 if (!file)
1622 return -EBADFD;
1623 pcm_file = file->private_data;
1624 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001625 group = kmalloc(sizeof(*group), GFP_KERNEL);
1626 if (!group) {
1627 res = -ENOMEM;
1628 goto _nolock;
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 down_write(&snd_pcm_link_rwsem);
1631 write_lock_irq(&snd_pcm_link_rwlock);
1632 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1633 substream->runtime->status->state != substream1->runtime->status->state) {
1634 res = -EBADFD;
1635 goto _end;
1636 }
1637 if (snd_pcm_stream_linked(substream1)) {
1638 res = -EALREADY;
1639 goto _end;
1640 }
1641 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001642 substream->group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 spin_lock_init(&substream->group->lock);
1644 INIT_LIST_HEAD(&substream->group->substreams);
1645 list_add_tail(&substream->link_list, &substream->group->substreams);
1646 substream->group->count = 1;
1647 }
1648 list_add_tail(&substream1->link_list, &substream->group->substreams);
1649 substream->group->count++;
1650 substream1->group = substream->group;
1651 _end:
1652 write_unlock_irq(&snd_pcm_link_rwlock);
1653 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001654 _nolock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 fput(file);
Takashi Iwai16625912012-03-13 15:55:43 +01001656 if (res < 0)
1657 kfree(group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return res;
1659}
1660
Takashi Iwai877211f2005-11-17 13:59:38 +01001661static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 substream->group = &substream->self_group;
1664 INIT_LIST_HEAD(&substream->self_group.substreams);
1665 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1666}
1667
Takashi Iwai877211f2005-11-17 13:59:38 +01001668static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Takashi Iwaief991b92007-02-22 12:52:53 +01001670 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 int res = 0;
1672
1673 down_write(&snd_pcm_link_rwsem);
1674 write_lock_irq(&snd_pcm_link_rwlock);
1675 if (!snd_pcm_stream_linked(substream)) {
1676 res = -EALREADY;
1677 goto _end;
1678 }
1679 list_del(&substream->link_list);
1680 substream->group->count--;
1681 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001682 snd_pcm_group_for_each_entry(s, substream) {
1683 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 break;
1685 }
1686 kfree(substream->group);
1687 }
1688 relink_to_local(substream);
1689 _end:
1690 write_unlock_irq(&snd_pcm_link_rwlock);
1691 up_write(&snd_pcm_link_rwsem);
1692 return res;
1693}
1694
1695/*
1696 * hw configurator
1697 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001698static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1699 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Takashi Iwai877211f2005-11-17 13:59:38 +01001701 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1703 hw_param_interval_c(params, rule->deps[1]), &t);
1704 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1705}
1706
Takashi Iwai877211f2005-11-17 13:59:38 +01001707static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1708 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Takashi Iwai877211f2005-11-17 13:59:38 +01001710 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1712 hw_param_interval_c(params, rule->deps[1]), &t);
1713 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1714}
1715
Takashi Iwai877211f2005-11-17 13:59:38 +01001716static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1717 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Takashi Iwai877211f2005-11-17 13:59:38 +01001719 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1721 hw_param_interval_c(params, rule->deps[1]),
1722 (unsigned long) rule->private, &t);
1723 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1724}
1725
Takashi Iwai877211f2005-11-17 13:59:38 +01001726static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1727 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Takashi Iwai877211f2005-11-17 13:59:38 +01001729 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1731 (unsigned long) rule->private,
1732 hw_param_interval_c(params, rule->deps[1]), &t);
1733 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1734}
1735
Takashi Iwai877211f2005-11-17 13:59:38 +01001736static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1737 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001740 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1741 struct snd_mask m;
1742 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 snd_mask_any(&m);
1744 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1745 int bits;
1746 if (! snd_mask_test(mask, k))
1747 continue;
1748 bits = snd_pcm_format_physical_width(k);
1749 if (bits <= 0)
1750 continue; /* ignore invalid formats */
1751 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1752 snd_mask_reset(&m, k);
1753 }
1754 return snd_mask_refine(mask, &m);
1755}
1756
Takashi Iwai877211f2005-11-17 13:59:38 +01001757static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1758 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Takashi Iwai877211f2005-11-17 13:59:38 +01001760 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 unsigned int k;
1762 t.min = UINT_MAX;
1763 t.max = 0;
1764 t.openmin = 0;
1765 t.openmax = 0;
1766 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1767 int bits;
1768 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1769 continue;
1770 bits = snd_pcm_format_physical_width(k);
1771 if (bits <= 0)
1772 continue; /* ignore invalid formats */
1773 if (t.min > (unsigned)bits)
1774 t.min = bits;
1775 if (t.max < (unsigned)bits)
1776 t.max = bits;
1777 }
1778 t.integer = 1;
1779 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1780}
1781
1782#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1783#error "Change this table"
1784#endif
1785
1786static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1787 48000, 64000, 88200, 96000, 176400, 192000 };
1788
Clemens Ladisch7653d552007-08-13 17:38:54 +02001789const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1790 .count = ARRAY_SIZE(rates),
1791 .list = rates,
1792};
1793
Takashi Iwai877211f2005-11-17 13:59:38 +01001794static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1795 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Takashi Iwai877211f2005-11-17 13:59:38 +01001797 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001799 snd_pcm_known_rates.count,
1800 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Takashi Iwai877211f2005-11-17 13:59:38 +01001803static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1804 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Takashi Iwai877211f2005-11-17 13:59:38 +01001806 struct snd_interval t;
1807 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 t.min = 0;
1809 t.max = substream->buffer_bytes_max;
1810 t.openmin = 0;
1811 t.openmax = 0;
1812 t.integer = 1;
1813 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1814}
1815
Takashi Iwai877211f2005-11-17 13:59:38 +01001816int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Takashi Iwai877211f2005-11-17 13:59:38 +01001818 struct snd_pcm_runtime *runtime = substream->runtime;
1819 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 int k, err;
1821
1822 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1823 snd_mask_any(constrs_mask(constrs, k));
1824 }
1825
1826 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1827 snd_interval_any(constrs_interval(constrs, k));
1828 }
1829
1830 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1831 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1832 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1833 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1834 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1835
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1837 snd_pcm_hw_rule_format, NULL,
1838 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1839 if (err < 0)
1840 return err;
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1842 snd_pcm_hw_rule_sample_bits, NULL,
1843 SNDRV_PCM_HW_PARAM_FORMAT,
1844 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1845 if (err < 0)
1846 return err;
1847 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1848 snd_pcm_hw_rule_div, NULL,
1849 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1850 if (err < 0)
1851 return err;
1852 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1853 snd_pcm_hw_rule_mul, NULL,
1854 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1855 if (err < 0)
1856 return err;
1857 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1858 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1859 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1860 if (err < 0)
1861 return err;
1862 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1863 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1864 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1865 if (err < 0)
1866 return err;
1867 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1868 snd_pcm_hw_rule_div, NULL,
1869 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1870 if (err < 0)
1871 return err;
1872 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1873 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1874 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1875 if (err < 0)
1876 return err;
1877 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1878 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1879 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1880 if (err < 0)
1881 return err;
1882 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1883 snd_pcm_hw_rule_div, NULL,
1884 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1885 if (err < 0)
1886 return err;
1887 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1888 snd_pcm_hw_rule_div, NULL,
1889 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1890 if (err < 0)
1891 return err;
1892 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1893 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1894 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1895 if (err < 0)
1896 return err;
1897 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1898 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1899 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1900 if (err < 0)
1901 return err;
1902 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1903 snd_pcm_hw_rule_mul, NULL,
1904 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1905 if (err < 0)
1906 return err;
1907 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1908 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1909 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1910 if (err < 0)
1911 return err;
1912 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1913 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1914 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1915 if (err < 0)
1916 return err;
1917 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1918 snd_pcm_hw_rule_muldivk, (void*) 8,
1919 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1920 if (err < 0)
1921 return err;
1922 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1923 snd_pcm_hw_rule_muldivk, (void*) 8,
1924 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1925 if (err < 0)
1926 return err;
1927 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1928 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1929 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1930 if (err < 0)
1931 return err;
1932 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1933 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1934 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1935 if (err < 0)
1936 return err;
1937 return 0;
1938}
1939
Takashi Iwai877211f2005-11-17 13:59:38 +01001940int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Takashi Iwai877211f2005-11-17 13:59:38 +01001942 struct snd_pcm_runtime *runtime = substream->runtime;
1943 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 int err;
1945 unsigned int mask = 0;
1946
1947 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1948 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1949 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1950 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1951 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1952 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1953 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1954 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1955 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1956 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1957 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1958 }
1959 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001960 if (err < 0)
1961 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001964 if (err < 0)
1965 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001968 if (err < 0)
1969 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1972 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001973 if (err < 0)
1974 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1977 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001978 if (err < 0)
1979 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1982 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001983 if (err < 0)
1984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1987 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001988 if (err < 0)
1989 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1992 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001993 if (err < 0)
1994 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1997 snd_pcm_hw_rule_buffer_bytes_max, substream,
1998 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1999 if (err < 0)
2000 return err;
2001
2002 /* FIXME: remove */
2003 if (runtime->dma_bytes) {
2004 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002005 if (err < 0)
2006 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008
2009 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2010 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2011 snd_pcm_hw_rule_rate, hw,
2012 SNDRV_PCM_HW_PARAM_RATE, -1);
2013 if (err < 0)
2014 return err;
2015 }
2016
2017 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2019
2020 return 0;
2021}
2022
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002023static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002026}
2027
2028void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2029{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002030 substream->ref_count--;
2031 if (substream->ref_count > 0)
2032 return;
2033
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002034 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002035 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (substream->ops->hw_free != NULL)
2037 substream->ops->hw_free(substream);
2038 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002039 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002041 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2042 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002043 if (substream->pcm_release) {
2044 substream->pcm_release(substream);
2045 substream->pcm_release = NULL;
2046 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002047 snd_pcm_detach_substream(substream);
2048}
2049
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002050EXPORT_SYMBOL(snd_pcm_release_substream);
2051
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002052int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2053 struct file *file,
2054 struct snd_pcm_substream **rsubstream)
2055{
2056 struct snd_pcm_substream *substream;
2057 int err;
2058
2059 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2060 if (err < 0)
2061 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002062 if (substream->ref_count > 1) {
2063 *rsubstream = substream;
2064 return 0;
2065 }
2066
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002067 err = snd_pcm_hw_constraints_init(substream);
2068 if (err < 0) {
2069 snd_printd("snd_pcm_hw_constraints_init failed\n");
2070 goto error;
2071 }
2072
Liam Girdwood8ccfb4a2011-02-16 16:50:12 +00002073 if (substream->ops == NULL) {
2074 snd_printd("cannot open back end PCMs directly\n");
2075 err = -ENODEV;
2076 goto error;
2077 }
2078
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002079 if ((err = substream->ops->open(substream)) < 0)
2080 goto error;
2081
2082 substream->hw_opened = 1;
2083
2084 err = snd_pcm_hw_constraints_complete(substream);
2085 if (err < 0) {
2086 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2087 goto error;
2088 }
2089
2090 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002092
2093 error:
2094 snd_pcm_release_substream(substream);
2095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002098EXPORT_SYMBOL(snd_pcm_open_substream);
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002101 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002102 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Takashi Iwai877211f2005-11-17 13:59:38 +01002104 struct snd_pcm_file *pcm_file;
2105 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002106 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002108 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2109 if (err < 0)
2110 return err;
2111
Takashi Iwai548a6482006-07-31 16:51:51 +02002112 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2113 if (pcm_file == NULL) {
2114 snd_pcm_release_substream(substream);
2115 return -ENOMEM;
2116 }
2117 pcm_file->substream = substream;
2118 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002119 substream->file = pcm_file;
2120 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return 0;
2125}
2126
Clemens Ladischf87135f2005-11-20 14:06:59 +01002127static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Takashi Iwai877211f2005-11-17 13:59:38 +01002129 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002130 int err = nonseekable_open(inode, file);
2131 if (err < 0)
2132 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002133 pcm = snd_lookup_minor_data(iminor(inode),
2134 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2135 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2136}
2137
2138static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2139{
2140 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002141 int err = nonseekable_open(inode, file);
2142 if (err < 0)
2143 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002144 pcm = snd_lookup_minor_data(iminor(inode),
2145 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2146 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2147}
2148
2149static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2150{
2151 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 wait_queue_t wait;
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (pcm == NULL) {
2155 err = -ENODEV;
2156 goto __error1;
2157 }
2158 err = snd_card_file_add(pcm->card, file);
2159 if (err < 0)
2160 goto __error1;
2161 if (!try_module_get(pcm->card->module)) {
2162 err = -EFAULT;
2163 goto __error2;
2164 }
2165 init_waitqueue_entry(&wait, current);
2166 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002167 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002169 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (err >= 0)
2171 break;
2172 if (err == -EAGAIN) {
2173 if (file->f_flags & O_NONBLOCK) {
2174 err = -EBUSY;
2175 break;
2176 }
2177 } else
2178 break;
2179 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002180 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002182 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (signal_pending(current)) {
2184 err = -ERESTARTSYS;
2185 break;
2186 }
2187 }
2188 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002189 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (err < 0)
2191 goto __error;
2192 return err;
2193
2194 __error:
2195 module_put(pcm->card->module);
2196 __error2:
2197 snd_card_file_remove(pcm->card, file);
2198 __error1:
2199 return err;
2200}
2201
2202static int snd_pcm_release(struct inode *inode, struct file *file)
2203{
Takashi Iwai877211f2005-11-17 13:59:38 +01002204 struct snd_pcm *pcm;
2205 struct snd_pcm_substream *substream;
2206 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 pcm_file = file->private_data;
2209 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002210 if (snd_BUG_ON(!substream))
2211 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002213 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002214 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002215 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002216 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 wake_up(&pcm->open_wait);
2218 module_put(pcm->card->module);
2219 snd_card_file_remove(pcm->card, file);
2220 return 0;
2221}
2222
Takashi Iwai877211f2005-11-17 13:59:38 +01002223static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2224 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Takashi Iwai877211f2005-11-17 13:59:38 +01002226 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 snd_pcm_sframes_t appl_ptr;
2228 snd_pcm_sframes_t ret;
2229 snd_pcm_sframes_t hw_avail;
2230
2231 if (frames == 0)
2232 return 0;
2233
2234 snd_pcm_stream_lock_irq(substream);
2235 switch (runtime->status->state) {
2236 case SNDRV_PCM_STATE_PREPARED:
2237 break;
2238 case SNDRV_PCM_STATE_DRAINING:
2239 case SNDRV_PCM_STATE_RUNNING:
2240 if (snd_pcm_update_hw_ptr(substream) >= 0)
2241 break;
2242 /* Fall through */
2243 case SNDRV_PCM_STATE_XRUN:
2244 ret = -EPIPE;
2245 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002246 case SNDRV_PCM_STATE_SUSPENDED:
2247 ret = -ESTRPIPE;
2248 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 default:
2250 ret = -EBADFD;
2251 goto __end;
2252 }
2253
2254 hw_avail = snd_pcm_playback_hw_avail(runtime);
2255 if (hw_avail <= 0) {
2256 ret = 0;
2257 goto __end;
2258 }
2259 if (frames > (snd_pcm_uframes_t)hw_avail)
2260 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 appl_ptr = runtime->control->appl_ptr - frames;
2262 if (appl_ptr < 0)
2263 appl_ptr += runtime->boundary;
2264 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 ret = frames;
2266 __end:
2267 snd_pcm_stream_unlock_irq(substream);
2268 return ret;
2269}
2270
Takashi Iwai877211f2005-11-17 13:59:38 +01002271static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2272 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Takashi Iwai877211f2005-11-17 13:59:38 +01002274 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 snd_pcm_sframes_t appl_ptr;
2276 snd_pcm_sframes_t ret;
2277 snd_pcm_sframes_t hw_avail;
2278
2279 if (frames == 0)
2280 return 0;
2281
2282 snd_pcm_stream_lock_irq(substream);
2283 switch (runtime->status->state) {
2284 case SNDRV_PCM_STATE_PREPARED:
2285 case SNDRV_PCM_STATE_DRAINING:
2286 break;
2287 case SNDRV_PCM_STATE_RUNNING:
2288 if (snd_pcm_update_hw_ptr(substream) >= 0)
2289 break;
2290 /* Fall through */
2291 case SNDRV_PCM_STATE_XRUN:
2292 ret = -EPIPE;
2293 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002294 case SNDRV_PCM_STATE_SUSPENDED:
2295 ret = -ESTRPIPE;
2296 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 default:
2298 ret = -EBADFD;
2299 goto __end;
2300 }
2301
2302 hw_avail = snd_pcm_capture_hw_avail(runtime);
2303 if (hw_avail <= 0) {
2304 ret = 0;
2305 goto __end;
2306 }
2307 if (frames > (snd_pcm_uframes_t)hw_avail)
2308 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 appl_ptr = runtime->control->appl_ptr - frames;
2310 if (appl_ptr < 0)
2311 appl_ptr += runtime->boundary;
2312 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 ret = frames;
2314 __end:
2315 snd_pcm_stream_unlock_irq(substream);
2316 return ret;
2317}
2318
Takashi Iwai877211f2005-11-17 13:59:38 +01002319static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2320 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Takashi Iwai877211f2005-11-17 13:59:38 +01002322 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 snd_pcm_sframes_t appl_ptr;
2324 snd_pcm_sframes_t ret;
2325 snd_pcm_sframes_t avail;
2326
2327 if (frames == 0)
2328 return 0;
2329
2330 snd_pcm_stream_lock_irq(substream);
2331 switch (runtime->status->state) {
2332 case SNDRV_PCM_STATE_PREPARED:
2333 case SNDRV_PCM_STATE_PAUSED:
2334 break;
2335 case SNDRV_PCM_STATE_DRAINING:
2336 case SNDRV_PCM_STATE_RUNNING:
2337 if (snd_pcm_update_hw_ptr(substream) >= 0)
2338 break;
2339 /* Fall through */
2340 case SNDRV_PCM_STATE_XRUN:
2341 ret = -EPIPE;
2342 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002343 case SNDRV_PCM_STATE_SUSPENDED:
2344 ret = -ESTRPIPE;
2345 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 default:
2347 ret = -EBADFD;
2348 goto __end;
2349 }
2350
2351 avail = snd_pcm_playback_avail(runtime);
2352 if (avail <= 0) {
2353 ret = 0;
2354 goto __end;
2355 }
2356 if (frames > (snd_pcm_uframes_t)avail)
2357 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 appl_ptr = runtime->control->appl_ptr + frames;
2359 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2360 appl_ptr -= runtime->boundary;
2361 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 ret = frames;
2363 __end:
2364 snd_pcm_stream_unlock_irq(substream);
2365 return ret;
2366}
2367
Takashi Iwai877211f2005-11-17 13:59:38 +01002368static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2369 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Takashi Iwai877211f2005-11-17 13:59:38 +01002371 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 snd_pcm_sframes_t appl_ptr;
2373 snd_pcm_sframes_t ret;
2374 snd_pcm_sframes_t avail;
2375
2376 if (frames == 0)
2377 return 0;
2378
2379 snd_pcm_stream_lock_irq(substream);
2380 switch (runtime->status->state) {
2381 case SNDRV_PCM_STATE_PREPARED:
2382 case SNDRV_PCM_STATE_DRAINING:
2383 case SNDRV_PCM_STATE_PAUSED:
2384 break;
2385 case SNDRV_PCM_STATE_RUNNING:
2386 if (snd_pcm_update_hw_ptr(substream) >= 0)
2387 break;
2388 /* Fall through */
2389 case SNDRV_PCM_STATE_XRUN:
2390 ret = -EPIPE;
2391 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002392 case SNDRV_PCM_STATE_SUSPENDED:
2393 ret = -ESTRPIPE;
2394 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 default:
2396 ret = -EBADFD;
2397 goto __end;
2398 }
2399
2400 avail = snd_pcm_capture_avail(runtime);
2401 if (avail <= 0) {
2402 ret = 0;
2403 goto __end;
2404 }
2405 if (frames > (snd_pcm_uframes_t)avail)
2406 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 appl_ptr = runtime->control->appl_ptr + frames;
2408 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2409 appl_ptr -= runtime->boundary;
2410 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 ret = frames;
2412 __end:
2413 snd_pcm_stream_unlock_irq(substream);
2414 return ret;
2415}
2416
Takashi Iwai877211f2005-11-17 13:59:38 +01002417static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Takashi Iwai877211f2005-11-17 13:59:38 +01002419 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 int err;
2421
2422 snd_pcm_stream_lock_irq(substream);
2423 switch (runtime->status->state) {
2424 case SNDRV_PCM_STATE_DRAINING:
2425 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2426 goto __badfd;
2427 case SNDRV_PCM_STATE_RUNNING:
2428 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2429 break;
2430 /* Fall through */
2431 case SNDRV_PCM_STATE_PREPARED:
2432 case SNDRV_PCM_STATE_SUSPENDED:
2433 err = 0;
2434 break;
2435 case SNDRV_PCM_STATE_XRUN:
2436 err = -EPIPE;
2437 break;
2438 default:
2439 __badfd:
2440 err = -EBADFD;
2441 break;
2442 }
2443 snd_pcm_stream_unlock_irq(substream);
2444 return err;
2445}
2446
Takashi Iwai877211f2005-11-17 13:59:38 +01002447static int snd_pcm_delay(struct snd_pcm_substream *substream,
2448 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Takashi Iwai877211f2005-11-17 13:59:38 +01002450 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 int err;
2452 snd_pcm_sframes_t n = 0;
2453
2454 snd_pcm_stream_lock_irq(substream);
2455 switch (runtime->status->state) {
2456 case SNDRV_PCM_STATE_DRAINING:
2457 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2458 goto __badfd;
2459 case SNDRV_PCM_STATE_RUNNING:
2460 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2461 break;
2462 /* Fall through */
2463 case SNDRV_PCM_STATE_PREPARED:
2464 case SNDRV_PCM_STATE_SUSPENDED:
2465 err = 0;
2466 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2467 n = snd_pcm_playback_hw_avail(runtime);
2468 else
2469 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002470 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 break;
2472 case SNDRV_PCM_STATE_XRUN:
2473 err = -EPIPE;
2474 break;
2475 default:
2476 __badfd:
2477 err = -EBADFD;
2478 break;
2479 }
2480 snd_pcm_stream_unlock_irq(substream);
2481 if (!err)
2482 if (put_user(n, res))
2483 err = -EFAULT;
2484 return err;
2485}
2486
Takashi Iwai877211f2005-11-17 13:59:38 +01002487static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2488 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Takashi Iwai877211f2005-11-17 13:59:38 +01002490 struct snd_pcm_runtime *runtime = substream->runtime;
2491 struct snd_pcm_sync_ptr sync_ptr;
2492 volatile struct snd_pcm_mmap_status *status;
2493 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 int err;
Santosh Mardic4fac022012-10-09 10:32:42 +05302495 snd_pcm_uframes_t hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 memset(&sync_ptr, 0, sizeof(sync_ptr));
2498 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2499 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002500 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 -07002501 return -EFAULT;
2502 status = runtime->status;
2503 control = runtime->control;
2504 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2505 err = snd_pcm_hwsync(substream);
2506 if (err < 0)
2507 return err;
2508 }
2509 snd_pcm_stream_lock_irq(substream);
2510 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2511 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2512 else
2513 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2514 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2515 control->avail_min = sync_ptr.c.control.avail_min;
2516 else
2517 sync_ptr.c.control.avail_min = control->avail_min;
Santosh Mardic4fac022012-10-09 10:32:42 +05302518
2519 if (runtime->render_flag & SNDRV_NON_DMA_MODE) {
2520 hw_avail = snd_pcm_playback_hw_avail(runtime);
2521 if ((hw_avail >= runtime->start_threshold)
2522 && (runtime->render_flag &
2523 SNDRV_RENDER_STOPPED)) {
2524 if (substream->ops->restart)
2525 substream->ops->restart(substream);
2526 }
2527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 sync_ptr.s.status.state = status->state;
2529 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2530 sync_ptr.s.status.tstamp = status->tstamp;
2531 sync_ptr.s.status.suspended_state = status->suspended_state;
2532 snd_pcm_stream_unlock_irq(substream);
2533 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2534 return -EFAULT;
2535 return 0;
2536}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002537
2538static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2539{
2540 struct snd_pcm_runtime *runtime = substream->runtime;
2541 int arg;
2542
2543 if (get_user(arg, _arg))
2544 return -EFAULT;
2545 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2546 return -EINVAL;
2547 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2548 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2549 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2550 return 0;
2551}
Krishnankutty Kolathappilly6f52d712012-08-28 15:47:48 -07002552
Takashi Iwai0df63e42006-04-28 15:13:41 +02002553static int snd_pcm_common_ioctl1(struct file *file,
2554 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 unsigned int cmd, void __user *arg)
2556{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 switch (cmd) {
2558 case SNDRV_PCM_IOCTL_PVERSION:
2559 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2560 case SNDRV_PCM_IOCTL_INFO:
2561 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002562 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2563 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002564 case SNDRV_PCM_IOCTL_TTSTAMP:
2565 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 case SNDRV_PCM_IOCTL_HW_REFINE:
2567 return snd_pcm_hw_refine_user(substream, arg);
2568 case SNDRV_PCM_IOCTL_HW_PARAMS:
2569 return snd_pcm_hw_params_user(substream, arg);
2570 case SNDRV_PCM_IOCTL_HW_FREE:
2571 return snd_pcm_hw_free(substream);
2572 case SNDRV_PCM_IOCTL_SW_PARAMS:
2573 return snd_pcm_sw_params_user(substream, arg);
2574 case SNDRV_PCM_IOCTL_STATUS:
2575 return snd_pcm_status_user(substream, arg);
2576 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2577 return snd_pcm_channel_info_user(substream, arg);
2578 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002579 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 case SNDRV_PCM_IOCTL_RESET:
2581 return snd_pcm_reset(substream);
2582 case SNDRV_PCM_IOCTL_START:
2583 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2584 case SNDRV_PCM_IOCTL_LINK:
2585 return snd_pcm_link(substream, (int)(unsigned long) arg);
2586 case SNDRV_PCM_IOCTL_UNLINK:
2587 return snd_pcm_unlink(substream);
2588 case SNDRV_PCM_IOCTL_RESUME:
2589 return snd_pcm_resume(substream);
2590 case SNDRV_PCM_IOCTL_XRUN:
2591 return snd_pcm_xrun(substream);
2592 case SNDRV_PCM_IOCTL_HWSYNC:
2593 return snd_pcm_hwsync(substream);
2594 case SNDRV_PCM_IOCTL_DELAY:
2595 return snd_pcm_delay(substream, arg);
2596 case SNDRV_PCM_IOCTL_SYNC_PTR:
2597 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002598#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2600 return snd_pcm_hw_refine_old_user(substream, arg);
2601 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2602 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002603#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002605 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 case SNDRV_PCM_IOCTL_DROP:
2607 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002608 case SNDRV_PCM_IOCTL_PAUSE:
2609 {
2610 int res;
2611 snd_pcm_stream_lock_irq(substream);
2612 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2613 snd_pcm_stream_unlock_irq(substream);
2614 return res;
2615 }
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302616 case SNDRV_COMPRESS_GET_CAPS:
2617 case SNDRV_COMPRESS_GET_CODEC_CAPS:
2618 case SNDRV_COMPRESS_SET_PARAMS:
2619 case SNDRV_COMPRESS_GET_PARAMS:
2620 case SNDRV_COMPRESS_TSTAMP:
Krishnankutty Kolathappilly6f52d712012-08-28 15:47:48 -07002621 case SNDRV_COMPRESS_DRAIN:
Krishnankutty Kolathappilly3426df02013-04-07 18:04:18 -07002622 case SNDRV_COMPRESS_METADATA_MODE:
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302623 return snd_compressed_ioctl(substream, cmd, arg);
Joonwoo Parked3bab02013-02-13 15:59:44 -08002624 default:
2625 if (((cmd >> 8) & 0xff) == 'U')
2626 return snd_user_ioctl(substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
2628 snd_printd("unknown ioctl = 0x%x\n", cmd);
2629 return -ENOTTY;
2630}
2631
Takashi Iwai0df63e42006-04-28 15:13:41 +02002632static int snd_pcm_playback_ioctl1(struct file *file,
2633 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 unsigned int cmd, void __user *arg)
2635{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002636 if (snd_BUG_ON(!substream))
2637 return -ENXIO;
2638 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2639 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 switch (cmd) {
2641 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2642 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002643 struct snd_xferi xferi;
2644 struct snd_xferi __user *_xferi = arg;
2645 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 snd_pcm_sframes_t result;
2647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2648 return -EBADFD;
2649 if (put_user(0, &_xferi->result))
2650 return -EFAULT;
2651 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2652 return -EFAULT;
2653 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2654 __put_user(result, &_xferi->result);
2655 return result < 0 ? result : 0;
2656 }
2657 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2658 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002659 struct snd_xfern xfern;
2660 struct snd_xfern __user *_xfern = arg;
2661 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 void __user **bufs;
2663 snd_pcm_sframes_t result;
2664 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2665 return -EBADFD;
2666 if (runtime->channels > 128)
2667 return -EINVAL;
2668 if (put_user(0, &_xfern->result))
2669 return -EFAULT;
2670 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2671 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002672
2673 bufs = memdup_user(xfern.bufs,
2674 sizeof(void *) * runtime->channels);
2675 if (IS_ERR(bufs))
2676 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2678 kfree(bufs);
2679 __put_user(result, &_xfern->result);
2680 return result < 0 ? result : 0;
2681 }
2682 case SNDRV_PCM_IOCTL_REWIND:
2683 {
2684 snd_pcm_uframes_t frames;
2685 snd_pcm_uframes_t __user *_frames = arg;
2686 snd_pcm_sframes_t result;
2687 if (get_user(frames, _frames))
2688 return -EFAULT;
2689 if (put_user(0, _frames))
2690 return -EFAULT;
2691 result = snd_pcm_playback_rewind(substream, frames);
2692 __put_user(result, _frames);
2693 return result < 0 ? result : 0;
2694 }
2695 case SNDRV_PCM_IOCTL_FORWARD:
2696 {
2697 snd_pcm_uframes_t frames;
2698 snd_pcm_uframes_t __user *_frames = arg;
2699 snd_pcm_sframes_t result;
2700 if (get_user(frames, _frames))
2701 return -EFAULT;
2702 if (put_user(0, _frames))
2703 return -EFAULT;
2704 result = snd_pcm_playback_forward(substream, frames);
2705 __put_user(result, _frames);
2706 return result < 0 ? result : 0;
2707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002709 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Takashi Iwai0df63e42006-04-28 15:13:41 +02002712static int snd_pcm_capture_ioctl1(struct file *file,
2713 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 unsigned int cmd, void __user *arg)
2715{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002716 if (snd_BUG_ON(!substream))
2717 return -ENXIO;
2718 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2719 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 switch (cmd) {
2721 case SNDRV_PCM_IOCTL_READI_FRAMES:
2722 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002723 struct snd_xferi xferi;
2724 struct snd_xferi __user *_xferi = arg;
2725 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 snd_pcm_sframes_t result;
2727 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2728 return -EBADFD;
2729 if (put_user(0, &_xferi->result))
2730 return -EFAULT;
2731 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2732 return -EFAULT;
2733 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2734 __put_user(result, &_xferi->result);
2735 return result < 0 ? result : 0;
2736 }
2737 case SNDRV_PCM_IOCTL_READN_FRAMES:
2738 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002739 struct snd_xfern xfern;
2740 struct snd_xfern __user *_xfern = arg;
2741 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 void *bufs;
2743 snd_pcm_sframes_t result;
2744 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2745 return -EBADFD;
2746 if (runtime->channels > 128)
2747 return -EINVAL;
2748 if (put_user(0, &_xfern->result))
2749 return -EFAULT;
2750 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2751 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002752
2753 bufs = memdup_user(xfern.bufs,
2754 sizeof(void *) * runtime->channels);
2755 if (IS_ERR(bufs))
2756 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2758 kfree(bufs);
2759 __put_user(result, &_xfern->result);
2760 return result < 0 ? result : 0;
2761 }
2762 case SNDRV_PCM_IOCTL_REWIND:
2763 {
2764 snd_pcm_uframes_t frames;
2765 snd_pcm_uframes_t __user *_frames = arg;
2766 snd_pcm_sframes_t result;
2767 if (get_user(frames, _frames))
2768 return -EFAULT;
2769 if (put_user(0, _frames))
2770 return -EFAULT;
2771 result = snd_pcm_capture_rewind(substream, frames);
2772 __put_user(result, _frames);
2773 return result < 0 ? result : 0;
2774 }
2775 case SNDRV_PCM_IOCTL_FORWARD:
2776 {
2777 snd_pcm_uframes_t frames;
2778 snd_pcm_uframes_t __user *_frames = arg;
2779 snd_pcm_sframes_t result;
2780 if (get_user(frames, _frames))
2781 return -EFAULT;
2782 if (put_user(0, _frames))
2783 return -EFAULT;
2784 result = snd_pcm_capture_forward(substream, frames);
2785 __put_user(result, _frames);
2786 return result < 0 ? result : 0;
2787 }
2788 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002789 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
2791
Takashi Iwai877211f2005-11-17 13:59:38 +01002792static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2793 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
Takashi Iwai877211f2005-11-17 13:59:38 +01002795 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 pcm_file = file->private_data;
2798
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302799 if ((((cmd >> 8) & 0xff) != 'A') && (((cmd >> 8) & 0xff) != 'C'))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return -ENOTTY;
2801
Takashi Iwai0df63e42006-04-28 15:13:41 +02002802 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2803 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
Takashi Iwai877211f2005-11-17 13:59:38 +01002806static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2807 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
Takashi Iwai877211f2005-11-17 13:59:38 +01002809 struct snd_pcm_file *pcm_file;
Joonwoo Parked3bab02013-02-13 15:59:44 -08002810 unsigned char ioctl_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 pcm_file = file->private_data;
Joonwoo Parked3bab02013-02-13 15:59:44 -08002813 ioctl_magic = ((cmd >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Joonwoo Parked3bab02013-02-13 15:59:44 -08002815 if (ioctl_magic != 'A' && ioctl_magic != 'C' && ioctl_magic != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return -ENOTTY;
2817
Takashi Iwai0df63e42006-04-28 15:13:41 +02002818 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2819 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
Takashi Iwai877211f2005-11-17 13:59:38 +01002822int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 unsigned int cmd, void *arg)
2824{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002825 mm_segment_t fs;
2826 int result;
2827
2828 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 switch (substream->stream) {
2830 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002831 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2832 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002833 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002835 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2836 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002837 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002839 result = -EINVAL;
2840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002842 snd_leave_user(fs);
2843 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002846EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2847
Takashi Iwai877211f2005-11-17 13:59:38 +01002848static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2849 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Takashi Iwai877211f2005-11-17 13:59:38 +01002851 struct snd_pcm_file *pcm_file;
2852 struct snd_pcm_substream *substream;
2853 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 snd_pcm_sframes_t result;
2855
2856 pcm_file = file->private_data;
2857 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002858 if (PCM_RUNTIME_CHECK(substream))
2859 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 runtime = substream->runtime;
2861 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2862 return -EBADFD;
2863 if (!frame_aligned(runtime, count))
2864 return -EINVAL;
2865 count = bytes_to_frames(runtime, count);
2866 result = snd_pcm_lib_read(substream, buf, count);
2867 if (result > 0)
2868 result = frames_to_bytes(runtime, result);
2869 return result;
2870}
2871
Takashi Iwai877211f2005-11-17 13:59:38 +01002872static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2873 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
Takashi Iwai877211f2005-11-17 13:59:38 +01002875 struct snd_pcm_file *pcm_file;
2876 struct snd_pcm_substream *substream;
2877 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 snd_pcm_sframes_t result;
2879
2880 pcm_file = file->private_data;
2881 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002882 if (PCM_RUNTIME_CHECK(substream))
2883 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002885 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2886 return -EBADFD;
2887 if (!frame_aligned(runtime, count))
2888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 count = bytes_to_frames(runtime, count);
2890 result = snd_pcm_lib_write(substream, buf, count);
2891 if (result > 0)
2892 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return result;
2894}
2895
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002896static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2897 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
2899{
Takashi Iwai877211f2005-11-17 13:59:38 +01002900 struct snd_pcm_file *pcm_file;
2901 struct snd_pcm_substream *substream;
2902 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 snd_pcm_sframes_t result;
2904 unsigned long i;
2905 void __user **bufs;
2906 snd_pcm_uframes_t frames;
2907
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002908 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002910 if (PCM_RUNTIME_CHECK(substream))
2911 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 runtime = substream->runtime;
2913 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2914 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002915 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002917 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002919 frames = bytes_to_samples(runtime, iov->iov_len);
2920 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 if (bufs == NULL)
2922 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002923 for (i = 0; i < nr_segs; ++i)
2924 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 result = snd_pcm_lib_readv(substream, bufs, frames);
2926 if (result > 0)
2927 result = frames_to_bytes(runtime, result);
2928 kfree(bufs);
2929 return result;
2930}
2931
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002932static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2933 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Takashi Iwai877211f2005-11-17 13:59:38 +01002935 struct snd_pcm_file *pcm_file;
2936 struct snd_pcm_substream *substream;
2937 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 snd_pcm_sframes_t result;
2939 unsigned long i;
2940 void __user **bufs;
2941 snd_pcm_uframes_t frames;
2942
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002943 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002945 if (PCM_RUNTIME_CHECK(substream))
2946 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002948 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2949 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002950 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002951 !frame_aligned(runtime, iov->iov_len))
2952 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002953 frames = bytes_to_samples(runtime, iov->iov_len);
2954 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (bufs == NULL)
2956 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002957 for (i = 0; i < nr_segs; ++i)
2958 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 result = snd_pcm_lib_writev(substream, bufs, frames);
2960 if (result > 0)
2961 result = frames_to_bytes(runtime, result);
2962 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return result;
2964}
2965
2966static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2967{
Takashi Iwai877211f2005-11-17 13:59:38 +01002968 struct snd_pcm_file *pcm_file;
2969 struct snd_pcm_substream *substream;
2970 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 unsigned int mask;
2972 snd_pcm_uframes_t avail;
2973
2974 pcm_file = file->private_data;
2975
2976 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002977 if (PCM_RUNTIME_CHECK(substream))
2978 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 runtime = substream->runtime;
2980
2981 poll_wait(file, &runtime->sleep, wait);
2982
2983 snd_pcm_stream_lock_irq(substream);
2984 avail = snd_pcm_playback_avail(runtime);
2985 switch (runtime->status->state) {
2986 case SNDRV_PCM_STATE_RUNNING:
2987 case SNDRV_PCM_STATE_PREPARED:
2988 case SNDRV_PCM_STATE_PAUSED:
2989 if (avail >= runtime->control->avail_min) {
2990 mask = POLLOUT | POLLWRNORM;
2991 break;
2992 }
2993 /* Fall through */
2994 case SNDRV_PCM_STATE_DRAINING:
2995 mask = 0;
2996 break;
2997 default:
2998 mask = POLLOUT | POLLWRNORM | POLLERR;
2999 break;
3000 }
3001 snd_pcm_stream_unlock_irq(substream);
3002 return mask;
3003}
3004
3005static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3006{
Takashi Iwai877211f2005-11-17 13:59:38 +01003007 struct snd_pcm_file *pcm_file;
3008 struct snd_pcm_substream *substream;
3009 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 unsigned int mask;
3011 snd_pcm_uframes_t avail;
3012
3013 pcm_file = file->private_data;
3014
3015 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003016 if (PCM_RUNTIME_CHECK(substream))
3017 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 runtime = substream->runtime;
3019
3020 poll_wait(file, &runtime->sleep, wait);
3021
3022 snd_pcm_stream_lock_irq(substream);
3023 avail = snd_pcm_capture_avail(runtime);
3024 switch (runtime->status->state) {
3025 case SNDRV_PCM_STATE_RUNNING:
3026 case SNDRV_PCM_STATE_PREPARED:
3027 case SNDRV_PCM_STATE_PAUSED:
3028 if (avail >= runtime->control->avail_min) {
3029 mask = POLLIN | POLLRDNORM;
3030 break;
3031 }
3032 mask = 0;
3033 break;
3034 case SNDRV_PCM_STATE_DRAINING:
3035 if (avail > 0) {
3036 mask = POLLIN | POLLRDNORM;
3037 break;
3038 }
3039 /* Fall through */
3040 default:
3041 mask = POLLIN | POLLRDNORM | POLLERR;
3042 break;
3043 }
3044 snd_pcm_stream_unlock_irq(substream);
3045 return mask;
3046}
3047
3048/*
3049 * mmap support
3050 */
3051
3052/*
3053 * Only on coherent architectures, we can mmap the status and the control records
3054 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3055 */
3056#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3057/*
3058 * mmap status record
3059 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003060static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3061 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Takashi Iwai877211f2005-11-17 13:59:38 +01003063 struct snd_pcm_substream *substream = area->vm_private_data;
3064 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003067 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003069 vmf->page = virt_to_page(runtime->status);
3070 get_page(vmf->page);
3071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003074static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003076 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077};
3078
Takashi Iwai877211f2005-11-17 13:59:38 +01003079static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 struct vm_area_struct *area)
3081{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 long size;
3083 if (!(area->vm_flags & VM_READ))
3084 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003086 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return -EINVAL;
3088 area->vm_ops = &snd_pcm_vm_ops_status;
3089 area->vm_private_data = substream;
3090 area->vm_flags |= VM_RESERVED;
3091 return 0;
3092}
3093
3094/*
3095 * mmap control record
3096 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003097static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3098 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Takashi Iwai877211f2005-11-17 13:59:38 +01003100 struct snd_pcm_substream *substream = area->vm_private_data;
3101 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003104 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003106 vmf->page = virt_to_page(runtime->control);
3107 get_page(vmf->page);
3108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109}
3110
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003111static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003113 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114};
3115
Takashi Iwai877211f2005-11-17 13:59:38 +01003116static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 struct vm_area_struct *area)
3118{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 long size;
3120 if (!(area->vm_flags & VM_READ))
3121 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003123 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return -EINVAL;
3125 area->vm_ops = &snd_pcm_vm_ops_control;
3126 area->vm_private_data = substream;
3127 area->vm_flags |= VM_RESERVED;
3128 return 0;
3129}
3130#else /* ! coherent mmap */
3131/*
3132 * don't support mmap for status and control records.
3133 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003134static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 struct vm_area_struct *area)
3136{
3137 return -ENXIO;
3138}
Takashi Iwai877211f2005-11-17 13:59:38 +01003139static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 struct vm_area_struct *area)
3141{
3142 return -ENXIO;
3143}
3144#endif /* coherent mmap */
3145
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003146static inline struct page *
3147snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3148{
3149 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003150#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3151 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3152 return virt_to_page(CAC_ADDR(vaddr));
3153#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003154#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3155 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3156 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3157 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3158 /* assume dma_handle set via pfn_to_phys() in
3159 * mm/dma-noncoherent.c
3160 */
3161 return pfn_to_page(addr >> PAGE_SHIFT);
3162 }
3163#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003164 return virt_to_page(vaddr);
3165}
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003168 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003170static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3171 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Takashi Iwai877211f2005-11-17 13:59:38 +01003173 struct snd_pcm_substream *substream = area->vm_private_data;
3174 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 unsigned long offset;
3176 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 size_t dma_bytes;
3178
3179 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003180 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003182 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3184 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003185 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003186 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003188 else
3189 page = snd_pcm_default_page_ops(substream, offset);
3190 if (!page)
3191 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003192 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003193 vmf->page = page;
3194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
Takashi Iwai657b1982009-11-26 12:40:21 +01003197static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3198 .open = snd_pcm_mmap_data_open,
3199 .close = snd_pcm_mmap_data_close,
3200};
3201
3202static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 .open = snd_pcm_mmap_data_open,
3204 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003205 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206};
3207
Takashi Iwai657b1982009-11-26 12:40:21 +01003208#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3209/* This should be defined / handled globally! */
3210#ifdef CONFIG_ARM
3211#define ARCH_HAS_DMA_MMAP_COHERENT
3212#endif
3213#endif
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215/*
3216 * mmap the DMA buffer on RAM
3217 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003218int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3219 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003222#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3223 if (!substream->ops->page &&
3224 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3225 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3226 area,
3227 substream->runtime->dma_area,
3228 substream->runtime->dma_addr,
3229 area->vm_end - area->vm_start);
Takashi Iwai9fe17b52010-05-12 10:32:42 +02003230#elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3231 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3232 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3233 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Takashi Iwai657b1982009-11-26 12:40:21 +01003234#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3235 /* mmap with fault handler */
3236 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return 0;
3238}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003239EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
3241/*
3242 * mmap the DMA buffer on I/O memory area
3243 */
3244#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003245int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3246 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
3248 long size;
3249 unsigned long offset;
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 area->vm_flags |= VM_IO;
3253 size = area->vm_end - area->vm_start;
3254 offset = area->vm_pgoff << PAGE_SHIFT;
3255 if (io_remap_pfn_range(area, area->vm_start,
3256 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3257 size, area->vm_page_prot))
3258 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return 0;
3260}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003261
3262EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263#endif /* SNDRV_PCM_INFO_MMAP */
3264
3265/*
3266 * mmap DMA buffer
3267 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003268int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 struct vm_area_struct *area)
3270{
Takashi Iwai877211f2005-11-17 13:59:38 +01003271 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 long size;
3273 unsigned long offset;
3274 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003275 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3278 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3279 return -EINVAL;
3280 } else {
3281 if (!(area->vm_flags & VM_READ))
3282 return -EINVAL;
3283 }
3284 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3286 return -EBADFD;
3287 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3288 return -ENXIO;
3289 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3290 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3291 return -EINVAL;
3292 size = area->vm_end - area->vm_start;
3293 offset = area->vm_pgoff << PAGE_SHIFT;
3294 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3295 if ((size_t)size > dma_bytes)
3296 return -EINVAL;
3297 if (offset > dma_bytes - size)
3298 return -EINVAL;
3299
Takashi Iwai657b1982009-11-26 12:40:21 +01003300 area->vm_ops = &snd_pcm_vm_ops_data;
3301 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003303 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003305 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003306 if (!err)
3307 atomic_inc(&substream->mmap_count);
3308 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003311EXPORT_SYMBOL(snd_pcm_mmap_data);
3312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3314{
Takashi Iwai877211f2005-11-17 13:59:38 +01003315 struct snd_pcm_file * pcm_file;
3316 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 unsigned long offset;
3318
3319 pcm_file = file->private_data;
3320 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003321 if (PCM_RUNTIME_CHECK(substream))
3322 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 offset = area->vm_pgoff << PAGE_SHIFT;
3325 switch (offset) {
3326 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003327 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 return -ENXIO;
3329 return snd_pcm_mmap_status(substream, file, area);
3330 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003331 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 return -ENXIO;
3333 return snd_pcm_mmap_control(substream, file, area);
3334 default:
3335 return snd_pcm_mmap_data(substream, file, area);
3336 }
3337 return 0;
3338}
3339
3340static int snd_pcm_fasync(int fd, struct file * file, int on)
3341{
Takashi Iwai877211f2005-11-17 13:59:38 +01003342 struct snd_pcm_file * pcm_file;
3343 struct snd_pcm_substream *substream;
3344 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
3346 pcm_file = file->private_data;
3347 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003348 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003349 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003351 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
3354/*
3355 * ioctl32 compat
3356 */
3357#ifdef CONFIG_COMPAT
3358#include "pcm_compat.c"
3359#else
3360#define snd_pcm_ioctl_compat NULL
3361#endif
3362
3363/*
3364 * To be removed helpers to keep binary compatibility
3365 */
3366
Takashi Iwai59d48582005-12-01 10:51:58 +01003367#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3369#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3370
Takashi Iwai877211f2005-11-17 13:59:38 +01003371static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3372 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
3374 unsigned int i;
3375
3376 memset(params, 0, sizeof(*params));
3377 params->flags = oparams->flags;
3378 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3379 params->masks[i].bits[0] = oparams->masks[i];
3380 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3381 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3382 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3383 params->info = oparams->info;
3384 params->msbits = oparams->msbits;
3385 params->rate_num = oparams->rate_num;
3386 params->rate_den = oparams->rate_den;
3387 params->fifo_size = oparams->fifo_size;
3388}
3389
Takashi Iwai877211f2005-11-17 13:59:38 +01003390static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3391 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 unsigned int i;
3394
3395 memset(oparams, 0, sizeof(*oparams));
3396 oparams->flags = params->flags;
3397 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3398 oparams->masks[i] = params->masks[i].bits[0];
3399 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3400 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3401 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3402 oparams->info = params->info;
3403 oparams->msbits = params->msbits;
3404 oparams->rate_num = params->rate_num;
3405 oparams->rate_den = params->rate_den;
3406 oparams->fifo_size = params->fifo_size;
3407}
3408
Takashi Iwai877211f2005-11-17 13:59:38 +01003409static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3410 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
Takashi Iwai877211f2005-11-17 13:59:38 +01003412 struct snd_pcm_hw_params *params;
3413 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 int err;
3415
3416 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003417 if (!params)
3418 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Li Zefanef44a1e2009-04-10 09:43:08 +08003420 oparams = memdup_user(_oparams, sizeof(*oparams));
3421 if (IS_ERR(oparams)) {
3422 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 goto out;
3424 }
3425 snd_pcm_hw_convert_from_old_params(params, oparams);
3426 err = snd_pcm_hw_refine(substream, params);
3427 snd_pcm_hw_convert_to_old_params(oparams, params);
3428 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3429 if (!err)
3430 err = -EFAULT;
3431 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003432
3433 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434out:
3435 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return err;
3437}
3438
Takashi Iwai877211f2005-11-17 13:59:38 +01003439static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3440 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
Takashi Iwai877211f2005-11-17 13:59:38 +01003442 struct snd_pcm_hw_params *params;
3443 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 int err;
3445
3446 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003447 if (!params)
3448 return -ENOMEM;
3449
3450 oparams = memdup_user(_oparams, sizeof(*oparams));
3451 if (IS_ERR(oparams)) {
3452 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 goto out;
3454 }
3455 snd_pcm_hw_convert_from_old_params(params, oparams);
3456 err = snd_pcm_hw_params(substream, params);
3457 snd_pcm_hw_convert_to_old_params(oparams, params);
3458 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3459 if (!err)
3460 err = -EFAULT;
3461 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003462
3463 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464out:
3465 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return err;
3467}
Takashi Iwai59d48582005-12-01 10:51:58 +01003468#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Cliff Cai70036092008-09-03 10:54:36 +02003470#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003471static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3472 unsigned long addr,
3473 unsigned long len,
3474 unsigned long pgoff,
3475 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003476{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003477 struct snd_pcm_file *pcm_file = file->private_data;
3478 struct snd_pcm_substream *substream = pcm_file->substream;
3479 struct snd_pcm_runtime *runtime = substream->runtime;
3480 unsigned long offset = pgoff << PAGE_SHIFT;
3481
3482 switch (offset) {
3483 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3484 return (unsigned long)runtime->status;
3485 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3486 return (unsigned long)runtime->control;
3487 default:
3488 return (unsigned long)runtime->dma_area + offset;
3489 }
Cliff Cai70036092008-09-03 10:54:36 +02003490}
3491#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003492# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003493#endif
3494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495/*
3496 * Register section
3497 */
3498
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003499const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003501 .owner = THIS_MODULE,
3502 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003503 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003504 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003505 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003506 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003507 .poll = snd_pcm_playback_poll,
3508 .unlocked_ioctl = snd_pcm_playback_ioctl,
3509 .compat_ioctl = snd_pcm_ioctl_compat,
3510 .mmap = snd_pcm_mmap,
3511 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003512 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 },
3514 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003515 .owner = THIS_MODULE,
3516 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003517 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003518 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003519 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003520 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003521 .poll = snd_pcm_capture_poll,
3522 .unlocked_ioctl = snd_pcm_capture_ioctl,
3523 .compat_ioctl = snd_pcm_ioctl_compat,
3524 .mmap = snd_pcm_mmap,
3525 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003526 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528};