blob: df2299a8cefd4d464966ee8b6fbc3988ad94a2cc [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
189 printk("%s = ", snd_pcm_hw_param_names[k]);
190 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
209 printk("%s = ", snd_pcm_hw_param_names[k]);
210 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
254 printk("Rule %d [%p]: ", k, r->func);
255 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/**
878 * snd_pcm_start
Takashi Iwaidf8db932005-09-07 13:38:19 +0200879 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 *
881 * Start all linked streams.
882 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100883int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Takashi Iwai877211f2005-11-17 13:59:38 +0100885 return snd_pcm_action(&snd_pcm_action_start, substream,
886 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889/*
890 * stop callbacks
891 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100892static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Takashi Iwai877211f2005-11-17 13:59:38 +0100894 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
896 return -EBADFD;
897 runtime->trigger_master = substream;
898 return 0;
899}
900
Takashi Iwai877211f2005-11-17 13:59:38 +0100901static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 if (substream->runtime->trigger_master == substream &&
904 snd_pcm_running(substream))
905 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
906 return 0; /* unconditonally stop all substreams */
907}
908
Takashi Iwai877211f2005-11-17 13:59:38 +0100909static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwai877211f2005-11-17 13:59:38 +0100911 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (runtime->status->state != state) {
913 snd_pcm_trigger_tstamp(substream);
914 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100915 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
916 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 wake_up(&runtime->sleep);
920}
921
922static struct action_ops snd_pcm_action_stop = {
923 .pre_action = snd_pcm_pre_stop,
924 .do_action = snd_pcm_do_stop,
925 .post_action = snd_pcm_post_stop
926};
927
928/**
929 * snd_pcm_stop
Takashi Iwaidf8db932005-09-07 13:38:19 +0200930 * @substream: the PCM substream instance
931 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 *
933 * Try to stop all running streams in the substream group.
934 * The state of each stream is changed to the given value after that unconditionally.
935 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100936int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
939}
940
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200941EXPORT_SYMBOL(snd_pcm_stop);
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943/**
944 * snd_pcm_drain_done
Takashi Iwaidf8db932005-09-07 13:38:19 +0200945 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 *
947 * Stop the DMA only when the given stream is playback.
948 * The state is changed to SETUP.
949 * Unlike snd_pcm_stop(), this affects only the given stream.
950 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100951int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Takashi Iwai877211f2005-11-17 13:59:38 +0100953 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
954 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*
958 * pause callbacks
959 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100960static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Takashi Iwai877211f2005-11-17 13:59:38 +0100962 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
964 return -ENOSYS;
965 if (push) {
966 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
967 return -EBADFD;
968 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
969 return -EBADFD;
970 runtime->trigger_master = substream;
971 return 0;
972}
973
Takashi Iwai877211f2005-11-17 13:59:38 +0100974static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 if (substream->runtime->trigger_master != substream)
977 return 0;
978 return substream->ops->trigger(substream,
979 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
980 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
981}
982
Takashi Iwai877211f2005-11-17 13:59:38 +0100983static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 if (substream->runtime->trigger_master == substream)
986 substream->ops->trigger(substream,
987 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
988 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
989}
990
Takashi Iwai877211f2005-11-17 13:59:38 +0100991static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Takashi Iwai877211f2005-11-17 13:59:38 +0100993 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 snd_pcm_trigger_tstamp(substream);
995 if (push) {
996 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
997 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100998 snd_timer_notify(substream->timer,
999 SNDRV_TIMER_EVENT_MPAUSE,
1000 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 wake_up(&runtime->sleep);
1002 } else {
1003 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001005 snd_timer_notify(substream->timer,
1006 SNDRV_TIMER_EVENT_MCONTINUE,
1007 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009}
1010
1011static struct action_ops snd_pcm_action_pause = {
1012 .pre_action = snd_pcm_pre_pause,
1013 .do_action = snd_pcm_do_pause,
1014 .undo_action = snd_pcm_undo_pause,
1015 .post_action = snd_pcm_post_pause
1016};
1017
1018/*
1019 * Push/release the pause for all linked streams.
1020 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001021static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1024}
1025
1026#ifdef CONFIG_PM
1027/* suspend */
1028
Takashi Iwai877211f2005-11-17 13:59:38 +01001029static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Takashi Iwai877211f2005-11-17 13:59:38 +01001031 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1033 return -EBUSY;
1034 runtime->trigger_master = substream;
1035 return 0;
1036}
1037
Takashi Iwai877211f2005-11-17 13:59:38 +01001038static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Takashi Iwai877211f2005-11-17 13:59:38 +01001040 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (runtime->trigger_master != substream)
1042 return 0;
1043 if (! snd_pcm_running(substream))
1044 return 0;
1045 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1046 return 0; /* suspend unconditionally */
1047}
1048
Takashi Iwai877211f2005-11-17 13:59:38 +01001049static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Takashi Iwai877211f2005-11-17 13:59:38 +01001051 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 snd_pcm_trigger_tstamp(substream);
1053 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001054 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1055 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 runtime->status->suspended_state = runtime->status->state;
1057 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 wake_up(&runtime->sleep);
1059}
1060
1061static struct action_ops snd_pcm_action_suspend = {
1062 .pre_action = snd_pcm_pre_suspend,
1063 .do_action = snd_pcm_do_suspend,
1064 .post_action = snd_pcm_post_suspend
1065};
1066
1067/**
1068 * snd_pcm_suspend
Takashi Iwaidf8db932005-09-07 13:38:19 +02001069 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 *
1071 * Trigger SUSPEND to all linked streams.
1072 * After this call, all streams are changed to SUSPENDED state.
1073 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001074int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 int err;
1077 unsigned long flags;
1078
Takashi Iwai603bf5242005-11-17 15:59:14 +01001079 if (! substream)
1080 return 0;
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 snd_pcm_stream_lock_irqsave(substream, flags);
1083 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1084 snd_pcm_stream_unlock_irqrestore(substream, flags);
1085 return err;
1086}
1087
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001088EXPORT_SYMBOL(snd_pcm_suspend);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090/**
1091 * snd_pcm_suspend_all
Takashi Iwaidf8db932005-09-07 13:38:19 +02001092 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 *
1094 * Trigger SUSPEND to all substreams in the given pcm.
1095 * After this call, all streams are changed to SUSPENDED state.
1096 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001097int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Takashi Iwai877211f2005-11-17 13:59:38 +01001099 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int stream, err = 0;
1101
Takashi Iwai603bf5242005-11-17 15:59:14 +01001102 if (! pcm)
1103 return 0;
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001106 for (substream = pcm->streams[stream].substream;
1107 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* FIXME: the open/close code should lock this as well */
1109 if (substream->runtime == NULL)
1110 continue;
1111 err = snd_pcm_suspend(substream);
1112 if (err < 0 && err != -EBUSY)
1113 return err;
1114 }
1115 }
1116 return 0;
1117}
1118
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001119EXPORT_SYMBOL(snd_pcm_suspend_all);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/* resume */
1122
Takashi Iwai877211f2005-11-17 13:59:38 +01001123static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Takashi Iwai877211f2005-11-17 13:59:38 +01001125 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1127 return -ENOSYS;
1128 runtime->trigger_master = substream;
1129 return 0;
1130}
1131
Takashi Iwai877211f2005-11-17 13:59:38 +01001132static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Takashi Iwai877211f2005-11-17 13:59:38 +01001134 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (runtime->trigger_master != substream)
1136 return 0;
1137 /* DMA not running previously? */
1138 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1139 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1140 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1141 return 0;
1142 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1143}
1144
Takashi Iwai877211f2005-11-17 13:59:38 +01001145static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 if (substream->runtime->trigger_master == substream &&
1148 snd_pcm_running(substream))
1149 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1150}
1151
Takashi Iwai877211f2005-11-17 13:59:38 +01001152static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Takashi Iwai877211f2005-11-17 13:59:38 +01001154 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 snd_pcm_trigger_tstamp(substream);
1156 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001157 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1158 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162static struct action_ops snd_pcm_action_resume = {
1163 .pre_action = snd_pcm_pre_resume,
1164 .do_action = snd_pcm_do_resume,
1165 .undo_action = snd_pcm_undo_resume,
1166 .post_action = snd_pcm_post_resume
1167};
1168
Takashi Iwai877211f2005-11-17 13:59:38 +01001169static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Takashi Iwai877211f2005-11-17 13:59:38 +01001171 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 int res;
1173
1174 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001175 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1177 snd_power_unlock(card);
1178 return res;
1179}
1180
1181#else
1182
Takashi Iwai877211f2005-11-17 13:59:38 +01001183static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 return -ENOSYS;
1186}
1187
1188#endif /* CONFIG_PM */
1189
1190/*
1191 * xrun ioctl
1192 *
1193 * Change the RUNNING stream(s) to XRUN state.
1194 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001195static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Takashi Iwai877211f2005-11-17 13:59:38 +01001197 struct snd_card *card = substream->pcm->card;
1198 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int result;
1200
1201 snd_power_lock(card);
1202 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001203 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (result < 0)
1205 goto _unlock;
1206 }
1207
1208 snd_pcm_stream_lock_irq(substream);
1209 switch (runtime->status->state) {
1210 case SNDRV_PCM_STATE_XRUN:
1211 result = 0; /* already there */
1212 break;
1213 case SNDRV_PCM_STATE_RUNNING:
1214 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1215 break;
1216 default:
1217 result = -EBADFD;
1218 }
1219 snd_pcm_stream_unlock_irq(substream);
1220 _unlock:
1221 snd_power_unlock(card);
1222 return result;
1223}
1224
1225/*
1226 * reset ioctl
1227 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001228static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Takashi Iwai877211f2005-11-17 13:59:38 +01001230 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 switch (runtime->status->state) {
1232 case SNDRV_PCM_STATE_RUNNING:
1233 case SNDRV_PCM_STATE_PREPARED:
1234 case SNDRV_PCM_STATE_PAUSED:
1235 case SNDRV_PCM_STATE_SUSPENDED:
1236 return 0;
1237 default:
1238 return -EBADFD;
1239 }
1240}
1241
Takashi Iwai877211f2005-11-17 13:59:38 +01001242static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Takashi Iwai877211f2005-11-17 13:59:38 +01001244 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1246 if (err < 0)
1247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001249 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1250 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 runtime->silence_start = runtime->status->hw_ptr;
1252 runtime->silence_filled = 0;
1253 return 0;
1254}
1255
Takashi Iwai877211f2005-11-17 13:59:38 +01001256static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Takashi Iwai877211f2005-11-17 13:59:38 +01001258 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 runtime->control->appl_ptr = runtime->status->hw_ptr;
1260 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1261 runtime->silence_size > 0)
1262 snd_pcm_playback_silence(substream, ULONG_MAX);
1263}
1264
1265static struct action_ops snd_pcm_action_reset = {
1266 .pre_action = snd_pcm_pre_reset,
1267 .do_action = snd_pcm_do_reset,
1268 .post_action = snd_pcm_post_reset
1269};
1270
Takashi Iwai877211f2005-11-17 13:59:38 +01001271static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1274}
1275
1276/*
1277 * prepare ioctl
1278 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001279/* we use the second argument for updating f_flags */
1280static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1281 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Takashi Iwai877211f2005-11-17 13:59:38 +01001283 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001284 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1285 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -EBADFD;
1287 if (snd_pcm_running(substream))
1288 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001289 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return 0;
1291}
1292
Takashi Iwai877211f2005-11-17 13:59:38 +01001293static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 int err;
1296 err = substream->ops->prepare(substream);
1297 if (err < 0)
1298 return err;
1299 return snd_pcm_do_reset(substream, 0);
1300}
1301
Takashi Iwai877211f2005-11-17 13:59:38 +01001302static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Takashi Iwai877211f2005-11-17 13:59:38 +01001304 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 runtime->control->appl_ptr = runtime->status->hw_ptr;
1306 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1307}
1308
1309static struct action_ops snd_pcm_action_prepare = {
1310 .pre_action = snd_pcm_pre_prepare,
1311 .do_action = snd_pcm_do_prepare,
1312 .post_action = snd_pcm_post_prepare
1313};
1314
1315/**
1316 * snd_pcm_prepare
Takashi Iwaidf8db932005-09-07 13:38:19 +02001317 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001318 * @file: file to refer f_flags
Takashi Iwaidf8db932005-09-07 13:38:19 +02001319 *
1320 * Prepare the PCM substream to be triggerable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001322static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1323 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001326 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001327 int f_flags;
1328
1329 if (file)
1330 f_flags = file->f_flags;
1331 else
1332 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001335 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001336 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1337 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 snd_power_unlock(card);
1339 return res;
1340}
1341
1342/*
1343 * drain ioctl
1344 */
1345
Takashi Iwai877211f2005-11-17 13:59:38 +01001346static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001348 if (substream->f_flags & O_NONBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return -EAGAIN;
1350 substream->runtime->trigger_master = substream;
1351 return 0;
1352}
1353
Takashi Iwai877211f2005-11-17 13:59:38 +01001354static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Takashi Iwai877211f2005-11-17 13:59:38 +01001356 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1358 switch (runtime->status->state) {
1359 case SNDRV_PCM_STATE_PREPARED:
1360 /* start playback stream if possible */
1361 if (! snd_pcm_playback_empty(substream)) {
1362 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1363 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1364 }
1365 break;
1366 case SNDRV_PCM_STATE_RUNNING:
1367 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1368 break;
1369 default:
1370 break;
1371 }
1372 } else {
1373 /* stop running stream */
1374 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001375 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001377 snd_pcm_do_stop(substream, new_state);
1378 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 }
1381 return 0;
1382}
1383
Takashi Iwai877211f2005-11-17 13:59:38 +01001384static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386}
1387
1388static struct action_ops snd_pcm_action_drain_init = {
1389 .pre_action = snd_pcm_pre_drain_init,
1390 .do_action = snd_pcm_do_drain_init,
1391 .post_action = snd_pcm_post_drain_init
1392};
1393
1394struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001395 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 wait_queue_t wait;
1397 snd_pcm_uframes_t stop_threshold;
1398};
1399
Takashi Iwai877211f2005-11-17 13:59:38 +01001400static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402/*
1403 * Drain the stream(s).
1404 * When the substream is linked, sync until the draining of all playback streams
1405 * is finished.
1406 * After this call, all streams are supposed to be either SETUP or DRAINING
1407 * (capture only) state.
1408 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001409static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Takashi Iwai877211f2005-11-17 13:59:38 +01001411 struct snd_card *card;
1412 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001413 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 int result = 0;
1415 int i, num_drecs;
1416 struct drain_rec *drec, drec_tmp, *d;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 card = substream->pcm->card;
1419 runtime = substream->runtime;
1420
1421 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1422 return -EBADFD;
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 snd_power_lock(card);
1425 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001426 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001427 if (result < 0) {
1428 snd_power_unlock(card);
1429 return result;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432
1433 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001434 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (snd_pcm_stream_linked(substream)) {
1436 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1437 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001438 up_read(&snd_pcm_link_rwsem);
1439 snd_power_unlock(card);
1440 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442 } else
1443 drec = &drec_tmp;
1444
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001445 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 num_drecs = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001447 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1450 d = &drec[num_drecs++];
1451 d->substream = s;
1452 init_waitqueue_entry(&d->wait, current);
1453 add_wait_queue(&runtime->sleep, &d->wait);
1454 /* stop_threshold fixup to avoid endless loop when
1455 * stop_threshold > buffer_size
1456 */
1457 d->stop_threshold = runtime->stop_threshold;
1458 if (runtime->stop_threshold > runtime->buffer_size)
1459 runtime->stop_threshold = runtime->buffer_size;
1460 }
1461 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001462 up_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001463
1464 snd_pcm_stream_lock_irq(substream);
1465 /* resume pause */
Takashi Iwai1a7fa542007-07-06 12:27:25 +02001466 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001467 snd_pcm_pause(substream, 0);
1468
1469 /* pre-start/stop - all running streams are changed to DRAINING state */
1470 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1471 if (result < 0) {
1472 snd_pcm_stream_unlock_irq(substream);
1473 goto _error;
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 for (;;) {
1477 long tout;
1478 if (signal_pending(current)) {
1479 result = -ERESTARTSYS;
1480 break;
1481 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001482 /* all finished? */
1483 for (i = 0; i < num_drecs; i++) {
1484 runtime = drec[i].substream->runtime;
1485 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1486 break;
1487 }
1488 if (i == num_drecs)
1489 break; /* yes, all drained */
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 set_current_state(TASK_INTERRUPTIBLE);
1492 snd_pcm_stream_unlock_irq(substream);
1493 snd_power_unlock(card);
1494 tout = schedule_timeout(10 * HZ);
1495 snd_power_lock(card);
1496 snd_pcm_stream_lock_irq(substream);
1497 if (tout == 0) {
1498 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1499 result = -ESTRPIPE;
1500 else {
1501 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1502 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1503 result = -EIO;
1504 }
1505 break;
1506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001508
1509 snd_pcm_stream_unlock_irq(substream);
1510
1511 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 for (i = 0; i < num_drecs; i++) {
1513 d = &drec[i];
1514 runtime = d->substream->runtime;
1515 remove_wait_queue(&runtime->sleep, &d->wait);
1516 runtime->stop_threshold = d->stop_threshold;
1517 }
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (drec != &drec_tmp)
1520 kfree(drec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 return result;
1524}
1525
1526/*
1527 * drop ioctl
1528 *
1529 * Immediately put all linked substreams into SETUP state.
1530 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001531static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Takashi Iwai877211f2005-11-17 13:59:38 +01001533 struct snd_pcm_runtime *runtime;
1534 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int result = 0;
1536
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001537 if (PCM_RUNTIME_CHECK(substream))
1538 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 runtime = substream->runtime;
1540 card = substream->pcm->card;
1541
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001542 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001543 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1544 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return -EBADFD;
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 snd_pcm_stream_lock_irq(substream);
1548 /* resume pause */
1549 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1550 snd_pcm_pause(substream, 0);
1551
1552 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1553 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1554 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return result;
1557}
1558
1559
1560/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561static struct file *snd_pcm_file_fd(int fd)
1562{
1563 struct file *file;
1564 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001565 unsigned int minor;
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 file = fget(fd);
1568 if (!file)
1569 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001570 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (!S_ISCHR(inode->i_mode) ||
1572 imajor(inode) != snd_major) {
1573 fput(file);
1574 return NULL;
1575 }
1576 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001577 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1578 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 fput(file);
1580 return NULL;
1581 }
1582 return file;
1583}
1584
1585/*
1586 * PCM link handling
1587 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001588static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 int res = 0;
1591 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001592 struct snd_pcm_file *pcm_file;
1593 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 file = snd_pcm_file_fd(fd);
1596 if (!file)
1597 return -EBADFD;
1598 pcm_file = file->private_data;
1599 substream1 = pcm_file->substream;
1600 down_write(&snd_pcm_link_rwsem);
1601 write_lock_irq(&snd_pcm_link_rwlock);
1602 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1603 substream->runtime->status->state != substream1->runtime->status->state) {
1604 res = -EBADFD;
1605 goto _end;
1606 }
1607 if (snd_pcm_stream_linked(substream1)) {
1608 res = -EALREADY;
1609 goto _end;
1610 }
1611 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001612 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (substream->group == NULL) {
1614 res = -ENOMEM;
1615 goto _end;
1616 }
1617 spin_lock_init(&substream->group->lock);
1618 INIT_LIST_HEAD(&substream->group->substreams);
1619 list_add_tail(&substream->link_list, &substream->group->substreams);
1620 substream->group->count = 1;
1621 }
1622 list_add_tail(&substream1->link_list, &substream->group->substreams);
1623 substream->group->count++;
1624 substream1->group = substream->group;
1625 _end:
1626 write_unlock_irq(&snd_pcm_link_rwlock);
1627 up_write(&snd_pcm_link_rwsem);
1628 fput(file);
1629 return res;
1630}
1631
Takashi Iwai877211f2005-11-17 13:59:38 +01001632static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 substream->group = &substream->self_group;
1635 INIT_LIST_HEAD(&substream->self_group.substreams);
1636 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1637}
1638
Takashi Iwai877211f2005-11-17 13:59:38 +01001639static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Takashi Iwaief991b92007-02-22 12:52:53 +01001641 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int res = 0;
1643
1644 down_write(&snd_pcm_link_rwsem);
1645 write_lock_irq(&snd_pcm_link_rwlock);
1646 if (!snd_pcm_stream_linked(substream)) {
1647 res = -EALREADY;
1648 goto _end;
1649 }
1650 list_del(&substream->link_list);
1651 substream->group->count--;
1652 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001653 snd_pcm_group_for_each_entry(s, substream) {
1654 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 break;
1656 }
1657 kfree(substream->group);
1658 }
1659 relink_to_local(substream);
1660 _end:
1661 write_unlock_irq(&snd_pcm_link_rwlock);
1662 up_write(&snd_pcm_link_rwsem);
1663 return res;
1664}
1665
1666/*
1667 * hw configurator
1668 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001669static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1670 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Takashi Iwai877211f2005-11-17 13:59:38 +01001672 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1674 hw_param_interval_c(params, rule->deps[1]), &t);
1675 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1676}
1677
Takashi Iwai877211f2005-11-17 13:59:38 +01001678static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1679 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Takashi Iwai877211f2005-11-17 13:59:38 +01001681 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1683 hw_param_interval_c(params, rule->deps[1]), &t);
1684 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1685}
1686
Takashi Iwai877211f2005-11-17 13:59:38 +01001687static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1688 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Takashi Iwai877211f2005-11-17 13:59:38 +01001690 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1692 hw_param_interval_c(params, rule->deps[1]),
1693 (unsigned long) rule->private, &t);
1694 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1695}
1696
Takashi Iwai877211f2005-11-17 13:59:38 +01001697static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1698 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Takashi Iwai877211f2005-11-17 13:59:38 +01001700 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1702 (unsigned long) rule->private,
1703 hw_param_interval_c(params, rule->deps[1]), &t);
1704 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1705}
1706
Takashi Iwai877211f2005-11-17 13:59:38 +01001707static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1708 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001711 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1712 struct snd_mask m;
1713 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 snd_mask_any(&m);
1715 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1716 int bits;
1717 if (! snd_mask_test(mask, k))
1718 continue;
1719 bits = snd_pcm_format_physical_width(k);
1720 if (bits <= 0)
1721 continue; /* ignore invalid formats */
1722 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1723 snd_mask_reset(&m, k);
1724 }
1725 return snd_mask_refine(mask, &m);
1726}
1727
Takashi Iwai877211f2005-11-17 13:59:38 +01001728static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1729 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Takashi Iwai877211f2005-11-17 13:59:38 +01001731 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 unsigned int k;
1733 t.min = UINT_MAX;
1734 t.max = 0;
1735 t.openmin = 0;
1736 t.openmax = 0;
1737 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1738 int bits;
1739 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1740 continue;
1741 bits = snd_pcm_format_physical_width(k);
1742 if (bits <= 0)
1743 continue; /* ignore invalid formats */
1744 if (t.min > (unsigned)bits)
1745 t.min = bits;
1746 if (t.max < (unsigned)bits)
1747 t.max = bits;
1748 }
1749 t.integer = 1;
1750 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1751}
1752
1753#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1754#error "Change this table"
1755#endif
1756
1757static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1758 48000, 64000, 88200, 96000, 176400, 192000 };
1759
Clemens Ladisch7653d552007-08-13 17:38:54 +02001760const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1761 .count = ARRAY_SIZE(rates),
1762 .list = rates,
1763};
1764
Takashi Iwai877211f2005-11-17 13:59:38 +01001765static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1766 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Takashi Iwai877211f2005-11-17 13:59:38 +01001768 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001770 snd_pcm_known_rates.count,
1771 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Takashi Iwai877211f2005-11-17 13:59:38 +01001774static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1775 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwai877211f2005-11-17 13:59:38 +01001777 struct snd_interval t;
1778 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 t.min = 0;
1780 t.max = substream->buffer_bytes_max;
1781 t.openmin = 0;
1782 t.openmax = 0;
1783 t.integer = 1;
1784 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1785}
1786
Takashi Iwai877211f2005-11-17 13:59:38 +01001787int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Takashi Iwai877211f2005-11-17 13:59:38 +01001789 struct snd_pcm_runtime *runtime = substream->runtime;
1790 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int k, err;
1792
1793 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1794 snd_mask_any(constrs_mask(constrs, k));
1795 }
1796
1797 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1798 snd_interval_any(constrs_interval(constrs, k));
1799 }
1800
1801 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1802 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1803 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1804 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1805 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1806
1807 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1808 snd_pcm_hw_rule_format, NULL,
1809 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1810 if (err < 0)
1811 return err;
1812 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1813 snd_pcm_hw_rule_sample_bits, NULL,
1814 SNDRV_PCM_HW_PARAM_FORMAT,
1815 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1816 if (err < 0)
1817 return err;
1818 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1819 snd_pcm_hw_rule_div, NULL,
1820 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1821 if (err < 0)
1822 return err;
1823 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1824 snd_pcm_hw_rule_mul, NULL,
1825 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1826 if (err < 0)
1827 return err;
1828 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1829 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1830 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1831 if (err < 0)
1832 return err;
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1834 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1835 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1836 if (err < 0)
1837 return err;
1838 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1839 snd_pcm_hw_rule_div, NULL,
1840 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1841 if (err < 0)
1842 return err;
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1844 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1845 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1846 if (err < 0)
1847 return err;
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1849 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1850 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1851 if (err < 0)
1852 return err;
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1854 snd_pcm_hw_rule_div, NULL,
1855 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1856 if (err < 0)
1857 return err;
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1859 snd_pcm_hw_rule_div, NULL,
1860 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1861 if (err < 0)
1862 return err;
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1864 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1865 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1866 if (err < 0)
1867 return err;
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1869 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1870 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1871 if (err < 0)
1872 return err;
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1874 snd_pcm_hw_rule_mul, NULL,
1875 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1876 if (err < 0)
1877 return err;
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1879 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1880 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1881 if (err < 0)
1882 return err;
1883 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1884 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1885 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1886 if (err < 0)
1887 return err;
1888 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1889 snd_pcm_hw_rule_muldivk, (void*) 8,
1890 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1891 if (err < 0)
1892 return err;
1893 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1894 snd_pcm_hw_rule_muldivk, (void*) 8,
1895 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1896 if (err < 0)
1897 return err;
1898 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1899 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1900 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1901 if (err < 0)
1902 return err;
1903 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1904 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1905 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1906 if (err < 0)
1907 return err;
1908 return 0;
1909}
1910
Takashi Iwai877211f2005-11-17 13:59:38 +01001911int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Takashi Iwai877211f2005-11-17 13:59:38 +01001913 struct snd_pcm_runtime *runtime = substream->runtime;
1914 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 int err;
1916 unsigned int mask = 0;
1917
1918 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1919 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1920 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1921 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1922 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1923 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1924 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1925 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1926 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1927 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1928 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1929 }
1930 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
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_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001935 if (err < 0)
1936 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001939 if (err < 0)
1940 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1943 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001944 if (err < 0)
1945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1948 hw->rate_min, hw->rate_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001949 if (err < 0)
1950 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1953 hw->period_bytes_min, hw->period_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001954 if (err < 0)
1955 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1958 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001959 if (err < 0)
1960 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1963 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001964 if (err < 0)
1965 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1968 snd_pcm_hw_rule_buffer_bytes_max, substream,
1969 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1970 if (err < 0)
1971 return err;
1972
1973 /* FIXME: remove */
1974 if (runtime->dma_bytes) {
1975 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001976 if (err < 0)
1977 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
1980 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1981 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1982 snd_pcm_hw_rule_rate, hw,
1983 SNDRV_PCM_HW_PARAM_RATE, -1);
1984 if (err < 0)
1985 return err;
1986 }
1987
1988 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1990
1991 return 0;
1992}
1993
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001994static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001997}
1998
1999void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2000{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002001 substream->ref_count--;
2002 if (substream->ref_count > 0)
2003 return;
2004
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002005 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002006 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (substream->ops->hw_free != NULL)
2008 substream->ops->hw_free(substream);
2009 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002010 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
Takashi Iwai15762742006-04-06 19:47:42 +02002012 if (substream->pcm_release) {
2013 substream->pcm_release(substream);
2014 substream->pcm_release = NULL;
2015 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002016 snd_pcm_detach_substream(substream);
2017}
2018
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002019EXPORT_SYMBOL(snd_pcm_release_substream);
2020
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002021int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2022 struct file *file,
2023 struct snd_pcm_substream **rsubstream)
2024{
2025 struct snd_pcm_substream *substream;
2026 int err;
2027
2028 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2029 if (err < 0)
2030 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002031 if (substream->ref_count > 1) {
2032 *rsubstream = substream;
2033 return 0;
2034 }
2035
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002036 err = snd_pcm_hw_constraints_init(substream);
2037 if (err < 0) {
2038 snd_printd("snd_pcm_hw_constraints_init failed\n");
2039 goto error;
2040 }
2041
2042 if ((err = substream->ops->open(substream)) < 0)
2043 goto error;
2044
2045 substream->hw_opened = 1;
2046
2047 err = snd_pcm_hw_constraints_complete(substream);
2048 if (err < 0) {
2049 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2050 goto error;
2051 }
2052
2053 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002055
2056 error:
2057 snd_pcm_release_substream(substream);
2058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002061EXPORT_SYMBOL(snd_pcm_open_substream);
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002064 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002066 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Takashi Iwai877211f2005-11-17 13:59:38 +01002068 struct snd_pcm_file *pcm_file;
2069 struct snd_pcm_substream *substream;
2070 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002071 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002073 if (rpcm_file)
2074 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002076 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2077 if (err < 0)
2078 return err;
2079
Takashi Iwai548a6482006-07-31 16:51:51 +02002080 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2081 if (pcm_file == NULL) {
2082 snd_pcm_release_substream(substream);
2083 return -ENOMEM;
2084 }
2085 pcm_file->substream = substream;
2086 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002087 str = substream->pstr;
2088 substream->file = pcm_file;
2089 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002092 if (rpcm_file)
2093 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 0;
2095}
2096
Clemens Ladischf87135f2005-11-20 14:06:59 +01002097static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Takashi Iwai877211f2005-11-17 13:59:38 +01002099 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002100
2101 pcm = snd_lookup_minor_data(iminor(inode),
2102 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2103 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2104}
2105
2106static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2107{
2108 struct snd_pcm *pcm;
2109
2110 pcm = snd_lookup_minor_data(iminor(inode),
2111 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2112 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2113}
2114
2115static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2116{
2117 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002118 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 wait_queue_t wait;
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (pcm == NULL) {
2122 err = -ENODEV;
2123 goto __error1;
2124 }
2125 err = snd_card_file_add(pcm->card, file);
2126 if (err < 0)
2127 goto __error1;
2128 if (!try_module_get(pcm->card->module)) {
2129 err = -EFAULT;
2130 goto __error2;
2131 }
2132 init_waitqueue_entry(&wait, current);
2133 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002134 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002136 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (err >= 0)
2138 break;
2139 if (err == -EAGAIN) {
2140 if (file->f_flags & O_NONBLOCK) {
2141 err = -EBUSY;
2142 break;
2143 }
2144 } else
2145 break;
2146 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002147 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002149 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (signal_pending(current)) {
2151 err = -ERESTARTSYS;
2152 break;
2153 }
2154 }
2155 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002156 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (err < 0)
2158 goto __error;
2159 return err;
2160
2161 __error:
2162 module_put(pcm->card->module);
2163 __error2:
2164 snd_card_file_remove(pcm->card, file);
2165 __error1:
2166 return err;
2167}
2168
2169static int snd_pcm_release(struct inode *inode, struct file *file)
2170{
Takashi Iwai877211f2005-11-17 13:59:38 +01002171 struct snd_pcm *pcm;
2172 struct snd_pcm_substream *substream;
2173 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 pcm_file = file->private_data;
2176 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002177 if (snd_BUG_ON(!substream))
2178 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 pcm = substream->pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 fasync_helper(-1, file, 0, &substream->runtime->fasync);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002181 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002182 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002183 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002184 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 wake_up(&pcm->open_wait);
2186 module_put(pcm->card->module);
2187 snd_card_file_remove(pcm->card, file);
2188 return 0;
2189}
2190
Takashi Iwai877211f2005-11-17 13:59:38 +01002191static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2192 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Takashi Iwai877211f2005-11-17 13:59:38 +01002194 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 snd_pcm_sframes_t appl_ptr;
2196 snd_pcm_sframes_t ret;
2197 snd_pcm_sframes_t hw_avail;
2198
2199 if (frames == 0)
2200 return 0;
2201
2202 snd_pcm_stream_lock_irq(substream);
2203 switch (runtime->status->state) {
2204 case SNDRV_PCM_STATE_PREPARED:
2205 break;
2206 case SNDRV_PCM_STATE_DRAINING:
2207 case SNDRV_PCM_STATE_RUNNING:
2208 if (snd_pcm_update_hw_ptr(substream) >= 0)
2209 break;
2210 /* Fall through */
2211 case SNDRV_PCM_STATE_XRUN:
2212 ret = -EPIPE;
2213 goto __end;
2214 default:
2215 ret = -EBADFD;
2216 goto __end;
2217 }
2218
2219 hw_avail = snd_pcm_playback_hw_avail(runtime);
2220 if (hw_avail <= 0) {
2221 ret = 0;
2222 goto __end;
2223 }
2224 if (frames > (snd_pcm_uframes_t)hw_avail)
2225 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 appl_ptr = runtime->control->appl_ptr - frames;
2227 if (appl_ptr < 0)
2228 appl_ptr += runtime->boundary;
2229 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 ret = frames;
2231 __end:
2232 snd_pcm_stream_unlock_irq(substream);
2233 return ret;
2234}
2235
Takashi Iwai877211f2005-11-17 13:59:38 +01002236static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2237 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Takashi Iwai877211f2005-11-17 13:59:38 +01002239 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 snd_pcm_sframes_t appl_ptr;
2241 snd_pcm_sframes_t ret;
2242 snd_pcm_sframes_t hw_avail;
2243
2244 if (frames == 0)
2245 return 0;
2246
2247 snd_pcm_stream_lock_irq(substream);
2248 switch (runtime->status->state) {
2249 case SNDRV_PCM_STATE_PREPARED:
2250 case SNDRV_PCM_STATE_DRAINING:
2251 break;
2252 case SNDRV_PCM_STATE_RUNNING:
2253 if (snd_pcm_update_hw_ptr(substream) >= 0)
2254 break;
2255 /* Fall through */
2256 case SNDRV_PCM_STATE_XRUN:
2257 ret = -EPIPE;
2258 goto __end;
2259 default:
2260 ret = -EBADFD;
2261 goto __end;
2262 }
2263
2264 hw_avail = snd_pcm_capture_hw_avail(runtime);
2265 if (hw_avail <= 0) {
2266 ret = 0;
2267 goto __end;
2268 }
2269 if (frames > (snd_pcm_uframes_t)hw_avail)
2270 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 appl_ptr = runtime->control->appl_ptr - frames;
2272 if (appl_ptr < 0)
2273 appl_ptr += runtime->boundary;
2274 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 ret = frames;
2276 __end:
2277 snd_pcm_stream_unlock_irq(substream);
2278 return ret;
2279}
2280
Takashi Iwai877211f2005-11-17 13:59:38 +01002281static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2282 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Takashi Iwai877211f2005-11-17 13:59:38 +01002284 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 snd_pcm_sframes_t appl_ptr;
2286 snd_pcm_sframes_t ret;
2287 snd_pcm_sframes_t avail;
2288
2289 if (frames == 0)
2290 return 0;
2291
2292 snd_pcm_stream_lock_irq(substream);
2293 switch (runtime->status->state) {
2294 case SNDRV_PCM_STATE_PREPARED:
2295 case SNDRV_PCM_STATE_PAUSED:
2296 break;
2297 case SNDRV_PCM_STATE_DRAINING:
2298 case SNDRV_PCM_STATE_RUNNING:
2299 if (snd_pcm_update_hw_ptr(substream) >= 0)
2300 break;
2301 /* Fall through */
2302 case SNDRV_PCM_STATE_XRUN:
2303 ret = -EPIPE;
2304 goto __end;
2305 default:
2306 ret = -EBADFD;
2307 goto __end;
2308 }
2309
2310 avail = snd_pcm_playback_avail(runtime);
2311 if (avail <= 0) {
2312 ret = 0;
2313 goto __end;
2314 }
2315 if (frames > (snd_pcm_uframes_t)avail)
2316 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 appl_ptr = runtime->control->appl_ptr + frames;
2318 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2319 appl_ptr -= runtime->boundary;
2320 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 ret = frames;
2322 __end:
2323 snd_pcm_stream_unlock_irq(substream);
2324 return ret;
2325}
2326
Takashi Iwai877211f2005-11-17 13:59:38 +01002327static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2328 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Takashi Iwai877211f2005-11-17 13:59:38 +01002330 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 snd_pcm_sframes_t appl_ptr;
2332 snd_pcm_sframes_t ret;
2333 snd_pcm_sframes_t avail;
2334
2335 if (frames == 0)
2336 return 0;
2337
2338 snd_pcm_stream_lock_irq(substream);
2339 switch (runtime->status->state) {
2340 case SNDRV_PCM_STATE_PREPARED:
2341 case SNDRV_PCM_STATE_DRAINING:
2342 case SNDRV_PCM_STATE_PAUSED:
2343 break;
2344 case SNDRV_PCM_STATE_RUNNING:
2345 if (snd_pcm_update_hw_ptr(substream) >= 0)
2346 break;
2347 /* Fall through */
2348 case SNDRV_PCM_STATE_XRUN:
2349 ret = -EPIPE;
2350 goto __end;
2351 default:
2352 ret = -EBADFD;
2353 goto __end;
2354 }
2355
2356 avail = snd_pcm_capture_avail(runtime);
2357 if (avail <= 0) {
2358 ret = 0;
2359 goto __end;
2360 }
2361 if (frames > (snd_pcm_uframes_t)avail)
2362 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 appl_ptr = runtime->control->appl_ptr + frames;
2364 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2365 appl_ptr -= runtime->boundary;
2366 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 ret = frames;
2368 __end:
2369 snd_pcm_stream_unlock_irq(substream);
2370 return ret;
2371}
2372
Takashi Iwai877211f2005-11-17 13:59:38 +01002373static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Takashi Iwai877211f2005-11-17 13:59:38 +01002375 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 int err;
2377
2378 snd_pcm_stream_lock_irq(substream);
2379 switch (runtime->status->state) {
2380 case SNDRV_PCM_STATE_DRAINING:
2381 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2382 goto __badfd;
2383 case SNDRV_PCM_STATE_RUNNING:
2384 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2385 break;
2386 /* Fall through */
2387 case SNDRV_PCM_STATE_PREPARED:
2388 case SNDRV_PCM_STATE_SUSPENDED:
2389 err = 0;
2390 break;
2391 case SNDRV_PCM_STATE_XRUN:
2392 err = -EPIPE;
2393 break;
2394 default:
2395 __badfd:
2396 err = -EBADFD;
2397 break;
2398 }
2399 snd_pcm_stream_unlock_irq(substream);
2400 return err;
2401}
2402
Takashi Iwai877211f2005-11-17 13:59:38 +01002403static int snd_pcm_delay(struct snd_pcm_substream *substream,
2404 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
Takashi Iwai877211f2005-11-17 13:59:38 +01002406 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 int err;
2408 snd_pcm_sframes_t n = 0;
2409
2410 snd_pcm_stream_lock_irq(substream);
2411 switch (runtime->status->state) {
2412 case SNDRV_PCM_STATE_DRAINING:
2413 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2414 goto __badfd;
2415 case SNDRV_PCM_STATE_RUNNING:
2416 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2417 break;
2418 /* Fall through */
2419 case SNDRV_PCM_STATE_PREPARED:
2420 case SNDRV_PCM_STATE_SUSPENDED:
2421 err = 0;
2422 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2423 n = snd_pcm_playback_hw_avail(runtime);
2424 else
2425 n = snd_pcm_capture_avail(runtime);
2426 break;
2427 case SNDRV_PCM_STATE_XRUN:
2428 err = -EPIPE;
2429 break;
2430 default:
2431 __badfd:
2432 err = -EBADFD;
2433 break;
2434 }
2435 snd_pcm_stream_unlock_irq(substream);
2436 if (!err)
2437 if (put_user(n, res))
2438 err = -EFAULT;
2439 return err;
2440}
2441
Takashi Iwai877211f2005-11-17 13:59:38 +01002442static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2443 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Takashi Iwai877211f2005-11-17 13:59:38 +01002445 struct snd_pcm_runtime *runtime = substream->runtime;
2446 struct snd_pcm_sync_ptr sync_ptr;
2447 volatile struct snd_pcm_mmap_status *status;
2448 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 int err;
2450
2451 memset(&sync_ptr, 0, sizeof(sync_ptr));
2452 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2453 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002454 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 -07002455 return -EFAULT;
2456 status = runtime->status;
2457 control = runtime->control;
2458 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2459 err = snd_pcm_hwsync(substream);
2460 if (err < 0)
2461 return err;
2462 }
2463 snd_pcm_stream_lock_irq(substream);
2464 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2465 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2466 else
2467 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2468 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2469 control->avail_min = sync_ptr.c.control.avail_min;
2470 else
2471 sync_ptr.c.control.avail_min = control->avail_min;
2472 sync_ptr.s.status.state = status->state;
2473 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2474 sync_ptr.s.status.tstamp = status->tstamp;
2475 sync_ptr.s.status.suspended_state = status->suspended_state;
2476 snd_pcm_stream_unlock_irq(substream);
2477 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2478 return -EFAULT;
2479 return 0;
2480}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002481
2482static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2483{
2484 struct snd_pcm_runtime *runtime = substream->runtime;
2485 int arg;
2486
2487 if (get_user(arg, _arg))
2488 return -EFAULT;
2489 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2490 return -EINVAL;
2491 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2492 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2493 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2494 return 0;
2495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Takashi Iwai0df63e42006-04-28 15:13:41 +02002497static int snd_pcm_common_ioctl1(struct file *file,
2498 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 unsigned int cmd, void __user *arg)
2500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 switch (cmd) {
2502 case SNDRV_PCM_IOCTL_PVERSION:
2503 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2504 case SNDRV_PCM_IOCTL_INFO:
2505 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002506 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2507 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002508 case SNDRV_PCM_IOCTL_TTSTAMP:
2509 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 case SNDRV_PCM_IOCTL_HW_REFINE:
2511 return snd_pcm_hw_refine_user(substream, arg);
2512 case SNDRV_PCM_IOCTL_HW_PARAMS:
2513 return snd_pcm_hw_params_user(substream, arg);
2514 case SNDRV_PCM_IOCTL_HW_FREE:
2515 return snd_pcm_hw_free(substream);
2516 case SNDRV_PCM_IOCTL_SW_PARAMS:
2517 return snd_pcm_sw_params_user(substream, arg);
2518 case SNDRV_PCM_IOCTL_STATUS:
2519 return snd_pcm_status_user(substream, arg);
2520 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2521 return snd_pcm_channel_info_user(substream, arg);
2522 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002523 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 case SNDRV_PCM_IOCTL_RESET:
2525 return snd_pcm_reset(substream);
2526 case SNDRV_PCM_IOCTL_START:
2527 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2528 case SNDRV_PCM_IOCTL_LINK:
2529 return snd_pcm_link(substream, (int)(unsigned long) arg);
2530 case SNDRV_PCM_IOCTL_UNLINK:
2531 return snd_pcm_unlink(substream);
2532 case SNDRV_PCM_IOCTL_RESUME:
2533 return snd_pcm_resume(substream);
2534 case SNDRV_PCM_IOCTL_XRUN:
2535 return snd_pcm_xrun(substream);
2536 case SNDRV_PCM_IOCTL_HWSYNC:
2537 return snd_pcm_hwsync(substream);
2538 case SNDRV_PCM_IOCTL_DELAY:
2539 return snd_pcm_delay(substream, arg);
2540 case SNDRV_PCM_IOCTL_SYNC_PTR:
2541 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002542#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2544 return snd_pcm_hw_refine_old_user(substream, arg);
2545 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2546 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002547#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 case SNDRV_PCM_IOCTL_DRAIN:
2549 return snd_pcm_drain(substream);
2550 case SNDRV_PCM_IOCTL_DROP:
2551 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002552 case SNDRV_PCM_IOCTL_PAUSE:
2553 {
2554 int res;
2555 snd_pcm_stream_lock_irq(substream);
2556 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2557 snd_pcm_stream_unlock_irq(substream);
2558 return res;
2559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561 snd_printd("unknown ioctl = 0x%x\n", cmd);
2562 return -ENOTTY;
2563}
2564
Takashi Iwai0df63e42006-04-28 15:13:41 +02002565static int snd_pcm_playback_ioctl1(struct file *file,
2566 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 unsigned int cmd, void __user *arg)
2568{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002569 if (snd_BUG_ON(!substream))
2570 return -ENXIO;
2571 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 switch (cmd) {
2574 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2575 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002576 struct snd_xferi xferi;
2577 struct snd_xferi __user *_xferi = arg;
2578 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 snd_pcm_sframes_t result;
2580 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2581 return -EBADFD;
2582 if (put_user(0, &_xferi->result))
2583 return -EFAULT;
2584 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2585 return -EFAULT;
2586 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2587 __put_user(result, &_xferi->result);
2588 return result < 0 ? result : 0;
2589 }
2590 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2591 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002592 struct snd_xfern xfern;
2593 struct snd_xfern __user *_xfern = arg;
2594 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 void __user **bufs;
2596 snd_pcm_sframes_t result;
2597 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2598 return -EBADFD;
2599 if (runtime->channels > 128)
2600 return -EINVAL;
2601 if (put_user(0, &_xfern->result))
2602 return -EFAULT;
2603 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2604 return -EFAULT;
2605 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2606 if (bufs == NULL)
2607 return -ENOMEM;
2608 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2609 kfree(bufs);
2610 return -EFAULT;
2611 }
2612 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2613 kfree(bufs);
2614 __put_user(result, &_xfern->result);
2615 return result < 0 ? result : 0;
2616 }
2617 case SNDRV_PCM_IOCTL_REWIND:
2618 {
2619 snd_pcm_uframes_t frames;
2620 snd_pcm_uframes_t __user *_frames = arg;
2621 snd_pcm_sframes_t result;
2622 if (get_user(frames, _frames))
2623 return -EFAULT;
2624 if (put_user(0, _frames))
2625 return -EFAULT;
2626 result = snd_pcm_playback_rewind(substream, frames);
2627 __put_user(result, _frames);
2628 return result < 0 ? result : 0;
2629 }
2630 case SNDRV_PCM_IOCTL_FORWARD:
2631 {
2632 snd_pcm_uframes_t frames;
2633 snd_pcm_uframes_t __user *_frames = arg;
2634 snd_pcm_sframes_t result;
2635 if (get_user(frames, _frames))
2636 return -EFAULT;
2637 if (put_user(0, _frames))
2638 return -EFAULT;
2639 result = snd_pcm_playback_forward(substream, frames);
2640 __put_user(result, _frames);
2641 return result < 0 ? result : 0;
2642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002644 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
2646
Takashi Iwai0df63e42006-04-28 15:13:41 +02002647static int snd_pcm_capture_ioctl1(struct file *file,
2648 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 unsigned int cmd, void __user *arg)
2650{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002651 if (snd_BUG_ON(!substream))
2652 return -ENXIO;
2653 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2654 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 switch (cmd) {
2656 case SNDRV_PCM_IOCTL_READI_FRAMES:
2657 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002658 struct snd_xferi xferi;
2659 struct snd_xferi __user *_xferi = arg;
2660 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 snd_pcm_sframes_t result;
2662 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2663 return -EBADFD;
2664 if (put_user(0, &_xferi->result))
2665 return -EFAULT;
2666 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2667 return -EFAULT;
2668 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2669 __put_user(result, &_xferi->result);
2670 return result < 0 ? result : 0;
2671 }
2672 case SNDRV_PCM_IOCTL_READN_FRAMES:
2673 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002674 struct snd_xfern xfern;
2675 struct snd_xfern __user *_xfern = arg;
2676 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 void *bufs;
2678 snd_pcm_sframes_t result;
2679 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2680 return -EBADFD;
2681 if (runtime->channels > 128)
2682 return -EINVAL;
2683 if (put_user(0, &_xfern->result))
2684 return -EFAULT;
2685 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2686 return -EFAULT;
2687 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2688 if (bufs == NULL)
2689 return -ENOMEM;
2690 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2691 kfree(bufs);
2692 return -EFAULT;
2693 }
2694 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2695 kfree(bufs);
2696 __put_user(result, &_xfern->result);
2697 return result < 0 ? result : 0;
2698 }
2699 case SNDRV_PCM_IOCTL_REWIND:
2700 {
2701 snd_pcm_uframes_t frames;
2702 snd_pcm_uframes_t __user *_frames = arg;
2703 snd_pcm_sframes_t result;
2704 if (get_user(frames, _frames))
2705 return -EFAULT;
2706 if (put_user(0, _frames))
2707 return -EFAULT;
2708 result = snd_pcm_capture_rewind(substream, frames);
2709 __put_user(result, _frames);
2710 return result < 0 ? result : 0;
2711 }
2712 case SNDRV_PCM_IOCTL_FORWARD:
2713 {
2714 snd_pcm_uframes_t frames;
2715 snd_pcm_uframes_t __user *_frames = arg;
2716 snd_pcm_sframes_t result;
2717 if (get_user(frames, _frames))
2718 return -EFAULT;
2719 if (put_user(0, _frames))
2720 return -EFAULT;
2721 result = snd_pcm_capture_forward(substream, frames);
2722 __put_user(result, _frames);
2723 return result < 0 ? result : 0;
2724 }
2725 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002726 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
Takashi Iwai877211f2005-11-17 13:59:38 +01002729static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2730 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
Takashi Iwai877211f2005-11-17 13:59:38 +01002732 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 pcm_file = file->private_data;
2735
2736 if (((cmd >> 8) & 0xff) != 'A')
2737 return -ENOTTY;
2738
Takashi Iwai0df63e42006-04-28 15:13:41 +02002739 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2740 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
Takashi Iwai877211f2005-11-17 13:59:38 +01002743static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2744 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Takashi Iwai877211f2005-11-17 13:59:38 +01002746 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 pcm_file = file->private_data;
2749
2750 if (((cmd >> 8) & 0xff) != 'A')
2751 return -ENOTTY;
2752
Takashi Iwai0df63e42006-04-28 15:13:41 +02002753 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2754 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
Takashi Iwai877211f2005-11-17 13:59:38 +01002757int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 unsigned int cmd, void *arg)
2759{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002760 mm_segment_t fs;
2761 int result;
2762
2763 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 switch (substream->stream) {
2765 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002766 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2767 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002768 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002770 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2771 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002774 result = -EINVAL;
2775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002777 snd_leave_user(fs);
2778 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002781EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2782
Takashi Iwai877211f2005-11-17 13:59:38 +01002783static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2784 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Takashi Iwai877211f2005-11-17 13:59:38 +01002786 struct snd_pcm_file *pcm_file;
2787 struct snd_pcm_substream *substream;
2788 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 snd_pcm_sframes_t result;
2790
2791 pcm_file = file->private_data;
2792 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002793 if (PCM_RUNTIME_CHECK(substream))
2794 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 runtime = substream->runtime;
2796 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2797 return -EBADFD;
2798 if (!frame_aligned(runtime, count))
2799 return -EINVAL;
2800 count = bytes_to_frames(runtime, count);
2801 result = snd_pcm_lib_read(substream, buf, count);
2802 if (result > 0)
2803 result = frames_to_bytes(runtime, result);
2804 return result;
2805}
2806
Takashi Iwai877211f2005-11-17 13:59:38 +01002807static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2808 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Takashi Iwai877211f2005-11-17 13:59:38 +01002810 struct snd_pcm_file *pcm_file;
2811 struct snd_pcm_substream *substream;
2812 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 snd_pcm_sframes_t result;
2814
2815 pcm_file = file->private_data;
2816 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002817 if (PCM_RUNTIME_CHECK(substream))
2818 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002820 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2821 return -EBADFD;
2822 if (!frame_aligned(runtime, count))
2823 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 count = bytes_to_frames(runtime, count);
2825 result = snd_pcm_lib_write(substream, buf, count);
2826 if (result > 0)
2827 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return result;
2829}
2830
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002831static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2832 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834{
Takashi Iwai877211f2005-11-17 13:59:38 +01002835 struct snd_pcm_file *pcm_file;
2836 struct snd_pcm_substream *substream;
2837 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 snd_pcm_sframes_t result;
2839 unsigned long i;
2840 void __user **bufs;
2841 snd_pcm_uframes_t frames;
2842
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002843 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002845 if (PCM_RUNTIME_CHECK(substream))
2846 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 runtime = substream->runtime;
2848 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2849 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002850 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002852 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002854 frames = bytes_to_samples(runtime, iov->iov_len);
2855 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (bufs == NULL)
2857 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002858 for (i = 0; i < nr_segs; ++i)
2859 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 result = snd_pcm_lib_readv(substream, bufs, frames);
2861 if (result > 0)
2862 result = frames_to_bytes(runtime, result);
2863 kfree(bufs);
2864 return result;
2865}
2866
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002867static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2868 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Takashi Iwai877211f2005-11-17 13:59:38 +01002870 struct snd_pcm_file *pcm_file;
2871 struct snd_pcm_substream *substream;
2872 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 snd_pcm_sframes_t result;
2874 unsigned long i;
2875 void __user **bufs;
2876 snd_pcm_uframes_t frames;
2877
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002878 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002880 if (PCM_RUNTIME_CHECK(substream))
2881 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002883 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2884 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002885 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002886 !frame_aligned(runtime, iov->iov_len))
2887 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002888 frames = bytes_to_samples(runtime, iov->iov_len);
2889 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (bufs == NULL)
2891 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002892 for (i = 0; i < nr_segs; ++i)
2893 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 result = snd_pcm_lib_writev(substream, bufs, frames);
2895 if (result > 0)
2896 result = frames_to_bytes(runtime, result);
2897 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return result;
2899}
2900
2901static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2902{
Takashi Iwai877211f2005-11-17 13:59:38 +01002903 struct snd_pcm_file *pcm_file;
2904 struct snd_pcm_substream *substream;
2905 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 unsigned int mask;
2907 snd_pcm_uframes_t avail;
2908
2909 pcm_file = file->private_data;
2910
2911 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002912 if (PCM_RUNTIME_CHECK(substream))
2913 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 runtime = substream->runtime;
2915
2916 poll_wait(file, &runtime->sleep, wait);
2917
2918 snd_pcm_stream_lock_irq(substream);
2919 avail = snd_pcm_playback_avail(runtime);
2920 switch (runtime->status->state) {
2921 case SNDRV_PCM_STATE_RUNNING:
2922 case SNDRV_PCM_STATE_PREPARED:
2923 case SNDRV_PCM_STATE_PAUSED:
2924 if (avail >= runtime->control->avail_min) {
2925 mask = POLLOUT | POLLWRNORM;
2926 break;
2927 }
2928 /* Fall through */
2929 case SNDRV_PCM_STATE_DRAINING:
2930 mask = 0;
2931 break;
2932 default:
2933 mask = POLLOUT | POLLWRNORM | POLLERR;
2934 break;
2935 }
2936 snd_pcm_stream_unlock_irq(substream);
2937 return mask;
2938}
2939
2940static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2941{
Takashi Iwai877211f2005-11-17 13:59:38 +01002942 struct snd_pcm_file *pcm_file;
2943 struct snd_pcm_substream *substream;
2944 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 unsigned int mask;
2946 snd_pcm_uframes_t avail;
2947
2948 pcm_file = file->private_data;
2949
2950 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002951 if (PCM_RUNTIME_CHECK(substream))
2952 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 runtime = substream->runtime;
2954
2955 poll_wait(file, &runtime->sleep, wait);
2956
2957 snd_pcm_stream_lock_irq(substream);
2958 avail = snd_pcm_capture_avail(runtime);
2959 switch (runtime->status->state) {
2960 case SNDRV_PCM_STATE_RUNNING:
2961 case SNDRV_PCM_STATE_PREPARED:
2962 case SNDRV_PCM_STATE_PAUSED:
2963 if (avail >= runtime->control->avail_min) {
2964 mask = POLLIN | POLLRDNORM;
2965 break;
2966 }
2967 mask = 0;
2968 break;
2969 case SNDRV_PCM_STATE_DRAINING:
2970 if (avail > 0) {
2971 mask = POLLIN | POLLRDNORM;
2972 break;
2973 }
2974 /* Fall through */
2975 default:
2976 mask = POLLIN | POLLRDNORM | POLLERR;
2977 break;
2978 }
2979 snd_pcm_stream_unlock_irq(substream);
2980 return mask;
2981}
2982
2983/*
2984 * mmap support
2985 */
2986
2987/*
2988 * Only on coherent architectures, we can mmap the status and the control records
2989 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2990 */
2991#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2992/*
2993 * mmap status record
2994 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002995static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2996 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Takashi Iwai877211f2005-11-17 13:59:38 +01002998 struct snd_pcm_substream *substream = area->vm_private_data;
2999 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
3001 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003002 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003004 vmf->page = virt_to_page(runtime->status);
3005 get_page(vmf->page);
3006 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
3009static struct vm_operations_struct snd_pcm_vm_ops_status =
3010{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003011 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012};
3013
Takashi Iwai877211f2005-11-17 13:59:38 +01003014static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 struct vm_area_struct *area)
3016{
Takashi Iwai877211f2005-11-17 13:59:38 +01003017 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 long size;
3019 if (!(area->vm_flags & VM_READ))
3020 return -EINVAL;
3021 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003023 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return -EINVAL;
3025 area->vm_ops = &snd_pcm_vm_ops_status;
3026 area->vm_private_data = substream;
3027 area->vm_flags |= VM_RESERVED;
3028 return 0;
3029}
3030
3031/*
3032 * mmap control record
3033 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003034static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3035 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
Takashi Iwai877211f2005-11-17 13:59:38 +01003037 struct snd_pcm_substream *substream = area->vm_private_data;
3038 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003041 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003043 vmf->page = virt_to_page(runtime->control);
3044 get_page(vmf->page);
3045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
3048static struct vm_operations_struct snd_pcm_vm_ops_control =
3049{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003050 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051};
3052
Takashi Iwai877211f2005-11-17 13:59:38 +01003053static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct vm_area_struct *area)
3055{
Takashi Iwai877211f2005-11-17 13:59:38 +01003056 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 long size;
3058 if (!(area->vm_flags & VM_READ))
3059 return -EINVAL;
3060 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003062 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return -EINVAL;
3064 area->vm_ops = &snd_pcm_vm_ops_control;
3065 area->vm_private_data = substream;
3066 area->vm_flags |= VM_RESERVED;
3067 return 0;
3068}
3069#else /* ! coherent mmap */
3070/*
3071 * don't support mmap for status and control records.
3072 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003073static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 struct vm_area_struct *area)
3075{
3076 return -ENXIO;
3077}
Takashi Iwai877211f2005-11-17 13:59:38 +01003078static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 struct vm_area_struct *area)
3080{
3081 return -ENXIO;
3082}
3083#endif /* coherent mmap */
3084
3085/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003086 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003088static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3089 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Takashi Iwai877211f2005-11-17 13:59:38 +01003091 struct snd_pcm_substream *substream = area->vm_private_data;
3092 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 unsigned long offset;
3094 struct page * page;
3095 void *vaddr;
3096 size_t dma_bytes;
3097
3098 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003099 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003101 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3103 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003104 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if (substream->ops->page) {
3106 page = substream->ops->page(substream, offset);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003107 if (!page)
3108 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 } else {
3110 vaddr = runtime->dma_area + offset;
3111 page = virt_to_page(vaddr);
3112 }
Nick Pigginb5810032005-10-29 18:16:12 -07003113 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003114 vmf->page = page;
3115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
3118static struct vm_operations_struct snd_pcm_vm_ops_data =
3119{
3120 .open = snd_pcm_mmap_data_open,
3121 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003122 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123};
3124
3125/*
3126 * mmap the DMA buffer on RAM
3127 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003128static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3129 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
3131 area->vm_ops = &snd_pcm_vm_ops_data;
3132 area->vm_private_data = substream;
3133 area->vm_flags |= VM_RESERVED;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003134 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return 0;
3136}
3137
3138/*
3139 * mmap the DMA buffer on I/O memory area
3140 */
3141#if SNDRV_PCM_INFO_MMAP_IOMEM
3142static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3143{
3144 .open = snd_pcm_mmap_data_open,
3145 .close = snd_pcm_mmap_data_close,
3146};
3147
Takashi Iwai877211f2005-11-17 13:59:38 +01003148int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3149 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
3151 long size;
3152 unsigned long offset;
3153
3154#ifdef pgprot_noncached
3155 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3156#endif
3157 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3158 area->vm_private_data = substream;
3159 area->vm_flags |= VM_IO;
3160 size = area->vm_end - area->vm_start;
3161 offset = area->vm_pgoff << PAGE_SHIFT;
3162 if (io_remap_pfn_range(area, area->vm_start,
3163 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3164 size, area->vm_page_prot))
3165 return -EAGAIN;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003166 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return 0;
3168}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003169
3170EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171#endif /* SNDRV_PCM_INFO_MMAP */
3172
3173/*
3174 * mmap DMA buffer
3175 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003176int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 struct vm_area_struct *area)
3178{
Takashi Iwai877211f2005-11-17 13:59:38 +01003179 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 long size;
3181 unsigned long offset;
3182 size_t dma_bytes;
3183
3184 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3185 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3186 return -EINVAL;
3187 } else {
3188 if (!(area->vm_flags & VM_READ))
3189 return -EINVAL;
3190 }
3191 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3193 return -EBADFD;
3194 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3195 return -ENXIO;
3196 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3197 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3198 return -EINVAL;
3199 size = area->vm_end - area->vm_start;
3200 offset = area->vm_pgoff << PAGE_SHIFT;
3201 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3202 if ((size_t)size > dma_bytes)
3203 return -EINVAL;
3204 if (offset > dma_bytes - size)
3205 return -EINVAL;
3206
3207 if (substream->ops->mmap)
3208 return substream->ops->mmap(substream, area);
3209 else
3210 return snd_pcm_default_mmap(substream, area);
3211}
3212
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003213EXPORT_SYMBOL(snd_pcm_mmap_data);
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3216{
Takashi Iwai877211f2005-11-17 13:59:38 +01003217 struct snd_pcm_file * pcm_file;
3218 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 unsigned long offset;
3220
3221 pcm_file = file->private_data;
3222 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003223 if (PCM_RUNTIME_CHECK(substream))
3224 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 offset = area->vm_pgoff << PAGE_SHIFT;
3227 switch (offset) {
3228 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003229 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return -ENXIO;
3231 return snd_pcm_mmap_status(substream, file, area);
3232 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003233 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return -ENXIO;
3235 return snd_pcm_mmap_control(substream, file, area);
3236 default:
3237 return snd_pcm_mmap_data(substream, file, area);
3238 }
3239 return 0;
3240}
3241
3242static int snd_pcm_fasync(int fd, struct file * file, int on)
3243{
Takashi Iwai877211f2005-11-17 13:59:38 +01003244 struct snd_pcm_file * pcm_file;
3245 struct snd_pcm_substream *substream;
3246 struct snd_pcm_runtime *runtime;
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003247 int err = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003249 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 pcm_file = file->private_data;
3251 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003252 if (PCM_RUNTIME_CHECK(substream))
3253 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 err = fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds685d87f2008-08-06 19:24:47 -07003256out:
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003257 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 if (err < 0)
3259 return err;
3260 return 0;
3261}
3262
3263/*
3264 * ioctl32 compat
3265 */
3266#ifdef CONFIG_COMPAT
3267#include "pcm_compat.c"
3268#else
3269#define snd_pcm_ioctl_compat NULL
3270#endif
3271
3272/*
3273 * To be removed helpers to keep binary compatibility
3274 */
3275
Takashi Iwai59d48582005-12-01 10:51:58 +01003276#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3278#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3279
Takashi Iwai877211f2005-11-17 13:59:38 +01003280static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3281 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
3283 unsigned int i;
3284
3285 memset(params, 0, sizeof(*params));
3286 params->flags = oparams->flags;
3287 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3288 params->masks[i].bits[0] = oparams->masks[i];
3289 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3290 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3291 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3292 params->info = oparams->info;
3293 params->msbits = oparams->msbits;
3294 params->rate_num = oparams->rate_num;
3295 params->rate_den = oparams->rate_den;
3296 params->fifo_size = oparams->fifo_size;
3297}
3298
Takashi Iwai877211f2005-11-17 13:59:38 +01003299static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3300 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301{
3302 unsigned int i;
3303
3304 memset(oparams, 0, sizeof(*oparams));
3305 oparams->flags = params->flags;
3306 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3307 oparams->masks[i] = params->masks[i].bits[0];
3308 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3309 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3310 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3311 oparams->info = params->info;
3312 oparams->msbits = params->msbits;
3313 oparams->rate_num = params->rate_num;
3314 oparams->rate_den = params->rate_den;
3315 oparams->fifo_size = params->fifo_size;
3316}
3317
Takashi Iwai877211f2005-11-17 13:59:38 +01003318static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3319 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
Takashi Iwai877211f2005-11-17 13:59:38 +01003321 struct snd_pcm_hw_params *params;
3322 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 int err;
3324
3325 params = kmalloc(sizeof(*params), GFP_KERNEL);
3326 if (!params) {
3327 err = -ENOMEM;
3328 goto out;
3329 }
3330 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3331 if (!oparams) {
3332 err = -ENOMEM;
3333 goto out;
3334 }
3335
3336 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3337 err = -EFAULT;
3338 goto out;
3339 }
3340 snd_pcm_hw_convert_from_old_params(params, oparams);
3341 err = snd_pcm_hw_refine(substream, params);
3342 snd_pcm_hw_convert_to_old_params(oparams, params);
3343 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3344 if (!err)
3345 err = -EFAULT;
3346 }
3347out:
3348 kfree(params);
3349 kfree(oparams);
3350 return err;
3351}
3352
Takashi Iwai877211f2005-11-17 13:59:38 +01003353static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3354 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Takashi Iwai877211f2005-11-17 13:59:38 +01003356 struct snd_pcm_hw_params *params;
3357 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 int err;
3359
3360 params = kmalloc(sizeof(*params), GFP_KERNEL);
3361 if (!params) {
3362 err = -ENOMEM;
3363 goto out;
3364 }
3365 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3366 if (!oparams) {
3367 err = -ENOMEM;
3368 goto out;
3369 }
3370 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3371 err = -EFAULT;
3372 goto out;
3373 }
3374 snd_pcm_hw_convert_from_old_params(params, oparams);
3375 err = snd_pcm_hw_params(substream, params);
3376 snd_pcm_hw_convert_to_old_params(oparams, params);
3377 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3378 if (!err)
3379 err = -EFAULT;
3380 }
3381out:
3382 kfree(params);
3383 kfree(oparams);
3384 return err;
3385}
Takashi Iwai59d48582005-12-01 10:51:58 +01003386#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
3388/*
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 },
3405 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003406 .owner = THIS_MODULE,
3407 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003408 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003409 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003410 .release = snd_pcm_release,
3411 .poll = snd_pcm_capture_poll,
3412 .unlocked_ioctl = snd_pcm_capture_ioctl,
3413 .compat_ioctl = snd_pcm_ioctl_compat,
3414 .mmap = snd_pcm_mmap,
3415 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
3417};