blob: a151fb01ba82a70b1a4a2c4eb9d7d079e2cf9912 [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)
315 params->info = hw->info;
316 if (!params->fifo_size)
317 params->fifo_size = hw->fifo_size;
318 params->rmask = 0;
319 return 0;
320}
321
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200322EXPORT_SYMBOL(snd_pcm_hw_refine);
323
Takashi Iwai877211f2005-11-17 13:59:38 +0100324static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
325 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Takashi Iwai877211f2005-11-17 13:59:38 +0100327 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 int err;
329
330 params = kmalloc(sizeof(*params), GFP_KERNEL);
331 if (!params) {
332 err = -ENOMEM;
333 goto out;
334 }
335 if (copy_from_user(params, _params, sizeof(*params))) {
336 err = -EFAULT;
337 goto out;
338 }
339 err = snd_pcm_hw_refine(substream, params);
340 if (copy_to_user(_params, params, sizeof(*params))) {
341 if (!err)
342 err = -EFAULT;
343 }
344out:
345 kfree(params);
346 return err;
347}
348
Takashi Iwai9442e692006-09-30 23:27:19 -0700349static int period_to_usecs(struct snd_pcm_runtime *runtime)
350{
351 int usecs;
352
353 if (! runtime->rate)
354 return -1; /* invalid */
355
356 /* take 75% of period time as the deadline */
357 usecs = (750000 / runtime->rate) * runtime->period_size;
358 usecs += ((750000 % runtime->rate) * runtime->period_size) /
359 runtime->rate;
360
361 return usecs;
362}
363
Takashi Iwai877211f2005-11-17 13:59:38 +0100364static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
365 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Takashi Iwai877211f2005-11-17 13:59:38 +0100367 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700368 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned int bits;
370 snd_pcm_uframes_t frames;
371
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200372 if (PCM_RUNTIME_CHECK(substream))
373 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 snd_pcm_stream_lock_irq(substream);
376 switch (runtime->status->state) {
377 case SNDRV_PCM_STATE_OPEN:
378 case SNDRV_PCM_STATE_SETUP:
379 case SNDRV_PCM_STATE_PREPARED:
380 break;
381 default:
382 snd_pcm_stream_unlock_irq(substream);
383 return -EBADFD;
384 }
385 snd_pcm_stream_unlock_irq(substream);
386#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
387 if (!substream->oss.oss)
388#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200389 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -EBADFD;
391
392 params->rmask = ~0U;
393 err = snd_pcm_hw_refine(substream, params);
394 if (err < 0)
395 goto _error;
396
397 err = snd_pcm_hw_params_choose(substream, params);
398 if (err < 0)
399 goto _error;
400
401 if (substream->ops->hw_params != NULL) {
402 err = substream->ops->hw_params(substream, params);
403 if (err < 0)
404 goto _error;
405 }
406
407 runtime->access = params_access(params);
408 runtime->format = params_format(params);
409 runtime->subformat = params_subformat(params);
410 runtime->channels = params_channels(params);
411 runtime->rate = params_rate(params);
412 runtime->period_size = params_period_size(params);
413 runtime->periods = params_periods(params);
414 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 runtime->info = params->info;
416 runtime->rate_num = params->rate_num;
417 runtime->rate_den = params->rate_den;
418
419 bits = snd_pcm_format_physical_width(runtime->format);
420 runtime->sample_bits = bits;
421 bits *= runtime->channels;
422 runtime->frame_bits = bits;
423 frames = 1;
424 while (bits % 8 != 0) {
425 bits *= 2;
426 frames *= 2;
427 }
428 runtime->byte_align = bits / 8;
429 runtime->min_align = frames;
430
431 /* Default sw params */
432 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
433 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 runtime->start_threshold = 1;
436 runtime->stop_threshold = runtime->buffer_size;
437 runtime->silence_threshold = 0;
438 runtime->silence_size = 0;
439 runtime->boundary = runtime->buffer_size;
440 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
441 runtime->boundary *= 2;
442
443 snd_pcm_timer_resolution_change(substream);
444 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700445
Mark Grossf011e2e2008-02-04 22:30:09 -0800446 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
447 substream->latency_id);
Takashi Iwai9442e692006-09-30 23:27:19 -0700448 if ((usecs = period_to_usecs(runtime)) >= 0)
Mark Grossf011e2e2008-02-04 22:30:09 -0800449 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
450 substream->latency_id, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0;
452 _error:
453 /* hardware might be unuseable from this time,
454 so we force application to retry to set
455 the correct hardware parameter settings */
456 runtime->status->state = SNDRV_PCM_STATE_OPEN;
457 if (substream->ops->hw_free != NULL)
458 substream->ops->hw_free(substream);
459 return err;
460}
461
Takashi Iwai877211f2005-11-17 13:59:38 +0100462static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
463 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Takashi Iwai877211f2005-11-17 13:59:38 +0100465 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int err;
467
468 params = kmalloc(sizeof(*params), GFP_KERNEL);
469 if (!params) {
470 err = -ENOMEM;
471 goto out;
472 }
473 if (copy_from_user(params, _params, sizeof(*params))) {
474 err = -EFAULT;
475 goto out;
476 }
477 err = snd_pcm_hw_params(substream, params);
478 if (copy_to_user(_params, params, sizeof(*params))) {
479 if (!err)
480 err = -EFAULT;
481 }
482out:
483 kfree(params);
484 return err;
485}
486
Takashi Iwai877211f2005-11-17 13:59:38 +0100487static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Takashi Iwai877211f2005-11-17 13:59:38 +0100489 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int result = 0;
491
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200492 if (PCM_RUNTIME_CHECK(substream))
493 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 snd_pcm_stream_lock_irq(substream);
496 switch (runtime->status->state) {
497 case SNDRV_PCM_STATE_SETUP:
498 case SNDRV_PCM_STATE_PREPARED:
499 break;
500 default:
501 snd_pcm_stream_unlock_irq(substream);
502 return -EBADFD;
503 }
504 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200505 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return -EBADFD;
507 if (substream->ops->hw_free)
508 result = substream->ops->hw_free(substream);
509 runtime->status->state = SNDRV_PCM_STATE_OPEN;
Mark Grossf011e2e2008-02-04 22:30:09 -0800510 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
511 substream->latency_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return result;
513}
514
Takashi Iwai877211f2005-11-17 13:59:38 +0100515static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
516 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Takashi Iwai877211f2005-11-17 13:59:38 +0100518 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200520 if (PCM_RUNTIME_CHECK(substream))
521 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 snd_pcm_stream_lock_irq(substream);
524 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
525 snd_pcm_stream_unlock_irq(substream);
526 return -EBADFD;
527 }
528 snd_pcm_stream_unlock_irq(substream);
529
530 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
531 return -EINVAL;
532 if (params->avail_min == 0)
533 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (params->silence_size >= runtime->boundary) {
535 if (params->silence_threshold != 0)
536 return -EINVAL;
537 } else {
538 if (params->silence_size > params->silence_threshold)
539 return -EINVAL;
540 if (params->silence_threshold > runtime->buffer_size)
541 return -EINVAL;
542 }
543 snd_pcm_stream_lock_irq(substream);
544 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 runtime->period_step = params->period_step;
546 runtime->control->avail_min = params->avail_min;
547 runtime->start_threshold = params->start_threshold;
548 runtime->stop_threshold = params->stop_threshold;
549 runtime->silence_threshold = params->silence_threshold;
550 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 params->boundary = runtime->boundary;
552 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
554 runtime->silence_size > 0)
555 snd_pcm_playback_silence(substream, ULONG_MAX);
556 wake_up(&runtime->sleep);
557 }
558 snd_pcm_stream_unlock_irq(substream);
559 return 0;
560}
561
Takashi Iwai877211f2005-11-17 13:59:38 +0100562static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
563 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Takashi Iwai877211f2005-11-17 13:59:38 +0100565 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int err;
567 if (copy_from_user(&params, _params, sizeof(params)))
568 return -EFAULT;
569 err = snd_pcm_sw_params(substream, &params);
570 if (copy_to_user(_params, &params, sizeof(params)))
571 return -EFAULT;
572 return err;
573}
574
Takashi Iwai877211f2005-11-17 13:59:38 +0100575int snd_pcm_status(struct snd_pcm_substream *substream,
576 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Takashi Iwai877211f2005-11-17 13:59:38 +0100578 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 snd_pcm_stream_lock_irq(substream);
581 status->state = runtime->status->state;
582 status->suspended_state = runtime->status->suspended_state;
583 if (status->state == SNDRV_PCM_STATE_OPEN)
584 goto _end;
585 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100586 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100588 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
589 status->tstamp = runtime->status->tstamp;
590 goto _tstamp_end;
591 }
592 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100593 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100594 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 status->appl_ptr = runtime->control->appl_ptr;
596 status->hw_ptr = runtime->status->hw_ptr;
597 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
598 status->avail = snd_pcm_playback_avail(runtime);
599 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
600 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
601 status->delay = runtime->buffer_size - status->avail;
602 else
603 status->delay = 0;
604 } else {
605 status->avail = snd_pcm_capture_avail(runtime);
606 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
607 status->delay = status->avail;
608 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);
861 runtime->status->state = state;
862 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
863 runtime->silence_size > 0)
864 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100866 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
867 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870static struct action_ops snd_pcm_action_start = {
871 .pre_action = snd_pcm_pre_start,
872 .do_action = snd_pcm_do_start,
873 .undo_action = snd_pcm_undo_start,
874 .post_action = snd_pcm_post_start
875};
876
877/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700878 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200879 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100881int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwai877211f2005-11-17 13:59:38 +0100883 return snd_pcm_action(&snd_pcm_action_start, substream,
884 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887/*
888 * stop callbacks
889 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100890static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Takashi Iwai877211f2005-11-17 13:59:38 +0100892 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
894 return -EBADFD;
895 runtime->trigger_master = substream;
896 return 0;
897}
898
Takashi Iwai877211f2005-11-17 13:59:38 +0100899static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 if (substream->runtime->trigger_master == substream &&
902 snd_pcm_running(substream))
903 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
904 return 0; /* unconditonally stop all substreams */
905}
906
Takashi Iwai877211f2005-11-17 13:59:38 +0100907static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Takashi Iwai877211f2005-11-17 13:59:38 +0100909 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (runtime->status->state != state) {
911 snd_pcm_trigger_tstamp(substream);
912 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100913 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
914 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 wake_up(&runtime->sleep);
918}
919
920static struct action_ops snd_pcm_action_stop = {
921 .pre_action = snd_pcm_pre_stop,
922 .do_action = snd_pcm_do_stop,
923 .post_action = snd_pcm_post_stop
924};
925
926/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700927 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200928 * @substream: the PCM substream instance
929 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700931 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100933int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
936}
937
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200938EXPORT_SYMBOL(snd_pcm_stop);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700941 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200942 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700944 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * Unlike snd_pcm_stop(), this affects only the given stream.
946 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100947int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Takashi Iwai877211f2005-11-17 13:59:38 +0100949 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
950 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953/*
954 * pause callbacks
955 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100956static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Takashi Iwai877211f2005-11-17 13:59:38 +0100958 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
960 return -ENOSYS;
961 if (push) {
962 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
963 return -EBADFD;
964 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
965 return -EBADFD;
966 runtime->trigger_master = substream;
967 return 0;
968}
969
Takashi Iwai877211f2005-11-17 13:59:38 +0100970static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 if (substream->runtime->trigger_master != substream)
973 return 0;
974 return substream->ops->trigger(substream,
975 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
976 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
977}
978
Takashi Iwai877211f2005-11-17 13:59:38 +0100979static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 if (substream->runtime->trigger_master == substream)
982 substream->ops->trigger(substream,
983 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
984 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
985}
986
Takashi Iwai877211f2005-11-17 13:59:38 +0100987static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Takashi Iwai877211f2005-11-17 13:59:38 +0100989 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 snd_pcm_trigger_tstamp(substream);
991 if (push) {
992 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
993 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100994 snd_timer_notify(substream->timer,
995 SNDRV_TIMER_EVENT_MPAUSE,
996 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 wake_up(&runtime->sleep);
998 } else {
999 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001001 snd_timer_notify(substream->timer,
1002 SNDRV_TIMER_EVENT_MCONTINUE,
1003 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005}
1006
1007static struct action_ops snd_pcm_action_pause = {
1008 .pre_action = snd_pcm_pre_pause,
1009 .do_action = snd_pcm_do_pause,
1010 .undo_action = snd_pcm_undo_pause,
1011 .post_action = snd_pcm_post_pause
1012};
1013
1014/*
1015 * Push/release the pause for all linked streams.
1016 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001017static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1020}
1021
1022#ifdef CONFIG_PM
1023/* suspend */
1024
Takashi Iwai877211f2005-11-17 13:59:38 +01001025static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Takashi Iwai877211f2005-11-17 13:59:38 +01001027 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1029 return -EBUSY;
1030 runtime->trigger_master = substream;
1031 return 0;
1032}
1033
Takashi Iwai877211f2005-11-17 13:59:38 +01001034static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Takashi Iwai877211f2005-11-17 13:59:38 +01001036 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (runtime->trigger_master != substream)
1038 return 0;
1039 if (! snd_pcm_running(substream))
1040 return 0;
1041 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1042 return 0; /* suspend unconditionally */
1043}
1044
Takashi Iwai877211f2005-11-17 13:59:38 +01001045static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Takashi Iwai877211f2005-11-17 13:59:38 +01001047 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 snd_pcm_trigger_tstamp(substream);
1049 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001050 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1051 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 runtime->status->suspended_state = runtime->status->state;
1053 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 wake_up(&runtime->sleep);
1055}
1056
1057static struct action_ops snd_pcm_action_suspend = {
1058 .pre_action = snd_pcm_pre_suspend,
1059 .do_action = snd_pcm_do_suspend,
1060 .post_action = snd_pcm_post_suspend
1061};
1062
1063/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001064 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001065 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 * After this call, all streams are changed to SUSPENDED state.
1068 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001069int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 int err;
1072 unsigned long flags;
1073
Takashi Iwai603bf522005-11-17 15:59:14 +01001074 if (! substream)
1075 return 0;
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 snd_pcm_stream_lock_irqsave(substream, flags);
1078 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1079 snd_pcm_stream_unlock_irqrestore(substream, flags);
1080 return err;
1081}
1082
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001083EXPORT_SYMBOL(snd_pcm_suspend);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001086 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001087 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * After this call, all streams are changed to SUSPENDED state.
1090 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001091int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Takashi Iwai877211f2005-11-17 13:59:38 +01001093 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 int stream, err = 0;
1095
Takashi Iwai603bf522005-11-17 15:59:14 +01001096 if (! pcm)
1097 return 0;
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001100 for (substream = pcm->streams[stream].substream;
1101 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* FIXME: the open/close code should lock this as well */
1103 if (substream->runtime == NULL)
1104 continue;
1105 err = snd_pcm_suspend(substream);
1106 if (err < 0 && err != -EBUSY)
1107 return err;
1108 }
1109 }
1110 return 0;
1111}
1112
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001113EXPORT_SYMBOL(snd_pcm_suspend_all);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115/* resume */
1116
Takashi Iwai877211f2005-11-17 13:59:38 +01001117static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Takashi Iwai877211f2005-11-17 13:59:38 +01001119 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1121 return -ENOSYS;
1122 runtime->trigger_master = substream;
1123 return 0;
1124}
1125
Takashi Iwai877211f2005-11-17 13:59:38 +01001126static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Takashi Iwai877211f2005-11-17 13:59:38 +01001128 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (runtime->trigger_master != substream)
1130 return 0;
1131 /* DMA not running previously? */
1132 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1133 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1134 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1135 return 0;
1136 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1137}
1138
Takashi Iwai877211f2005-11-17 13:59:38 +01001139static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 if (substream->runtime->trigger_master == substream &&
1142 snd_pcm_running(substream))
1143 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1144}
1145
Takashi Iwai877211f2005-11-17 13:59:38 +01001146static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Takashi Iwai877211f2005-11-17 13:59:38 +01001148 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 snd_pcm_trigger_tstamp(substream);
1150 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001151 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1152 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156static struct action_ops snd_pcm_action_resume = {
1157 .pre_action = snd_pcm_pre_resume,
1158 .do_action = snd_pcm_do_resume,
1159 .undo_action = snd_pcm_undo_resume,
1160 .post_action = snd_pcm_post_resume
1161};
1162
Takashi Iwai877211f2005-11-17 13:59:38 +01001163static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Takashi Iwai877211f2005-11-17 13:59:38 +01001165 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int res;
1167
1168 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001169 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1171 snd_power_unlock(card);
1172 return res;
1173}
1174
1175#else
1176
Takashi Iwai877211f2005-11-17 13:59:38 +01001177static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 return -ENOSYS;
1180}
1181
1182#endif /* CONFIG_PM */
1183
1184/*
1185 * xrun ioctl
1186 *
1187 * Change the RUNNING stream(s) to XRUN state.
1188 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001189static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Takashi Iwai877211f2005-11-17 13:59:38 +01001191 struct snd_card *card = substream->pcm->card;
1192 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 int result;
1194
1195 snd_power_lock(card);
1196 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001197 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (result < 0)
1199 goto _unlock;
1200 }
1201
1202 snd_pcm_stream_lock_irq(substream);
1203 switch (runtime->status->state) {
1204 case SNDRV_PCM_STATE_XRUN:
1205 result = 0; /* already there */
1206 break;
1207 case SNDRV_PCM_STATE_RUNNING:
1208 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1209 break;
1210 default:
1211 result = -EBADFD;
1212 }
1213 snd_pcm_stream_unlock_irq(substream);
1214 _unlock:
1215 snd_power_unlock(card);
1216 return result;
1217}
1218
1219/*
1220 * reset ioctl
1221 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001222static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Takashi Iwai877211f2005-11-17 13:59:38 +01001224 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 switch (runtime->status->state) {
1226 case SNDRV_PCM_STATE_RUNNING:
1227 case SNDRV_PCM_STATE_PREPARED:
1228 case SNDRV_PCM_STATE_PAUSED:
1229 case SNDRV_PCM_STATE_SUSPENDED:
1230 return 0;
1231 default:
1232 return -EBADFD;
1233 }
1234}
1235
Takashi Iwai877211f2005-11-17 13:59:38 +01001236static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Takashi Iwai877211f2005-11-17 13:59:38 +01001238 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1240 if (err < 0)
1241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001243 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1244 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 runtime->silence_start = runtime->status->hw_ptr;
1246 runtime->silence_filled = 0;
1247 return 0;
1248}
1249
Takashi Iwai877211f2005-11-17 13:59:38 +01001250static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Takashi Iwai877211f2005-11-17 13:59:38 +01001252 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 runtime->control->appl_ptr = runtime->status->hw_ptr;
1254 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1255 runtime->silence_size > 0)
1256 snd_pcm_playback_silence(substream, ULONG_MAX);
1257}
1258
1259static struct action_ops snd_pcm_action_reset = {
1260 .pre_action = snd_pcm_pre_reset,
1261 .do_action = snd_pcm_do_reset,
1262 .post_action = snd_pcm_post_reset
1263};
1264
Takashi Iwai877211f2005-11-17 13:59:38 +01001265static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1268}
1269
1270/*
1271 * prepare ioctl
1272 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001273/* we use the second argument for updating f_flags */
1274static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1275 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Takashi Iwai877211f2005-11-17 13:59:38 +01001277 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001278 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1279 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return -EBADFD;
1281 if (snd_pcm_running(substream))
1282 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001283 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285}
1286
Takashi Iwai877211f2005-11-17 13:59:38 +01001287static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 int err;
1290 err = substream->ops->prepare(substream);
1291 if (err < 0)
1292 return err;
1293 return snd_pcm_do_reset(substream, 0);
1294}
1295
Takashi Iwai877211f2005-11-17 13:59:38 +01001296static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Takashi Iwai877211f2005-11-17 13:59:38 +01001298 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 runtime->control->appl_ptr = runtime->status->hw_ptr;
1300 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1301}
1302
1303static struct action_ops snd_pcm_action_prepare = {
1304 .pre_action = snd_pcm_pre_prepare,
1305 .do_action = snd_pcm_do_prepare,
1306 .post_action = snd_pcm_post_prepare
1307};
1308
1309/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001310 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001311 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001312 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001314static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1315 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001318 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001319 int f_flags;
1320
1321 if (file)
1322 f_flags = file->f_flags;
1323 else
1324 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001327 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001328 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1329 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 snd_power_unlock(card);
1331 return res;
1332}
1333
1334/*
1335 * drain ioctl
1336 */
1337
Takashi Iwai877211f2005-11-17 13:59:38 +01001338static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001340 if (substream->f_flags & O_NONBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return -EAGAIN;
1342 substream->runtime->trigger_master = substream;
1343 return 0;
1344}
1345
Takashi Iwai877211f2005-11-17 13:59:38 +01001346static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Takashi Iwai877211f2005-11-17 13:59:38 +01001348 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1350 switch (runtime->status->state) {
1351 case SNDRV_PCM_STATE_PREPARED:
1352 /* start playback stream if possible */
1353 if (! snd_pcm_playback_empty(substream)) {
1354 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1355 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1356 }
1357 break;
1358 case SNDRV_PCM_STATE_RUNNING:
1359 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1360 break;
1361 default:
1362 break;
1363 }
1364 } else {
1365 /* stop running stream */
1366 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001367 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001369 snd_pcm_do_stop(substream, new_state);
1370 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 }
1373 return 0;
1374}
1375
Takashi Iwai877211f2005-11-17 13:59:38 +01001376static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378}
1379
1380static struct action_ops snd_pcm_action_drain_init = {
1381 .pre_action = snd_pcm_pre_drain_init,
1382 .do_action = snd_pcm_do_drain_init,
1383 .post_action = snd_pcm_post_drain_init
1384};
1385
1386struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001387 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 wait_queue_t wait;
1389 snd_pcm_uframes_t stop_threshold;
1390};
1391
Takashi Iwai877211f2005-11-17 13:59:38 +01001392static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394/*
1395 * Drain the stream(s).
1396 * When the substream is linked, sync until the draining of all playback streams
1397 * is finished.
1398 * After this call, all streams are supposed to be either SETUP or DRAINING
1399 * (capture only) state.
1400 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001401static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwai877211f2005-11-17 13:59:38 +01001403 struct snd_card *card;
1404 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001405 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int result = 0;
1407 int i, num_drecs;
1408 struct drain_rec *drec, drec_tmp, *d;
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 card = substream->pcm->card;
1411 runtime = substream->runtime;
1412
1413 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1414 return -EBADFD;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 snd_power_lock(card);
1417 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001418 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001419 if (result < 0) {
1420 snd_power_unlock(card);
1421 return result;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
1425 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001426 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (snd_pcm_stream_linked(substream)) {
1428 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1429 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001430 up_read(&snd_pcm_link_rwsem);
1431 snd_power_unlock(card);
1432 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 } else
1435 drec = &drec_tmp;
1436
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001437 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 num_drecs = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001439 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1442 d = &drec[num_drecs++];
1443 d->substream = s;
1444 init_waitqueue_entry(&d->wait, current);
1445 add_wait_queue(&runtime->sleep, &d->wait);
1446 /* stop_threshold fixup to avoid endless loop when
1447 * stop_threshold > buffer_size
1448 */
1449 d->stop_threshold = runtime->stop_threshold;
1450 if (runtime->stop_threshold > runtime->buffer_size)
1451 runtime->stop_threshold = runtime->buffer_size;
1452 }
1453 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001454 up_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001455
1456 snd_pcm_stream_lock_irq(substream);
1457 /* resume pause */
Takashi Iwai1a7fa542007-07-06 12:27:25 +02001458 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001459 snd_pcm_pause(substream, 0);
1460
1461 /* pre-start/stop - all running streams are changed to DRAINING state */
1462 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1463 if (result < 0) {
1464 snd_pcm_stream_unlock_irq(substream);
1465 goto _error;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 for (;;) {
1469 long tout;
1470 if (signal_pending(current)) {
1471 result = -ERESTARTSYS;
1472 break;
1473 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001474 /* all finished? */
1475 for (i = 0; i < num_drecs; i++) {
1476 runtime = drec[i].substream->runtime;
1477 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1478 break;
1479 }
1480 if (i == num_drecs)
1481 break; /* yes, all drained */
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 set_current_state(TASK_INTERRUPTIBLE);
1484 snd_pcm_stream_unlock_irq(substream);
1485 snd_power_unlock(card);
1486 tout = schedule_timeout(10 * HZ);
1487 snd_power_lock(card);
1488 snd_pcm_stream_lock_irq(substream);
1489 if (tout == 0) {
1490 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1491 result = -ESTRPIPE;
1492 else {
1493 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1494 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1495 result = -EIO;
1496 }
1497 break;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001500
1501 snd_pcm_stream_unlock_irq(substream);
1502
1503 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 for (i = 0; i < num_drecs; i++) {
1505 d = &drec[i];
1506 runtime = d->substream->runtime;
1507 remove_wait_queue(&runtime->sleep, &d->wait);
1508 runtime->stop_threshold = d->stop_threshold;
1509 }
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (drec != &drec_tmp)
1512 kfree(drec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 return result;
1516}
1517
1518/*
1519 * drop ioctl
1520 *
1521 * Immediately put all linked substreams into SETUP state.
1522 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001523static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Takashi Iwai877211f2005-11-17 13:59:38 +01001525 struct snd_pcm_runtime *runtime;
1526 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int result = 0;
1528
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001529 if (PCM_RUNTIME_CHECK(substream))
1530 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 runtime = substream->runtime;
1532 card = substream->pcm->card;
1533
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001534 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001535 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1536 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EBADFD;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 snd_pcm_stream_lock_irq(substream);
1540 /* resume pause */
1541 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1542 snd_pcm_pause(substream, 0);
1543
1544 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1545 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1546 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return result;
1549}
1550
1551
1552/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553static struct file *snd_pcm_file_fd(int fd)
1554{
1555 struct file *file;
1556 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001557 unsigned int minor;
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 file = fget(fd);
1560 if (!file)
1561 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001562 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (!S_ISCHR(inode->i_mode) ||
1564 imajor(inode) != snd_major) {
1565 fput(file);
1566 return NULL;
1567 }
1568 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001569 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1570 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 fput(file);
1572 return NULL;
1573 }
1574 return file;
1575}
1576
1577/*
1578 * PCM link handling
1579 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001580static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 int res = 0;
1583 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001584 struct snd_pcm_file *pcm_file;
1585 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 file = snd_pcm_file_fd(fd);
1588 if (!file)
1589 return -EBADFD;
1590 pcm_file = file->private_data;
1591 substream1 = pcm_file->substream;
1592 down_write(&snd_pcm_link_rwsem);
1593 write_lock_irq(&snd_pcm_link_rwlock);
1594 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1595 substream->runtime->status->state != substream1->runtime->status->state) {
1596 res = -EBADFD;
1597 goto _end;
1598 }
1599 if (snd_pcm_stream_linked(substream1)) {
1600 res = -EALREADY;
1601 goto _end;
1602 }
1603 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001604 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (substream->group == NULL) {
1606 res = -ENOMEM;
1607 goto _end;
1608 }
1609 spin_lock_init(&substream->group->lock);
1610 INIT_LIST_HEAD(&substream->group->substreams);
1611 list_add_tail(&substream->link_list, &substream->group->substreams);
1612 substream->group->count = 1;
1613 }
1614 list_add_tail(&substream1->link_list, &substream->group->substreams);
1615 substream->group->count++;
1616 substream1->group = substream->group;
1617 _end:
1618 write_unlock_irq(&snd_pcm_link_rwlock);
1619 up_write(&snd_pcm_link_rwsem);
1620 fput(file);
1621 return res;
1622}
1623
Takashi Iwai877211f2005-11-17 13:59:38 +01001624static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 substream->group = &substream->self_group;
1627 INIT_LIST_HEAD(&substream->self_group.substreams);
1628 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1629}
1630
Takashi Iwai877211f2005-11-17 13:59:38 +01001631static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwaief991b92007-02-22 12:52:53 +01001633 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int res = 0;
1635
1636 down_write(&snd_pcm_link_rwsem);
1637 write_lock_irq(&snd_pcm_link_rwlock);
1638 if (!snd_pcm_stream_linked(substream)) {
1639 res = -EALREADY;
1640 goto _end;
1641 }
1642 list_del(&substream->link_list);
1643 substream->group->count--;
1644 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001645 snd_pcm_group_for_each_entry(s, substream) {
1646 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 break;
1648 }
1649 kfree(substream->group);
1650 }
1651 relink_to_local(substream);
1652 _end:
1653 write_unlock_irq(&snd_pcm_link_rwlock);
1654 up_write(&snd_pcm_link_rwsem);
1655 return res;
1656}
1657
1658/*
1659 * hw configurator
1660 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001661static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1662 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Takashi Iwai877211f2005-11-17 13:59:38 +01001664 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1666 hw_param_interval_c(params, rule->deps[1]), &t);
1667 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1668}
1669
Takashi Iwai877211f2005-11-17 13:59:38 +01001670static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1671 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Takashi Iwai877211f2005-11-17 13:59:38 +01001673 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1675 hw_param_interval_c(params, rule->deps[1]), &t);
1676 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1677}
1678
Takashi Iwai877211f2005-11-17 13:59:38 +01001679static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1680 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Takashi Iwai877211f2005-11-17 13:59:38 +01001682 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1684 hw_param_interval_c(params, rule->deps[1]),
1685 (unsigned long) rule->private, &t);
1686 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1687}
1688
Takashi Iwai877211f2005-11-17 13:59:38 +01001689static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1690 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Takashi Iwai877211f2005-11-17 13:59:38 +01001692 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1694 (unsigned long) rule->private,
1695 hw_param_interval_c(params, rule->deps[1]), &t);
1696 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1697}
1698
Takashi Iwai877211f2005-11-17 13:59:38 +01001699static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1700 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001703 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1704 struct snd_mask m;
1705 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 snd_mask_any(&m);
1707 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1708 int bits;
1709 if (! snd_mask_test(mask, k))
1710 continue;
1711 bits = snd_pcm_format_physical_width(k);
1712 if (bits <= 0)
1713 continue; /* ignore invalid formats */
1714 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1715 snd_mask_reset(&m, k);
1716 }
1717 return snd_mask_refine(mask, &m);
1718}
1719
Takashi Iwai877211f2005-11-17 13:59:38 +01001720static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1721 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Takashi Iwai877211f2005-11-17 13:59:38 +01001723 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 unsigned int k;
1725 t.min = UINT_MAX;
1726 t.max = 0;
1727 t.openmin = 0;
1728 t.openmax = 0;
1729 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1730 int bits;
1731 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1732 continue;
1733 bits = snd_pcm_format_physical_width(k);
1734 if (bits <= 0)
1735 continue; /* ignore invalid formats */
1736 if (t.min > (unsigned)bits)
1737 t.min = bits;
1738 if (t.max < (unsigned)bits)
1739 t.max = bits;
1740 }
1741 t.integer = 1;
1742 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1743}
1744
1745#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1746#error "Change this table"
1747#endif
1748
1749static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1750 48000, 64000, 88200, 96000, 176400, 192000 };
1751
Clemens Ladisch7653d552007-08-13 17:38:54 +02001752const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1753 .count = ARRAY_SIZE(rates),
1754 .list = rates,
1755};
1756
Takashi Iwai877211f2005-11-17 13:59:38 +01001757static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1758 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Takashi Iwai877211f2005-11-17 13:59:38 +01001760 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001762 snd_pcm_known_rates.count,
1763 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Takashi Iwai877211f2005-11-17 13:59:38 +01001766static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1767 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Takashi Iwai877211f2005-11-17 13:59:38 +01001769 struct snd_interval t;
1770 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 t.min = 0;
1772 t.max = substream->buffer_bytes_max;
1773 t.openmin = 0;
1774 t.openmax = 0;
1775 t.integer = 1;
1776 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1777}
1778
Takashi Iwai877211f2005-11-17 13:59:38 +01001779int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Takashi Iwai877211f2005-11-17 13:59:38 +01001781 struct snd_pcm_runtime *runtime = substream->runtime;
1782 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int k, err;
1784
1785 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1786 snd_mask_any(constrs_mask(constrs, k));
1787 }
1788
1789 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1790 snd_interval_any(constrs_interval(constrs, k));
1791 }
1792
1793 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1794 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1795 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1796 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1797 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1798
1799 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1800 snd_pcm_hw_rule_format, NULL,
1801 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1802 if (err < 0)
1803 return err;
1804 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1805 snd_pcm_hw_rule_sample_bits, NULL,
1806 SNDRV_PCM_HW_PARAM_FORMAT,
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_div, NULL,
1812 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1813 if (err < 0)
1814 return err;
1815 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1816 snd_pcm_hw_rule_mul, NULL,
1817 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1818 if (err < 0)
1819 return err;
1820 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1821 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1822 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1823 if (err < 0)
1824 return err;
1825 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1826 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1827 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1828 if (err < 0)
1829 return err;
1830 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1831 snd_pcm_hw_rule_div, NULL,
1832 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1833 if (err < 0)
1834 return err;
1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1836 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1837 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1838 if (err < 0)
1839 return err;
1840 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1841 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1842 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1843 if (err < 0)
1844 return err;
1845 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1846 snd_pcm_hw_rule_div, NULL,
1847 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1848 if (err < 0)
1849 return err;
1850 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1851 snd_pcm_hw_rule_div, NULL,
1852 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1853 if (err < 0)
1854 return err;
1855 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1856 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1857 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1858 if (err < 0)
1859 return err;
1860 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1861 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1862 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1863 if (err < 0)
1864 return err;
1865 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1866 snd_pcm_hw_rule_mul, NULL,
1867 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1868 if (err < 0)
1869 return err;
1870 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1871 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1872 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1873 if (err < 0)
1874 return err;
1875 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1876 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1877 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1878 if (err < 0)
1879 return err;
1880 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1881 snd_pcm_hw_rule_muldivk, (void*) 8,
1882 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1883 if (err < 0)
1884 return err;
1885 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1886 snd_pcm_hw_rule_muldivk, (void*) 8,
1887 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1888 if (err < 0)
1889 return err;
1890 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1891 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1892 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1893 if (err < 0)
1894 return err;
1895 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1896 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1897 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1898 if (err < 0)
1899 return err;
1900 return 0;
1901}
1902
Takashi Iwai877211f2005-11-17 13:59:38 +01001903int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Takashi Iwai877211f2005-11-17 13:59:38 +01001905 struct snd_pcm_runtime *runtime = substream->runtime;
1906 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 int err;
1908 unsigned int mask = 0;
1909
1910 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1911 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1912 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1913 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1914 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1915 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1916 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1917 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1918 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1919 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1920 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1921 }
1922 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001923 if (err < 0)
1924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001927 if (err < 0)
1928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001931 if (err < 0)
1932 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1935 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001936 if (err < 0)
1937 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1940 hw->rate_min, hw->rate_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001941 if (err < 0)
1942 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1945 hw->period_bytes_min, hw->period_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001946 if (err < 0)
1947 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1950 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001951 if (err < 0)
1952 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1955 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001956 if (err < 0)
1957 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1960 snd_pcm_hw_rule_buffer_bytes_max, substream,
1961 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1962 if (err < 0)
1963 return err;
1964
1965 /* FIXME: remove */
1966 if (runtime->dma_bytes) {
1967 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001968 if (err < 0)
1969 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971
1972 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1973 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1974 snd_pcm_hw_rule_rate, hw,
1975 SNDRV_PCM_HW_PARAM_RATE, -1);
1976 if (err < 0)
1977 return err;
1978 }
1979
1980 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1982
1983 return 0;
1984}
1985
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001986static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001989}
1990
1991void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1992{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001993 substream->ref_count--;
1994 if (substream->ref_count > 0)
1995 return;
1996
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001997 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001998 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (substream->ops->hw_free != NULL)
2000 substream->ops->hw_free(substream);
2001 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002002 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
Takashi Iwai15762742006-04-06 19:47:42 +02002004 if (substream->pcm_release) {
2005 substream->pcm_release(substream);
2006 substream->pcm_release = NULL;
2007 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002008 snd_pcm_detach_substream(substream);
2009}
2010
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002011EXPORT_SYMBOL(snd_pcm_release_substream);
2012
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002013int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2014 struct file *file,
2015 struct snd_pcm_substream **rsubstream)
2016{
2017 struct snd_pcm_substream *substream;
2018 int err;
2019
2020 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2021 if (err < 0)
2022 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002023 if (substream->ref_count > 1) {
2024 *rsubstream = substream;
2025 return 0;
2026 }
2027
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002028 err = snd_pcm_hw_constraints_init(substream);
2029 if (err < 0) {
2030 snd_printd("snd_pcm_hw_constraints_init failed\n");
2031 goto error;
2032 }
2033
2034 if ((err = substream->ops->open(substream)) < 0)
2035 goto error;
2036
2037 substream->hw_opened = 1;
2038
2039 err = snd_pcm_hw_constraints_complete(substream);
2040 if (err < 0) {
2041 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2042 goto error;
2043 }
2044
2045 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002047
2048 error:
2049 snd_pcm_release_substream(substream);
2050 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002053EXPORT_SYMBOL(snd_pcm_open_substream);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002056 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002058 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Takashi Iwai877211f2005-11-17 13:59:38 +01002060 struct snd_pcm_file *pcm_file;
2061 struct snd_pcm_substream *substream;
2062 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002063 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002065 if (rpcm_file)
2066 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002068 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2069 if (err < 0)
2070 return err;
2071
Takashi Iwai548a6482006-07-31 16:51:51 +02002072 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2073 if (pcm_file == NULL) {
2074 snd_pcm_release_substream(substream);
2075 return -ENOMEM;
2076 }
2077 pcm_file->substream = substream;
2078 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002079 str = substream->pstr;
2080 substream->file = pcm_file;
2081 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002084 if (rpcm_file)
2085 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return 0;
2087}
2088
Clemens Ladischf87135f2005-11-20 14:06:59 +01002089static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Takashi Iwai877211f2005-11-17 13:59:38 +01002091 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002092
2093 pcm = snd_lookup_minor_data(iminor(inode),
2094 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2095 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2096}
2097
2098static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2099{
2100 struct snd_pcm *pcm;
2101
2102 pcm = snd_lookup_minor_data(iminor(inode),
2103 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2104 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2105}
2106
2107static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2108{
2109 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002110 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 wait_queue_t wait;
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (pcm == NULL) {
2114 err = -ENODEV;
2115 goto __error1;
2116 }
2117 err = snd_card_file_add(pcm->card, file);
2118 if (err < 0)
2119 goto __error1;
2120 if (!try_module_get(pcm->card->module)) {
2121 err = -EFAULT;
2122 goto __error2;
2123 }
2124 init_waitqueue_entry(&wait, current);
2125 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002126 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002128 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (err >= 0)
2130 break;
2131 if (err == -EAGAIN) {
2132 if (file->f_flags & O_NONBLOCK) {
2133 err = -EBUSY;
2134 break;
2135 }
2136 } else
2137 break;
2138 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002139 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002141 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (signal_pending(current)) {
2143 err = -ERESTARTSYS;
2144 break;
2145 }
2146 }
2147 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002148 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (err < 0)
2150 goto __error;
2151 return err;
2152
2153 __error:
2154 module_put(pcm->card->module);
2155 __error2:
2156 snd_card_file_remove(pcm->card, file);
2157 __error1:
2158 return err;
2159}
2160
2161static int snd_pcm_release(struct inode *inode, struct file *file)
2162{
Takashi Iwai877211f2005-11-17 13:59:38 +01002163 struct snd_pcm *pcm;
2164 struct snd_pcm_substream *substream;
2165 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 pcm_file = file->private_data;
2168 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002169 if (snd_BUG_ON(!substream))
2170 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002172 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002173 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002174 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002175 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 wake_up(&pcm->open_wait);
2177 module_put(pcm->card->module);
2178 snd_card_file_remove(pcm->card, file);
2179 return 0;
2180}
2181
Takashi Iwai877211f2005-11-17 13:59:38 +01002182static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2183 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Takashi Iwai877211f2005-11-17 13:59:38 +01002185 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 snd_pcm_sframes_t appl_ptr;
2187 snd_pcm_sframes_t ret;
2188 snd_pcm_sframes_t hw_avail;
2189
2190 if (frames == 0)
2191 return 0;
2192
2193 snd_pcm_stream_lock_irq(substream);
2194 switch (runtime->status->state) {
2195 case SNDRV_PCM_STATE_PREPARED:
2196 break;
2197 case SNDRV_PCM_STATE_DRAINING:
2198 case SNDRV_PCM_STATE_RUNNING:
2199 if (snd_pcm_update_hw_ptr(substream) >= 0)
2200 break;
2201 /* Fall through */
2202 case SNDRV_PCM_STATE_XRUN:
2203 ret = -EPIPE;
2204 goto __end;
2205 default:
2206 ret = -EBADFD;
2207 goto __end;
2208 }
2209
2210 hw_avail = snd_pcm_playback_hw_avail(runtime);
2211 if (hw_avail <= 0) {
2212 ret = 0;
2213 goto __end;
2214 }
2215 if (frames > (snd_pcm_uframes_t)hw_avail)
2216 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 appl_ptr = runtime->control->appl_ptr - frames;
2218 if (appl_ptr < 0)
2219 appl_ptr += runtime->boundary;
2220 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 ret = frames;
2222 __end:
2223 snd_pcm_stream_unlock_irq(substream);
2224 return ret;
2225}
2226
Takashi Iwai877211f2005-11-17 13:59:38 +01002227static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2228 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Takashi Iwai877211f2005-11-17 13:59:38 +01002230 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 snd_pcm_sframes_t appl_ptr;
2232 snd_pcm_sframes_t ret;
2233 snd_pcm_sframes_t hw_avail;
2234
2235 if (frames == 0)
2236 return 0;
2237
2238 snd_pcm_stream_lock_irq(substream);
2239 switch (runtime->status->state) {
2240 case SNDRV_PCM_STATE_PREPARED:
2241 case SNDRV_PCM_STATE_DRAINING:
2242 break;
2243 case SNDRV_PCM_STATE_RUNNING:
2244 if (snd_pcm_update_hw_ptr(substream) >= 0)
2245 break;
2246 /* Fall through */
2247 case SNDRV_PCM_STATE_XRUN:
2248 ret = -EPIPE;
2249 goto __end;
2250 default:
2251 ret = -EBADFD;
2252 goto __end;
2253 }
2254
2255 hw_avail = snd_pcm_capture_hw_avail(runtime);
2256 if (hw_avail <= 0) {
2257 ret = 0;
2258 goto __end;
2259 }
2260 if (frames > (snd_pcm_uframes_t)hw_avail)
2261 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 appl_ptr = runtime->control->appl_ptr - frames;
2263 if (appl_ptr < 0)
2264 appl_ptr += runtime->boundary;
2265 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 ret = frames;
2267 __end:
2268 snd_pcm_stream_unlock_irq(substream);
2269 return ret;
2270}
2271
Takashi Iwai877211f2005-11-17 13:59:38 +01002272static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2273 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Takashi Iwai877211f2005-11-17 13:59:38 +01002275 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 snd_pcm_sframes_t appl_ptr;
2277 snd_pcm_sframes_t ret;
2278 snd_pcm_sframes_t avail;
2279
2280 if (frames == 0)
2281 return 0;
2282
2283 snd_pcm_stream_lock_irq(substream);
2284 switch (runtime->status->state) {
2285 case SNDRV_PCM_STATE_PREPARED:
2286 case SNDRV_PCM_STATE_PAUSED:
2287 break;
2288 case SNDRV_PCM_STATE_DRAINING:
2289 case SNDRV_PCM_STATE_RUNNING:
2290 if (snd_pcm_update_hw_ptr(substream) >= 0)
2291 break;
2292 /* Fall through */
2293 case SNDRV_PCM_STATE_XRUN:
2294 ret = -EPIPE;
2295 goto __end;
2296 default:
2297 ret = -EBADFD;
2298 goto __end;
2299 }
2300
2301 avail = snd_pcm_playback_avail(runtime);
2302 if (avail <= 0) {
2303 ret = 0;
2304 goto __end;
2305 }
2306 if (frames > (snd_pcm_uframes_t)avail)
2307 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 appl_ptr = runtime->control->appl_ptr + frames;
2309 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2310 appl_ptr -= runtime->boundary;
2311 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 ret = frames;
2313 __end:
2314 snd_pcm_stream_unlock_irq(substream);
2315 return ret;
2316}
2317
Takashi Iwai877211f2005-11-17 13:59:38 +01002318static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2319 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Takashi Iwai877211f2005-11-17 13:59:38 +01002321 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 snd_pcm_sframes_t appl_ptr;
2323 snd_pcm_sframes_t ret;
2324 snd_pcm_sframes_t avail;
2325
2326 if (frames == 0)
2327 return 0;
2328
2329 snd_pcm_stream_lock_irq(substream);
2330 switch (runtime->status->state) {
2331 case SNDRV_PCM_STATE_PREPARED:
2332 case SNDRV_PCM_STATE_DRAINING:
2333 case SNDRV_PCM_STATE_PAUSED:
2334 break;
2335 case SNDRV_PCM_STATE_RUNNING:
2336 if (snd_pcm_update_hw_ptr(substream) >= 0)
2337 break;
2338 /* Fall through */
2339 case SNDRV_PCM_STATE_XRUN:
2340 ret = -EPIPE;
2341 goto __end;
2342 default:
2343 ret = -EBADFD;
2344 goto __end;
2345 }
2346
2347 avail = snd_pcm_capture_avail(runtime);
2348 if (avail <= 0) {
2349 ret = 0;
2350 goto __end;
2351 }
2352 if (frames > (snd_pcm_uframes_t)avail)
2353 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 appl_ptr = runtime->control->appl_ptr + frames;
2355 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2356 appl_ptr -= runtime->boundary;
2357 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 ret = frames;
2359 __end:
2360 snd_pcm_stream_unlock_irq(substream);
2361 return ret;
2362}
2363
Takashi Iwai877211f2005-11-17 13:59:38 +01002364static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Takashi Iwai877211f2005-11-17 13:59:38 +01002366 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 int err;
2368
2369 snd_pcm_stream_lock_irq(substream);
2370 switch (runtime->status->state) {
2371 case SNDRV_PCM_STATE_DRAINING:
2372 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2373 goto __badfd;
2374 case SNDRV_PCM_STATE_RUNNING:
2375 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2376 break;
2377 /* Fall through */
2378 case SNDRV_PCM_STATE_PREPARED:
2379 case SNDRV_PCM_STATE_SUSPENDED:
2380 err = 0;
2381 break;
2382 case SNDRV_PCM_STATE_XRUN:
2383 err = -EPIPE;
2384 break;
2385 default:
2386 __badfd:
2387 err = -EBADFD;
2388 break;
2389 }
2390 snd_pcm_stream_unlock_irq(substream);
2391 return err;
2392}
2393
Takashi Iwai877211f2005-11-17 13:59:38 +01002394static int snd_pcm_delay(struct snd_pcm_substream *substream,
2395 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Takashi Iwai877211f2005-11-17 13:59:38 +01002397 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 int err;
2399 snd_pcm_sframes_t n = 0;
2400
2401 snd_pcm_stream_lock_irq(substream);
2402 switch (runtime->status->state) {
2403 case SNDRV_PCM_STATE_DRAINING:
2404 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2405 goto __badfd;
2406 case SNDRV_PCM_STATE_RUNNING:
2407 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2408 break;
2409 /* Fall through */
2410 case SNDRV_PCM_STATE_PREPARED:
2411 case SNDRV_PCM_STATE_SUSPENDED:
2412 err = 0;
2413 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2414 n = snd_pcm_playback_hw_avail(runtime);
2415 else
2416 n = snd_pcm_capture_avail(runtime);
2417 break;
2418 case SNDRV_PCM_STATE_XRUN:
2419 err = -EPIPE;
2420 break;
2421 default:
2422 __badfd:
2423 err = -EBADFD;
2424 break;
2425 }
2426 snd_pcm_stream_unlock_irq(substream);
2427 if (!err)
2428 if (put_user(n, res))
2429 err = -EFAULT;
2430 return err;
2431}
2432
Takashi Iwai877211f2005-11-17 13:59:38 +01002433static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2434 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Takashi Iwai877211f2005-11-17 13:59:38 +01002436 struct snd_pcm_runtime *runtime = substream->runtime;
2437 struct snd_pcm_sync_ptr sync_ptr;
2438 volatile struct snd_pcm_mmap_status *status;
2439 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 int err;
2441
2442 memset(&sync_ptr, 0, sizeof(sync_ptr));
2443 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2444 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002445 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 -07002446 return -EFAULT;
2447 status = runtime->status;
2448 control = runtime->control;
2449 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2450 err = snd_pcm_hwsync(substream);
2451 if (err < 0)
2452 return err;
2453 }
2454 snd_pcm_stream_lock_irq(substream);
2455 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2456 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2457 else
2458 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2459 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2460 control->avail_min = sync_ptr.c.control.avail_min;
2461 else
2462 sync_ptr.c.control.avail_min = control->avail_min;
2463 sync_ptr.s.status.state = status->state;
2464 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2465 sync_ptr.s.status.tstamp = status->tstamp;
2466 sync_ptr.s.status.suspended_state = status->suspended_state;
2467 snd_pcm_stream_unlock_irq(substream);
2468 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2469 return -EFAULT;
2470 return 0;
2471}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002472
2473static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2474{
2475 struct snd_pcm_runtime *runtime = substream->runtime;
2476 int arg;
2477
2478 if (get_user(arg, _arg))
2479 return -EFAULT;
2480 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2481 return -EINVAL;
2482 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2483 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2484 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2485 return 0;
2486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Takashi Iwai0df63e42006-04-28 15:13:41 +02002488static int snd_pcm_common_ioctl1(struct file *file,
2489 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 unsigned int cmd, void __user *arg)
2491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 switch (cmd) {
2493 case SNDRV_PCM_IOCTL_PVERSION:
2494 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2495 case SNDRV_PCM_IOCTL_INFO:
2496 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002497 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2498 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002499 case SNDRV_PCM_IOCTL_TTSTAMP:
2500 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 case SNDRV_PCM_IOCTL_HW_REFINE:
2502 return snd_pcm_hw_refine_user(substream, arg);
2503 case SNDRV_PCM_IOCTL_HW_PARAMS:
2504 return snd_pcm_hw_params_user(substream, arg);
2505 case SNDRV_PCM_IOCTL_HW_FREE:
2506 return snd_pcm_hw_free(substream);
2507 case SNDRV_PCM_IOCTL_SW_PARAMS:
2508 return snd_pcm_sw_params_user(substream, arg);
2509 case SNDRV_PCM_IOCTL_STATUS:
2510 return snd_pcm_status_user(substream, arg);
2511 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2512 return snd_pcm_channel_info_user(substream, arg);
2513 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002514 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 case SNDRV_PCM_IOCTL_RESET:
2516 return snd_pcm_reset(substream);
2517 case SNDRV_PCM_IOCTL_START:
2518 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2519 case SNDRV_PCM_IOCTL_LINK:
2520 return snd_pcm_link(substream, (int)(unsigned long) arg);
2521 case SNDRV_PCM_IOCTL_UNLINK:
2522 return snd_pcm_unlink(substream);
2523 case SNDRV_PCM_IOCTL_RESUME:
2524 return snd_pcm_resume(substream);
2525 case SNDRV_PCM_IOCTL_XRUN:
2526 return snd_pcm_xrun(substream);
2527 case SNDRV_PCM_IOCTL_HWSYNC:
2528 return snd_pcm_hwsync(substream);
2529 case SNDRV_PCM_IOCTL_DELAY:
2530 return snd_pcm_delay(substream, arg);
2531 case SNDRV_PCM_IOCTL_SYNC_PTR:
2532 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002533#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2535 return snd_pcm_hw_refine_old_user(substream, arg);
2536 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2537 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002538#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 case SNDRV_PCM_IOCTL_DRAIN:
2540 return snd_pcm_drain(substream);
2541 case SNDRV_PCM_IOCTL_DROP:
2542 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002543 case SNDRV_PCM_IOCTL_PAUSE:
2544 {
2545 int res;
2546 snd_pcm_stream_lock_irq(substream);
2547 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2548 snd_pcm_stream_unlock_irq(substream);
2549 return res;
2550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552 snd_printd("unknown ioctl = 0x%x\n", cmd);
2553 return -ENOTTY;
2554}
2555
Takashi Iwai0df63e42006-04-28 15:13:41 +02002556static int snd_pcm_playback_ioctl1(struct file *file,
2557 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 unsigned int cmd, void __user *arg)
2559{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002560 if (snd_BUG_ON(!substream))
2561 return -ENXIO;
2562 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2563 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 switch (cmd) {
2565 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2566 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002567 struct snd_xferi xferi;
2568 struct snd_xferi __user *_xferi = arg;
2569 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 snd_pcm_sframes_t result;
2571 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2572 return -EBADFD;
2573 if (put_user(0, &_xferi->result))
2574 return -EFAULT;
2575 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2576 return -EFAULT;
2577 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2578 __put_user(result, &_xferi->result);
2579 return result < 0 ? result : 0;
2580 }
2581 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2582 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002583 struct snd_xfern xfern;
2584 struct snd_xfern __user *_xfern = arg;
2585 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 void __user **bufs;
2587 snd_pcm_sframes_t result;
2588 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2589 return -EBADFD;
2590 if (runtime->channels > 128)
2591 return -EINVAL;
2592 if (put_user(0, &_xfern->result))
2593 return -EFAULT;
2594 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2595 return -EFAULT;
2596 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2597 if (bufs == NULL)
2598 return -ENOMEM;
2599 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2600 kfree(bufs);
2601 return -EFAULT;
2602 }
2603 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2604 kfree(bufs);
2605 __put_user(result, &_xfern->result);
2606 return result < 0 ? result : 0;
2607 }
2608 case SNDRV_PCM_IOCTL_REWIND:
2609 {
2610 snd_pcm_uframes_t frames;
2611 snd_pcm_uframes_t __user *_frames = arg;
2612 snd_pcm_sframes_t result;
2613 if (get_user(frames, _frames))
2614 return -EFAULT;
2615 if (put_user(0, _frames))
2616 return -EFAULT;
2617 result = snd_pcm_playback_rewind(substream, frames);
2618 __put_user(result, _frames);
2619 return result < 0 ? result : 0;
2620 }
2621 case SNDRV_PCM_IOCTL_FORWARD:
2622 {
2623 snd_pcm_uframes_t frames;
2624 snd_pcm_uframes_t __user *_frames = arg;
2625 snd_pcm_sframes_t result;
2626 if (get_user(frames, _frames))
2627 return -EFAULT;
2628 if (put_user(0, _frames))
2629 return -EFAULT;
2630 result = snd_pcm_playback_forward(substream, frames);
2631 __put_user(result, _frames);
2632 return result < 0 ? result : 0;
2633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002635 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
Takashi Iwai0df63e42006-04-28 15:13:41 +02002638static int snd_pcm_capture_ioctl1(struct file *file,
2639 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 unsigned int cmd, void __user *arg)
2641{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002642 if (snd_BUG_ON(!substream))
2643 return -ENXIO;
2644 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 switch (cmd) {
2647 case SNDRV_PCM_IOCTL_READI_FRAMES:
2648 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002649 struct snd_xferi xferi;
2650 struct snd_xferi __user *_xferi = arg;
2651 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 snd_pcm_sframes_t result;
2653 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2654 return -EBADFD;
2655 if (put_user(0, &_xferi->result))
2656 return -EFAULT;
2657 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2658 return -EFAULT;
2659 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2660 __put_user(result, &_xferi->result);
2661 return result < 0 ? result : 0;
2662 }
2663 case SNDRV_PCM_IOCTL_READN_FRAMES:
2664 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002665 struct snd_xfern xfern;
2666 struct snd_xfern __user *_xfern = arg;
2667 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 void *bufs;
2669 snd_pcm_sframes_t result;
2670 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2671 return -EBADFD;
2672 if (runtime->channels > 128)
2673 return -EINVAL;
2674 if (put_user(0, &_xfern->result))
2675 return -EFAULT;
2676 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2677 return -EFAULT;
2678 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2679 if (bufs == NULL)
2680 return -ENOMEM;
2681 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2682 kfree(bufs);
2683 return -EFAULT;
2684 }
2685 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2686 kfree(bufs);
2687 __put_user(result, &_xfern->result);
2688 return result < 0 ? result : 0;
2689 }
2690 case SNDRV_PCM_IOCTL_REWIND:
2691 {
2692 snd_pcm_uframes_t frames;
2693 snd_pcm_uframes_t __user *_frames = arg;
2694 snd_pcm_sframes_t result;
2695 if (get_user(frames, _frames))
2696 return -EFAULT;
2697 if (put_user(0, _frames))
2698 return -EFAULT;
2699 result = snd_pcm_capture_rewind(substream, frames);
2700 __put_user(result, _frames);
2701 return result < 0 ? result : 0;
2702 }
2703 case SNDRV_PCM_IOCTL_FORWARD:
2704 {
2705 snd_pcm_uframes_t frames;
2706 snd_pcm_uframes_t __user *_frames = arg;
2707 snd_pcm_sframes_t result;
2708 if (get_user(frames, _frames))
2709 return -EFAULT;
2710 if (put_user(0, _frames))
2711 return -EFAULT;
2712 result = snd_pcm_capture_forward(substream, frames);
2713 __put_user(result, _frames);
2714 return result < 0 ? result : 0;
2715 }
2716 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002717 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
Takashi Iwai877211f2005-11-17 13:59:38 +01002720static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2721 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Takashi Iwai877211f2005-11-17 13:59:38 +01002723 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 pcm_file = file->private_data;
2726
2727 if (((cmd >> 8) & 0xff) != 'A')
2728 return -ENOTTY;
2729
Takashi Iwai0df63e42006-04-28 15:13:41 +02002730 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2731 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Takashi Iwai877211f2005-11-17 13:59:38 +01002734static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2735 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Takashi Iwai877211f2005-11-17 13:59:38 +01002737 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 pcm_file = file->private_data;
2740
2741 if (((cmd >> 8) & 0xff) != 'A')
2742 return -ENOTTY;
2743
Takashi Iwai0df63e42006-04-28 15:13:41 +02002744 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2745 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
Takashi Iwai877211f2005-11-17 13:59:38 +01002748int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 unsigned int cmd, void *arg)
2750{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002751 mm_segment_t fs;
2752 int result;
2753
2754 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 switch (substream->stream) {
2756 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002757 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2758 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002761 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2762 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002765 result = -EINVAL;
2766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002768 snd_leave_user(fs);
2769 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002772EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2773
Takashi Iwai877211f2005-11-17 13:59:38 +01002774static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2775 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
Takashi Iwai877211f2005-11-17 13:59:38 +01002777 struct snd_pcm_file *pcm_file;
2778 struct snd_pcm_substream *substream;
2779 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 snd_pcm_sframes_t result;
2781
2782 pcm_file = file->private_data;
2783 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002784 if (PCM_RUNTIME_CHECK(substream))
2785 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 runtime = substream->runtime;
2787 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2788 return -EBADFD;
2789 if (!frame_aligned(runtime, count))
2790 return -EINVAL;
2791 count = bytes_to_frames(runtime, count);
2792 result = snd_pcm_lib_read(substream, buf, count);
2793 if (result > 0)
2794 result = frames_to_bytes(runtime, result);
2795 return result;
2796}
2797
Takashi Iwai877211f2005-11-17 13:59:38 +01002798static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2799 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
Takashi Iwai877211f2005-11-17 13:59:38 +01002801 struct snd_pcm_file *pcm_file;
2802 struct snd_pcm_substream *substream;
2803 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 snd_pcm_sframes_t result;
2805
2806 pcm_file = file->private_data;
2807 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002808 if (PCM_RUNTIME_CHECK(substream))
2809 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002811 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2812 return -EBADFD;
2813 if (!frame_aligned(runtime, count))
2814 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 count = bytes_to_frames(runtime, count);
2816 result = snd_pcm_lib_write(substream, buf, count);
2817 if (result > 0)
2818 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return result;
2820}
2821
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002822static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2823 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825{
Takashi Iwai877211f2005-11-17 13:59:38 +01002826 struct snd_pcm_file *pcm_file;
2827 struct snd_pcm_substream *substream;
2828 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 snd_pcm_sframes_t result;
2830 unsigned long i;
2831 void __user **bufs;
2832 snd_pcm_uframes_t frames;
2833
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002834 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002836 if (PCM_RUNTIME_CHECK(substream))
2837 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 runtime = substream->runtime;
2839 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2840 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002841 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002843 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002845 frames = bytes_to_samples(runtime, iov->iov_len);
2846 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 if (bufs == NULL)
2848 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002849 for (i = 0; i < nr_segs; ++i)
2850 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 result = snd_pcm_lib_readv(substream, bufs, frames);
2852 if (result > 0)
2853 result = frames_to_bytes(runtime, result);
2854 kfree(bufs);
2855 return result;
2856}
2857
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002858static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2859 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Takashi Iwai877211f2005-11-17 13:59:38 +01002861 struct snd_pcm_file *pcm_file;
2862 struct snd_pcm_substream *substream;
2863 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 snd_pcm_sframes_t result;
2865 unsigned long i;
2866 void __user **bufs;
2867 snd_pcm_uframes_t frames;
2868
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002869 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002871 if (PCM_RUNTIME_CHECK(substream))
2872 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002874 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2875 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002876 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002877 !frame_aligned(runtime, iov->iov_len))
2878 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002879 frames = bytes_to_samples(runtime, iov->iov_len);
2880 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 if (bufs == NULL)
2882 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002883 for (i = 0; i < nr_segs; ++i)
2884 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 result = snd_pcm_lib_writev(substream, bufs, frames);
2886 if (result > 0)
2887 result = frames_to_bytes(runtime, result);
2888 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return result;
2890}
2891
2892static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2893{
Takashi Iwai877211f2005-11-17 13:59:38 +01002894 struct snd_pcm_file *pcm_file;
2895 struct snd_pcm_substream *substream;
2896 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 unsigned int mask;
2898 snd_pcm_uframes_t avail;
2899
2900 pcm_file = file->private_data;
2901
2902 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002903 if (PCM_RUNTIME_CHECK(substream))
2904 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 runtime = substream->runtime;
2906
2907 poll_wait(file, &runtime->sleep, wait);
2908
2909 snd_pcm_stream_lock_irq(substream);
2910 avail = snd_pcm_playback_avail(runtime);
2911 switch (runtime->status->state) {
2912 case SNDRV_PCM_STATE_RUNNING:
2913 case SNDRV_PCM_STATE_PREPARED:
2914 case SNDRV_PCM_STATE_PAUSED:
2915 if (avail >= runtime->control->avail_min) {
2916 mask = POLLOUT | POLLWRNORM;
2917 break;
2918 }
2919 /* Fall through */
2920 case SNDRV_PCM_STATE_DRAINING:
2921 mask = 0;
2922 break;
2923 default:
2924 mask = POLLOUT | POLLWRNORM | POLLERR;
2925 break;
2926 }
2927 snd_pcm_stream_unlock_irq(substream);
2928 return mask;
2929}
2930
2931static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2932{
Takashi Iwai877211f2005-11-17 13:59:38 +01002933 struct snd_pcm_file *pcm_file;
2934 struct snd_pcm_substream *substream;
2935 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 unsigned int mask;
2937 snd_pcm_uframes_t avail;
2938
2939 pcm_file = file->private_data;
2940
2941 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002942 if (PCM_RUNTIME_CHECK(substream))
2943 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 runtime = substream->runtime;
2945
2946 poll_wait(file, &runtime->sleep, wait);
2947
2948 snd_pcm_stream_lock_irq(substream);
2949 avail = snd_pcm_capture_avail(runtime);
2950 switch (runtime->status->state) {
2951 case SNDRV_PCM_STATE_RUNNING:
2952 case SNDRV_PCM_STATE_PREPARED:
2953 case SNDRV_PCM_STATE_PAUSED:
2954 if (avail >= runtime->control->avail_min) {
2955 mask = POLLIN | POLLRDNORM;
2956 break;
2957 }
2958 mask = 0;
2959 break;
2960 case SNDRV_PCM_STATE_DRAINING:
2961 if (avail > 0) {
2962 mask = POLLIN | POLLRDNORM;
2963 break;
2964 }
2965 /* Fall through */
2966 default:
2967 mask = POLLIN | POLLRDNORM | POLLERR;
2968 break;
2969 }
2970 snd_pcm_stream_unlock_irq(substream);
2971 return mask;
2972}
2973
2974/*
2975 * mmap support
2976 */
2977
2978/*
2979 * Only on coherent architectures, we can mmap the status and the control records
2980 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2981 */
2982#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2983/*
2984 * mmap status record
2985 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002986static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2987 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Takashi Iwai877211f2005-11-17 13:59:38 +01002989 struct snd_pcm_substream *substream = area->vm_private_data;
2990 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002993 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002995 vmf->page = virt_to_page(runtime->status);
2996 get_page(vmf->page);
2997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
3000static struct vm_operations_struct snd_pcm_vm_ops_status =
3001{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003002 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003};
3004
Takashi Iwai877211f2005-11-17 13:59:38 +01003005static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 struct vm_area_struct *area)
3007{
Takashi Iwai877211f2005-11-17 13:59:38 +01003008 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 long size;
3010 if (!(area->vm_flags & VM_READ))
3011 return -EINVAL;
3012 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003014 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return -EINVAL;
3016 area->vm_ops = &snd_pcm_vm_ops_status;
3017 area->vm_private_data = substream;
3018 area->vm_flags |= VM_RESERVED;
3019 return 0;
3020}
3021
3022/*
3023 * mmap control record
3024 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003025static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3026 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Takashi Iwai877211f2005-11-17 13:59:38 +01003028 struct snd_pcm_substream *substream = area->vm_private_data;
3029 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003032 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003034 vmf->page = virt_to_page(runtime->control);
3035 get_page(vmf->page);
3036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
3039static struct vm_operations_struct snd_pcm_vm_ops_control =
3040{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003041 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042};
3043
Takashi Iwai877211f2005-11-17 13:59:38 +01003044static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 struct vm_area_struct *area)
3046{
Takashi Iwai877211f2005-11-17 13:59:38 +01003047 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 long size;
3049 if (!(area->vm_flags & VM_READ))
3050 return -EINVAL;
3051 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003053 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return -EINVAL;
3055 area->vm_ops = &snd_pcm_vm_ops_control;
3056 area->vm_private_data = substream;
3057 area->vm_flags |= VM_RESERVED;
3058 return 0;
3059}
3060#else /* ! coherent mmap */
3061/*
3062 * don't support mmap for status and control records.
3063 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003064static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct vm_area_struct *area)
3066{
3067 return -ENXIO;
3068}
Takashi Iwai877211f2005-11-17 13:59:38 +01003069static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct vm_area_struct *area)
3071{
3072 return -ENXIO;
3073}
3074#endif /* coherent mmap */
3075
3076/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003077 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003079static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3080 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Takashi Iwai877211f2005-11-17 13:59:38 +01003082 struct snd_pcm_substream *substream = area->vm_private_data;
3083 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 unsigned long offset;
3085 struct page * page;
3086 void *vaddr;
3087 size_t dma_bytes;
3088
3089 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003090 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003092 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3094 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003095 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (substream->ops->page) {
3097 page = substream->ops->page(substream, offset);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003098 if (!page)
3099 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 } else {
3101 vaddr = runtime->dma_area + offset;
3102 page = virt_to_page(vaddr);
3103 }
Nick Pigginb5810032005-10-29 18:16:12 -07003104 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003105 vmf->page = page;
3106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
3109static struct vm_operations_struct snd_pcm_vm_ops_data =
3110{
3111 .open = snd_pcm_mmap_data_open,
3112 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003113 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114};
3115
3116/*
3117 * mmap the DMA buffer on RAM
3118 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003119static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3120 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
3122 area->vm_ops = &snd_pcm_vm_ops_data;
3123 area->vm_private_data = substream;
3124 area->vm_flags |= VM_RESERVED;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003125 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 return 0;
3127}
3128
3129/*
3130 * mmap the DMA buffer on I/O memory area
3131 */
3132#if SNDRV_PCM_INFO_MMAP_IOMEM
3133static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3134{
3135 .open = snd_pcm_mmap_data_open,
3136 .close = snd_pcm_mmap_data_close,
3137};
3138
Takashi Iwai877211f2005-11-17 13:59:38 +01003139int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3140 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
3142 long size;
3143 unsigned long offset;
3144
3145#ifdef pgprot_noncached
3146 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3147#endif
3148 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3149 area->vm_private_data = substream;
3150 area->vm_flags |= VM_IO;
3151 size = area->vm_end - area->vm_start;
3152 offset = area->vm_pgoff << PAGE_SHIFT;
3153 if (io_remap_pfn_range(area, area->vm_start,
3154 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3155 size, area->vm_page_prot))
3156 return -EAGAIN;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003157 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 return 0;
3159}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003160
3161EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162#endif /* SNDRV_PCM_INFO_MMAP */
3163
3164/*
3165 * mmap DMA buffer
3166 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003167int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 struct vm_area_struct *area)
3169{
Takashi Iwai877211f2005-11-17 13:59:38 +01003170 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 long size;
3172 unsigned long offset;
3173 size_t dma_bytes;
3174
3175 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3176 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3177 return -EINVAL;
3178 } else {
3179 if (!(area->vm_flags & VM_READ))
3180 return -EINVAL;
3181 }
3182 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3184 return -EBADFD;
3185 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3186 return -ENXIO;
3187 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3188 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3189 return -EINVAL;
3190 size = area->vm_end - area->vm_start;
3191 offset = area->vm_pgoff << PAGE_SHIFT;
3192 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3193 if ((size_t)size > dma_bytes)
3194 return -EINVAL;
3195 if (offset > dma_bytes - size)
3196 return -EINVAL;
3197
3198 if (substream->ops->mmap)
3199 return substream->ops->mmap(substream, area);
3200 else
3201 return snd_pcm_default_mmap(substream, area);
3202}
3203
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003204EXPORT_SYMBOL(snd_pcm_mmap_data);
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3207{
Takashi Iwai877211f2005-11-17 13:59:38 +01003208 struct snd_pcm_file * pcm_file;
3209 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 unsigned long offset;
3211
3212 pcm_file = file->private_data;
3213 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003214 if (PCM_RUNTIME_CHECK(substream))
3215 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 offset = area->vm_pgoff << PAGE_SHIFT;
3218 switch (offset) {
3219 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003220 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return -ENXIO;
3222 return snd_pcm_mmap_status(substream, file, area);
3223 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003224 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return -ENXIO;
3226 return snd_pcm_mmap_control(substream, file, area);
3227 default:
3228 return snd_pcm_mmap_data(substream, file, area);
3229 }
3230 return 0;
3231}
3232
3233static int snd_pcm_fasync(int fd, struct file * file, int on)
3234{
Takashi Iwai877211f2005-11-17 13:59:38 +01003235 struct snd_pcm_file * pcm_file;
3236 struct snd_pcm_substream *substream;
3237 struct snd_pcm_runtime *runtime;
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003238 int err = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003240 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 pcm_file = file->private_data;
3242 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003243 if (PCM_RUNTIME_CHECK(substream))
3244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 err = fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds685d87f2008-08-06 19:24:47 -07003247out:
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003248 unlock_kernel();
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003249 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
3252/*
3253 * ioctl32 compat
3254 */
3255#ifdef CONFIG_COMPAT
3256#include "pcm_compat.c"
3257#else
3258#define snd_pcm_ioctl_compat NULL
3259#endif
3260
3261/*
3262 * To be removed helpers to keep binary compatibility
3263 */
3264
Takashi Iwai59d48582005-12-01 10:51:58 +01003265#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3267#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3268
Takashi Iwai877211f2005-11-17 13:59:38 +01003269static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3270 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
3272 unsigned int i;
3273
3274 memset(params, 0, sizeof(*params));
3275 params->flags = oparams->flags;
3276 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3277 params->masks[i].bits[0] = oparams->masks[i];
3278 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3279 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3280 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3281 params->info = oparams->info;
3282 params->msbits = oparams->msbits;
3283 params->rate_num = oparams->rate_num;
3284 params->rate_den = oparams->rate_den;
3285 params->fifo_size = oparams->fifo_size;
3286}
3287
Takashi Iwai877211f2005-11-17 13:59:38 +01003288static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3289 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
3291 unsigned int i;
3292
3293 memset(oparams, 0, sizeof(*oparams));
3294 oparams->flags = params->flags;
3295 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3296 oparams->masks[i] = params->masks[i].bits[0];
3297 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3298 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3299 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3300 oparams->info = params->info;
3301 oparams->msbits = params->msbits;
3302 oparams->rate_num = params->rate_num;
3303 oparams->rate_den = params->rate_den;
3304 oparams->fifo_size = params->fifo_size;
3305}
3306
Takashi Iwai877211f2005-11-17 13:59:38 +01003307static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3308 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
Takashi Iwai877211f2005-11-17 13:59:38 +01003310 struct snd_pcm_hw_params *params;
3311 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 int err;
3313
3314 params = kmalloc(sizeof(*params), GFP_KERNEL);
3315 if (!params) {
3316 err = -ENOMEM;
3317 goto out;
3318 }
3319 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3320 if (!oparams) {
3321 err = -ENOMEM;
3322 goto out;
3323 }
3324
3325 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3326 err = -EFAULT;
3327 goto out;
3328 }
3329 snd_pcm_hw_convert_from_old_params(params, oparams);
3330 err = snd_pcm_hw_refine(substream, params);
3331 snd_pcm_hw_convert_to_old_params(oparams, params);
3332 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3333 if (!err)
3334 err = -EFAULT;
3335 }
3336out:
3337 kfree(params);
3338 kfree(oparams);
3339 return err;
3340}
3341
Takashi Iwai877211f2005-11-17 13:59:38 +01003342static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3343 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
Takashi Iwai877211f2005-11-17 13:59:38 +01003345 struct snd_pcm_hw_params *params;
3346 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 int err;
3348
3349 params = kmalloc(sizeof(*params), GFP_KERNEL);
3350 if (!params) {
3351 err = -ENOMEM;
3352 goto out;
3353 }
3354 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3355 if (!oparams) {
3356 err = -ENOMEM;
3357 goto out;
3358 }
3359 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3360 err = -EFAULT;
3361 goto out;
3362 }
3363 snd_pcm_hw_convert_from_old_params(params, oparams);
3364 err = snd_pcm_hw_params(substream, params);
3365 snd_pcm_hw_convert_to_old_params(oparams, params);
3366 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3367 if (!err)
3368 err = -EFAULT;
3369 }
3370out:
3371 kfree(params);
3372 kfree(oparams);
3373 return err;
3374}
Takashi Iwai59d48582005-12-01 10:51:58 +01003375#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Cliff Cai70036092008-09-03 10:54:36 +02003377#ifndef CONFIG_MMU
3378unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3379 unsigned long len, unsigned long pgoff,
3380 unsigned long flags)
3381{
3382 return 0;
3383}
3384#else
3385# define dummy_get_unmapped_area NULL
3386#endif
3387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388/*
3389 * Register section
3390 */
3391
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003392const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003394 .owner = THIS_MODULE,
3395 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003396 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003397 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003398 .release = snd_pcm_release,
3399 .poll = snd_pcm_playback_poll,
3400 .unlocked_ioctl = snd_pcm_playback_ioctl,
3401 .compat_ioctl = snd_pcm_ioctl_compat,
3402 .mmap = snd_pcm_mmap,
3403 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003404 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 },
3406 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003407 .owner = THIS_MODULE,
3408 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003409 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003410 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003411 .release = snd_pcm_release,
3412 .poll = snd_pcm_capture_poll,
3413 .unlocked_ioctl = snd_pcm_capture_ioctl,
3414 .compat_ioctl = snd_pcm_ioctl_compat,
3415 .mmap = snd_pcm_mmap,
3416 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003417 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
3419};