blob: 6dd815d3b239de6ba1ac5f9bb0da0573e3204b0a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/file.h>
23#include <linux/slab.h>
24#include <linux/time.h>
Mark Grossf011e2e2008-02-04 22:30:09 -080025#include <linux/pm_qos_params.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/uio.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010027#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/control.h>
Asish Bhattacharya9048f752011-11-01 20:33:44 +053030#include <sound/snd_compress_params.h>
31#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;
451 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
452 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 snd_pcm_timer_resolution_change(substream);
455 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700456
James Bottomley82f68252010-07-05 22:53:06 +0200457 if (pm_qos_request_active(&substream->latency_pm_qos_req))
458 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700459 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200460 pm_qos_add_request(&substream->latency_pm_qos_req,
461 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
463 _error:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300464 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 so we force application to retry to set
466 the correct hardware parameter settings */
467 runtime->status->state = SNDRV_PCM_STATE_OPEN;
468 if (substream->ops->hw_free != NULL)
469 substream->ops->hw_free(substream);
470 return err;
471}
472
Takashi Iwai877211f2005-11-17 13:59:38 +0100473static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
474 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Takashi Iwai877211f2005-11-17 13:59:38 +0100476 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int err;
478
Li Zefanef44a1e2009-04-10 09:43:08 +0800479 params = memdup_user(_params, sizeof(*params));
480 if (IS_ERR(params))
481 return PTR_ERR(params);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 err = snd_pcm_hw_params(substream, params);
484 if (copy_to_user(_params, params, sizeof(*params))) {
485 if (!err)
486 err = -EFAULT;
487 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 kfree(params);
490 return err;
491}
492
Takashi Iwai877211f2005-11-17 13:59:38 +0100493static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Takashi Iwai877211f2005-11-17 13:59:38 +0100495 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int result = 0;
497
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200498 if (PCM_RUNTIME_CHECK(substream))
499 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 snd_pcm_stream_lock_irq(substream);
502 switch (runtime->status->state) {
503 case SNDRV_PCM_STATE_SETUP:
504 case SNDRV_PCM_STATE_PREPARED:
505 break;
506 default:
507 snd_pcm_stream_unlock_irq(substream);
508 return -EBADFD;
509 }
510 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200511 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EBADFD;
513 if (substream->ops->hw_free)
514 result = substream->ops->hw_free(substream);
515 runtime->status->state = SNDRV_PCM_STATE_OPEN;
James Bottomley82f68252010-07-05 22:53:06 +0200516 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return result;
518}
519
Takashi Iwai877211f2005-11-17 13:59:38 +0100520static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
521 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Takashi Iwai877211f2005-11-17 13:59:38 +0100523 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100524 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200526 if (PCM_RUNTIME_CHECK(substream))
527 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 snd_pcm_stream_lock_irq(substream);
530 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
531 snd_pcm_stream_unlock_irq(substream);
532 return -EBADFD;
533 }
534 snd_pcm_stream_unlock_irq(substream);
535
536 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
537 return -EINVAL;
538 if (params->avail_min == 0)
539 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (params->silence_size >= runtime->boundary) {
541 if (params->silence_threshold != 0)
542 return -EINVAL;
543 } else {
544 if (params->silence_size > params->silence_threshold)
545 return -EINVAL;
546 if (params->silence_threshold > runtime->buffer_size)
547 return -EINVAL;
548 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100549 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 snd_pcm_stream_lock_irq(substream);
551 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 runtime->period_step = params->period_step;
553 runtime->control->avail_min = params->avail_min;
554 runtime->start_threshold = params->start_threshold;
555 runtime->stop_threshold = params->stop_threshold;
556 runtime->silence_threshold = params->silence_threshold;
557 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 params->boundary = runtime->boundary;
559 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
561 runtime->silence_size > 0)
562 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100563 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Takashi Iwai877211f2005-11-17 13:59:38 +0100569static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
570 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwai877211f2005-11-17 13:59:38 +0100572 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int err;
574 if (copy_from_user(&params, _params, sizeof(params)))
575 return -EFAULT;
576 err = snd_pcm_sw_params(substream, &params);
577 if (copy_to_user(_params, &params, sizeof(params)))
578 return -EFAULT;
579 return err;
580}
581
Takashi Iwai877211f2005-11-17 13:59:38 +0100582int snd_pcm_status(struct snd_pcm_substream *substream,
583 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Takashi Iwai877211f2005-11-17 13:59:38 +0100585 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 snd_pcm_stream_lock_irq(substream);
588 status->state = runtime->status->state;
589 status->suspended_state = runtime->status->suspended_state;
590 if (status->state == SNDRV_PCM_STATE_OPEN)
591 goto _end;
592 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100593 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100595 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
596 status->tstamp = runtime->status->tstamp;
597 goto _tstamp_end;
598 }
599 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100600 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100601 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 status->appl_ptr = runtime->control->appl_ptr;
603 status->hw_ptr = runtime->status->hw_ptr;
604 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
605 status->avail = snd_pcm_playback_avail(runtime);
606 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200607 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200609 status->delay += runtime->delay;
610 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 status->delay = 0;
612 } else {
613 status->avail = snd_pcm_capture_avail(runtime);
614 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200615 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 else
617 status->delay = 0;
618 }
619 status->avail_max = runtime->avail_max;
620 status->overrange = runtime->overrange;
621 runtime->avail_max = 0;
622 runtime->overrange = 0;
623 _end:
624 snd_pcm_stream_unlock_irq(substream);
625 return 0;
626}
627
Takashi Iwai877211f2005-11-17 13:59:38 +0100628static int snd_pcm_status_user(struct snd_pcm_substream *substream,
629 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Takashi Iwai877211f2005-11-17 13:59:38 +0100631 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int res;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 memset(&status, 0, sizeof(status));
635 res = snd_pcm_status(substream, &status);
636 if (res < 0)
637 return res;
638 if (copy_to_user(_status, &status, sizeof(status)))
639 return -EFAULT;
640 return 0;
641}
642
Takashi Iwai877211f2005-11-17 13:59:38 +0100643static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
644 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Takashi Iwai877211f2005-11-17 13:59:38 +0100646 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned int channel;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 channel = info->channel;
650 runtime = substream->runtime;
651 snd_pcm_stream_lock_irq(substream);
652 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
653 snd_pcm_stream_unlock_irq(substream);
654 return -EBADFD;
655 }
656 snd_pcm_stream_unlock_irq(substream);
657 if (channel >= runtime->channels)
658 return -EINVAL;
659 memset(info, 0, sizeof(*info));
660 info->channel = channel;
661 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
662}
663
Takashi Iwai877211f2005-11-17 13:59:38 +0100664static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
665 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai877211f2005-11-17 13:59:38 +0100667 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int res;
669
670 if (copy_from_user(&info, _info, sizeof(info)))
671 return -EFAULT;
672 res = snd_pcm_channel_info(substream, &info);
673 if (res < 0)
674 return res;
675 if (copy_to_user(_info, &info, sizeof(info)))
676 return -EFAULT;
677 return 0;
678}
679
Takashi Iwai877211f2005-11-17 13:59:38 +0100680static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Takashi Iwai877211f2005-11-17 13:59:38 +0100682 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (runtime->trigger_master == NULL)
684 return;
685 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100686 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 } else {
688 snd_pcm_trigger_tstamp(runtime->trigger_master);
689 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
690 }
691 runtime->trigger_master = NULL;
692}
693
694struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100695 int (*pre_action)(struct snd_pcm_substream *substream, int state);
696 int (*do_action)(struct snd_pcm_substream *substream, int state);
697 void (*undo_action)(struct snd_pcm_substream *substream, int state);
698 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
701/*
702 * this functions is core for handling of linked stream
703 * Note: the stream state might be changed also on failure
704 * Note2: call with calling stream lock + link lock
705 */
706static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100707 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int state, int do_lock)
709{
Takashi Iwai877211f2005-11-17 13:59:38 +0100710 struct snd_pcm_substream *s = NULL;
711 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int res = 0;
713
Takashi Iwaief991b92007-02-22 12:52:53 +0100714 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200716 spin_lock_nested(&s->self_group.lock,
717 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 res = ops->pre_action(s, state);
719 if (res < 0)
720 goto _unlock;
721 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100722 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 res = ops->do_action(s, state);
724 if (res < 0) {
725 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100726 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (s1 == s) /* failed stream */
728 break;
729 ops->undo_action(s1, state);
730 }
731 }
732 s = NULL; /* unlock all */
733 goto _unlock;
734 }
735 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100736 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ops->post_action(s, state);
738 }
739 _unlock:
740 if (do_lock) {
741 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100742 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (s1 != substream)
744 spin_unlock(&s1->self_group.lock);
745 if (s1 == s) /* end */
746 break;
747 }
748 }
749 return res;
750}
751
752/*
753 * Note: call with stream lock
754 */
755static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100756 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int state)
758{
759 int res;
760
761 res = ops->pre_action(substream, state);
762 if (res < 0)
763 return res;
764 res = ops->do_action(substream, state);
765 if (res == 0)
766 ops->post_action(substream, state);
767 else if (ops->undo_action)
768 ops->undo_action(substream, state);
769 return res;
770}
771
772/*
773 * Note: call with stream lock
774 */
775static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100776 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int state)
778{
779 int res;
780
781 if (snd_pcm_stream_linked(substream)) {
782 if (!spin_trylock(&substream->group->lock)) {
783 spin_unlock(&substream->self_group.lock);
784 spin_lock(&substream->group->lock);
785 spin_lock(&substream->self_group.lock);
786 }
787 res = snd_pcm_action_group(ops, substream, state, 1);
788 spin_unlock(&substream->group->lock);
789 } else {
790 res = snd_pcm_action_single(ops, substream, state);
791 }
792 return res;
793}
794
795/*
796 * Note: don't use any locks before
797 */
798static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100799 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int state)
801{
802 int res;
803
804 read_lock_irq(&snd_pcm_link_rwlock);
805 if (snd_pcm_stream_linked(substream)) {
806 spin_lock(&substream->group->lock);
807 spin_lock(&substream->self_group.lock);
808 res = snd_pcm_action_group(ops, substream, state, 1);
809 spin_unlock(&substream->self_group.lock);
810 spin_unlock(&substream->group->lock);
811 } else {
812 spin_lock(&substream->self_group.lock);
813 res = snd_pcm_action_single(ops, substream, state);
814 spin_unlock(&substream->self_group.lock);
815 }
816 read_unlock_irq(&snd_pcm_link_rwlock);
817 return res;
818}
819
820/*
821 */
822static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100823 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int state)
825{
826 int res;
827
828 down_read(&snd_pcm_link_rwsem);
829 if (snd_pcm_stream_linked(substream))
830 res = snd_pcm_action_group(ops, substream, state, 0);
831 else
832 res = snd_pcm_action_single(ops, substream, state);
833 up_read(&snd_pcm_link_rwsem);
834 return res;
835}
836
837/*
838 * start callbacks
839 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100840static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Takashi Iwai877211f2005-11-17 13:59:38 +0100842 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
844 return -EBADFD;
845 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Liam Girdwoodad2581e2011-02-01 21:31:31 +0000846 !substream->hw_no_buffer &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 !snd_pcm_playback_data(substream))
848 return -EPIPE;
849 runtime->trigger_master = substream;
850 return 0;
851}
852
Takashi Iwai877211f2005-11-17 13:59:38 +0100853static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 if (substream->runtime->trigger_master != substream)
856 return 0;
857 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
858}
859
Takashi Iwai877211f2005-11-17 13:59:38 +0100860static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 if (substream->runtime->trigger_master == substream)
863 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
864}
865
Takashi Iwai877211f2005-11-17 13:59:38 +0100866static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Takashi Iwai877211f2005-11-17 13:59:38 +0100868 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200870 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200871 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
872 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 runtime->status->state = state;
874 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
875 runtime->silence_size > 0)
876 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100878 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
879 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882static struct action_ops snd_pcm_action_start = {
883 .pre_action = snd_pcm_pre_start,
884 .do_action = snd_pcm_do_start,
885 .undo_action = snd_pcm_undo_start,
886 .post_action = snd_pcm_post_start
887};
888
889/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700890 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200891 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100893int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwai877211f2005-11-17 13:59:38 +0100895 return snd_pcm_action(&snd_pcm_action_start, substream,
896 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899/*
900 * stop callbacks
901 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100902static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwai877211f2005-11-17 13:59:38 +0100904 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
906 return -EBADFD;
907 runtime->trigger_master = substream;
908 return 0;
909}
910
Takashi Iwai877211f2005-11-17 13:59:38 +0100911static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 if (substream->runtime->trigger_master == substream &&
914 snd_pcm_running(substream))
915 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
916 return 0; /* unconditonally stop all substreams */
917}
918
Takashi Iwai877211f2005-11-17 13:59:38 +0100919static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Takashi Iwai877211f2005-11-17 13:59:38 +0100921 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (runtime->status->state != state) {
923 snd_pcm_trigger_tstamp(substream);
924 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100925 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
926 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100930 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933static struct action_ops snd_pcm_action_stop = {
934 .pre_action = snd_pcm_pre_stop,
935 .do_action = snd_pcm_do_stop,
936 .post_action = snd_pcm_post_stop
937};
938
939/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700940 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200941 * @substream: the PCM substream instance
942 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700944 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
Clemens Ladischfea952e2011-02-14 11:00:47 +0100946int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
949}
950
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200951EXPORT_SYMBOL(snd_pcm_stop);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700954 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200955 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700957 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * Unlike snd_pcm_stop(), this affects only the given stream.
959 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100960int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Takashi Iwai877211f2005-11-17 13:59:38 +0100962 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
963 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/*
967 * pause callbacks
968 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100969static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Takashi Iwai877211f2005-11-17 13:59:38 +0100971 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
973 return -ENOSYS;
974 if (push) {
975 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
976 return -EBADFD;
977 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
978 return -EBADFD;
979 runtime->trigger_master = substream;
980 return 0;
981}
982
Takashi Iwai877211f2005-11-17 13:59:38 +0100983static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 if (substream->runtime->trigger_master != substream)
986 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200987 /* some drivers might use hw_ptr to recover from the pause -
988 update the hw_ptr now */
989 if (push)
990 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200991 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400992 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200993 * delta, effectively to skip the check once.
994 */
995 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return substream->ops->trigger(substream,
997 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
998 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
999}
1000
Takashi Iwai877211f2005-11-17 13:59:38 +01001001static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 if (substream->runtime->trigger_master == substream)
1004 substream->ops->trigger(substream,
1005 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1006 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1007}
1008
Takashi Iwai877211f2005-11-17 13:59:38 +01001009static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Takashi Iwai877211f2005-11-17 13:59:38 +01001011 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 snd_pcm_trigger_tstamp(substream);
1013 if (push) {
1014 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1015 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001016 snd_timer_notify(substream->timer,
1017 SNDRV_TIMER_EVENT_MPAUSE,
1018 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001020 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 } else {
1022 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001024 snd_timer_notify(substream->timer,
1025 SNDRV_TIMER_EVENT_MCONTINUE,
1026 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028}
1029
1030static struct action_ops snd_pcm_action_pause = {
1031 .pre_action = snd_pcm_pre_pause,
1032 .do_action = snd_pcm_do_pause,
1033 .undo_action = snd_pcm_undo_pause,
1034 .post_action = snd_pcm_post_pause
1035};
1036
1037/*
1038 * Push/release the pause for all linked streams.
1039 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001040static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1043}
1044
1045#ifdef CONFIG_PM
1046/* suspend */
1047
Takashi Iwai877211f2005-11-17 13:59:38 +01001048static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai877211f2005-11-17 13:59:38 +01001050 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1052 return -EBUSY;
1053 runtime->trigger_master = substream;
1054 return 0;
1055}
1056
Takashi Iwai877211f2005-11-17 13:59:38 +01001057static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Takashi Iwai877211f2005-11-17 13:59:38 +01001059 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (runtime->trigger_master != substream)
1061 return 0;
1062 if (! snd_pcm_running(substream))
1063 return 0;
1064 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1065 return 0; /* suspend unconditionally */
1066}
1067
Takashi Iwai877211f2005-11-17 13:59:38 +01001068static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwai877211f2005-11-17 13:59:38 +01001070 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 snd_pcm_trigger_tstamp(substream);
1072 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001073 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1074 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 runtime->status->suspended_state = runtime->status->state;
1076 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001078 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081static struct action_ops snd_pcm_action_suspend = {
1082 .pre_action = snd_pcm_pre_suspend,
1083 .do_action = snd_pcm_do_suspend,
1084 .post_action = snd_pcm_post_suspend
1085};
1086
1087/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001088 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001089 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * After this call, all streams are changed to SUSPENDED state.
1092 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001093int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 int err;
1096 unsigned long flags;
1097
Takashi Iwai603bf522005-11-17 15:59:14 +01001098 if (! substream)
1099 return 0;
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 snd_pcm_stream_lock_irqsave(substream, flags);
1102 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1103 snd_pcm_stream_unlock_irqrestore(substream, flags);
1104 return err;
1105}
1106
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001107EXPORT_SYMBOL(snd_pcm_suspend);
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001110 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001111 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 * After this call, all streams are changed to SUSPENDED state.
1114 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001115int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Takashi Iwai877211f2005-11-17 13:59:38 +01001117 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int stream, err = 0;
1119
Takashi Iwai603bf522005-11-17 15:59:14 +01001120 if (! pcm)
1121 return 0;
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001124 for (substream = pcm->streams[stream].substream;
1125 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* FIXME: the open/close code should lock this as well */
1127 if (substream->runtime == NULL)
1128 continue;
1129 err = snd_pcm_suspend(substream);
1130 if (err < 0 && err != -EBUSY)
1131 return err;
1132 }
1133 }
1134 return 0;
1135}
1136
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001137EXPORT_SYMBOL(snd_pcm_suspend_all);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/* resume */
1140
Takashi Iwai877211f2005-11-17 13:59:38 +01001141static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Takashi Iwai877211f2005-11-17 13:59:38 +01001143 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1145 return -ENOSYS;
1146 runtime->trigger_master = substream;
1147 return 0;
1148}
1149
Takashi Iwai877211f2005-11-17 13:59:38 +01001150static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Takashi Iwai877211f2005-11-17 13:59:38 +01001152 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (runtime->trigger_master != substream)
1154 return 0;
1155 /* DMA not running previously? */
1156 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1157 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1158 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1159 return 0;
1160 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1161}
1162
Takashi Iwai877211f2005-11-17 13:59:38 +01001163static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 if (substream->runtime->trigger_master == substream &&
1166 snd_pcm_running(substream))
1167 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1168}
1169
Takashi Iwai877211f2005-11-17 13:59:38 +01001170static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Takashi Iwai877211f2005-11-17 13:59:38 +01001172 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 snd_pcm_trigger_tstamp(substream);
1174 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001175 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1176 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180static struct action_ops snd_pcm_action_resume = {
1181 .pre_action = snd_pcm_pre_resume,
1182 .do_action = snd_pcm_do_resume,
1183 .undo_action = snd_pcm_undo_resume,
1184 .post_action = snd_pcm_post_resume
1185};
1186
Takashi Iwai877211f2005-11-17 13:59:38 +01001187static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Takashi Iwai877211f2005-11-17 13:59:38 +01001189 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int res;
1191
1192 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001193 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1195 snd_power_unlock(card);
1196 return res;
1197}
1198
1199#else
1200
Takashi Iwai877211f2005-11-17 13:59:38 +01001201static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 return -ENOSYS;
1204}
1205
1206#endif /* CONFIG_PM */
1207
1208/*
1209 * xrun ioctl
1210 *
1211 * Change the RUNNING stream(s) to XRUN state.
1212 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001213static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwai877211f2005-11-17 13:59:38 +01001215 struct snd_card *card = substream->pcm->card;
1216 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int result;
1218
1219 snd_power_lock(card);
1220 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001221 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (result < 0)
1223 goto _unlock;
1224 }
1225
1226 snd_pcm_stream_lock_irq(substream);
1227 switch (runtime->status->state) {
1228 case SNDRV_PCM_STATE_XRUN:
1229 result = 0; /* already there */
1230 break;
1231 case SNDRV_PCM_STATE_RUNNING:
1232 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1233 break;
1234 default:
1235 result = -EBADFD;
1236 }
1237 snd_pcm_stream_unlock_irq(substream);
1238 _unlock:
1239 snd_power_unlock(card);
1240 return result;
1241}
1242
1243/*
1244 * reset ioctl
1245 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001246static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Takashi Iwai877211f2005-11-17 13:59:38 +01001248 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 switch (runtime->status->state) {
1250 case SNDRV_PCM_STATE_RUNNING:
1251 case SNDRV_PCM_STATE_PREPARED:
1252 case SNDRV_PCM_STATE_PAUSED:
1253 case SNDRV_PCM_STATE_SUSPENDED:
1254 return 0;
1255 default:
1256 return -EBADFD;
1257 }
1258}
1259
Takashi Iwai877211f2005-11-17 13:59:38 +01001260static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Takashi Iwai877211f2005-11-17 13:59:38 +01001262 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1264 if (err < 0)
1265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001267 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1268 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 runtime->silence_start = runtime->status->hw_ptr;
1270 runtime->silence_filled = 0;
1271 return 0;
1272}
1273
Takashi Iwai877211f2005-11-17 13:59:38 +01001274static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Takashi Iwai877211f2005-11-17 13:59:38 +01001276 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 runtime->control->appl_ptr = runtime->status->hw_ptr;
1278 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1279 runtime->silence_size > 0)
1280 snd_pcm_playback_silence(substream, ULONG_MAX);
1281}
1282
1283static struct action_ops snd_pcm_action_reset = {
1284 .pre_action = snd_pcm_pre_reset,
1285 .do_action = snd_pcm_do_reset,
1286 .post_action = snd_pcm_post_reset
1287};
1288
Takashi Iwai877211f2005-11-17 13:59:38 +01001289static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1292}
1293
1294/*
1295 * prepare ioctl
1296 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001297/* we use the second argument for updating f_flags */
1298static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1299 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Takashi Iwai877211f2005-11-17 13:59:38 +01001301 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001302 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1303 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EBADFD;
1305 if (snd_pcm_running(substream))
1306 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001307 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309}
1310
Takashi Iwai877211f2005-11-17 13:59:38 +01001311static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 int err;
1314 err = substream->ops->prepare(substream);
1315 if (err < 0)
1316 return err;
1317 return snd_pcm_do_reset(substream, 0);
1318}
1319
Takashi Iwai877211f2005-11-17 13:59:38 +01001320static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Takashi Iwai877211f2005-11-17 13:59:38 +01001322 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 runtime->control->appl_ptr = runtime->status->hw_ptr;
1324 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1325}
1326
1327static struct action_ops snd_pcm_action_prepare = {
1328 .pre_action = snd_pcm_pre_prepare,
1329 .do_action = snd_pcm_do_prepare,
1330 .post_action = snd_pcm_post_prepare
1331};
1332
1333/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001334 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001335 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001336 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001338static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1339 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001342 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001343 int f_flags;
1344
1345 if (file)
1346 f_flags = file->f_flags;
1347 else
1348 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001351 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001352 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1353 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 snd_power_unlock(card);
1355 return res;
1356}
1357
1358/*
1359 * drain ioctl
1360 */
1361
Takashi Iwai877211f2005-11-17 13:59:38 +01001362static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 substream->runtime->trigger_master = substream;
1365 return 0;
1366}
1367
Takashi Iwai877211f2005-11-17 13:59:38 +01001368static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Takashi Iwai877211f2005-11-17 13:59:38 +01001370 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1372 switch (runtime->status->state) {
1373 case SNDRV_PCM_STATE_PREPARED:
1374 /* start playback stream if possible */
1375 if (! snd_pcm_playback_empty(substream)) {
1376 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1377 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1378 }
1379 break;
1380 case SNDRV_PCM_STATE_RUNNING:
1381 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1382 break;
1383 default:
1384 break;
1385 }
1386 } else {
1387 /* stop running stream */
1388 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001389 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001391 snd_pcm_do_stop(substream, new_state);
1392 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394 }
1395 return 0;
1396}
1397
Takashi Iwai877211f2005-11-17 13:59:38 +01001398static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400}
1401
1402static struct action_ops snd_pcm_action_drain_init = {
1403 .pre_action = snd_pcm_pre_drain_init,
1404 .do_action = snd_pcm_do_drain_init,
1405 .post_action = snd_pcm_post_drain_init
1406};
1407
Takashi Iwai877211f2005-11-17 13:59:38 +01001408static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410/*
1411 * Drain the stream(s).
1412 * When the substream is linked, sync until the draining of all playback streams
1413 * is finished.
1414 * After this call, all streams are supposed to be either SETUP or DRAINING
1415 * (capture only) state.
1416 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001417static int snd_pcm_drain(struct snd_pcm_substream *substream,
1418 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Takashi Iwai877211f2005-11-17 13:59:38 +01001420 struct snd_card *card;
1421 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001422 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001423 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001425 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 card = substream->pcm->card;
1428 runtime = substream->runtime;
1429
1430 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1431 return -EBADFD;
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 snd_power_lock(card);
1434 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001435 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001436 if (result < 0) {
1437 snd_power_unlock(card);
1438 return result;
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001442 if (file) {
1443 if (file->f_flags & O_NONBLOCK)
1444 nonblock = 1;
1445 } else if (substream->f_flags & O_NONBLOCK)
1446 nonblock = 1;
1447
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001448 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001449 snd_pcm_stream_lock_irq(substream);
1450 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001451 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001452 snd_pcm_pause(substream, 0);
1453
1454 /* pre-start/stop - all running streams are changed to DRAINING state */
1455 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001456 if (result < 0)
1457 goto unlock;
1458 /* in non-blocking, we don't wait in ioctl but let caller poll */
1459 if (nonblock) {
1460 result = -EAGAIN;
1461 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 for (;;) {
1465 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001466 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (signal_pending(current)) {
1468 result = -ERESTARTSYS;
1469 break;
1470 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001471 /* find a substream to drain */
1472 to_check = NULL;
1473 snd_pcm_group_for_each_entry(s, substream) {
1474 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1475 continue;
1476 runtime = s->runtime;
1477 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1478 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001479 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001480 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001481 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001482 if (!to_check)
1483 break; /* all drained */
1484 init_waitqueue_entry(&wait, current);
1485 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001487 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 snd_power_unlock(card);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001489 if (runtime->no_period_wakeup)
1490 tout = MAX_SCHEDULE_TIMEOUT;
1491 else {
1492 tout = 10;
1493 if (runtime->rate) {
1494 long t = runtime->period_size * 2 / runtime->rate;
1495 tout = max(t, tout);
1496 }
1497 tout = msecs_to_jiffies(tout * 1000);
1498 }
1499 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001501 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001503 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (tout == 0) {
1505 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1506 result = -ESTRPIPE;
1507 else {
1508 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1509 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1510 result = -EIO;
1511 }
1512 break;
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001515
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001516 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001517 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001518 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 return result;
1522}
1523
Asish Bhattacharya9048f752011-11-01 20:33:44 +05301524static int snd_compressed_ioctl(struct snd_pcm_substream *substream,
1525 unsigned int cmd, void __user *arg)
1526{
1527 struct snd_pcm_runtime *runtime;
1528 int err = 0;
1529
1530 if (PCM_RUNTIME_CHECK(substream))
1531 return -ENXIO;
1532 runtime = substream->runtime;
1533 if (runtime->status->state != SNDRV_PCM_STATE_OPEN)
1534 return -EBADFD;
1535 pr_err("%s called with cmd = %d\n", __func__, cmd);
1536 err = substream->ops->ioctl(substream, cmd, arg);
1537 return err;
1538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/*
1540 * drop ioctl
1541 *
1542 * Immediately put all linked substreams into SETUP state.
1543 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001544static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Takashi Iwai877211f2005-11-17 13:59:38 +01001546 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 int result = 0;
1548
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001549 if (PCM_RUNTIME_CHECK(substream))
1550 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001553 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001554 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1555 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return -EBADFD;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 snd_pcm_stream_lock_irq(substream);
1559 /* resume pause */
1560 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1561 snd_pcm_pause(substream, 0);
1562
1563 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1564 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1565 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return result;
1568}
1569
1570
1571/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572static struct file *snd_pcm_file_fd(int fd)
1573{
1574 struct file *file;
1575 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001576 unsigned int minor;
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 file = fget(fd);
1579 if (!file)
1580 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001581 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (!S_ISCHR(inode->i_mode) ||
1583 imajor(inode) != snd_major) {
1584 fput(file);
1585 return NULL;
1586 }
1587 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001588 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1589 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 fput(file);
1591 return NULL;
1592 }
1593 return file;
1594}
1595
1596/*
1597 * PCM link handling
1598 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001599static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 int res = 0;
1602 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001603 struct snd_pcm_file *pcm_file;
1604 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 file = snd_pcm_file_fd(fd);
1607 if (!file)
1608 return -EBADFD;
1609 pcm_file = file->private_data;
1610 substream1 = pcm_file->substream;
1611 down_write(&snd_pcm_link_rwsem);
1612 write_lock_irq(&snd_pcm_link_rwlock);
1613 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1614 substream->runtime->status->state != substream1->runtime->status->state) {
1615 res = -EBADFD;
1616 goto _end;
1617 }
1618 if (snd_pcm_stream_linked(substream1)) {
1619 res = -EALREADY;
1620 goto _end;
1621 }
1622 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001623 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (substream->group == NULL) {
1625 res = -ENOMEM;
1626 goto _end;
1627 }
1628 spin_lock_init(&substream->group->lock);
1629 INIT_LIST_HEAD(&substream->group->substreams);
1630 list_add_tail(&substream->link_list, &substream->group->substreams);
1631 substream->group->count = 1;
1632 }
1633 list_add_tail(&substream1->link_list, &substream->group->substreams);
1634 substream->group->count++;
1635 substream1->group = substream->group;
1636 _end:
1637 write_unlock_irq(&snd_pcm_link_rwlock);
1638 up_write(&snd_pcm_link_rwsem);
1639 fput(file);
1640 return res;
1641}
1642
Takashi Iwai877211f2005-11-17 13:59:38 +01001643static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 substream->group = &substream->self_group;
1646 INIT_LIST_HEAD(&substream->self_group.substreams);
1647 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1648}
1649
Takashi Iwai877211f2005-11-17 13:59:38 +01001650static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Takashi Iwaief991b92007-02-22 12:52:53 +01001652 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 int res = 0;
1654
1655 down_write(&snd_pcm_link_rwsem);
1656 write_lock_irq(&snd_pcm_link_rwlock);
1657 if (!snd_pcm_stream_linked(substream)) {
1658 res = -EALREADY;
1659 goto _end;
1660 }
1661 list_del(&substream->link_list);
1662 substream->group->count--;
1663 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001664 snd_pcm_group_for_each_entry(s, substream) {
1665 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667 }
1668 kfree(substream->group);
1669 }
1670 relink_to_local(substream);
1671 _end:
1672 write_unlock_irq(&snd_pcm_link_rwlock);
1673 up_write(&snd_pcm_link_rwsem);
1674 return res;
1675}
1676
1677/*
1678 * hw configurator
1679 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001680static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1681 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Takashi Iwai877211f2005-11-17 13:59:38 +01001683 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1685 hw_param_interval_c(params, rule->deps[1]), &t);
1686 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1687}
1688
Takashi Iwai877211f2005-11-17 13:59:38 +01001689static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1690 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Takashi Iwai877211f2005-11-17 13:59:38 +01001692 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1694 hw_param_interval_c(params, rule->deps[1]), &t);
1695 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1696}
1697
Takashi Iwai877211f2005-11-17 13:59:38 +01001698static int snd_pcm_hw_rule_muldivk(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_muldivk(hw_param_interval_c(params, rule->deps[0]),
1703 hw_param_interval_c(params, rule->deps[1]),
1704 (unsigned long) rule->private, &t);
1705 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1706}
1707
Takashi Iwai877211f2005-11-17 13:59:38 +01001708static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1709 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Takashi Iwai877211f2005-11-17 13:59:38 +01001711 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1713 (unsigned long) rule->private,
1714 hw_param_interval_c(params, rule->deps[1]), &t);
1715 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1716}
1717
Takashi Iwai877211f2005-11-17 13:59:38 +01001718static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1719 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001722 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1723 struct snd_mask m;
1724 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 snd_mask_any(&m);
1726 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1727 int bits;
1728 if (! snd_mask_test(mask, k))
1729 continue;
1730 bits = snd_pcm_format_physical_width(k);
1731 if (bits <= 0)
1732 continue; /* ignore invalid formats */
1733 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1734 snd_mask_reset(&m, k);
1735 }
1736 return snd_mask_refine(mask, &m);
1737}
1738
Takashi Iwai877211f2005-11-17 13:59:38 +01001739static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1740 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Takashi Iwai877211f2005-11-17 13:59:38 +01001742 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 unsigned int k;
1744 t.min = UINT_MAX;
1745 t.max = 0;
1746 t.openmin = 0;
1747 t.openmax = 0;
1748 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1749 int bits;
1750 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1751 continue;
1752 bits = snd_pcm_format_physical_width(k);
1753 if (bits <= 0)
1754 continue; /* ignore invalid formats */
1755 if (t.min > (unsigned)bits)
1756 t.min = bits;
1757 if (t.max < (unsigned)bits)
1758 t.max = bits;
1759 }
1760 t.integer = 1;
1761 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1762}
1763
1764#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1765#error "Change this table"
1766#endif
1767
1768static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1769 48000, 64000, 88200, 96000, 176400, 192000 };
1770
Clemens Ladisch7653d552007-08-13 17:38:54 +02001771const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1772 .count = ARRAY_SIZE(rates),
1773 .list = rates,
1774};
1775
Takashi Iwai877211f2005-11-17 13:59:38 +01001776static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1777 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Takashi Iwai877211f2005-11-17 13:59:38 +01001779 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001781 snd_pcm_known_rates.count,
1782 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Takashi Iwai877211f2005-11-17 13:59:38 +01001785static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1786 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Takashi Iwai877211f2005-11-17 13:59:38 +01001788 struct snd_interval t;
1789 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 t.min = 0;
1791 t.max = substream->buffer_bytes_max;
1792 t.openmin = 0;
1793 t.openmax = 0;
1794 t.integer = 1;
1795 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1796}
1797
Takashi Iwai877211f2005-11-17 13:59:38 +01001798int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Takashi Iwai877211f2005-11-17 13:59:38 +01001800 struct snd_pcm_runtime *runtime = substream->runtime;
1801 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 int k, err;
1803
1804 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1805 snd_mask_any(constrs_mask(constrs, k));
1806 }
1807
1808 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1809 snd_interval_any(constrs_interval(constrs, k));
1810 }
1811
1812 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1813 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1814 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1815 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1816 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1817
1818 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1819 snd_pcm_hw_rule_format, NULL,
1820 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1821 if (err < 0)
1822 return err;
1823 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1824 snd_pcm_hw_rule_sample_bits, NULL,
1825 SNDRV_PCM_HW_PARAM_FORMAT,
1826 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1827 if (err < 0)
1828 return err;
1829 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1830 snd_pcm_hw_rule_div, NULL,
1831 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1832 if (err < 0)
1833 return err;
1834 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1835 snd_pcm_hw_rule_mul, NULL,
1836 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1837 if (err < 0)
1838 return err;
1839 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1840 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1841 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1842 if (err < 0)
1843 return err;
1844 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1845 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1846 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1847 if (err < 0)
1848 return err;
1849 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1850 snd_pcm_hw_rule_div, NULL,
1851 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1852 if (err < 0)
1853 return err;
1854 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1855 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1856 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1857 if (err < 0)
1858 return err;
1859 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1860 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1861 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1862 if (err < 0)
1863 return err;
1864 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1865 snd_pcm_hw_rule_div, NULL,
1866 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1867 if (err < 0)
1868 return err;
1869 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1870 snd_pcm_hw_rule_div, NULL,
1871 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1872 if (err < 0)
1873 return err;
1874 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1875 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1876 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1877 if (err < 0)
1878 return err;
1879 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1880 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1881 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1882 if (err < 0)
1883 return err;
1884 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1885 snd_pcm_hw_rule_mul, NULL,
1886 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1887 if (err < 0)
1888 return err;
1889 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1890 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1891 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1892 if (err < 0)
1893 return err;
1894 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1895 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1896 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1897 if (err < 0)
1898 return err;
1899 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1900 snd_pcm_hw_rule_muldivk, (void*) 8,
1901 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1902 if (err < 0)
1903 return err;
1904 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1905 snd_pcm_hw_rule_muldivk, (void*) 8,
1906 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1907 if (err < 0)
1908 return err;
1909 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1910 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1911 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1912 if (err < 0)
1913 return err;
1914 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1915 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1916 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1917 if (err < 0)
1918 return err;
1919 return 0;
1920}
1921
Takashi Iwai877211f2005-11-17 13:59:38 +01001922int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Takashi Iwai877211f2005-11-17 13:59:38 +01001924 struct snd_pcm_runtime *runtime = substream->runtime;
1925 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 int err;
1927 unsigned int mask = 0;
1928
1929 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1930 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1931 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1932 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1933 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1934 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1935 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1936 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1937 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1938 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1939 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1940 }
1941 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001942 if (err < 0)
1943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001946 if (err < 0)
1947 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001950 if (err < 0)
1951 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1954 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001955 if (err < 0)
1956 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1959 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001960 if (err < 0)
1961 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1964 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001965 if (err < 0)
1966 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1969 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001970 if (err < 0)
1971 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1974 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001975 if (err < 0)
1976 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1979 snd_pcm_hw_rule_buffer_bytes_max, substream,
1980 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1981 if (err < 0)
1982 return err;
1983
1984 /* FIXME: remove */
1985 if (runtime->dma_bytes) {
1986 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001987 if (err < 0)
1988 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
1990
1991 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1992 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1993 snd_pcm_hw_rule_rate, hw,
1994 SNDRV_PCM_HW_PARAM_RATE, -1);
1995 if (err < 0)
1996 return err;
1997 }
1998
1999 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2001
2002 return 0;
2003}
2004
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002005static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002008}
2009
2010void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2011{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002012 substream->ref_count--;
2013 if (substream->ref_count > 0)
2014 return;
2015
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002016 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002017 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (substream->ops->hw_free != NULL)
2019 substream->ops->hw_free(substream);
2020 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002021 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002023 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2024 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002025 if (substream->pcm_release) {
2026 substream->pcm_release(substream);
2027 substream->pcm_release = NULL;
2028 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002029 snd_pcm_detach_substream(substream);
2030}
2031
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002032EXPORT_SYMBOL(snd_pcm_release_substream);
2033
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002034int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2035 struct file *file,
2036 struct snd_pcm_substream **rsubstream)
2037{
2038 struct snd_pcm_substream *substream;
2039 int err;
2040
2041 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2042 if (err < 0)
2043 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002044 if (substream->ref_count > 1) {
2045 *rsubstream = substream;
2046 return 0;
2047 }
2048
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002049 err = snd_pcm_hw_constraints_init(substream);
2050 if (err < 0) {
2051 snd_printd("snd_pcm_hw_constraints_init failed\n");
2052 goto error;
2053 }
2054
Liam Girdwood8ccfb4a2011-02-16 16:50:12 +00002055 if (substream->ops == NULL) {
2056 snd_printd("cannot open back end PCMs directly\n");
2057 err = -ENODEV;
2058 goto error;
2059 }
2060
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002061 if ((err = substream->ops->open(substream)) < 0)
2062 goto error;
2063
2064 substream->hw_opened = 1;
2065
2066 err = snd_pcm_hw_constraints_complete(substream);
2067 if (err < 0) {
2068 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2069 goto error;
2070 }
2071
2072 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002074
2075 error:
2076 snd_pcm_release_substream(substream);
2077 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002080EXPORT_SYMBOL(snd_pcm_open_substream);
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002083 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002085 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Takashi Iwai877211f2005-11-17 13:59:38 +01002087 struct snd_pcm_file *pcm_file;
2088 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002089 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002091 if (rpcm_file)
2092 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002094 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2095 if (err < 0)
2096 return err;
2097
Takashi Iwai548a6482006-07-31 16:51:51 +02002098 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2099 if (pcm_file == NULL) {
2100 snd_pcm_release_substream(substream);
2101 return -ENOMEM;
2102 }
2103 pcm_file->substream = substream;
2104 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002105 substream->file = pcm_file;
2106 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002109 if (rpcm_file)
2110 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return 0;
2112}
2113
Clemens Ladischf87135f2005-11-20 14:06:59 +01002114static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Takashi Iwai877211f2005-11-17 13:59:38 +01002116 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002117 int err = nonseekable_open(inode, file);
2118 if (err < 0)
2119 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002120 pcm = snd_lookup_minor_data(iminor(inode),
2121 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2122 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2123}
2124
2125static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2126{
2127 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002128 int err = nonseekable_open(inode, file);
2129 if (err < 0)
2130 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002131 pcm = snd_lookup_minor_data(iminor(inode),
2132 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2133 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2134}
2135
2136static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2137{
2138 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002139 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 wait_queue_t wait;
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (pcm == NULL) {
2143 err = -ENODEV;
2144 goto __error1;
2145 }
2146 err = snd_card_file_add(pcm->card, file);
2147 if (err < 0)
2148 goto __error1;
2149 if (!try_module_get(pcm->card->module)) {
2150 err = -EFAULT;
2151 goto __error2;
2152 }
2153 init_waitqueue_entry(&wait, current);
2154 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002155 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002157 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (err >= 0)
2159 break;
2160 if (err == -EAGAIN) {
2161 if (file->f_flags & O_NONBLOCK) {
2162 err = -EBUSY;
2163 break;
2164 }
2165 } else
2166 break;
2167 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002168 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002170 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (signal_pending(current)) {
2172 err = -ERESTARTSYS;
2173 break;
2174 }
2175 }
2176 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002177 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (err < 0)
2179 goto __error;
2180 return err;
2181
2182 __error:
2183 module_put(pcm->card->module);
2184 __error2:
2185 snd_card_file_remove(pcm->card, file);
2186 __error1:
2187 return err;
2188}
2189
2190static int snd_pcm_release(struct inode *inode, struct file *file)
2191{
Takashi Iwai877211f2005-11-17 13:59:38 +01002192 struct snd_pcm *pcm;
2193 struct snd_pcm_substream *substream;
2194 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 pcm_file = file->private_data;
2197 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002198 if (snd_BUG_ON(!substream))
2199 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002201 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002202 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002203 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002204 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 wake_up(&pcm->open_wait);
2206 module_put(pcm->card->module);
2207 snd_card_file_remove(pcm->card, file);
2208 return 0;
2209}
2210
Takashi Iwai877211f2005-11-17 13:59:38 +01002211static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2212 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Takashi Iwai877211f2005-11-17 13:59:38 +01002214 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 snd_pcm_sframes_t appl_ptr;
2216 snd_pcm_sframes_t ret;
2217 snd_pcm_sframes_t hw_avail;
2218
2219 if (frames == 0)
2220 return 0;
2221
2222 snd_pcm_stream_lock_irq(substream);
2223 switch (runtime->status->state) {
2224 case SNDRV_PCM_STATE_PREPARED:
2225 break;
2226 case SNDRV_PCM_STATE_DRAINING:
2227 case SNDRV_PCM_STATE_RUNNING:
2228 if (snd_pcm_update_hw_ptr(substream) >= 0)
2229 break;
2230 /* Fall through */
2231 case SNDRV_PCM_STATE_XRUN:
2232 ret = -EPIPE;
2233 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002234 case SNDRV_PCM_STATE_SUSPENDED:
2235 ret = -ESTRPIPE;
2236 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 default:
2238 ret = -EBADFD;
2239 goto __end;
2240 }
2241
2242 hw_avail = snd_pcm_playback_hw_avail(runtime);
2243 if (hw_avail <= 0) {
2244 ret = 0;
2245 goto __end;
2246 }
2247 if (frames > (snd_pcm_uframes_t)hw_avail)
2248 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 appl_ptr = runtime->control->appl_ptr - frames;
2250 if (appl_ptr < 0)
2251 appl_ptr += runtime->boundary;
2252 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 ret = frames;
2254 __end:
2255 snd_pcm_stream_unlock_irq(substream);
2256 return ret;
2257}
2258
Takashi Iwai877211f2005-11-17 13:59:38 +01002259static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2260 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Takashi Iwai877211f2005-11-17 13:59:38 +01002262 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 snd_pcm_sframes_t appl_ptr;
2264 snd_pcm_sframes_t ret;
2265 snd_pcm_sframes_t hw_avail;
2266
2267 if (frames == 0)
2268 return 0;
2269
2270 snd_pcm_stream_lock_irq(substream);
2271 switch (runtime->status->state) {
2272 case SNDRV_PCM_STATE_PREPARED:
2273 case SNDRV_PCM_STATE_DRAINING:
2274 break;
2275 case SNDRV_PCM_STATE_RUNNING:
2276 if (snd_pcm_update_hw_ptr(substream) >= 0)
2277 break;
2278 /* Fall through */
2279 case SNDRV_PCM_STATE_XRUN:
2280 ret = -EPIPE;
2281 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002282 case SNDRV_PCM_STATE_SUSPENDED:
2283 ret = -ESTRPIPE;
2284 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 default:
2286 ret = -EBADFD;
2287 goto __end;
2288 }
2289
2290 hw_avail = snd_pcm_capture_hw_avail(runtime);
2291 if (hw_avail <= 0) {
2292 ret = 0;
2293 goto __end;
2294 }
2295 if (frames > (snd_pcm_uframes_t)hw_avail)
2296 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 appl_ptr = runtime->control->appl_ptr - frames;
2298 if (appl_ptr < 0)
2299 appl_ptr += runtime->boundary;
2300 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 ret = frames;
2302 __end:
2303 snd_pcm_stream_unlock_irq(substream);
2304 return ret;
2305}
2306
Takashi Iwai877211f2005-11-17 13:59:38 +01002307static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2308 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Takashi Iwai877211f2005-11-17 13:59:38 +01002310 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 snd_pcm_sframes_t appl_ptr;
2312 snd_pcm_sframes_t ret;
2313 snd_pcm_sframes_t avail;
2314
2315 if (frames == 0)
2316 return 0;
2317
2318 snd_pcm_stream_lock_irq(substream);
2319 switch (runtime->status->state) {
2320 case SNDRV_PCM_STATE_PREPARED:
2321 case SNDRV_PCM_STATE_PAUSED:
2322 break;
2323 case SNDRV_PCM_STATE_DRAINING:
2324 case SNDRV_PCM_STATE_RUNNING:
2325 if (snd_pcm_update_hw_ptr(substream) >= 0)
2326 break;
2327 /* Fall through */
2328 case SNDRV_PCM_STATE_XRUN:
2329 ret = -EPIPE;
2330 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002331 case SNDRV_PCM_STATE_SUSPENDED:
2332 ret = -ESTRPIPE;
2333 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 default:
2335 ret = -EBADFD;
2336 goto __end;
2337 }
2338
2339 avail = snd_pcm_playback_avail(runtime);
2340 if (avail <= 0) {
2341 ret = 0;
2342 goto __end;
2343 }
2344 if (frames > (snd_pcm_uframes_t)avail)
2345 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 appl_ptr = runtime->control->appl_ptr + frames;
2347 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2348 appl_ptr -= runtime->boundary;
2349 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 ret = frames;
2351 __end:
2352 snd_pcm_stream_unlock_irq(substream);
2353 return ret;
2354}
2355
Takashi Iwai877211f2005-11-17 13:59:38 +01002356static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2357 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
Takashi Iwai877211f2005-11-17 13:59:38 +01002359 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 snd_pcm_sframes_t appl_ptr;
2361 snd_pcm_sframes_t ret;
2362 snd_pcm_sframes_t avail;
2363
2364 if (frames == 0)
2365 return 0;
2366
2367 snd_pcm_stream_lock_irq(substream);
2368 switch (runtime->status->state) {
2369 case SNDRV_PCM_STATE_PREPARED:
2370 case SNDRV_PCM_STATE_DRAINING:
2371 case SNDRV_PCM_STATE_PAUSED:
2372 break;
2373 case SNDRV_PCM_STATE_RUNNING:
2374 if (snd_pcm_update_hw_ptr(substream) >= 0)
2375 break;
2376 /* Fall through */
2377 case SNDRV_PCM_STATE_XRUN:
2378 ret = -EPIPE;
2379 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002380 case SNDRV_PCM_STATE_SUSPENDED:
2381 ret = -ESTRPIPE;
2382 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 default:
2384 ret = -EBADFD;
2385 goto __end;
2386 }
2387
2388 avail = snd_pcm_capture_avail(runtime);
2389 if (avail <= 0) {
2390 ret = 0;
2391 goto __end;
2392 }
2393 if (frames > (snd_pcm_uframes_t)avail)
2394 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 appl_ptr = runtime->control->appl_ptr + frames;
2396 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2397 appl_ptr -= runtime->boundary;
2398 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 ret = frames;
2400 __end:
2401 snd_pcm_stream_unlock_irq(substream);
2402 return ret;
2403}
2404
Takashi Iwai877211f2005-11-17 13:59:38 +01002405static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Takashi Iwai877211f2005-11-17 13:59:38 +01002407 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int err;
2409
2410 snd_pcm_stream_lock_irq(substream);
2411 switch (runtime->status->state) {
2412 case SNDRV_PCM_STATE_DRAINING:
2413 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2414 goto __badfd;
2415 case SNDRV_PCM_STATE_RUNNING:
2416 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2417 break;
2418 /* Fall through */
2419 case SNDRV_PCM_STATE_PREPARED:
2420 case SNDRV_PCM_STATE_SUSPENDED:
2421 err = 0;
2422 break;
2423 case SNDRV_PCM_STATE_XRUN:
2424 err = -EPIPE;
2425 break;
2426 default:
2427 __badfd:
2428 err = -EBADFD;
2429 break;
2430 }
2431 snd_pcm_stream_unlock_irq(substream);
2432 return err;
2433}
2434
Takashi Iwai877211f2005-11-17 13:59:38 +01002435static int snd_pcm_delay(struct snd_pcm_substream *substream,
2436 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Takashi Iwai877211f2005-11-17 13:59:38 +01002438 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 int err;
2440 snd_pcm_sframes_t n = 0;
2441
2442 snd_pcm_stream_lock_irq(substream);
2443 switch (runtime->status->state) {
2444 case SNDRV_PCM_STATE_DRAINING:
2445 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2446 goto __badfd;
2447 case SNDRV_PCM_STATE_RUNNING:
2448 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2449 break;
2450 /* Fall through */
2451 case SNDRV_PCM_STATE_PREPARED:
2452 case SNDRV_PCM_STATE_SUSPENDED:
2453 err = 0;
2454 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2455 n = snd_pcm_playback_hw_avail(runtime);
2456 else
2457 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002458 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 break;
2460 case SNDRV_PCM_STATE_XRUN:
2461 err = -EPIPE;
2462 break;
2463 default:
2464 __badfd:
2465 err = -EBADFD;
2466 break;
2467 }
2468 snd_pcm_stream_unlock_irq(substream);
2469 if (!err)
2470 if (put_user(n, res))
2471 err = -EFAULT;
2472 return err;
2473}
2474
Takashi Iwai877211f2005-11-17 13:59:38 +01002475static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2476 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Takashi Iwai877211f2005-11-17 13:59:38 +01002478 struct snd_pcm_runtime *runtime = substream->runtime;
2479 struct snd_pcm_sync_ptr sync_ptr;
2480 volatile struct snd_pcm_mmap_status *status;
2481 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 int err;
2483
2484 memset(&sync_ptr, 0, sizeof(sync_ptr));
2485 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2486 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002487 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 -07002488 return -EFAULT;
2489 status = runtime->status;
2490 control = runtime->control;
2491 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2492 err = snd_pcm_hwsync(substream);
2493 if (err < 0)
2494 return err;
2495 }
2496 snd_pcm_stream_lock_irq(substream);
2497 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2498 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2499 else
2500 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2501 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2502 control->avail_min = sync_ptr.c.control.avail_min;
2503 else
2504 sync_ptr.c.control.avail_min = control->avail_min;
2505 sync_ptr.s.status.state = status->state;
2506 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2507 sync_ptr.s.status.tstamp = status->tstamp;
2508 sync_ptr.s.status.suspended_state = status->suspended_state;
2509 snd_pcm_stream_unlock_irq(substream);
2510 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2511 return -EFAULT;
2512 return 0;
2513}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002514
2515static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2516{
2517 struct snd_pcm_runtime *runtime = substream->runtime;
2518 int arg;
2519
2520 if (get_user(arg, _arg))
2521 return -EFAULT;
2522 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2523 return -EINVAL;
2524 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2525 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2526 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2527 return 0;
2528}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Takashi Iwai0df63e42006-04-28 15:13:41 +02002530static int snd_pcm_common_ioctl1(struct file *file,
2531 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 unsigned int cmd, void __user *arg)
2533{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 switch (cmd) {
2535 case SNDRV_PCM_IOCTL_PVERSION:
2536 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2537 case SNDRV_PCM_IOCTL_INFO:
2538 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002539 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2540 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002541 case SNDRV_PCM_IOCTL_TTSTAMP:
2542 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 case SNDRV_PCM_IOCTL_HW_REFINE:
2544 return snd_pcm_hw_refine_user(substream, arg);
2545 case SNDRV_PCM_IOCTL_HW_PARAMS:
2546 return snd_pcm_hw_params_user(substream, arg);
2547 case SNDRV_PCM_IOCTL_HW_FREE:
2548 return snd_pcm_hw_free(substream);
2549 case SNDRV_PCM_IOCTL_SW_PARAMS:
2550 return snd_pcm_sw_params_user(substream, arg);
2551 case SNDRV_PCM_IOCTL_STATUS:
2552 return snd_pcm_status_user(substream, arg);
2553 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2554 return snd_pcm_channel_info_user(substream, arg);
2555 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002556 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 case SNDRV_PCM_IOCTL_RESET:
2558 return snd_pcm_reset(substream);
2559 case SNDRV_PCM_IOCTL_START:
2560 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2561 case SNDRV_PCM_IOCTL_LINK:
2562 return snd_pcm_link(substream, (int)(unsigned long) arg);
2563 case SNDRV_PCM_IOCTL_UNLINK:
2564 return snd_pcm_unlink(substream);
2565 case SNDRV_PCM_IOCTL_RESUME:
2566 return snd_pcm_resume(substream);
2567 case SNDRV_PCM_IOCTL_XRUN:
2568 return snd_pcm_xrun(substream);
2569 case SNDRV_PCM_IOCTL_HWSYNC:
2570 return snd_pcm_hwsync(substream);
2571 case SNDRV_PCM_IOCTL_DELAY:
2572 return snd_pcm_delay(substream, arg);
2573 case SNDRV_PCM_IOCTL_SYNC_PTR:
2574 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002575#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2577 return snd_pcm_hw_refine_old_user(substream, arg);
2578 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2579 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002580#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002582 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 case SNDRV_PCM_IOCTL_DROP:
2584 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002585 case SNDRV_PCM_IOCTL_PAUSE:
2586 {
2587 int res;
2588 snd_pcm_stream_lock_irq(substream);
2589 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2590 snd_pcm_stream_unlock_irq(substream);
2591 return res;
2592 }
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302593 case SNDRV_COMPRESS_GET_CAPS:
2594 case SNDRV_COMPRESS_GET_CODEC_CAPS:
2595 case SNDRV_COMPRESS_SET_PARAMS:
2596 case SNDRV_COMPRESS_GET_PARAMS:
2597 case SNDRV_COMPRESS_TSTAMP:
2598 return snd_compressed_ioctl(substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 }
2600 snd_printd("unknown ioctl = 0x%x\n", cmd);
2601 return -ENOTTY;
2602}
2603
Takashi Iwai0df63e42006-04-28 15:13:41 +02002604static int snd_pcm_playback_ioctl1(struct file *file,
2605 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 unsigned int cmd, void __user *arg)
2607{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002608 if (snd_BUG_ON(!substream))
2609 return -ENXIO;
2610 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2611 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 switch (cmd) {
2613 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2614 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002615 struct snd_xferi xferi;
2616 struct snd_xferi __user *_xferi = arg;
2617 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 snd_pcm_sframes_t result;
2619 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2620 return -EBADFD;
2621 if (put_user(0, &_xferi->result))
2622 return -EFAULT;
2623 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2624 return -EFAULT;
2625 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2626 __put_user(result, &_xferi->result);
2627 return result < 0 ? result : 0;
2628 }
2629 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2630 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002631 struct snd_xfern xfern;
2632 struct snd_xfern __user *_xfern = arg;
2633 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 void __user **bufs;
2635 snd_pcm_sframes_t result;
2636 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2637 return -EBADFD;
2638 if (runtime->channels > 128)
2639 return -EINVAL;
2640 if (put_user(0, &_xfern->result))
2641 return -EFAULT;
2642 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2643 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002644
2645 bufs = memdup_user(xfern.bufs,
2646 sizeof(void *) * runtime->channels);
2647 if (IS_ERR(bufs))
2648 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2650 kfree(bufs);
2651 __put_user(result, &_xfern->result);
2652 return result < 0 ? result : 0;
2653 }
2654 case SNDRV_PCM_IOCTL_REWIND:
2655 {
2656 snd_pcm_uframes_t frames;
2657 snd_pcm_uframes_t __user *_frames = arg;
2658 snd_pcm_sframes_t result;
2659 if (get_user(frames, _frames))
2660 return -EFAULT;
2661 if (put_user(0, _frames))
2662 return -EFAULT;
2663 result = snd_pcm_playback_rewind(substream, frames);
2664 __put_user(result, _frames);
2665 return result < 0 ? result : 0;
2666 }
2667 case SNDRV_PCM_IOCTL_FORWARD:
2668 {
2669 snd_pcm_uframes_t frames;
2670 snd_pcm_uframes_t __user *_frames = arg;
2671 snd_pcm_sframes_t result;
2672 if (get_user(frames, _frames))
2673 return -EFAULT;
2674 if (put_user(0, _frames))
2675 return -EFAULT;
2676 result = snd_pcm_playback_forward(substream, frames);
2677 __put_user(result, _frames);
2678 return result < 0 ? result : 0;
2679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002681 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683
Takashi Iwai0df63e42006-04-28 15:13:41 +02002684static int snd_pcm_capture_ioctl1(struct file *file,
2685 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 unsigned int cmd, void __user *arg)
2687{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002688 if (snd_BUG_ON(!substream))
2689 return -ENXIO;
2690 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2691 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 switch (cmd) {
2693 case SNDRV_PCM_IOCTL_READI_FRAMES:
2694 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002695 struct snd_xferi xferi;
2696 struct snd_xferi __user *_xferi = arg;
2697 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 snd_pcm_sframes_t result;
2699 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2700 return -EBADFD;
2701 if (put_user(0, &_xferi->result))
2702 return -EFAULT;
2703 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2704 return -EFAULT;
2705 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2706 __put_user(result, &_xferi->result);
2707 return result < 0 ? result : 0;
2708 }
2709 case SNDRV_PCM_IOCTL_READN_FRAMES:
2710 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002711 struct snd_xfern xfern;
2712 struct snd_xfern __user *_xfern = arg;
2713 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 void *bufs;
2715 snd_pcm_sframes_t result;
2716 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2717 return -EBADFD;
2718 if (runtime->channels > 128)
2719 return -EINVAL;
2720 if (put_user(0, &_xfern->result))
2721 return -EFAULT;
2722 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2723 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002724
2725 bufs = memdup_user(xfern.bufs,
2726 sizeof(void *) * runtime->channels);
2727 if (IS_ERR(bufs))
2728 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2730 kfree(bufs);
2731 __put_user(result, &_xfern->result);
2732 return result < 0 ? result : 0;
2733 }
2734 case SNDRV_PCM_IOCTL_REWIND:
2735 {
2736 snd_pcm_uframes_t frames;
2737 snd_pcm_uframes_t __user *_frames = arg;
2738 snd_pcm_sframes_t result;
2739 if (get_user(frames, _frames))
2740 return -EFAULT;
2741 if (put_user(0, _frames))
2742 return -EFAULT;
2743 result = snd_pcm_capture_rewind(substream, frames);
2744 __put_user(result, _frames);
2745 return result < 0 ? result : 0;
2746 }
2747 case SNDRV_PCM_IOCTL_FORWARD:
2748 {
2749 snd_pcm_uframes_t frames;
2750 snd_pcm_uframes_t __user *_frames = arg;
2751 snd_pcm_sframes_t result;
2752 if (get_user(frames, _frames))
2753 return -EFAULT;
2754 if (put_user(0, _frames))
2755 return -EFAULT;
2756 result = snd_pcm_capture_forward(substream, frames);
2757 __put_user(result, _frames);
2758 return result < 0 ? result : 0;
2759 }
2760 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002761 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
Takashi Iwai877211f2005-11-17 13:59:38 +01002764static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2765 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Takashi Iwai877211f2005-11-17 13:59:38 +01002767 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 pcm_file = file->private_data;
2770
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302771 if ((((cmd >> 8) & 0xff) != 'A') && (((cmd >> 8) & 0xff) != 'C'))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return -ENOTTY;
2773
Takashi Iwai0df63e42006-04-28 15:13:41 +02002774 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2775 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
Takashi Iwai877211f2005-11-17 13:59:38 +01002778static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2779 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
Takashi Iwai877211f2005-11-17 13:59:38 +01002781 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 pcm_file = file->private_data;
2784
2785 if (((cmd >> 8) & 0xff) != 'A')
2786 return -ENOTTY;
2787
Takashi Iwai0df63e42006-04-28 15:13:41 +02002788 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2789 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
2791
Takashi Iwai877211f2005-11-17 13:59:38 +01002792int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 unsigned int cmd, void *arg)
2794{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002795 mm_segment_t fs;
2796 int result;
2797
2798 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 switch (substream->stream) {
2800 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002801 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2802 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002805 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2806 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002809 result = -EINVAL;
2810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002812 snd_leave_user(fs);
2813 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002816EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2817
Takashi Iwai877211f2005-11-17 13:59:38 +01002818static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2819 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Takashi Iwai877211f2005-11-17 13:59:38 +01002821 struct snd_pcm_file *pcm_file;
2822 struct snd_pcm_substream *substream;
2823 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 snd_pcm_sframes_t result;
2825
2826 pcm_file = file->private_data;
2827 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002828 if (PCM_RUNTIME_CHECK(substream))
2829 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 runtime = substream->runtime;
2831 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2832 return -EBADFD;
2833 if (!frame_aligned(runtime, count))
2834 return -EINVAL;
2835 count = bytes_to_frames(runtime, count);
2836 result = snd_pcm_lib_read(substream, buf, count);
2837 if (result > 0)
2838 result = frames_to_bytes(runtime, result);
2839 return result;
2840}
2841
Takashi Iwai877211f2005-11-17 13:59:38 +01002842static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2843 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Takashi Iwai877211f2005-11-17 13:59:38 +01002845 struct snd_pcm_file *pcm_file;
2846 struct snd_pcm_substream *substream;
2847 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 snd_pcm_sframes_t result;
2849
2850 pcm_file = file->private_data;
2851 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002852 if (PCM_RUNTIME_CHECK(substream))
2853 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002855 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2856 return -EBADFD;
2857 if (!frame_aligned(runtime, count))
2858 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 count = bytes_to_frames(runtime, count);
2860 result = snd_pcm_lib_write(substream, buf, count);
2861 if (result > 0)
2862 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return result;
2864}
2865
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002866static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2867 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869{
Takashi Iwai877211f2005-11-17 13:59:38 +01002870 struct snd_pcm_file *pcm_file;
2871 struct snd_pcm_substream *substream;
2872 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 snd_pcm_sframes_t result;
2874 unsigned long i;
2875 void __user **bufs;
2876 snd_pcm_uframes_t frames;
2877
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002878 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002880 if (PCM_RUNTIME_CHECK(substream))
2881 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 runtime = substream->runtime;
2883 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2884 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002885 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002887 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002889 frames = bytes_to_samples(runtime, iov->iov_len);
2890 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (bufs == NULL)
2892 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002893 for (i = 0; i < nr_segs; ++i)
2894 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 result = snd_pcm_lib_readv(substream, bufs, frames);
2896 if (result > 0)
2897 result = frames_to_bytes(runtime, result);
2898 kfree(bufs);
2899 return result;
2900}
2901
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002902static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2903 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Takashi Iwai877211f2005-11-17 13:59:38 +01002905 struct snd_pcm_file *pcm_file;
2906 struct snd_pcm_substream *substream;
2907 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 snd_pcm_sframes_t result;
2909 unsigned long i;
2910 void __user **bufs;
2911 snd_pcm_uframes_t frames;
2912
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002913 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002915 if (PCM_RUNTIME_CHECK(substream))
2916 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002918 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2919 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002920 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002921 !frame_aligned(runtime, iov->iov_len))
2922 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002923 frames = bytes_to_samples(runtime, iov->iov_len);
2924 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 if (bufs == NULL)
2926 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002927 for (i = 0; i < nr_segs; ++i)
2928 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 result = snd_pcm_lib_writev(substream, bufs, frames);
2930 if (result > 0)
2931 result = frames_to_bytes(runtime, result);
2932 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return result;
2934}
2935
2936static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2937{
Takashi Iwai877211f2005-11-17 13:59:38 +01002938 struct snd_pcm_file *pcm_file;
2939 struct snd_pcm_substream *substream;
2940 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 unsigned int mask;
2942 snd_pcm_uframes_t avail;
2943
2944 pcm_file = file->private_data;
2945
2946 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002947 if (PCM_RUNTIME_CHECK(substream))
2948 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 runtime = substream->runtime;
2950
2951 poll_wait(file, &runtime->sleep, wait);
2952
2953 snd_pcm_stream_lock_irq(substream);
2954 avail = snd_pcm_playback_avail(runtime);
2955 switch (runtime->status->state) {
2956 case SNDRV_PCM_STATE_RUNNING:
2957 case SNDRV_PCM_STATE_PREPARED:
2958 case SNDRV_PCM_STATE_PAUSED:
2959 if (avail >= runtime->control->avail_min) {
2960 mask = POLLOUT | POLLWRNORM;
2961 break;
2962 }
2963 /* Fall through */
2964 case SNDRV_PCM_STATE_DRAINING:
2965 mask = 0;
2966 break;
2967 default:
2968 mask = POLLOUT | POLLWRNORM | POLLERR;
2969 break;
2970 }
2971 snd_pcm_stream_unlock_irq(substream);
2972 return mask;
2973}
2974
2975static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2976{
Takashi Iwai877211f2005-11-17 13:59:38 +01002977 struct snd_pcm_file *pcm_file;
2978 struct snd_pcm_substream *substream;
2979 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 unsigned int mask;
2981 snd_pcm_uframes_t avail;
2982
2983 pcm_file = file->private_data;
2984
2985 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002986 if (PCM_RUNTIME_CHECK(substream))
2987 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 runtime = substream->runtime;
2989
2990 poll_wait(file, &runtime->sleep, wait);
2991
2992 snd_pcm_stream_lock_irq(substream);
2993 avail = snd_pcm_capture_avail(runtime);
2994 switch (runtime->status->state) {
2995 case SNDRV_PCM_STATE_RUNNING:
2996 case SNDRV_PCM_STATE_PREPARED:
2997 case SNDRV_PCM_STATE_PAUSED:
2998 if (avail >= runtime->control->avail_min) {
2999 mask = POLLIN | POLLRDNORM;
3000 break;
3001 }
3002 mask = 0;
3003 break;
3004 case SNDRV_PCM_STATE_DRAINING:
3005 if (avail > 0) {
3006 mask = POLLIN | POLLRDNORM;
3007 break;
3008 }
3009 /* Fall through */
3010 default:
3011 mask = POLLIN | POLLRDNORM | POLLERR;
3012 break;
3013 }
3014 snd_pcm_stream_unlock_irq(substream);
3015 return mask;
3016}
3017
3018/*
3019 * mmap support
3020 */
3021
3022/*
3023 * Only on coherent architectures, we can mmap the status and the control records
3024 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3025 */
3026#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3027/*
3028 * mmap status record
3029 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003030static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3031 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Takashi Iwai877211f2005-11-17 13:59:38 +01003033 struct snd_pcm_substream *substream = area->vm_private_data;
3034 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003037 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003039 vmf->page = virt_to_page(runtime->status);
3040 get_page(vmf->page);
3041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003044static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003046 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047};
3048
Takashi Iwai877211f2005-11-17 13:59:38 +01003049static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 struct vm_area_struct *area)
3051{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 long size;
3053 if (!(area->vm_flags & VM_READ))
3054 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003056 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return -EINVAL;
3058 area->vm_ops = &snd_pcm_vm_ops_status;
3059 area->vm_private_data = substream;
3060 area->vm_flags |= VM_RESERVED;
3061 return 0;
3062}
3063
3064/*
3065 * mmap control record
3066 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003067static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3068 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Takashi Iwai877211f2005-11-17 13:59:38 +01003070 struct snd_pcm_substream *substream = area->vm_private_data;
3071 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003074 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003076 vmf->page = virt_to_page(runtime->control);
3077 get_page(vmf->page);
3078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003081static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003083 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084};
3085
Takashi Iwai877211f2005-11-17 13:59:38 +01003086static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 struct vm_area_struct *area)
3088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 long size;
3090 if (!(area->vm_flags & VM_READ))
3091 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003093 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return -EINVAL;
3095 area->vm_ops = &snd_pcm_vm_ops_control;
3096 area->vm_private_data = substream;
3097 area->vm_flags |= VM_RESERVED;
3098 return 0;
3099}
3100#else /* ! coherent mmap */
3101/*
3102 * don't support mmap for status and control records.
3103 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003104static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 struct vm_area_struct *area)
3106{
3107 return -ENXIO;
3108}
Takashi Iwai877211f2005-11-17 13:59:38 +01003109static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 struct vm_area_struct *area)
3111{
3112 return -ENXIO;
3113}
3114#endif /* coherent mmap */
3115
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003116static inline struct page *
3117snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3118{
3119 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003120#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3121 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3122 return virt_to_page(CAC_ADDR(vaddr));
3123#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003124#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3125 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3126 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3127 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3128 /* assume dma_handle set via pfn_to_phys() in
3129 * mm/dma-noncoherent.c
3130 */
3131 return pfn_to_page(addr >> PAGE_SHIFT);
3132 }
3133#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003134 return virt_to_page(vaddr);
3135}
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003138 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003140static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3141 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Takashi Iwai877211f2005-11-17 13:59:38 +01003143 struct snd_pcm_substream *substream = area->vm_private_data;
3144 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 unsigned long offset;
3146 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 size_t dma_bytes;
3148
3149 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003150 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003152 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3154 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003155 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003156 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003158 else
3159 page = snd_pcm_default_page_ops(substream, offset);
3160 if (!page)
3161 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003162 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003163 vmf->page = page;
3164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Takashi Iwai657b1982009-11-26 12:40:21 +01003167static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3168 .open = snd_pcm_mmap_data_open,
3169 .close = snd_pcm_mmap_data_close,
3170};
3171
3172static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 .open = snd_pcm_mmap_data_open,
3174 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003175 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176};
3177
Takashi Iwai657b1982009-11-26 12:40:21 +01003178#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3179/* This should be defined / handled globally! */
3180#ifdef CONFIG_ARM
3181#define ARCH_HAS_DMA_MMAP_COHERENT
3182#endif
3183#endif
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185/*
3186 * mmap the DMA buffer on RAM
3187 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003188static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3189 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003192#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3193 if (!substream->ops->page &&
3194 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3195 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3196 area,
3197 substream->runtime->dma_area,
3198 substream->runtime->dma_addr,
3199 area->vm_end - area->vm_start);
Takashi Iwai9fe17b52010-05-12 10:32:42 +02003200#elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3201 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3202 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3203 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Takashi Iwai657b1982009-11-26 12:40:21 +01003204#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3205 /* mmap with fault handler */
3206 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return 0;
3208}
3209
3210/*
3211 * mmap the DMA buffer on I/O memory area
3212 */
3213#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003214int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3215 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
3217 long size;
3218 unsigned long offset;
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 area->vm_flags |= VM_IO;
3222 size = area->vm_end - area->vm_start;
3223 offset = area->vm_pgoff << PAGE_SHIFT;
3224 if (io_remap_pfn_range(area, area->vm_start,
3225 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3226 size, area->vm_page_prot))
3227 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return 0;
3229}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003230
3231EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232#endif /* SNDRV_PCM_INFO_MMAP */
3233
3234/*
3235 * mmap DMA buffer
3236 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003237int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 struct vm_area_struct *area)
3239{
Takashi Iwai877211f2005-11-17 13:59:38 +01003240 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 long size;
3242 unsigned long offset;
3243 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003244 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3247 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3248 return -EINVAL;
3249 } else {
3250 if (!(area->vm_flags & VM_READ))
3251 return -EINVAL;
3252 }
3253 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3255 return -EBADFD;
3256 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3257 return -ENXIO;
3258 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3259 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3260 return -EINVAL;
3261 size = area->vm_end - area->vm_start;
3262 offset = area->vm_pgoff << PAGE_SHIFT;
3263 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3264 if ((size_t)size > dma_bytes)
3265 return -EINVAL;
3266 if (offset > dma_bytes - size)
3267 return -EINVAL;
3268
Takashi Iwai657b1982009-11-26 12:40:21 +01003269 area->vm_ops = &snd_pcm_vm_ops_data;
3270 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003272 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 else
Takashi Iwai657b1982009-11-26 12:40:21 +01003274 err = snd_pcm_default_mmap(substream, area);
3275 if (!err)
3276 atomic_inc(&substream->mmap_count);
3277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003280EXPORT_SYMBOL(snd_pcm_mmap_data);
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3283{
Takashi Iwai877211f2005-11-17 13:59:38 +01003284 struct snd_pcm_file * pcm_file;
3285 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 unsigned long offset;
3287
3288 pcm_file = file->private_data;
3289 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003290 if (PCM_RUNTIME_CHECK(substream))
3291 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 offset = area->vm_pgoff << PAGE_SHIFT;
3294 switch (offset) {
3295 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003296 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return -ENXIO;
3298 return snd_pcm_mmap_status(substream, file, area);
3299 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003300 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return -ENXIO;
3302 return snd_pcm_mmap_control(substream, file, area);
3303 default:
3304 return snd_pcm_mmap_data(substream, file, area);
3305 }
3306 return 0;
3307}
3308
3309static int snd_pcm_fasync(int fd, struct file * file, int on)
3310{
Takashi Iwai877211f2005-11-17 13:59:38 +01003311 struct snd_pcm_file * pcm_file;
3312 struct snd_pcm_substream *substream;
3313 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 pcm_file = file->private_data;
3316 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003317 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003318 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003320 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
3323/*
3324 * ioctl32 compat
3325 */
3326#ifdef CONFIG_COMPAT
3327#include "pcm_compat.c"
3328#else
3329#define snd_pcm_ioctl_compat NULL
3330#endif
3331
3332/*
3333 * To be removed helpers to keep binary compatibility
3334 */
3335
Takashi Iwai59d48582005-12-01 10:51:58 +01003336#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3338#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3339
Takashi Iwai877211f2005-11-17 13:59:38 +01003340static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3341 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
3343 unsigned int i;
3344
3345 memset(params, 0, sizeof(*params));
3346 params->flags = oparams->flags;
3347 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3348 params->masks[i].bits[0] = oparams->masks[i];
3349 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3350 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3351 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3352 params->info = oparams->info;
3353 params->msbits = oparams->msbits;
3354 params->rate_num = oparams->rate_num;
3355 params->rate_den = oparams->rate_den;
3356 params->fifo_size = oparams->fifo_size;
3357}
3358
Takashi Iwai877211f2005-11-17 13:59:38 +01003359static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3360 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
3362 unsigned int i;
3363
3364 memset(oparams, 0, sizeof(*oparams));
3365 oparams->flags = params->flags;
3366 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3367 oparams->masks[i] = params->masks[i].bits[0];
3368 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3369 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3370 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3371 oparams->info = params->info;
3372 oparams->msbits = params->msbits;
3373 oparams->rate_num = params->rate_num;
3374 oparams->rate_den = params->rate_den;
3375 oparams->fifo_size = params->fifo_size;
3376}
3377
Takashi Iwai877211f2005-11-17 13:59:38 +01003378static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3379 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
Takashi Iwai877211f2005-11-17 13:59:38 +01003381 struct snd_pcm_hw_params *params;
3382 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 int err;
3384
3385 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003386 if (!params)
3387 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Li Zefanef44a1e2009-04-10 09:43:08 +08003389 oparams = memdup_user(_oparams, sizeof(*oparams));
3390 if (IS_ERR(oparams)) {
3391 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 goto out;
3393 }
3394 snd_pcm_hw_convert_from_old_params(params, oparams);
3395 err = snd_pcm_hw_refine(substream, params);
3396 snd_pcm_hw_convert_to_old_params(oparams, params);
3397 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3398 if (!err)
3399 err = -EFAULT;
3400 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003401
3402 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403out:
3404 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return err;
3406}
3407
Takashi Iwai877211f2005-11-17 13:59:38 +01003408static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3409 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Takashi Iwai877211f2005-11-17 13:59:38 +01003411 struct snd_pcm_hw_params *params;
3412 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 int err;
3414
3415 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003416 if (!params)
3417 return -ENOMEM;
3418
3419 oparams = memdup_user(_oparams, sizeof(*oparams));
3420 if (IS_ERR(oparams)) {
3421 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 goto out;
3423 }
3424 snd_pcm_hw_convert_from_old_params(params, oparams);
3425 err = snd_pcm_hw_params(substream, params);
3426 snd_pcm_hw_convert_to_old_params(oparams, params);
3427 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3428 if (!err)
3429 err = -EFAULT;
3430 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003431
3432 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433out:
3434 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return err;
3436}
Takashi Iwai59d48582005-12-01 10:51:58 +01003437#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Cliff Cai70036092008-09-03 10:54:36 +02003439#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003440static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3441 unsigned long addr,
3442 unsigned long len,
3443 unsigned long pgoff,
3444 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003445{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003446 struct snd_pcm_file *pcm_file = file->private_data;
3447 struct snd_pcm_substream *substream = pcm_file->substream;
3448 struct snd_pcm_runtime *runtime = substream->runtime;
3449 unsigned long offset = pgoff << PAGE_SHIFT;
3450
3451 switch (offset) {
3452 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3453 return (unsigned long)runtime->status;
3454 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3455 return (unsigned long)runtime->control;
3456 default:
3457 return (unsigned long)runtime->dma_area + offset;
3458 }
Cliff Cai70036092008-09-03 10:54:36 +02003459}
3460#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003461# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003462#endif
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464/*
3465 * Register section
3466 */
3467
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003468const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003470 .owner = THIS_MODULE,
3471 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003472 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003473 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003474 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003475 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003476 .poll = snd_pcm_playback_poll,
3477 .unlocked_ioctl = snd_pcm_playback_ioctl,
3478 .compat_ioctl = snd_pcm_ioctl_compat,
3479 .mmap = snd_pcm_mmap,
3480 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003481 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 },
3483 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003484 .owner = THIS_MODULE,
3485 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003486 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003487 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003488 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003489 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003490 .poll = snd_pcm_capture_poll,
3491 .unlocked_ioctl = snd_pcm_capture_ioctl,
3492 .compat_ioctl = snd_pcm_ioctl_compat,
3493 .mmap = snd_pcm_mmap,
3494 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003495 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
3497};