blob: d89c816ae5bfc767fbe35c7e8e7860c3ef185430 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/file.h>
24#include <linux/slab.h>
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -060025#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/time.h>
Mark Grossf011e2e2008-02-04 22:30:09 -080027#include <linux/pm_qos_params.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/uio.h>
29#include <sound/core.h>
30#include <sound/control.h>
31#include <sound/info.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/timer.h>
35#include <sound/minors.h>
36#include <asm/io.h>
37
38/*
39 * Compatibility
40 */
41
Takashi Iwai877211f2005-11-17 13:59:38 +010042struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned int flags;
44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
45 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010046 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
48 unsigned int rmask;
49 unsigned int cmask;
50 unsigned int info;
51 unsigned int msbits;
52 unsigned int rate_num;
53 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010054 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 unsigned char reserved[64];
56};
57
Takashi Iwai59d48582005-12-01 10:51:58 +010058#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010059#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Takashi Iwai877211f2005-11-17 13:59:38 +010062static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params_old __user * _oparams);
64static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010066#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010067static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 *
71 */
72
73DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020074EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020076static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static inline mm_segment_t snd_enter_user(void)
79{
80 mm_segment_t fs = get_fs();
81 set_fs(get_ds());
82 return fs;
83}
84
85static inline void snd_leave_user(mm_segment_t fs)
86{
87 set_fs(fs);
88}
89
90
91
Takashi Iwai877211f2005-11-17 13:59:38 +010092int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Takashi Iwai877211f2005-11-17 13:59:38 +010094 struct snd_pcm_runtime *runtime;
95 struct snd_pcm *pcm = substream->pcm;
96 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 memset(info, 0, sizeof(*info));
99 info->card = pcm->card->number;
100 info->device = pcm->device;
101 info->stream = substream->stream;
102 info->subdevice = substream->number;
103 strlcpy(info->id, pcm->id, sizeof(info->id));
104 strlcpy(info->name, pcm->name, sizeof(info->name));
105 info->dev_class = pcm->dev_class;
106 info->dev_subclass = pcm->dev_subclass;
107 info->subdevices_count = pstr->substream_count;
108 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
109 strlcpy(info->subname, substream->name, sizeof(info->subname));
110 runtime = substream->runtime;
111 /* AB: FIXME!!! This is definitely nonsense */
112 if (runtime) {
113 info->sync = runtime->sync;
114 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
115 }
116 return 0;
117}
118
Takashi Iwai877211f2005-11-17 13:59:38 +0100119int snd_pcm_info_user(struct snd_pcm_substream *substream,
120 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Takashi Iwai877211f2005-11-17 13:59:38 +0100122 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int err;
124
125 info = kmalloc(sizeof(*info), GFP_KERNEL);
126 if (! info)
127 return -ENOMEM;
128 err = snd_pcm_info(substream, info);
129 if (err >= 0) {
130 if (copy_to_user(_info, info, sizeof(*info)))
131 err = -EFAULT;
132 }
133 kfree(info);
134 return err;
135}
136
137#undef RULES_DEBUG
138
139#ifdef RULES_DEBUG
140#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
141char *snd_pcm_hw_param_names[] = {
142 HW_PARAM(ACCESS),
143 HW_PARAM(FORMAT),
144 HW_PARAM(SUBFORMAT),
145 HW_PARAM(SAMPLE_BITS),
146 HW_PARAM(FRAME_BITS),
147 HW_PARAM(CHANNELS),
148 HW_PARAM(RATE),
149 HW_PARAM(PERIOD_TIME),
150 HW_PARAM(PERIOD_SIZE),
151 HW_PARAM(PERIOD_BYTES),
152 HW_PARAM(PERIODS),
153 HW_PARAM(BUFFER_TIME),
154 HW_PARAM(BUFFER_SIZE),
155 HW_PARAM(BUFFER_BYTES),
156 HW_PARAM(TICK_TIME),
157};
158#endif
159
Takashi Iwai877211f2005-11-17 13:59:38 +0100160int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100164 struct snd_pcm_hardware *hw;
165 struct snd_interval *i = NULL;
166 struct snd_mask *m = NULL;
167 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int rstamps[constrs->rules_num];
169 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
170 unsigned int stamp = 2;
171 int changed, again;
172
173 params->info = 0;
174 params->fifo_size = 0;
175 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
176 params->msbits = 0;
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
178 params->rate_num = 0;
179 params->rate_den = 0;
180 }
181
182 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
183 m = hw_param_mask(params, k);
184 if (snd_mask_empty(m))
185 return -EINVAL;
186 if (!(params->rmask & (1 << k)))
187 continue;
188#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100189 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
191#endif
192 changed = snd_mask_refine(m, constrs_mask(constrs, k));
193#ifdef RULES_DEBUG
194 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
195#endif
196 if (changed)
197 params->cmask |= 1 << k;
198 if (changed < 0)
199 return changed;
200 }
201
202 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
203 i = hw_param_interval(params, k);
204 if (snd_interval_empty(i))
205 return -EINVAL;
206 if (!(params->rmask & (1 << k)))
207 continue;
208#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100209 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (i->empty)
211 printk("empty");
212 else
213 printk("%c%u %u%c",
214 i->openmin ? '(' : '[', i->min,
215 i->max, i->openmax ? ')' : ']');
216 printk(" -> ");
217#endif
218 changed = snd_interval_refine(i, constrs_interval(constrs, k));
219#ifdef RULES_DEBUG
220 if (i->empty)
221 printk("empty\n");
222 else
223 printk("%c%u %u%c\n",
224 i->openmin ? '(' : '[', i->min,
225 i->max, i->openmax ? ')' : ']');
226#endif
227 if (changed)
228 params->cmask |= 1 << k;
229 if (changed < 0)
230 return changed;
231 }
232
233 for (k = 0; k < constrs->rules_num; k++)
234 rstamps[k] = 0;
235 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
236 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
237 do {
238 again = 0;
239 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100240 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned int d;
242 int doit = 0;
243 if (r->cond && !(r->cond & params->flags))
244 continue;
245 for (d = 0; r->deps[d] >= 0; d++) {
246 if (vstamps[r->deps[d]] > rstamps[k]) {
247 doit = 1;
248 break;
249 }
250 }
251 if (!doit)
252 continue;
253#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100254 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (r->var >= 0) {
256 printk("%s = ", snd_pcm_hw_param_names[r->var]);
257 if (hw_is_mask(r->var)) {
258 m = hw_param_mask(params, r->var);
259 printk("%x", *m->bits);
260 } else {
261 i = hw_param_interval(params, r->var);
262 if (i->empty)
263 printk("empty");
264 else
265 printk("%c%u %u%c",
266 i->openmin ? '(' : '[', i->min,
267 i->max, i->openmax ? ')' : ']');
268 }
269 }
270#endif
271 changed = r->func(params, r);
272#ifdef RULES_DEBUG
273 if (r->var >= 0) {
274 printk(" -> ");
275 if (hw_is_mask(r->var))
276 printk("%x", *m->bits);
277 else {
278 if (i->empty)
279 printk("empty");
280 else
281 printk("%c%u %u%c",
282 i->openmin ? '(' : '[', i->min,
283 i->max, i->openmax ? ')' : ']');
284 }
285 }
286 printk("\n");
287#endif
288 rstamps[k] = stamp;
289 if (changed && r->var >= 0) {
290 params->cmask |= (1 << r->var);
291 vstamps[r->var] = stamp;
292 again = 1;
293 }
294 if (changed < 0)
295 return changed;
296 stamp++;
297 }
298 } while (again);
299 if (!params->msbits) {
300 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
301 if (snd_interval_single(i))
302 params->msbits = snd_interval_value(i);
303 }
304
305 if (!params->rate_den) {
306 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
307 if (snd_interval_single(i)) {
308 params->rate_num = snd_interval_value(i);
309 params->rate_den = 1;
310 }
311 }
312
313 hw = &substream->runtime->hw;
314 if (!params->info)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200315 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
316 if (!params->fifo_size) {
317 if (snd_mask_min(&params->masks[SNDRV_PCM_HW_PARAM_FORMAT]) ==
318 snd_mask_max(&params->masks[SNDRV_PCM_HW_PARAM_FORMAT]) &&
319 snd_mask_min(&params->masks[SNDRV_PCM_HW_PARAM_CHANNELS]) ==
320 snd_mask_max(&params->masks[SNDRV_PCM_HW_PARAM_CHANNELS])) {
321 changed = substream->ops->ioctl(substream,
322 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200323 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200324 return changed;
325 }
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 params->rmask = 0;
328 return 0;
329}
330
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200331EXPORT_SYMBOL(snd_pcm_hw_refine);
332
Takashi Iwai877211f2005-11-17 13:59:38 +0100333static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
334 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Takashi Iwai877211f2005-11-17 13:59:38 +0100336 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int err;
338
Li Zefanef44a1e2009-04-10 09:43:08 +0800339 params = memdup_user(_params, sizeof(*params));
340 if (IS_ERR(params))
341 return PTR_ERR(params);
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 err = snd_pcm_hw_refine(substream, params);
344 if (copy_to_user(_params, params, sizeof(*params))) {
345 if (!err)
346 err = -EFAULT;
347 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 kfree(params);
350 return err;
351}
352
Takashi Iwai9442e692006-09-30 23:27:19 -0700353static int period_to_usecs(struct snd_pcm_runtime *runtime)
354{
355 int usecs;
356
357 if (! runtime->rate)
358 return -1; /* invalid */
359
360 /* take 75% of period time as the deadline */
361 usecs = (750000 / runtime->rate) * runtime->period_size;
362 usecs += ((750000 % runtime->rate) * runtime->period_size) /
363 runtime->rate;
364
365 return usecs;
366}
367
Takashi Iwai877211f2005-11-17 13:59:38 +0100368static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
369 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Takashi Iwai877211f2005-11-17 13:59:38 +0100371 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700372 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 unsigned int bits;
374 snd_pcm_uframes_t frames;
375
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200376 if (PCM_RUNTIME_CHECK(substream))
377 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 snd_pcm_stream_lock_irq(substream);
380 switch (runtime->status->state) {
381 case SNDRV_PCM_STATE_OPEN:
382 case SNDRV_PCM_STATE_SETUP:
383 case SNDRV_PCM_STATE_PREPARED:
384 break;
385 default:
386 snd_pcm_stream_unlock_irq(substream);
387 return -EBADFD;
388 }
389 snd_pcm_stream_unlock_irq(substream);
390#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
391 if (!substream->oss.oss)
392#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200393 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -EBADFD;
395
396 params->rmask = ~0U;
397 err = snd_pcm_hw_refine(substream, params);
398 if (err < 0)
399 goto _error;
400
401 err = snd_pcm_hw_params_choose(substream, params);
402 if (err < 0)
403 goto _error;
404
405 if (substream->ops->hw_params != NULL) {
406 err = substream->ops->hw_params(substream, params);
407 if (err < 0)
408 goto _error;
409 }
410
411 runtime->access = params_access(params);
412 runtime->format = params_format(params);
413 runtime->subformat = params_subformat(params);
414 runtime->channels = params_channels(params);
415 runtime->rate = params_rate(params);
416 runtime->period_size = params_period_size(params);
417 runtime->periods = params_periods(params);
418 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 runtime->info = params->info;
420 runtime->rate_num = params->rate_num;
421 runtime->rate_den = params->rate_den;
422
423 bits = snd_pcm_format_physical_width(runtime->format);
424 runtime->sample_bits = bits;
425 bits *= runtime->channels;
426 runtime->frame_bits = bits;
427 frames = 1;
428 while (bits % 8 != 0) {
429 bits *= 2;
430 frames *= 2;
431 }
432 runtime->byte_align = bits / 8;
433 runtime->min_align = frames;
434
435 /* Default sw params */
436 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
437 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 runtime->start_threshold = 1;
440 runtime->stop_threshold = runtime->buffer_size;
441 runtime->silence_threshold = 0;
442 runtime->silence_size = 0;
443 runtime->boundary = runtime->buffer_size;
444 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
445 runtime->boundary *= 2;
446
447 snd_pcm_timer_resolution_change(substream);
448 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700449
Mark Grossf011e2e2008-02-04 22:30:09 -0800450 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
451 substream->latency_id);
Takashi Iwai9442e692006-09-30 23:27:19 -0700452 if ((usecs = period_to_usecs(runtime)) >= 0)
Mark Grossf011e2e2008-02-04 22:30:09 -0800453 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
454 substream->latency_id, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
456 _error:
457 /* hardware might be unuseable from this time,
458 so we force application to retry to set
459 the correct hardware parameter settings */
460 runtime->status->state = SNDRV_PCM_STATE_OPEN;
461 if (substream->ops->hw_free != NULL)
462 substream->ops->hw_free(substream);
463 return err;
464}
465
Takashi Iwai877211f2005-11-17 13:59:38 +0100466static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
467 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Takashi Iwai877211f2005-11-17 13:59:38 +0100469 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int err;
471
Li Zefanef44a1e2009-04-10 09:43:08 +0800472 params = memdup_user(_params, sizeof(*params));
473 if (IS_ERR(params))
474 return PTR_ERR(params);
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 err = snd_pcm_hw_params(substream, params);
477 if (copy_to_user(_params, params, sizeof(*params))) {
478 if (!err)
479 err = -EFAULT;
480 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 kfree(params);
483 return err;
484}
485
Takashi Iwai877211f2005-11-17 13:59:38 +0100486static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Takashi Iwai877211f2005-11-17 13:59:38 +0100488 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int result = 0;
490
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200491 if (PCM_RUNTIME_CHECK(substream))
492 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 snd_pcm_stream_lock_irq(substream);
495 switch (runtime->status->state) {
496 case SNDRV_PCM_STATE_SETUP:
497 case SNDRV_PCM_STATE_PREPARED:
498 break;
499 default:
500 snd_pcm_stream_unlock_irq(substream);
501 return -EBADFD;
502 }
503 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200504 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -EBADFD;
506 if (substream->ops->hw_free)
507 result = substream->ops->hw_free(substream);
508 runtime->status->state = SNDRV_PCM_STATE_OPEN;
Mark Grossf011e2e2008-02-04 22:30:09 -0800509 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
510 substream->latency_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return result;
512}
513
Takashi Iwai877211f2005-11-17 13:59:38 +0100514static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
515 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Takashi Iwai877211f2005-11-17 13:59:38 +0100517 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200519 if (PCM_RUNTIME_CHECK(substream))
520 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 snd_pcm_stream_lock_irq(substream);
523 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
524 snd_pcm_stream_unlock_irq(substream);
525 return -EBADFD;
526 }
527 snd_pcm_stream_unlock_irq(substream);
528
529 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
530 return -EINVAL;
531 if (params->avail_min == 0)
532 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (params->silence_size >= runtime->boundary) {
534 if (params->silence_threshold != 0)
535 return -EINVAL;
536 } else {
537 if (params->silence_size > params->silence_threshold)
538 return -EINVAL;
539 if (params->silence_threshold > runtime->buffer_size)
540 return -EINVAL;
541 }
542 snd_pcm_stream_lock_irq(substream);
543 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 runtime->period_step = params->period_step;
545 runtime->control->avail_min = params->avail_min;
546 runtime->start_threshold = params->start_threshold;
547 runtime->stop_threshold = params->stop_threshold;
548 runtime->silence_threshold = params->silence_threshold;
549 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 params->boundary = runtime->boundary;
551 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
553 runtime->silence_size > 0)
554 snd_pcm_playback_silence(substream, ULONG_MAX);
555 wake_up(&runtime->sleep);
556 }
557 snd_pcm_stream_unlock_irq(substream);
558 return 0;
559}
560
Takashi Iwai877211f2005-11-17 13:59:38 +0100561static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
562 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Takashi Iwai877211f2005-11-17 13:59:38 +0100564 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int err;
566 if (copy_from_user(&params, _params, sizeof(params)))
567 return -EFAULT;
568 err = snd_pcm_sw_params(substream, &params);
569 if (copy_to_user(_params, &params, sizeof(params)))
570 return -EFAULT;
571 return err;
572}
573
Takashi Iwai877211f2005-11-17 13:59:38 +0100574int snd_pcm_status(struct snd_pcm_substream *substream,
575 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Takashi Iwai877211f2005-11-17 13:59:38 +0100577 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 snd_pcm_stream_lock_irq(substream);
580 status->state = runtime->status->state;
581 status->suspended_state = runtime->status->suspended_state;
582 if (status->state == SNDRV_PCM_STATE_OPEN)
583 goto _end;
584 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100585 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100587 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
588 status->tstamp = runtime->status->tstamp;
589 goto _tstamp_end;
590 }
591 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100592 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100593 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 status->appl_ptr = runtime->control->appl_ptr;
595 status->hw_ptr = runtime->status->hw_ptr;
596 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
597 status->avail = snd_pcm_playback_avail(runtime);
598 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200599 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200601 status->delay += runtime->delay;
602 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 status->delay = 0;
604 } else {
605 status->avail = snd_pcm_capture_avail(runtime);
606 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200607 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 else
609 status->delay = 0;
610 }
611 status->avail_max = runtime->avail_max;
612 status->overrange = runtime->overrange;
613 runtime->avail_max = 0;
614 runtime->overrange = 0;
615 _end:
616 snd_pcm_stream_unlock_irq(substream);
617 return 0;
618}
619
Takashi Iwai877211f2005-11-17 13:59:38 +0100620static int snd_pcm_status_user(struct snd_pcm_substream *substream,
621 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Takashi Iwai877211f2005-11-17 13:59:38 +0100623 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int res;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 memset(&status, 0, sizeof(status));
627 res = snd_pcm_status(substream, &status);
628 if (res < 0)
629 return res;
630 if (copy_to_user(_status, &status, sizeof(status)))
631 return -EFAULT;
632 return 0;
633}
634
Takashi Iwai877211f2005-11-17 13:59:38 +0100635static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
636 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Takashi Iwai877211f2005-11-17 13:59:38 +0100638 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 unsigned int channel;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 channel = info->channel;
642 runtime = substream->runtime;
643 snd_pcm_stream_lock_irq(substream);
644 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
645 snd_pcm_stream_unlock_irq(substream);
646 return -EBADFD;
647 }
648 snd_pcm_stream_unlock_irq(substream);
649 if (channel >= runtime->channels)
650 return -EINVAL;
651 memset(info, 0, sizeof(*info));
652 info->channel = channel;
653 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
654}
655
Takashi Iwai877211f2005-11-17 13:59:38 +0100656static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
657 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Takashi Iwai877211f2005-11-17 13:59:38 +0100659 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int res;
661
662 if (copy_from_user(&info, _info, sizeof(info)))
663 return -EFAULT;
664 res = snd_pcm_channel_info(substream, &info);
665 if (res < 0)
666 return res;
667 if (copy_to_user(_info, &info, sizeof(info)))
668 return -EFAULT;
669 return 0;
670}
671
Takashi Iwai877211f2005-11-17 13:59:38 +0100672static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Takashi Iwai877211f2005-11-17 13:59:38 +0100674 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (runtime->trigger_master == NULL)
676 return;
677 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100678 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 } else {
680 snd_pcm_trigger_tstamp(runtime->trigger_master);
681 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
682 }
683 runtime->trigger_master = NULL;
684}
685
686struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100687 int (*pre_action)(struct snd_pcm_substream *substream, int state);
688 int (*do_action)(struct snd_pcm_substream *substream, int state);
689 void (*undo_action)(struct snd_pcm_substream *substream, int state);
690 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691};
692
693/*
694 * this functions is core for handling of linked stream
695 * Note: the stream state might be changed also on failure
696 * Note2: call with calling stream lock + link lock
697 */
698static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100699 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int state, int do_lock)
701{
Takashi Iwai877211f2005-11-17 13:59:38 +0100702 struct snd_pcm_substream *s = NULL;
703 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int res = 0;
705
Takashi Iwaief991b92007-02-22 12:52:53 +0100706 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200708 spin_lock_nested(&s->self_group.lock,
709 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 res = ops->pre_action(s, state);
711 if (res < 0)
712 goto _unlock;
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 res = ops->do_action(s, state);
716 if (res < 0) {
717 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100718 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (s1 == s) /* failed stream */
720 break;
721 ops->undo_action(s1, state);
722 }
723 }
724 s = NULL; /* unlock all */
725 goto _unlock;
726 }
727 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100728 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ops->post_action(s, state);
730 }
731 _unlock:
732 if (do_lock) {
733 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100734 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (s1 != substream)
736 spin_unlock(&s1->self_group.lock);
737 if (s1 == s) /* end */
738 break;
739 }
740 }
741 return res;
742}
743
744/*
745 * Note: call with stream lock
746 */
747static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100748 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 int state)
750{
751 int res;
752
753 res = ops->pre_action(substream, state);
754 if (res < 0)
755 return res;
756 res = ops->do_action(substream, state);
757 if (res == 0)
758 ops->post_action(substream, state);
759 else if (ops->undo_action)
760 ops->undo_action(substream, state);
761 return res;
762}
763
764/*
765 * Note: call with stream lock
766 */
767static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100768 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int state)
770{
771 int res;
772
773 if (snd_pcm_stream_linked(substream)) {
774 if (!spin_trylock(&substream->group->lock)) {
775 spin_unlock(&substream->self_group.lock);
776 spin_lock(&substream->group->lock);
777 spin_lock(&substream->self_group.lock);
778 }
779 res = snd_pcm_action_group(ops, substream, state, 1);
780 spin_unlock(&substream->group->lock);
781 } else {
782 res = snd_pcm_action_single(ops, substream, state);
783 }
784 return res;
785}
786
787/*
788 * Note: don't use any locks before
789 */
790static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100791 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int state)
793{
794 int res;
795
796 read_lock_irq(&snd_pcm_link_rwlock);
797 if (snd_pcm_stream_linked(substream)) {
798 spin_lock(&substream->group->lock);
799 spin_lock(&substream->self_group.lock);
800 res = snd_pcm_action_group(ops, substream, state, 1);
801 spin_unlock(&substream->self_group.lock);
802 spin_unlock(&substream->group->lock);
803 } else {
804 spin_lock(&substream->self_group.lock);
805 res = snd_pcm_action_single(ops, substream, state);
806 spin_unlock(&substream->self_group.lock);
807 }
808 read_unlock_irq(&snd_pcm_link_rwlock);
809 return res;
810}
811
812/*
813 */
814static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100815 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int state)
817{
818 int res;
819
820 down_read(&snd_pcm_link_rwsem);
821 if (snd_pcm_stream_linked(substream))
822 res = snd_pcm_action_group(ops, substream, state, 0);
823 else
824 res = snd_pcm_action_single(ops, substream, state);
825 up_read(&snd_pcm_link_rwsem);
826 return res;
827}
828
829/*
830 * start callbacks
831 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100832static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Takashi Iwai877211f2005-11-17 13:59:38 +0100834 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
836 return -EBADFD;
837 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
838 !snd_pcm_playback_data(substream))
839 return -EPIPE;
840 runtime->trigger_master = substream;
841 return 0;
842}
843
Takashi Iwai877211f2005-11-17 13:59:38 +0100844static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 if (substream->runtime->trigger_master != substream)
847 return 0;
848 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
849}
850
Takashi Iwai877211f2005-11-17 13:59:38 +0100851static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 if (substream->runtime->trigger_master == substream)
854 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
855}
856
Takashi Iwai877211f2005-11-17 13:59:38 +0100857static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Takashi Iwai877211f2005-11-17 13:59:38 +0100859 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200861 runtime->hw_ptr_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 runtime->status->state = state;
863 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
864 runtime->silence_size > 0)
865 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100867 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
868 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871static struct action_ops snd_pcm_action_start = {
872 .pre_action = snd_pcm_pre_start,
873 .do_action = snd_pcm_do_start,
874 .undo_action = snd_pcm_undo_start,
875 .post_action = snd_pcm_post_start
876};
877
878/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700879 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200880 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100882int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Takashi Iwai877211f2005-11-17 13:59:38 +0100884 return snd_pcm_action(&snd_pcm_action_start, substream,
885 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888/*
889 * stop callbacks
890 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100891static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Takashi Iwai877211f2005-11-17 13:59:38 +0100893 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
895 return -EBADFD;
896 runtime->trigger_master = substream;
897 return 0;
898}
899
Takashi Iwai877211f2005-11-17 13:59:38 +0100900static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 if (substream->runtime->trigger_master == substream &&
903 snd_pcm_running(substream))
904 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
905 return 0; /* unconditonally stop all substreams */
906}
907
Takashi Iwai877211f2005-11-17 13:59:38 +0100908static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Takashi Iwai877211f2005-11-17 13:59:38 +0100910 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (runtime->status->state != state) {
912 snd_pcm_trigger_tstamp(substream);
913 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100914 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
915 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 wake_up(&runtime->sleep);
919}
920
921static struct action_ops snd_pcm_action_stop = {
922 .pre_action = snd_pcm_pre_stop,
923 .do_action = snd_pcm_do_stop,
924 .post_action = snd_pcm_post_stop
925};
926
927/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700928 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200929 * @substream: the PCM substream instance
930 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700932 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100934int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
937}
938
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200939EXPORT_SYMBOL(snd_pcm_stop);
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700942 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200943 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700945 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * Unlike snd_pcm_stop(), this affects only the given stream.
947 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100948int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Takashi Iwai877211f2005-11-17 13:59:38 +0100950 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
951 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954/*
955 * pause callbacks
956 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100957static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Takashi Iwai877211f2005-11-17 13:59:38 +0100959 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
961 return -ENOSYS;
962 if (push) {
963 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
964 return -EBADFD;
965 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
966 return -EBADFD;
967 runtime->trigger_master = substream;
968 return 0;
969}
970
Takashi Iwai877211f2005-11-17 13:59:38 +0100971static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 if (substream->runtime->trigger_master != substream)
974 return 0;
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200975 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
976 * a delta betwen the current jiffies, this gives a large enough
977 * delta, effectively to skip the check once.
978 */
979 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return substream->ops->trigger(substream,
981 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
982 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
983}
984
Takashi Iwai877211f2005-11-17 13:59:38 +0100985static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 if (substream->runtime->trigger_master == substream)
988 substream->ops->trigger(substream,
989 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
990 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
991}
992
Takashi Iwai877211f2005-11-17 13:59:38 +0100993static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Takashi Iwai877211f2005-11-17 13:59:38 +0100995 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 snd_pcm_trigger_tstamp(substream);
997 if (push) {
998 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
999 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001000 snd_timer_notify(substream->timer,
1001 SNDRV_TIMER_EVENT_MPAUSE,
1002 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 wake_up(&runtime->sleep);
1004 } else {
1005 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001007 snd_timer_notify(substream->timer,
1008 SNDRV_TIMER_EVENT_MCONTINUE,
1009 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011}
1012
1013static struct action_ops snd_pcm_action_pause = {
1014 .pre_action = snd_pcm_pre_pause,
1015 .do_action = snd_pcm_do_pause,
1016 .undo_action = snd_pcm_undo_pause,
1017 .post_action = snd_pcm_post_pause
1018};
1019
1020/*
1021 * Push/release the pause for all linked streams.
1022 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001023static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1026}
1027
1028#ifdef CONFIG_PM
1029/* suspend */
1030
Takashi Iwai877211f2005-11-17 13:59:38 +01001031static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Takashi Iwai877211f2005-11-17 13:59:38 +01001033 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1035 return -EBUSY;
1036 runtime->trigger_master = substream;
1037 return 0;
1038}
1039
Takashi Iwai877211f2005-11-17 13:59:38 +01001040static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Takashi Iwai877211f2005-11-17 13:59:38 +01001042 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (runtime->trigger_master != substream)
1044 return 0;
1045 if (! snd_pcm_running(substream))
1046 return 0;
1047 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1048 return 0; /* suspend unconditionally */
1049}
1050
Takashi Iwai877211f2005-11-17 13:59:38 +01001051static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Takashi Iwai877211f2005-11-17 13:59:38 +01001053 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 snd_pcm_trigger_tstamp(substream);
1055 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001056 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1057 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 runtime->status->suspended_state = runtime->status->state;
1059 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 wake_up(&runtime->sleep);
1061}
1062
1063static struct action_ops snd_pcm_action_suspend = {
1064 .pre_action = snd_pcm_pre_suspend,
1065 .do_action = snd_pcm_do_suspend,
1066 .post_action = snd_pcm_post_suspend
1067};
1068
1069/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001070 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001071 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * After this call, all streams are changed to SUSPENDED state.
1074 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001075int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 int err;
1078 unsigned long flags;
1079
Takashi Iwai603bf522005-11-17 15:59:14 +01001080 if (! substream)
1081 return 0;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 snd_pcm_stream_lock_irqsave(substream, flags);
1084 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1085 snd_pcm_stream_unlock_irqrestore(substream, flags);
1086 return err;
1087}
1088
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001089EXPORT_SYMBOL(snd_pcm_suspend);
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001092 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001093 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * After this call, all streams are changed to SUSPENDED state.
1096 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001097int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Takashi Iwai877211f2005-11-17 13:59:38 +01001099 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int stream, err = 0;
1101
Takashi Iwai603bf522005-11-17 15:59:14 +01001102 if (! pcm)
1103 return 0;
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001106 for (substream = pcm->streams[stream].substream;
1107 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* FIXME: the open/close code should lock this as well */
1109 if (substream->runtime == NULL)
1110 continue;
1111 err = snd_pcm_suspend(substream);
1112 if (err < 0 && err != -EBUSY)
1113 return err;
1114 }
1115 }
1116 return 0;
1117}
1118
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001119EXPORT_SYMBOL(snd_pcm_suspend_all);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/* resume */
1122
Takashi Iwai877211f2005-11-17 13:59:38 +01001123static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Takashi Iwai877211f2005-11-17 13:59:38 +01001125 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1127 return -ENOSYS;
1128 runtime->trigger_master = substream;
1129 return 0;
1130}
1131
Takashi Iwai877211f2005-11-17 13:59:38 +01001132static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Takashi Iwai877211f2005-11-17 13:59:38 +01001134 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (runtime->trigger_master != substream)
1136 return 0;
1137 /* DMA not running previously? */
1138 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1139 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1140 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1141 return 0;
1142 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1143}
1144
Takashi Iwai877211f2005-11-17 13:59:38 +01001145static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 if (substream->runtime->trigger_master == substream &&
1148 snd_pcm_running(substream))
1149 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1150}
1151
Takashi Iwai877211f2005-11-17 13:59:38 +01001152static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Takashi Iwai877211f2005-11-17 13:59:38 +01001154 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 snd_pcm_trigger_tstamp(substream);
1156 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001157 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1158 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162static struct action_ops snd_pcm_action_resume = {
1163 .pre_action = snd_pcm_pre_resume,
1164 .do_action = snd_pcm_do_resume,
1165 .undo_action = snd_pcm_undo_resume,
1166 .post_action = snd_pcm_post_resume
1167};
1168
Takashi Iwai877211f2005-11-17 13:59:38 +01001169static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Takashi Iwai877211f2005-11-17 13:59:38 +01001171 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 int res;
1173
1174 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001175 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1177 snd_power_unlock(card);
1178 return res;
1179}
1180
1181#else
1182
Takashi Iwai877211f2005-11-17 13:59:38 +01001183static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 return -ENOSYS;
1186}
1187
1188#endif /* CONFIG_PM */
1189
1190/*
1191 * xrun ioctl
1192 *
1193 * Change the RUNNING stream(s) to XRUN state.
1194 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001195static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Takashi Iwai877211f2005-11-17 13:59:38 +01001197 struct snd_card *card = substream->pcm->card;
1198 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int result;
1200
1201 snd_power_lock(card);
1202 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001203 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (result < 0)
1205 goto _unlock;
1206 }
1207
1208 snd_pcm_stream_lock_irq(substream);
1209 switch (runtime->status->state) {
1210 case SNDRV_PCM_STATE_XRUN:
1211 result = 0; /* already there */
1212 break;
1213 case SNDRV_PCM_STATE_RUNNING:
1214 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1215 break;
1216 default:
1217 result = -EBADFD;
1218 }
1219 snd_pcm_stream_unlock_irq(substream);
1220 _unlock:
1221 snd_power_unlock(card);
1222 return result;
1223}
1224
1225/*
1226 * reset ioctl
1227 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001228static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Takashi Iwai877211f2005-11-17 13:59:38 +01001230 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 switch (runtime->status->state) {
1232 case SNDRV_PCM_STATE_RUNNING:
1233 case SNDRV_PCM_STATE_PREPARED:
1234 case SNDRV_PCM_STATE_PAUSED:
1235 case SNDRV_PCM_STATE_SUSPENDED:
1236 return 0;
1237 default:
1238 return -EBADFD;
1239 }
1240}
1241
Takashi Iwai877211f2005-11-17 13:59:38 +01001242static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Takashi Iwai877211f2005-11-17 13:59:38 +01001244 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1246 if (err < 0)
1247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001249 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1250 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 runtime->silence_start = runtime->status->hw_ptr;
1252 runtime->silence_filled = 0;
1253 return 0;
1254}
1255
Takashi Iwai877211f2005-11-17 13:59:38 +01001256static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Takashi Iwai877211f2005-11-17 13:59:38 +01001258 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 runtime->control->appl_ptr = runtime->status->hw_ptr;
1260 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1261 runtime->silence_size > 0)
1262 snd_pcm_playback_silence(substream, ULONG_MAX);
1263}
1264
1265static struct action_ops snd_pcm_action_reset = {
1266 .pre_action = snd_pcm_pre_reset,
1267 .do_action = snd_pcm_do_reset,
1268 .post_action = snd_pcm_post_reset
1269};
1270
Takashi Iwai877211f2005-11-17 13:59:38 +01001271static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1274}
1275
1276/*
1277 * prepare ioctl
1278 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001279/* we use the second argument for updating f_flags */
1280static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1281 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Takashi Iwai877211f2005-11-17 13:59:38 +01001283 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001284 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1285 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -EBADFD;
1287 if (snd_pcm_running(substream))
1288 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001289 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return 0;
1291}
1292
Takashi Iwai877211f2005-11-17 13:59:38 +01001293static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 int err;
1296 err = substream->ops->prepare(substream);
1297 if (err < 0)
1298 return err;
1299 return snd_pcm_do_reset(substream, 0);
1300}
1301
Takashi Iwai877211f2005-11-17 13:59:38 +01001302static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Takashi Iwai877211f2005-11-17 13:59:38 +01001304 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 runtime->control->appl_ptr = runtime->status->hw_ptr;
1306 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1307}
1308
1309static struct action_ops snd_pcm_action_prepare = {
1310 .pre_action = snd_pcm_pre_prepare,
1311 .do_action = snd_pcm_do_prepare,
1312 .post_action = snd_pcm_post_prepare
1313};
1314
1315/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001316 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001317 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001318 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001320static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1321 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001324 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001325 int f_flags;
1326
1327 if (file)
1328 f_flags = file->f_flags;
1329 else
1330 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001333 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001334 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1335 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 snd_power_unlock(card);
1337 return res;
1338}
1339
1340/*
1341 * drain ioctl
1342 */
1343
Takashi Iwai877211f2005-11-17 13:59:38 +01001344static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001346 if (substream->f_flags & O_NONBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EAGAIN;
1348 substream->runtime->trigger_master = substream;
1349 return 0;
1350}
1351
Takashi Iwai877211f2005-11-17 13:59:38 +01001352static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Takashi Iwai877211f2005-11-17 13:59:38 +01001354 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1356 switch (runtime->status->state) {
1357 case SNDRV_PCM_STATE_PREPARED:
1358 /* start playback stream if possible */
1359 if (! snd_pcm_playback_empty(substream)) {
1360 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1361 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1362 }
1363 break;
1364 case SNDRV_PCM_STATE_RUNNING:
1365 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1366 break;
1367 default:
1368 break;
1369 }
1370 } else {
1371 /* stop running stream */
1372 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001373 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001375 snd_pcm_do_stop(substream, new_state);
1376 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 }
1379 return 0;
1380}
1381
Takashi Iwai877211f2005-11-17 13:59:38 +01001382static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384}
1385
1386static struct action_ops snd_pcm_action_drain_init = {
1387 .pre_action = snd_pcm_pre_drain_init,
1388 .do_action = snd_pcm_do_drain_init,
1389 .post_action = snd_pcm_post_drain_init
1390};
1391
1392struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001393 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 wait_queue_t wait;
1395 snd_pcm_uframes_t stop_threshold;
1396};
1397
Takashi Iwai877211f2005-11-17 13:59:38 +01001398static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400/*
1401 * Drain the stream(s).
1402 * When the substream is linked, sync until the draining of all playback streams
1403 * is finished.
1404 * After this call, all streams are supposed to be either SETUP or DRAINING
1405 * (capture only) state.
1406 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001407static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Takashi Iwai877211f2005-11-17 13:59:38 +01001409 struct snd_card *card;
1410 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001411 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 int result = 0;
1413 int i, num_drecs;
1414 struct drain_rec *drec, drec_tmp, *d;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 card = substream->pcm->card;
1417 runtime = substream->runtime;
1418
1419 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1420 return -EBADFD;
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 snd_power_lock(card);
1423 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001424 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001425 if (result < 0) {
1426 snd_power_unlock(card);
1427 return result;
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001432 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (snd_pcm_stream_linked(substream)) {
1434 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1435 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001436 up_read(&snd_pcm_link_rwsem);
1437 snd_power_unlock(card);
1438 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440 } else
1441 drec = &drec_tmp;
1442
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001443 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 num_drecs = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001445 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1448 d = &drec[num_drecs++];
1449 d->substream = s;
1450 init_waitqueue_entry(&d->wait, current);
1451 add_wait_queue(&runtime->sleep, &d->wait);
1452 /* stop_threshold fixup to avoid endless loop when
1453 * stop_threshold > buffer_size
1454 */
1455 d->stop_threshold = runtime->stop_threshold;
1456 if (runtime->stop_threshold > runtime->buffer_size)
1457 runtime->stop_threshold = runtime->buffer_size;
1458 }
1459 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001460 up_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001461
1462 snd_pcm_stream_lock_irq(substream);
1463 /* resume pause */
Takashi Iwai1a7fa542007-07-06 12:27:25 +02001464 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001465 snd_pcm_pause(substream, 0);
1466
1467 /* pre-start/stop - all running streams are changed to DRAINING state */
1468 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1469 if (result < 0) {
1470 snd_pcm_stream_unlock_irq(substream);
1471 goto _error;
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 for (;;) {
1475 long tout;
1476 if (signal_pending(current)) {
1477 result = -ERESTARTSYS;
1478 break;
1479 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001480 /* all finished? */
1481 for (i = 0; i < num_drecs; i++) {
1482 runtime = drec[i].substream->runtime;
1483 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1484 break;
1485 }
1486 if (i == num_drecs)
1487 break; /* yes, all drained */
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 set_current_state(TASK_INTERRUPTIBLE);
1490 snd_pcm_stream_unlock_irq(substream);
1491 snd_power_unlock(card);
1492 tout = schedule_timeout(10 * HZ);
1493 snd_power_lock(card);
1494 snd_pcm_stream_lock_irq(substream);
1495 if (tout == 0) {
1496 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1497 result = -ESTRPIPE;
1498 else {
1499 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1500 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1501 result = -EIO;
1502 }
1503 break;
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001506
1507 snd_pcm_stream_unlock_irq(substream);
1508
1509 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 for (i = 0; i < num_drecs; i++) {
1511 d = &drec[i];
1512 runtime = d->substream->runtime;
1513 remove_wait_queue(&runtime->sleep, &d->wait);
1514 runtime->stop_threshold = d->stop_threshold;
1515 }
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (drec != &drec_tmp)
1518 kfree(drec);
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
1524/*
1525 * drop ioctl
1526 *
1527 * Immediately put all linked substreams into SETUP state.
1528 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001529static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Takashi Iwai877211f2005-11-17 13:59:38 +01001531 struct snd_pcm_runtime *runtime;
1532 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int result = 0;
1534
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001535 if (PCM_RUNTIME_CHECK(substream))
1536 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 runtime = substream->runtime;
1538 card = substream->pcm->card;
1539
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001540 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001541 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1542 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return -EBADFD;
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 snd_pcm_stream_lock_irq(substream);
1546 /* resume pause */
1547 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1548 snd_pcm_pause(substream, 0);
1549
1550 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1551 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1552 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return result;
1555}
1556
1557
1558/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559static struct file *snd_pcm_file_fd(int fd)
1560{
1561 struct file *file;
1562 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001563 unsigned int minor;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 file = fget(fd);
1566 if (!file)
1567 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001568 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (!S_ISCHR(inode->i_mode) ||
1570 imajor(inode) != snd_major) {
1571 fput(file);
1572 return NULL;
1573 }
1574 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001575 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1576 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 fput(file);
1578 return NULL;
1579 }
1580 return file;
1581}
1582
1583/*
1584 * PCM link handling
1585 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001586static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 int res = 0;
1589 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001590 struct snd_pcm_file *pcm_file;
1591 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 file = snd_pcm_file_fd(fd);
1594 if (!file)
1595 return -EBADFD;
1596 pcm_file = file->private_data;
1597 substream1 = pcm_file->substream;
1598 down_write(&snd_pcm_link_rwsem);
1599 write_lock_irq(&snd_pcm_link_rwlock);
1600 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1601 substream->runtime->status->state != substream1->runtime->status->state) {
1602 res = -EBADFD;
1603 goto _end;
1604 }
1605 if (snd_pcm_stream_linked(substream1)) {
1606 res = -EALREADY;
1607 goto _end;
1608 }
1609 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001610 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (substream->group == NULL) {
1612 res = -ENOMEM;
1613 goto _end;
1614 }
1615 spin_lock_init(&substream->group->lock);
1616 INIT_LIST_HEAD(&substream->group->substreams);
1617 list_add_tail(&substream->link_list, &substream->group->substreams);
1618 substream->group->count = 1;
1619 }
1620 list_add_tail(&substream1->link_list, &substream->group->substreams);
1621 substream->group->count++;
1622 substream1->group = substream->group;
1623 _end:
1624 write_unlock_irq(&snd_pcm_link_rwlock);
1625 up_write(&snd_pcm_link_rwsem);
1626 fput(file);
1627 return res;
1628}
1629
Takashi Iwai877211f2005-11-17 13:59:38 +01001630static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 substream->group = &substream->self_group;
1633 INIT_LIST_HEAD(&substream->self_group.substreams);
1634 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1635}
1636
Takashi Iwai877211f2005-11-17 13:59:38 +01001637static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Takashi Iwaief991b92007-02-22 12:52:53 +01001639 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 int res = 0;
1641
1642 down_write(&snd_pcm_link_rwsem);
1643 write_lock_irq(&snd_pcm_link_rwlock);
1644 if (!snd_pcm_stream_linked(substream)) {
1645 res = -EALREADY;
1646 goto _end;
1647 }
1648 list_del(&substream->link_list);
1649 substream->group->count--;
1650 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001651 snd_pcm_group_for_each_entry(s, substream) {
1652 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 }
1655 kfree(substream->group);
1656 }
1657 relink_to_local(substream);
1658 _end:
1659 write_unlock_irq(&snd_pcm_link_rwlock);
1660 up_write(&snd_pcm_link_rwsem);
1661 return res;
1662}
1663
1664/*
1665 * hw configurator
1666 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001667static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1668 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Takashi Iwai877211f2005-11-17 13:59:38 +01001670 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1672 hw_param_interval_c(params, rule->deps[1]), &t);
1673 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1674}
1675
Takashi Iwai877211f2005-11-17 13:59:38 +01001676static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1677 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Takashi Iwai877211f2005-11-17 13:59:38 +01001679 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1681 hw_param_interval_c(params, rule->deps[1]), &t);
1682 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1683}
1684
Takashi Iwai877211f2005-11-17 13:59:38 +01001685static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1686 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Takashi Iwai877211f2005-11-17 13:59:38 +01001688 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1690 hw_param_interval_c(params, rule->deps[1]),
1691 (unsigned long) rule->private, &t);
1692 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1693}
1694
Takashi Iwai877211f2005-11-17 13:59:38 +01001695static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1696 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Takashi Iwai877211f2005-11-17 13:59:38 +01001698 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1700 (unsigned long) rule->private,
1701 hw_param_interval_c(params, rule->deps[1]), &t);
1702 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1703}
1704
Takashi Iwai877211f2005-11-17 13:59:38 +01001705static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1706 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001709 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1710 struct snd_mask m;
1711 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 snd_mask_any(&m);
1713 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1714 int bits;
1715 if (! snd_mask_test(mask, k))
1716 continue;
1717 bits = snd_pcm_format_physical_width(k);
1718 if (bits <= 0)
1719 continue; /* ignore invalid formats */
1720 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1721 snd_mask_reset(&m, k);
1722 }
1723 return snd_mask_refine(mask, &m);
1724}
1725
Takashi Iwai877211f2005-11-17 13:59:38 +01001726static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1727 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Takashi Iwai877211f2005-11-17 13:59:38 +01001729 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 unsigned int k;
1731 t.min = UINT_MAX;
1732 t.max = 0;
1733 t.openmin = 0;
1734 t.openmax = 0;
1735 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1736 int bits;
1737 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1738 continue;
1739 bits = snd_pcm_format_physical_width(k);
1740 if (bits <= 0)
1741 continue; /* ignore invalid formats */
1742 if (t.min > (unsigned)bits)
1743 t.min = bits;
1744 if (t.max < (unsigned)bits)
1745 t.max = bits;
1746 }
1747 t.integer = 1;
1748 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1749}
1750
1751#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1752#error "Change this table"
1753#endif
1754
1755static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1756 48000, 64000, 88200, 96000, 176400, 192000 };
1757
Clemens Ladisch7653d552007-08-13 17:38:54 +02001758const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1759 .count = ARRAY_SIZE(rates),
1760 .list = rates,
1761};
1762
Takashi Iwai877211f2005-11-17 13:59:38 +01001763static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1764 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Takashi Iwai877211f2005-11-17 13:59:38 +01001766 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001768 snd_pcm_known_rates.count,
1769 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Takashi Iwai877211f2005-11-17 13:59:38 +01001772static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1773 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Takashi Iwai877211f2005-11-17 13:59:38 +01001775 struct snd_interval t;
1776 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 t.min = 0;
1778 t.max = substream->buffer_bytes_max;
1779 t.openmin = 0;
1780 t.openmax = 0;
1781 t.integer = 1;
1782 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1783}
1784
Takashi Iwai877211f2005-11-17 13:59:38 +01001785int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Takashi Iwai877211f2005-11-17 13:59:38 +01001787 struct snd_pcm_runtime *runtime = substream->runtime;
1788 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 int k, err;
1790
1791 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1792 snd_mask_any(constrs_mask(constrs, k));
1793 }
1794
1795 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1796 snd_interval_any(constrs_interval(constrs, k));
1797 }
1798
1799 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1800 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1801 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1802 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1803 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1804
1805 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1806 snd_pcm_hw_rule_format, NULL,
1807 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1808 if (err < 0)
1809 return err;
1810 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1811 snd_pcm_hw_rule_sample_bits, NULL,
1812 SNDRV_PCM_HW_PARAM_FORMAT,
1813 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1814 if (err < 0)
1815 return err;
1816 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1817 snd_pcm_hw_rule_div, NULL,
1818 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1819 if (err < 0)
1820 return err;
1821 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1822 snd_pcm_hw_rule_mul, NULL,
1823 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1824 if (err < 0)
1825 return err;
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1827 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1828 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1829 if (err < 0)
1830 return err;
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1832 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1833 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1834 if (err < 0)
1835 return err;
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1837 snd_pcm_hw_rule_div, NULL,
1838 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1839 if (err < 0)
1840 return err;
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1842 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1843 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1844 if (err < 0)
1845 return err;
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1847 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1848 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1849 if (err < 0)
1850 return err;
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1852 snd_pcm_hw_rule_div, NULL,
1853 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1857 snd_pcm_hw_rule_div, NULL,
1858 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1859 if (err < 0)
1860 return err;
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1862 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1863 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1864 if (err < 0)
1865 return err;
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1867 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1868 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1872 snd_pcm_hw_rule_mul, NULL,
1873 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1877 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1878 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1879 if (err < 0)
1880 return err;
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1882 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1883 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1887 snd_pcm_hw_rule_muldivk, (void*) 8,
1888 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1889 if (err < 0)
1890 return err;
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1892 snd_pcm_hw_rule_muldivk, (void*) 8,
1893 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1894 if (err < 0)
1895 return err;
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1897 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1898 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1899 if (err < 0)
1900 return err;
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1902 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1903 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1904 if (err < 0)
1905 return err;
1906 return 0;
1907}
1908
Takashi Iwai877211f2005-11-17 13:59:38 +01001909int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Takashi Iwai877211f2005-11-17 13:59:38 +01001911 struct snd_pcm_runtime *runtime = substream->runtime;
1912 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 int err;
1914 unsigned int mask = 0;
1915
1916 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1917 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1918 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1919 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1920 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1921 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1922 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1923 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1924 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1925 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1926 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1927 }
1928 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001929 if (err < 0)
1930 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001933 if (err < 0)
1934 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001937 if (err < 0)
1938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1941 hw->channels_min, hw->channels_max);
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_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1946 hw->rate_min, hw->rate_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001947 if (err < 0)
1948 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1951 hw->period_bytes_min, hw->period_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001952 if (err < 0)
1953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1956 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001957 if (err < 0)
1958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1961 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001962 if (err < 0)
1963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1966 snd_pcm_hw_rule_buffer_bytes_max, substream,
1967 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1968 if (err < 0)
1969 return err;
1970
1971 /* FIXME: remove */
1972 if (runtime->dma_bytes) {
1973 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001974 if (err < 0)
1975 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977
1978 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1979 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1980 snd_pcm_hw_rule_rate, hw,
1981 SNDRV_PCM_HW_PARAM_RATE, -1);
1982 if (err < 0)
1983 return err;
1984 }
1985
1986 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1988
1989 return 0;
1990}
1991
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001992static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001995}
1996
1997void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1998{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001999 substream->ref_count--;
2000 if (substream->ref_count > 0)
2001 return;
2002
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002003 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002004 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (substream->ops->hw_free != NULL)
2006 substream->ops->hw_free(substream);
2007 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002008 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Takashi Iwai15762742006-04-06 19:47:42 +02002010 if (substream->pcm_release) {
2011 substream->pcm_release(substream);
2012 substream->pcm_release = NULL;
2013 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002014 snd_pcm_detach_substream(substream);
2015}
2016
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002017EXPORT_SYMBOL(snd_pcm_release_substream);
2018
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002019int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2020 struct file *file,
2021 struct snd_pcm_substream **rsubstream)
2022{
2023 struct snd_pcm_substream *substream;
2024 int err;
2025
2026 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2027 if (err < 0)
2028 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002029 if (substream->ref_count > 1) {
2030 *rsubstream = substream;
2031 return 0;
2032 }
2033
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002034 err = snd_pcm_hw_constraints_init(substream);
2035 if (err < 0) {
2036 snd_printd("snd_pcm_hw_constraints_init failed\n");
2037 goto error;
2038 }
2039
2040 if ((err = substream->ops->open(substream)) < 0)
2041 goto error;
2042
2043 substream->hw_opened = 1;
2044
2045 err = snd_pcm_hw_constraints_complete(substream);
2046 if (err < 0) {
2047 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2048 goto error;
2049 }
2050
2051 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002053
2054 error:
2055 snd_pcm_release_substream(substream);
2056 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002059EXPORT_SYMBOL(snd_pcm_open_substream);
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002062 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002064 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Takashi Iwai877211f2005-11-17 13:59:38 +01002066 struct snd_pcm_file *pcm_file;
2067 struct snd_pcm_substream *substream;
2068 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002069 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002071 if (rpcm_file)
2072 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002074 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2075 if (err < 0)
2076 return err;
2077
Takashi Iwai548a6482006-07-31 16:51:51 +02002078 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2079 if (pcm_file == NULL) {
2080 snd_pcm_release_substream(substream);
2081 return -ENOMEM;
2082 }
2083 pcm_file->substream = substream;
2084 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002085 str = substream->pstr;
2086 substream->file = pcm_file;
2087 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002090 if (rpcm_file)
2091 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return 0;
2093}
2094
Clemens Ladischf87135f2005-11-20 14:06:59 +01002095static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Takashi Iwai877211f2005-11-17 13:59:38 +01002097 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002098
2099 pcm = snd_lookup_minor_data(iminor(inode),
2100 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2101 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2102}
2103
2104static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2105{
2106 struct snd_pcm *pcm;
2107
2108 pcm = snd_lookup_minor_data(iminor(inode),
2109 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2110 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2111}
2112
2113static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2114{
2115 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002116 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 wait_queue_t wait;
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (pcm == NULL) {
2120 err = -ENODEV;
2121 goto __error1;
2122 }
2123 err = snd_card_file_add(pcm->card, file);
2124 if (err < 0)
2125 goto __error1;
2126 if (!try_module_get(pcm->card->module)) {
2127 err = -EFAULT;
2128 goto __error2;
2129 }
2130 init_waitqueue_entry(&wait, current);
2131 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002132 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002134 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (err >= 0)
2136 break;
2137 if (err == -EAGAIN) {
2138 if (file->f_flags & O_NONBLOCK) {
2139 err = -EBUSY;
2140 break;
2141 }
2142 } else
2143 break;
2144 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002145 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002147 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (signal_pending(current)) {
2149 err = -ERESTARTSYS;
2150 break;
2151 }
2152 }
2153 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002154 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (err < 0)
2156 goto __error;
2157 return err;
2158
2159 __error:
2160 module_put(pcm->card->module);
2161 __error2:
2162 snd_card_file_remove(pcm->card, file);
2163 __error1:
2164 return err;
2165}
2166
2167static int snd_pcm_release(struct inode *inode, struct file *file)
2168{
Takashi Iwai877211f2005-11-17 13:59:38 +01002169 struct snd_pcm *pcm;
2170 struct snd_pcm_substream *substream;
2171 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 pcm_file = file->private_data;
2174 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002175 if (snd_BUG_ON(!substream))
2176 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002178 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002179 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002180 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002181 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 wake_up(&pcm->open_wait);
2183 module_put(pcm->card->module);
2184 snd_card_file_remove(pcm->card, file);
2185 return 0;
2186}
2187
Takashi Iwai877211f2005-11-17 13:59:38 +01002188static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2189 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Takashi Iwai877211f2005-11-17 13:59:38 +01002191 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 snd_pcm_sframes_t appl_ptr;
2193 snd_pcm_sframes_t ret;
2194 snd_pcm_sframes_t hw_avail;
2195
2196 if (frames == 0)
2197 return 0;
2198
2199 snd_pcm_stream_lock_irq(substream);
2200 switch (runtime->status->state) {
2201 case SNDRV_PCM_STATE_PREPARED:
2202 break;
2203 case SNDRV_PCM_STATE_DRAINING:
2204 case SNDRV_PCM_STATE_RUNNING:
2205 if (snd_pcm_update_hw_ptr(substream) >= 0)
2206 break;
2207 /* Fall through */
2208 case SNDRV_PCM_STATE_XRUN:
2209 ret = -EPIPE;
2210 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002211 case SNDRV_PCM_STATE_SUSPENDED:
2212 ret = -ESTRPIPE;
2213 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 default:
2215 ret = -EBADFD;
2216 goto __end;
2217 }
2218
2219 hw_avail = snd_pcm_playback_hw_avail(runtime);
2220 if (hw_avail <= 0) {
2221 ret = 0;
2222 goto __end;
2223 }
2224 if (frames > (snd_pcm_uframes_t)hw_avail)
2225 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 appl_ptr = runtime->control->appl_ptr - frames;
2227 if (appl_ptr < 0)
2228 appl_ptr += runtime->boundary;
2229 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 ret = frames;
2231 __end:
2232 snd_pcm_stream_unlock_irq(substream);
2233 return ret;
2234}
2235
Takashi Iwai877211f2005-11-17 13:59:38 +01002236static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2237 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Takashi Iwai877211f2005-11-17 13:59:38 +01002239 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 snd_pcm_sframes_t appl_ptr;
2241 snd_pcm_sframes_t ret;
2242 snd_pcm_sframes_t hw_avail;
2243
2244 if (frames == 0)
2245 return 0;
2246
2247 snd_pcm_stream_lock_irq(substream);
2248 switch (runtime->status->state) {
2249 case SNDRV_PCM_STATE_PREPARED:
2250 case SNDRV_PCM_STATE_DRAINING:
2251 break;
2252 case SNDRV_PCM_STATE_RUNNING:
2253 if (snd_pcm_update_hw_ptr(substream) >= 0)
2254 break;
2255 /* Fall through */
2256 case SNDRV_PCM_STATE_XRUN:
2257 ret = -EPIPE;
2258 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002259 case SNDRV_PCM_STATE_SUSPENDED:
2260 ret = -ESTRPIPE;
2261 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 default:
2263 ret = -EBADFD;
2264 goto __end;
2265 }
2266
2267 hw_avail = snd_pcm_capture_hw_avail(runtime);
2268 if (hw_avail <= 0) {
2269 ret = 0;
2270 goto __end;
2271 }
2272 if (frames > (snd_pcm_uframes_t)hw_avail)
2273 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 appl_ptr = runtime->control->appl_ptr - frames;
2275 if (appl_ptr < 0)
2276 appl_ptr += runtime->boundary;
2277 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 ret = frames;
2279 __end:
2280 snd_pcm_stream_unlock_irq(substream);
2281 return ret;
2282}
2283
Takashi Iwai877211f2005-11-17 13:59:38 +01002284static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2285 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Takashi Iwai877211f2005-11-17 13:59:38 +01002287 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 snd_pcm_sframes_t appl_ptr;
2289 snd_pcm_sframes_t ret;
2290 snd_pcm_sframes_t avail;
2291
2292 if (frames == 0)
2293 return 0;
2294
2295 snd_pcm_stream_lock_irq(substream);
2296 switch (runtime->status->state) {
2297 case SNDRV_PCM_STATE_PREPARED:
2298 case SNDRV_PCM_STATE_PAUSED:
2299 break;
2300 case SNDRV_PCM_STATE_DRAINING:
2301 case SNDRV_PCM_STATE_RUNNING:
2302 if (snd_pcm_update_hw_ptr(substream) >= 0)
2303 break;
2304 /* Fall through */
2305 case SNDRV_PCM_STATE_XRUN:
2306 ret = -EPIPE;
2307 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002308 case SNDRV_PCM_STATE_SUSPENDED:
2309 ret = -ESTRPIPE;
2310 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 default:
2312 ret = -EBADFD;
2313 goto __end;
2314 }
2315
2316 avail = snd_pcm_playback_avail(runtime);
2317 if (avail <= 0) {
2318 ret = 0;
2319 goto __end;
2320 }
2321 if (frames > (snd_pcm_uframes_t)avail)
2322 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 appl_ptr = runtime->control->appl_ptr + frames;
2324 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2325 appl_ptr -= runtime->boundary;
2326 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 ret = frames;
2328 __end:
2329 snd_pcm_stream_unlock_irq(substream);
2330 return ret;
2331}
2332
Takashi Iwai877211f2005-11-17 13:59:38 +01002333static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2334 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Takashi Iwai877211f2005-11-17 13:59:38 +01002336 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 snd_pcm_sframes_t appl_ptr;
2338 snd_pcm_sframes_t ret;
2339 snd_pcm_sframes_t avail;
2340
2341 if (frames == 0)
2342 return 0;
2343
2344 snd_pcm_stream_lock_irq(substream);
2345 switch (runtime->status->state) {
2346 case SNDRV_PCM_STATE_PREPARED:
2347 case SNDRV_PCM_STATE_DRAINING:
2348 case SNDRV_PCM_STATE_PAUSED:
2349 break;
2350 case SNDRV_PCM_STATE_RUNNING:
2351 if (snd_pcm_update_hw_ptr(substream) >= 0)
2352 break;
2353 /* Fall through */
2354 case SNDRV_PCM_STATE_XRUN:
2355 ret = -EPIPE;
2356 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002357 case SNDRV_PCM_STATE_SUSPENDED:
2358 ret = -ESTRPIPE;
2359 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 default:
2361 ret = -EBADFD;
2362 goto __end;
2363 }
2364
2365 avail = snd_pcm_capture_avail(runtime);
2366 if (avail <= 0) {
2367 ret = 0;
2368 goto __end;
2369 }
2370 if (frames > (snd_pcm_uframes_t)avail)
2371 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 appl_ptr = runtime->control->appl_ptr + frames;
2373 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2374 appl_ptr -= runtime->boundary;
2375 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 ret = frames;
2377 __end:
2378 snd_pcm_stream_unlock_irq(substream);
2379 return ret;
2380}
2381
Takashi Iwai877211f2005-11-17 13:59:38 +01002382static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
Takashi Iwai877211f2005-11-17 13:59:38 +01002384 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 int err;
2386
2387 snd_pcm_stream_lock_irq(substream);
2388 switch (runtime->status->state) {
2389 case SNDRV_PCM_STATE_DRAINING:
2390 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2391 goto __badfd;
2392 case SNDRV_PCM_STATE_RUNNING:
2393 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2394 break;
2395 /* Fall through */
2396 case SNDRV_PCM_STATE_PREPARED:
2397 case SNDRV_PCM_STATE_SUSPENDED:
2398 err = 0;
2399 break;
2400 case SNDRV_PCM_STATE_XRUN:
2401 err = -EPIPE;
2402 break;
2403 default:
2404 __badfd:
2405 err = -EBADFD;
2406 break;
2407 }
2408 snd_pcm_stream_unlock_irq(substream);
2409 return err;
2410}
2411
Takashi Iwai877211f2005-11-17 13:59:38 +01002412static int snd_pcm_delay(struct snd_pcm_substream *substream,
2413 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Takashi Iwai877211f2005-11-17 13:59:38 +01002415 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 int err;
2417 snd_pcm_sframes_t n = 0;
2418
2419 snd_pcm_stream_lock_irq(substream);
2420 switch (runtime->status->state) {
2421 case SNDRV_PCM_STATE_DRAINING:
2422 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2423 goto __badfd;
2424 case SNDRV_PCM_STATE_RUNNING:
2425 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2426 break;
2427 /* Fall through */
2428 case SNDRV_PCM_STATE_PREPARED:
2429 case SNDRV_PCM_STATE_SUSPENDED:
2430 err = 0;
2431 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2432 n = snd_pcm_playback_hw_avail(runtime);
2433 else
2434 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002435 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 break;
2437 case SNDRV_PCM_STATE_XRUN:
2438 err = -EPIPE;
2439 break;
2440 default:
2441 __badfd:
2442 err = -EBADFD;
2443 break;
2444 }
2445 snd_pcm_stream_unlock_irq(substream);
2446 if (!err)
2447 if (put_user(n, res))
2448 err = -EFAULT;
2449 return err;
2450}
2451
Takashi Iwai877211f2005-11-17 13:59:38 +01002452static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2453 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Takashi Iwai877211f2005-11-17 13:59:38 +01002455 struct snd_pcm_runtime *runtime = substream->runtime;
2456 struct snd_pcm_sync_ptr sync_ptr;
2457 volatile struct snd_pcm_mmap_status *status;
2458 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 int err;
2460
2461 memset(&sync_ptr, 0, sizeof(sync_ptr));
2462 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2463 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002464 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 -07002465 return -EFAULT;
2466 status = runtime->status;
2467 control = runtime->control;
2468 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2469 err = snd_pcm_hwsync(substream);
2470 if (err < 0)
2471 return err;
2472 }
2473 snd_pcm_stream_lock_irq(substream);
2474 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2475 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2476 else
2477 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2478 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2479 control->avail_min = sync_ptr.c.control.avail_min;
2480 else
2481 sync_ptr.c.control.avail_min = control->avail_min;
2482 sync_ptr.s.status.state = status->state;
2483 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2484 sync_ptr.s.status.tstamp = status->tstamp;
2485 sync_ptr.s.status.suspended_state = status->suspended_state;
2486 snd_pcm_stream_unlock_irq(substream);
2487 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2488 return -EFAULT;
2489 return 0;
2490}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002491
2492static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2493{
2494 struct snd_pcm_runtime *runtime = substream->runtime;
2495 int arg;
2496
2497 if (get_user(arg, _arg))
2498 return -EFAULT;
2499 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2500 return -EINVAL;
2501 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2502 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2503 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2504 return 0;
2505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Takashi Iwai0df63e42006-04-28 15:13:41 +02002507static int snd_pcm_common_ioctl1(struct file *file,
2508 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 unsigned int cmd, void __user *arg)
2510{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 switch (cmd) {
2512 case SNDRV_PCM_IOCTL_PVERSION:
2513 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2514 case SNDRV_PCM_IOCTL_INFO:
2515 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002516 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2517 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002518 case SNDRV_PCM_IOCTL_TTSTAMP:
2519 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 case SNDRV_PCM_IOCTL_HW_REFINE:
2521 return snd_pcm_hw_refine_user(substream, arg);
2522 case SNDRV_PCM_IOCTL_HW_PARAMS:
2523 return snd_pcm_hw_params_user(substream, arg);
2524 case SNDRV_PCM_IOCTL_HW_FREE:
2525 return snd_pcm_hw_free(substream);
2526 case SNDRV_PCM_IOCTL_SW_PARAMS:
2527 return snd_pcm_sw_params_user(substream, arg);
2528 case SNDRV_PCM_IOCTL_STATUS:
2529 return snd_pcm_status_user(substream, arg);
2530 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2531 return snd_pcm_channel_info_user(substream, arg);
2532 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002533 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 case SNDRV_PCM_IOCTL_RESET:
2535 return snd_pcm_reset(substream);
2536 case SNDRV_PCM_IOCTL_START:
2537 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2538 case SNDRV_PCM_IOCTL_LINK:
2539 return snd_pcm_link(substream, (int)(unsigned long) arg);
2540 case SNDRV_PCM_IOCTL_UNLINK:
2541 return snd_pcm_unlink(substream);
2542 case SNDRV_PCM_IOCTL_RESUME:
2543 return snd_pcm_resume(substream);
2544 case SNDRV_PCM_IOCTL_XRUN:
2545 return snd_pcm_xrun(substream);
2546 case SNDRV_PCM_IOCTL_HWSYNC:
2547 return snd_pcm_hwsync(substream);
2548 case SNDRV_PCM_IOCTL_DELAY:
2549 return snd_pcm_delay(substream, arg);
2550 case SNDRV_PCM_IOCTL_SYNC_PTR:
2551 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002552#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2554 return snd_pcm_hw_refine_old_user(substream, arg);
2555 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2556 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002557#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 case SNDRV_PCM_IOCTL_DRAIN:
2559 return snd_pcm_drain(substream);
2560 case SNDRV_PCM_IOCTL_DROP:
2561 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002562 case SNDRV_PCM_IOCTL_PAUSE:
2563 {
2564 int res;
2565 snd_pcm_stream_lock_irq(substream);
2566 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2567 snd_pcm_stream_unlock_irq(substream);
2568 return res;
2569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 snd_printd("unknown ioctl = 0x%x\n", cmd);
2572 return -ENOTTY;
2573}
2574
Takashi Iwai0df63e42006-04-28 15:13:41 +02002575static int snd_pcm_playback_ioctl1(struct file *file,
2576 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 unsigned int cmd, void __user *arg)
2578{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002579 if (snd_BUG_ON(!substream))
2580 return -ENXIO;
2581 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2582 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 switch (cmd) {
2584 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2585 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002586 struct snd_xferi xferi;
2587 struct snd_xferi __user *_xferi = arg;
2588 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 snd_pcm_sframes_t result;
2590 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2591 return -EBADFD;
2592 if (put_user(0, &_xferi->result))
2593 return -EFAULT;
2594 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2595 return -EFAULT;
2596 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2597 __put_user(result, &_xferi->result);
2598 return result < 0 ? result : 0;
2599 }
2600 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2601 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002602 struct snd_xfern xfern;
2603 struct snd_xfern __user *_xfern = arg;
2604 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 void __user **bufs;
2606 snd_pcm_sframes_t result;
2607 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2608 return -EBADFD;
2609 if (runtime->channels > 128)
2610 return -EINVAL;
2611 if (put_user(0, &_xfern->result))
2612 return -EFAULT;
2613 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2614 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002615
2616 bufs = memdup_user(xfern.bufs,
2617 sizeof(void *) * runtime->channels);
2618 if (IS_ERR(bufs))
2619 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2621 kfree(bufs);
2622 __put_user(result, &_xfern->result);
2623 return result < 0 ? result : 0;
2624 }
2625 case SNDRV_PCM_IOCTL_REWIND:
2626 {
2627 snd_pcm_uframes_t frames;
2628 snd_pcm_uframes_t __user *_frames = arg;
2629 snd_pcm_sframes_t result;
2630 if (get_user(frames, _frames))
2631 return -EFAULT;
2632 if (put_user(0, _frames))
2633 return -EFAULT;
2634 result = snd_pcm_playback_rewind(substream, frames);
2635 __put_user(result, _frames);
2636 return result < 0 ? result : 0;
2637 }
2638 case SNDRV_PCM_IOCTL_FORWARD:
2639 {
2640 snd_pcm_uframes_t frames;
2641 snd_pcm_uframes_t __user *_frames = arg;
2642 snd_pcm_sframes_t result;
2643 if (get_user(frames, _frames))
2644 return -EFAULT;
2645 if (put_user(0, _frames))
2646 return -EFAULT;
2647 result = snd_pcm_playback_forward(substream, frames);
2648 __put_user(result, _frames);
2649 return result < 0 ? result : 0;
2650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002652 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653}
2654
Takashi Iwai0df63e42006-04-28 15:13:41 +02002655static int snd_pcm_capture_ioctl1(struct file *file,
2656 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 unsigned int cmd, void __user *arg)
2658{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002659 if (snd_BUG_ON(!substream))
2660 return -ENXIO;
2661 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2662 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 switch (cmd) {
2664 case SNDRV_PCM_IOCTL_READI_FRAMES:
2665 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002666 struct snd_xferi xferi;
2667 struct snd_xferi __user *_xferi = arg;
2668 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 snd_pcm_sframes_t result;
2670 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2671 return -EBADFD;
2672 if (put_user(0, &_xferi->result))
2673 return -EFAULT;
2674 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2675 return -EFAULT;
2676 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2677 __put_user(result, &_xferi->result);
2678 return result < 0 ? result : 0;
2679 }
2680 case SNDRV_PCM_IOCTL_READN_FRAMES:
2681 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002682 struct snd_xfern xfern;
2683 struct snd_xfern __user *_xfern = arg;
2684 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 void *bufs;
2686 snd_pcm_sframes_t result;
2687 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2688 return -EBADFD;
2689 if (runtime->channels > 128)
2690 return -EINVAL;
2691 if (put_user(0, &_xfern->result))
2692 return -EFAULT;
2693 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2694 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002695
2696 bufs = memdup_user(xfern.bufs,
2697 sizeof(void *) * runtime->channels);
2698 if (IS_ERR(bufs))
2699 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2701 kfree(bufs);
2702 __put_user(result, &_xfern->result);
2703 return result < 0 ? result : 0;
2704 }
2705 case SNDRV_PCM_IOCTL_REWIND:
2706 {
2707 snd_pcm_uframes_t frames;
2708 snd_pcm_uframes_t __user *_frames = arg;
2709 snd_pcm_sframes_t result;
2710 if (get_user(frames, _frames))
2711 return -EFAULT;
2712 if (put_user(0, _frames))
2713 return -EFAULT;
2714 result = snd_pcm_capture_rewind(substream, frames);
2715 __put_user(result, _frames);
2716 return result < 0 ? result : 0;
2717 }
2718 case SNDRV_PCM_IOCTL_FORWARD:
2719 {
2720 snd_pcm_uframes_t frames;
2721 snd_pcm_uframes_t __user *_frames = arg;
2722 snd_pcm_sframes_t result;
2723 if (get_user(frames, _frames))
2724 return -EFAULT;
2725 if (put_user(0, _frames))
2726 return -EFAULT;
2727 result = snd_pcm_capture_forward(substream, frames);
2728 __put_user(result, _frames);
2729 return result < 0 ? result : 0;
2730 }
2731 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002732 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
Takashi Iwai877211f2005-11-17 13:59:38 +01002735static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2736 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Takashi Iwai877211f2005-11-17 13:59:38 +01002738 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 pcm_file = file->private_data;
2741
2742 if (((cmd >> 8) & 0xff) != 'A')
2743 return -ENOTTY;
2744
Takashi Iwai0df63e42006-04-28 15:13:41 +02002745 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2746 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
Takashi Iwai877211f2005-11-17 13:59:38 +01002749static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2750 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
Takashi Iwai877211f2005-11-17 13:59:38 +01002752 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 pcm_file = file->private_data;
2755
2756 if (((cmd >> 8) & 0xff) != 'A')
2757 return -ENOTTY;
2758
Takashi Iwai0df63e42006-04-28 15:13:41 +02002759 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2760 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Takashi Iwai877211f2005-11-17 13:59:38 +01002763int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 unsigned int cmd, void *arg)
2765{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002766 mm_segment_t fs;
2767 int result;
2768
2769 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 switch (substream->stream) {
2771 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002772 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2773 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002776 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2777 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002780 result = -EINVAL;
2781 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002783 snd_leave_user(fs);
2784 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
2786
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002787EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2788
Takashi Iwai877211f2005-11-17 13:59:38 +01002789static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2790 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Takashi Iwai877211f2005-11-17 13:59:38 +01002792 struct snd_pcm_file *pcm_file;
2793 struct snd_pcm_substream *substream;
2794 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 snd_pcm_sframes_t result;
2796
2797 pcm_file = file->private_data;
2798 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002799 if (PCM_RUNTIME_CHECK(substream))
2800 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 runtime = substream->runtime;
2802 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2803 return -EBADFD;
2804 if (!frame_aligned(runtime, count))
2805 return -EINVAL;
2806 count = bytes_to_frames(runtime, count);
2807 result = snd_pcm_lib_read(substream, buf, count);
2808 if (result > 0)
2809 result = frames_to_bytes(runtime, result);
2810 return result;
2811}
2812
Takashi Iwai877211f2005-11-17 13:59:38 +01002813static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2814 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Takashi Iwai877211f2005-11-17 13:59:38 +01002816 struct snd_pcm_file *pcm_file;
2817 struct snd_pcm_substream *substream;
2818 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 snd_pcm_sframes_t result;
2820
2821 pcm_file = file->private_data;
2822 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002823 if (PCM_RUNTIME_CHECK(substream))
2824 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002826 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2827 return -EBADFD;
2828 if (!frame_aligned(runtime, count))
2829 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 count = bytes_to_frames(runtime, count);
2831 result = snd_pcm_lib_write(substream, buf, count);
2832 if (result > 0)
2833 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return result;
2835}
2836
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002837static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2838 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840{
Takashi Iwai877211f2005-11-17 13:59:38 +01002841 struct snd_pcm_file *pcm_file;
2842 struct snd_pcm_substream *substream;
2843 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 snd_pcm_sframes_t result;
2845 unsigned long i;
2846 void __user **bufs;
2847 snd_pcm_uframes_t frames;
2848
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002849 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002851 if (PCM_RUNTIME_CHECK(substream))
2852 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 runtime = substream->runtime;
2854 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2855 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002856 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002858 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002860 frames = bytes_to_samples(runtime, iov->iov_len);
2861 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (bufs == NULL)
2863 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002864 for (i = 0; i < nr_segs; ++i)
2865 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 result = snd_pcm_lib_readv(substream, bufs, frames);
2867 if (result > 0)
2868 result = frames_to_bytes(runtime, result);
2869 kfree(bufs);
2870 return result;
2871}
2872
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002873static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2874 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Takashi Iwai877211f2005-11-17 13:59:38 +01002876 struct snd_pcm_file *pcm_file;
2877 struct snd_pcm_substream *substream;
2878 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 snd_pcm_sframes_t result;
2880 unsigned long i;
2881 void __user **bufs;
2882 snd_pcm_uframes_t frames;
2883
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002884 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002886 if (PCM_RUNTIME_CHECK(substream))
2887 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002889 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2890 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002891 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002892 !frame_aligned(runtime, iov->iov_len))
2893 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002894 frames = bytes_to_samples(runtime, iov->iov_len);
2895 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (bufs == NULL)
2897 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002898 for (i = 0; i < nr_segs; ++i)
2899 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 result = snd_pcm_lib_writev(substream, bufs, frames);
2901 if (result > 0)
2902 result = frames_to_bytes(runtime, result);
2903 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return result;
2905}
2906
2907static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2908{
Takashi Iwai877211f2005-11-17 13:59:38 +01002909 struct snd_pcm_file *pcm_file;
2910 struct snd_pcm_substream *substream;
2911 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 unsigned int mask;
2913 snd_pcm_uframes_t avail;
2914
2915 pcm_file = file->private_data;
2916
2917 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002918 if (PCM_RUNTIME_CHECK(substream))
2919 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 runtime = substream->runtime;
2921
2922 poll_wait(file, &runtime->sleep, wait);
2923
2924 snd_pcm_stream_lock_irq(substream);
2925 avail = snd_pcm_playback_avail(runtime);
2926 switch (runtime->status->state) {
2927 case SNDRV_PCM_STATE_RUNNING:
2928 case SNDRV_PCM_STATE_PREPARED:
2929 case SNDRV_PCM_STATE_PAUSED:
2930 if (avail >= runtime->control->avail_min) {
2931 mask = POLLOUT | POLLWRNORM;
2932 break;
2933 }
2934 /* Fall through */
2935 case SNDRV_PCM_STATE_DRAINING:
2936 mask = 0;
2937 break;
2938 default:
2939 mask = POLLOUT | POLLWRNORM | POLLERR;
2940 break;
2941 }
2942 snd_pcm_stream_unlock_irq(substream);
2943 return mask;
2944}
2945
2946static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2947{
Takashi Iwai877211f2005-11-17 13:59:38 +01002948 struct snd_pcm_file *pcm_file;
2949 struct snd_pcm_substream *substream;
2950 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 unsigned int mask;
2952 snd_pcm_uframes_t avail;
2953
2954 pcm_file = file->private_data;
2955
2956 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002957 if (PCM_RUNTIME_CHECK(substream))
2958 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 runtime = substream->runtime;
2960
2961 poll_wait(file, &runtime->sleep, wait);
2962
2963 snd_pcm_stream_lock_irq(substream);
2964 avail = snd_pcm_capture_avail(runtime);
2965 switch (runtime->status->state) {
2966 case SNDRV_PCM_STATE_RUNNING:
2967 case SNDRV_PCM_STATE_PREPARED:
2968 case SNDRV_PCM_STATE_PAUSED:
2969 if (avail >= runtime->control->avail_min) {
2970 mask = POLLIN | POLLRDNORM;
2971 break;
2972 }
2973 mask = 0;
2974 break;
2975 case SNDRV_PCM_STATE_DRAINING:
2976 if (avail > 0) {
2977 mask = POLLIN | POLLRDNORM;
2978 break;
2979 }
2980 /* Fall through */
2981 default:
2982 mask = POLLIN | POLLRDNORM | POLLERR;
2983 break;
2984 }
2985 snd_pcm_stream_unlock_irq(substream);
2986 return mask;
2987}
2988
2989/*
2990 * mmap support
2991 */
2992
2993/*
2994 * Only on coherent architectures, we can mmap the status and the control records
2995 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2996 */
2997#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2998/*
2999 * mmap status record
3000 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003001static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3002 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Takashi Iwai877211f2005-11-17 13:59:38 +01003004 struct snd_pcm_substream *substream = area->vm_private_data;
3005 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003008 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003010 vmf->page = virt_to_page(runtime->status);
3011 get_page(vmf->page);
3012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
3015static struct vm_operations_struct snd_pcm_vm_ops_status =
3016{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003017 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018};
3019
Takashi Iwai877211f2005-11-17 13:59:38 +01003020static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 struct vm_area_struct *area)
3022{
Takashi Iwai877211f2005-11-17 13:59:38 +01003023 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 long size;
3025 if (!(area->vm_flags & VM_READ))
3026 return -EINVAL;
3027 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003029 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return -EINVAL;
3031 area->vm_ops = &snd_pcm_vm_ops_status;
3032 area->vm_private_data = substream;
3033 area->vm_flags |= VM_RESERVED;
3034 return 0;
3035}
3036
3037/*
3038 * mmap control record
3039 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003040static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3041 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Takashi Iwai877211f2005-11-17 13:59:38 +01003043 struct snd_pcm_substream *substream = area->vm_private_data;
3044 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003047 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003049 vmf->page = virt_to_page(runtime->control);
3050 get_page(vmf->page);
3051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
3054static struct vm_operations_struct snd_pcm_vm_ops_control =
3055{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003056 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057};
3058
Takashi Iwai877211f2005-11-17 13:59:38 +01003059static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 struct vm_area_struct *area)
3061{
Takashi Iwai877211f2005-11-17 13:59:38 +01003062 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 long size;
3064 if (!(area->vm_flags & VM_READ))
3065 return -EINVAL;
3066 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003068 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return -EINVAL;
3070 area->vm_ops = &snd_pcm_vm_ops_control;
3071 area->vm_private_data = substream;
3072 area->vm_flags |= VM_RESERVED;
3073 return 0;
3074}
3075#else /* ! coherent mmap */
3076/*
3077 * don't support mmap for status and control records.
3078 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003079static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 struct vm_area_struct *area)
3081{
3082 return -ENXIO;
3083}
Takashi Iwai877211f2005-11-17 13:59:38 +01003084static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 struct vm_area_struct *area)
3086{
3087 return -ENXIO;
3088}
3089#endif /* coherent mmap */
3090
3091/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003092 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003094static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3095 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
Takashi Iwai877211f2005-11-17 13:59:38 +01003097 struct snd_pcm_substream *substream = area->vm_private_data;
3098 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 unsigned long offset;
3100 struct page * page;
3101 void *vaddr;
3102 size_t dma_bytes;
3103
3104 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003105 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003107 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3109 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003110 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 if (substream->ops->page) {
3112 page = substream->ops->page(substream, offset);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003113 if (!page)
3114 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 } else {
3116 vaddr = runtime->dma_area + offset;
3117 page = virt_to_page(vaddr);
3118 }
Nick Pigginb5810032005-10-29 18:16:12 -07003119 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003120 vmf->page = page;
3121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
3123
3124static struct vm_operations_struct snd_pcm_vm_ops_data =
3125{
3126 .open = snd_pcm_mmap_data_open,
3127 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003128 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129};
3130
3131/*
3132 * mmap the DMA buffer on RAM
3133 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003134static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3135 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
3137 area->vm_ops = &snd_pcm_vm_ops_data;
3138 area->vm_private_data = substream;
3139 area->vm_flags |= VM_RESERVED;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003140 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return 0;
3142}
3143
3144/*
3145 * mmap the DMA buffer on I/O memory area
3146 */
3147#if SNDRV_PCM_INFO_MMAP_IOMEM
3148static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3149{
3150 .open = snd_pcm_mmap_data_open,
3151 .close = snd_pcm_mmap_data_close,
3152};
3153
Takashi Iwai877211f2005-11-17 13:59:38 +01003154int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3155 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
3157 long size;
3158 unsigned long offset;
3159
3160#ifdef pgprot_noncached
3161 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3162#endif
3163 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3164 area->vm_private_data = substream;
3165 area->vm_flags |= VM_IO;
3166 size = area->vm_end - area->vm_start;
3167 offset = area->vm_pgoff << PAGE_SHIFT;
3168 if (io_remap_pfn_range(area, area->vm_start,
3169 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3170 size, area->vm_page_prot))
3171 return -EAGAIN;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003172 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return 0;
3174}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003175
3176EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177#endif /* SNDRV_PCM_INFO_MMAP */
3178
3179/*
3180 * mmap DMA buffer
3181 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003182int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 struct vm_area_struct *area)
3184{
Takashi Iwai877211f2005-11-17 13:59:38 +01003185 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 long size;
3187 unsigned long offset;
3188 size_t dma_bytes;
3189
3190 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3191 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3192 return -EINVAL;
3193 } else {
3194 if (!(area->vm_flags & VM_READ))
3195 return -EINVAL;
3196 }
3197 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3199 return -EBADFD;
3200 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3201 return -ENXIO;
3202 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3203 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3204 return -EINVAL;
3205 size = area->vm_end - area->vm_start;
3206 offset = area->vm_pgoff << PAGE_SHIFT;
3207 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3208 if ((size_t)size > dma_bytes)
3209 return -EINVAL;
3210 if (offset > dma_bytes - size)
3211 return -EINVAL;
3212
3213 if (substream->ops->mmap)
3214 return substream->ops->mmap(substream, area);
3215 else
3216 return snd_pcm_default_mmap(substream, area);
3217}
3218
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003219EXPORT_SYMBOL(snd_pcm_mmap_data);
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3222{
Takashi Iwai877211f2005-11-17 13:59:38 +01003223 struct snd_pcm_file * pcm_file;
3224 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 unsigned long offset;
3226
3227 pcm_file = file->private_data;
3228 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003229 if (PCM_RUNTIME_CHECK(substream))
3230 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
3232 offset = area->vm_pgoff << PAGE_SHIFT;
3233 switch (offset) {
3234 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003235 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return -ENXIO;
3237 return snd_pcm_mmap_status(substream, file, area);
3238 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003239 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return -ENXIO;
3241 return snd_pcm_mmap_control(substream, file, area);
3242 default:
3243 return snd_pcm_mmap_data(substream, file, area);
3244 }
3245 return 0;
3246}
3247
3248static int snd_pcm_fasync(int fd, struct file * file, int on)
3249{
Takashi Iwai877211f2005-11-17 13:59:38 +01003250 struct snd_pcm_file * pcm_file;
3251 struct snd_pcm_substream *substream;
3252 struct snd_pcm_runtime *runtime;
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003253 int err = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003255 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 pcm_file = file->private_data;
3257 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003258 if (PCM_RUNTIME_CHECK(substream))
3259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 err = fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds685d87f2008-08-06 19:24:47 -07003262out:
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003263 unlock_kernel();
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
3267/*
3268 * ioctl32 compat
3269 */
3270#ifdef CONFIG_COMPAT
3271#include "pcm_compat.c"
3272#else
3273#define snd_pcm_ioctl_compat NULL
3274#endif
3275
3276/*
3277 * To be removed helpers to keep binary compatibility
3278 */
3279
Takashi Iwai59d48582005-12-01 10:51:58 +01003280#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3282#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3283
Takashi Iwai877211f2005-11-17 13:59:38 +01003284static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3285 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
3287 unsigned int i;
3288
3289 memset(params, 0, sizeof(*params));
3290 params->flags = oparams->flags;
3291 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3292 params->masks[i].bits[0] = oparams->masks[i];
3293 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3294 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3295 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3296 params->info = oparams->info;
3297 params->msbits = oparams->msbits;
3298 params->rate_num = oparams->rate_num;
3299 params->rate_den = oparams->rate_den;
3300 params->fifo_size = oparams->fifo_size;
3301}
3302
Takashi Iwai877211f2005-11-17 13:59:38 +01003303static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3304 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 unsigned int i;
3307
3308 memset(oparams, 0, sizeof(*oparams));
3309 oparams->flags = params->flags;
3310 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3311 oparams->masks[i] = params->masks[i].bits[0];
3312 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3313 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3314 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3315 oparams->info = params->info;
3316 oparams->msbits = params->msbits;
3317 oparams->rate_num = params->rate_num;
3318 oparams->rate_den = params->rate_den;
3319 oparams->fifo_size = params->fifo_size;
3320}
3321
Takashi Iwai877211f2005-11-17 13:59:38 +01003322static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3323 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
Takashi Iwai877211f2005-11-17 13:59:38 +01003325 struct snd_pcm_hw_params *params;
3326 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 int err;
3328
3329 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003330 if (!params)
3331 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Li Zefanef44a1e2009-04-10 09:43:08 +08003333 oparams = memdup_user(_oparams, sizeof(*oparams));
3334 if (IS_ERR(oparams)) {
3335 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 goto out;
3337 }
3338 snd_pcm_hw_convert_from_old_params(params, oparams);
3339 err = snd_pcm_hw_refine(substream, params);
3340 snd_pcm_hw_convert_to_old_params(oparams, params);
3341 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3342 if (!err)
3343 err = -EFAULT;
3344 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003345
3346 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347out:
3348 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 return err;
3350}
3351
Takashi Iwai877211f2005-11-17 13:59:38 +01003352static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3353 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Takashi Iwai877211f2005-11-17 13:59:38 +01003355 struct snd_pcm_hw_params *params;
3356 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 int err;
3358
3359 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003360 if (!params)
3361 return -ENOMEM;
3362
3363 oparams = memdup_user(_oparams, sizeof(*oparams));
3364 if (IS_ERR(oparams)) {
3365 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 goto out;
3367 }
3368 snd_pcm_hw_convert_from_old_params(params, oparams);
3369 err = snd_pcm_hw_params(substream, params);
3370 snd_pcm_hw_convert_to_old_params(oparams, params);
3371 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3372 if (!err)
3373 err = -EFAULT;
3374 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003375
3376 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377out:
3378 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return err;
3380}
Takashi Iwai59d48582005-12-01 10:51:58 +01003381#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Cliff Cai70036092008-09-03 10:54:36 +02003383#ifndef CONFIG_MMU
3384unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3385 unsigned long len, unsigned long pgoff,
3386 unsigned long flags)
3387{
3388 return 0;
3389}
3390#else
3391# define dummy_get_unmapped_area NULL
3392#endif
3393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394/*
3395 * Register section
3396 */
3397
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003398const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003400 .owner = THIS_MODULE,
3401 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003402 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003403 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003404 .release = snd_pcm_release,
3405 .poll = snd_pcm_playback_poll,
3406 .unlocked_ioctl = snd_pcm_playback_ioctl,
3407 .compat_ioctl = snd_pcm_ioctl_compat,
3408 .mmap = snd_pcm_mmap,
3409 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003410 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 },
3412 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003413 .owner = THIS_MODULE,
3414 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003415 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003416 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003417 .release = snd_pcm_release,
3418 .poll = snd_pcm_capture_poll,
3419 .unlocked_ioctl = snd_pcm_capture_ioctl,
3420 .compat_ioctl = snd_pcm_ioctl_compat,
3421 .mmap = snd_pcm_mmap,
3422 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003423 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
3425};