blob: 61f5d425b6305660e8cd3bf514664c1f9b225c58 [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>
25#include <linux/time.h>
Mark Grossf011e2e2008-02-04 22:30:09 -080026#include <linux/pm_qos_params.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/uio.h>
28#include <sound/core.h>
29#include <sound/control.h>
30#include <sound/info.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/timer.h>
34#include <sound/minors.h>
35#include <asm/io.h>
36
37/*
38 * Compatibility
39 */
40
Takashi Iwai877211f2005-11-17 13:59:38 +010041struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 unsigned int flags;
43 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
44 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010045 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
47 unsigned int rmask;
48 unsigned int cmask;
49 unsigned int info;
50 unsigned int msbits;
51 unsigned int rate_num;
52 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010053 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 unsigned char reserved[64];
55};
56
Takashi Iwai59d48582005-12-01 10:51:58 +010057#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010058#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
59#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Takashi Iwai877211f2005-11-17 13:59:38 +010061static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
62 struct snd_pcm_hw_params_old __user * _oparams);
63static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010065#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010066static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 *
70 */
71
72DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020073EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020075static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static inline mm_segment_t snd_enter_user(void)
78{
79 mm_segment_t fs = get_fs();
80 set_fs(get_ds());
81 return fs;
82}
83
84static inline void snd_leave_user(mm_segment_t fs)
85{
86 set_fs(fs);
87}
88
89
90
Takashi Iwai877211f2005-11-17 13:59:38 +010091int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Takashi Iwai877211f2005-11-17 13:59:38 +010093 struct snd_pcm_runtime *runtime;
94 struct snd_pcm *pcm = substream->pcm;
95 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 snd_assert(substream != NULL, return -ENXIO);
98 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
372 snd_assert(substream != NULL, return -ENXIO);
373 runtime = substream->runtime;
374 snd_assert(runtime != NULL, return -ENXIO);
375 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
492 snd_assert(substream != NULL, return -ENXIO);
493 runtime = substream->runtime;
494 snd_assert(runtime != NULL, return -ENXIO);
495 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
520 snd_assert(substream != NULL, return -ENXIO);
521 runtime = substream->runtime;
522 snd_assert(runtime != NULL, return -ENXIO);
523 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;
624 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int res;
626
627 snd_assert(substream != NULL, return -ENXIO);
628 runtime = substream->runtime;
629 memset(&status, 0, sizeof(status));
630 res = snd_pcm_status(substream, &status);
631 if (res < 0)
632 return res;
633 if (copy_to_user(_status, &status, sizeof(status)))
634 return -EFAULT;
635 return 0;
636}
637
Takashi Iwai877211f2005-11-17 13:59:38 +0100638static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
639 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Takashi Iwai877211f2005-11-17 13:59:38 +0100641 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 unsigned int channel;
643
644 snd_assert(substream != NULL, return -ENXIO);
645 channel = info->channel;
646 runtime = substream->runtime;
647 snd_pcm_stream_lock_irq(substream);
648 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
649 snd_pcm_stream_unlock_irq(substream);
650 return -EBADFD;
651 }
652 snd_pcm_stream_unlock_irq(substream);
653 if (channel >= runtime->channels)
654 return -EINVAL;
655 memset(info, 0, sizeof(*info));
656 info->channel = channel;
657 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
658}
659
Takashi Iwai877211f2005-11-17 13:59:38 +0100660static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
661 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Takashi Iwai877211f2005-11-17 13:59:38 +0100663 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int res;
665
666 if (copy_from_user(&info, _info, sizeof(info)))
667 return -EFAULT;
668 res = snd_pcm_channel_info(substream, &info);
669 if (res < 0)
670 return res;
671 if (copy_to_user(_info, &info, sizeof(info)))
672 return -EFAULT;
673 return 0;
674}
675
Takashi Iwai877211f2005-11-17 13:59:38 +0100676static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Takashi Iwai877211f2005-11-17 13:59:38 +0100678 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (runtime->trigger_master == NULL)
680 return;
681 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100682 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 } else {
684 snd_pcm_trigger_tstamp(runtime->trigger_master);
685 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
686 }
687 runtime->trigger_master = NULL;
688}
689
690struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100691 int (*pre_action)(struct snd_pcm_substream *substream, int state);
692 int (*do_action)(struct snd_pcm_substream *substream, int state);
693 void (*undo_action)(struct snd_pcm_substream *substream, int state);
694 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695};
696
697/*
698 * this functions is core for handling of linked stream
699 * Note: the stream state might be changed also on failure
700 * Note2: call with calling stream lock + link lock
701 */
702static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100703 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int state, int do_lock)
705{
Takashi Iwai877211f2005-11-17 13:59:38 +0100706 struct snd_pcm_substream *s = NULL;
707 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int res = 0;
709
Takashi Iwaief991b92007-02-22 12:52:53 +0100710 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200712 spin_lock_nested(&s->self_group.lock,
713 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 res = ops->pre_action(s, state);
715 if (res < 0)
716 goto _unlock;
717 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100718 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 res = ops->do_action(s, state);
720 if (res < 0) {
721 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100722 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (s1 == s) /* failed stream */
724 break;
725 ops->undo_action(s1, state);
726 }
727 }
728 s = NULL; /* unlock all */
729 goto _unlock;
730 }
731 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100732 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 ops->post_action(s, state);
734 }
735 _unlock:
736 if (do_lock) {
737 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100738 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (s1 != substream)
740 spin_unlock(&s1->self_group.lock);
741 if (s1 == s) /* end */
742 break;
743 }
744 }
745 return res;
746}
747
748/*
749 * Note: call with stream lock
750 */
751static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100752 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int state)
754{
755 int res;
756
757 res = ops->pre_action(substream, state);
758 if (res < 0)
759 return res;
760 res = ops->do_action(substream, state);
761 if (res == 0)
762 ops->post_action(substream, state);
763 else if (ops->undo_action)
764 ops->undo_action(substream, state);
765 return res;
766}
767
768/*
769 * Note: call with stream lock
770 */
771static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100772 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int state)
774{
775 int res;
776
777 if (snd_pcm_stream_linked(substream)) {
778 if (!spin_trylock(&substream->group->lock)) {
779 spin_unlock(&substream->self_group.lock);
780 spin_lock(&substream->group->lock);
781 spin_lock(&substream->self_group.lock);
782 }
783 res = snd_pcm_action_group(ops, substream, state, 1);
784 spin_unlock(&substream->group->lock);
785 } else {
786 res = snd_pcm_action_single(ops, substream, state);
787 }
788 return res;
789}
790
791/*
792 * Note: don't use any locks before
793 */
794static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100795 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int state)
797{
798 int res;
799
800 read_lock_irq(&snd_pcm_link_rwlock);
801 if (snd_pcm_stream_linked(substream)) {
802 spin_lock(&substream->group->lock);
803 spin_lock(&substream->self_group.lock);
804 res = snd_pcm_action_group(ops, substream, state, 1);
805 spin_unlock(&substream->self_group.lock);
806 spin_unlock(&substream->group->lock);
807 } else {
808 spin_lock(&substream->self_group.lock);
809 res = snd_pcm_action_single(ops, substream, state);
810 spin_unlock(&substream->self_group.lock);
811 }
812 read_unlock_irq(&snd_pcm_link_rwlock);
813 return res;
814}
815
816/*
817 */
818static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100819 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 int state)
821{
822 int res;
823
824 down_read(&snd_pcm_link_rwsem);
825 if (snd_pcm_stream_linked(substream))
826 res = snd_pcm_action_group(ops, substream, state, 0);
827 else
828 res = snd_pcm_action_single(ops, substream, state);
829 up_read(&snd_pcm_link_rwsem);
830 return res;
831}
832
833/*
834 * start callbacks
835 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100836static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Takashi Iwai877211f2005-11-17 13:59:38 +0100838 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
840 return -EBADFD;
841 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
842 !snd_pcm_playback_data(substream))
843 return -EPIPE;
844 runtime->trigger_master = substream;
845 return 0;
846}
847
Takashi Iwai877211f2005-11-17 13:59:38 +0100848static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 if (substream->runtime->trigger_master != substream)
851 return 0;
852 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
853}
854
Takashi Iwai877211f2005-11-17 13:59:38 +0100855static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 if (substream->runtime->trigger_master == substream)
858 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
859}
860
Takashi Iwai877211f2005-11-17 13:59:38 +0100861static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Takashi Iwai877211f2005-11-17 13:59:38 +0100863 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 snd_pcm_trigger_tstamp(substream);
865 runtime->status->state = state;
866 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
867 runtime->silence_size > 0)
868 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100870 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
871 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874static struct action_ops snd_pcm_action_start = {
875 .pre_action = snd_pcm_pre_start,
876 .do_action = snd_pcm_do_start,
877 .undo_action = snd_pcm_undo_start,
878 .post_action = snd_pcm_post_start
879};
880
881/**
882 * snd_pcm_start
Takashi Iwaidf8db932005-09-07 13:38:19 +0200883 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 *
885 * Start all linked streams.
886 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100887int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Takashi Iwai877211f2005-11-17 13:59:38 +0100889 return snd_pcm_action(&snd_pcm_action_start, substream,
890 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
894 * stop callbacks
895 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100896static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Takashi Iwai877211f2005-11-17 13:59:38 +0100898 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
900 return -EBADFD;
901 runtime->trigger_master = substream;
902 return 0;
903}
904
Takashi Iwai877211f2005-11-17 13:59:38 +0100905static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 if (substream->runtime->trigger_master == substream &&
908 snd_pcm_running(substream))
909 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
910 return 0; /* unconditonally stop all substreams */
911}
912
Takashi Iwai877211f2005-11-17 13:59:38 +0100913static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Takashi Iwai877211f2005-11-17 13:59:38 +0100915 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (runtime->status->state != state) {
917 snd_pcm_trigger_tstamp(substream);
918 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100919 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
920 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 wake_up(&runtime->sleep);
924}
925
926static struct action_ops snd_pcm_action_stop = {
927 .pre_action = snd_pcm_pre_stop,
928 .do_action = snd_pcm_do_stop,
929 .post_action = snd_pcm_post_stop
930};
931
932/**
933 * snd_pcm_stop
Takashi Iwaidf8db932005-09-07 13:38:19 +0200934 * @substream: the PCM substream instance
935 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *
937 * Try to stop all running streams in the substream group.
938 * The state of each stream is changed to the given value after that unconditionally.
939 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100940int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
943}
944
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200945EXPORT_SYMBOL(snd_pcm_stop);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/**
948 * snd_pcm_drain_done
Takashi Iwaidf8db932005-09-07 13:38:19 +0200949 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 *
951 * Stop the DMA only when the given stream is playback.
952 * The state is changed to SETUP.
953 * Unlike snd_pcm_stop(), this affects only the given stream.
954 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100955int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Takashi Iwai877211f2005-11-17 13:59:38 +0100957 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
958 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/*
962 * pause callbacks
963 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100964static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Takashi Iwai877211f2005-11-17 13:59:38 +0100966 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
968 return -ENOSYS;
969 if (push) {
970 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
971 return -EBADFD;
972 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
973 return -EBADFD;
974 runtime->trigger_master = substream;
975 return 0;
976}
977
Takashi Iwai877211f2005-11-17 13:59:38 +0100978static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 if (substream->runtime->trigger_master != substream)
981 return 0;
982 return substream->ops->trigger(substream,
983 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
984 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
985}
986
Takashi Iwai877211f2005-11-17 13:59:38 +0100987static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 if (substream->runtime->trigger_master == substream)
990 substream->ops->trigger(substream,
991 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
992 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
993}
994
Takashi Iwai877211f2005-11-17 13:59:38 +0100995static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Takashi Iwai877211f2005-11-17 13:59:38 +0100997 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 snd_pcm_trigger_tstamp(substream);
999 if (push) {
1000 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1001 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001002 snd_timer_notify(substream->timer,
1003 SNDRV_TIMER_EVENT_MPAUSE,
1004 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 wake_up(&runtime->sleep);
1006 } else {
1007 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001009 snd_timer_notify(substream->timer,
1010 SNDRV_TIMER_EVENT_MCONTINUE,
1011 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013}
1014
1015static struct action_ops snd_pcm_action_pause = {
1016 .pre_action = snd_pcm_pre_pause,
1017 .do_action = snd_pcm_do_pause,
1018 .undo_action = snd_pcm_undo_pause,
1019 .post_action = snd_pcm_post_pause
1020};
1021
1022/*
1023 * Push/release the pause for all linked streams.
1024 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001025static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1028}
1029
1030#ifdef CONFIG_PM
1031/* suspend */
1032
Takashi Iwai877211f2005-11-17 13:59:38 +01001033static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Takashi Iwai877211f2005-11-17 13:59:38 +01001035 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1037 return -EBUSY;
1038 runtime->trigger_master = substream;
1039 return 0;
1040}
1041
Takashi Iwai877211f2005-11-17 13:59:38 +01001042static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Takashi Iwai877211f2005-11-17 13:59:38 +01001044 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (runtime->trigger_master != substream)
1046 return 0;
1047 if (! snd_pcm_running(substream))
1048 return 0;
1049 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1050 return 0; /* suspend unconditionally */
1051}
1052
Takashi Iwai877211f2005-11-17 13:59:38 +01001053static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Takashi Iwai877211f2005-11-17 13:59:38 +01001055 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 snd_pcm_trigger_tstamp(substream);
1057 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001058 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1059 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 runtime->status->suspended_state = runtime->status->state;
1061 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 wake_up(&runtime->sleep);
1063}
1064
1065static struct action_ops snd_pcm_action_suspend = {
1066 .pre_action = snd_pcm_pre_suspend,
1067 .do_action = snd_pcm_do_suspend,
1068 .post_action = snd_pcm_post_suspend
1069};
1070
1071/**
1072 * snd_pcm_suspend
Takashi Iwaidf8db932005-09-07 13:38:19 +02001073 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
1075 * Trigger SUSPEND to all linked streams.
1076 * After this call, all streams are changed to SUSPENDED state.
1077 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001078int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 int err;
1081 unsigned long flags;
1082
Takashi Iwai603bf5242005-11-17 15:59:14 +01001083 if (! substream)
1084 return 0;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 snd_pcm_stream_lock_irqsave(substream, flags);
1087 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1088 snd_pcm_stream_unlock_irqrestore(substream, flags);
1089 return err;
1090}
1091
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001092EXPORT_SYMBOL(snd_pcm_suspend);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094/**
1095 * snd_pcm_suspend_all
Takashi Iwaidf8db932005-09-07 13:38:19 +02001096 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 *
1098 * Trigger SUSPEND to all substreams in the given pcm.
1099 * After this call, all streams are changed to SUSPENDED state.
1100 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001101int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Takashi Iwai877211f2005-11-17 13:59:38 +01001103 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int stream, err = 0;
1105
Takashi Iwai603bf5242005-11-17 15:59:14 +01001106 if (! pcm)
1107 return 0;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001110 for (substream = pcm->streams[stream].substream;
1111 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* FIXME: the open/close code should lock this as well */
1113 if (substream->runtime == NULL)
1114 continue;
1115 err = snd_pcm_suspend(substream);
1116 if (err < 0 && err != -EBUSY)
1117 return err;
1118 }
1119 }
1120 return 0;
1121}
1122
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001123EXPORT_SYMBOL(snd_pcm_suspend_all);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/* resume */
1126
Takashi Iwai877211f2005-11-17 13:59:38 +01001127static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Takashi Iwai877211f2005-11-17 13:59:38 +01001129 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1131 return -ENOSYS;
1132 runtime->trigger_master = substream;
1133 return 0;
1134}
1135
Takashi Iwai877211f2005-11-17 13:59:38 +01001136static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Takashi Iwai877211f2005-11-17 13:59:38 +01001138 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (runtime->trigger_master != substream)
1140 return 0;
1141 /* DMA not running previously? */
1142 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1143 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1144 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1145 return 0;
1146 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1147}
1148
Takashi Iwai877211f2005-11-17 13:59:38 +01001149static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 if (substream->runtime->trigger_master == substream &&
1152 snd_pcm_running(substream))
1153 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1154}
1155
Takashi Iwai877211f2005-11-17 13:59:38 +01001156static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Takashi Iwai877211f2005-11-17 13:59:38 +01001158 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 snd_pcm_trigger_tstamp(substream);
1160 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001161 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1162 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
1166static struct action_ops snd_pcm_action_resume = {
1167 .pre_action = snd_pcm_pre_resume,
1168 .do_action = snd_pcm_do_resume,
1169 .undo_action = snd_pcm_undo_resume,
1170 .post_action = snd_pcm_post_resume
1171};
1172
Takashi Iwai877211f2005-11-17 13:59:38 +01001173static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Takashi Iwai877211f2005-11-17 13:59:38 +01001175 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int res;
1177
1178 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001179 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1181 snd_power_unlock(card);
1182 return res;
1183}
1184
1185#else
1186
Takashi Iwai877211f2005-11-17 13:59:38 +01001187static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 return -ENOSYS;
1190}
1191
1192#endif /* CONFIG_PM */
1193
1194/*
1195 * xrun ioctl
1196 *
1197 * Change the RUNNING stream(s) to XRUN state.
1198 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001199static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Takashi Iwai877211f2005-11-17 13:59:38 +01001201 struct snd_card *card = substream->pcm->card;
1202 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int result;
1204
1205 snd_power_lock(card);
1206 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001207 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (result < 0)
1209 goto _unlock;
1210 }
1211
1212 snd_pcm_stream_lock_irq(substream);
1213 switch (runtime->status->state) {
1214 case SNDRV_PCM_STATE_XRUN:
1215 result = 0; /* already there */
1216 break;
1217 case SNDRV_PCM_STATE_RUNNING:
1218 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1219 break;
1220 default:
1221 result = -EBADFD;
1222 }
1223 snd_pcm_stream_unlock_irq(substream);
1224 _unlock:
1225 snd_power_unlock(card);
1226 return result;
1227}
1228
1229/*
1230 * reset ioctl
1231 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001232static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Takashi Iwai877211f2005-11-17 13:59:38 +01001234 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 switch (runtime->status->state) {
1236 case SNDRV_PCM_STATE_RUNNING:
1237 case SNDRV_PCM_STATE_PREPARED:
1238 case SNDRV_PCM_STATE_PAUSED:
1239 case SNDRV_PCM_STATE_SUSPENDED:
1240 return 0;
1241 default:
1242 return -EBADFD;
1243 }
1244}
1245
Takashi Iwai877211f2005-11-17 13:59:38 +01001246static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Takashi Iwai877211f2005-11-17 13:59:38 +01001248 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1250 if (err < 0)
1251 return err;
1252 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1253 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001254 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1255 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 runtime->silence_start = runtime->status->hw_ptr;
1257 runtime->silence_filled = 0;
1258 return 0;
1259}
1260
Takashi Iwai877211f2005-11-17 13:59:38 +01001261static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwai877211f2005-11-17 13:59:38 +01001263 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 runtime->control->appl_ptr = runtime->status->hw_ptr;
1265 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1266 runtime->silence_size > 0)
1267 snd_pcm_playback_silence(substream, ULONG_MAX);
1268}
1269
1270static struct action_ops snd_pcm_action_reset = {
1271 .pre_action = snd_pcm_pre_reset,
1272 .do_action = snd_pcm_do_reset,
1273 .post_action = snd_pcm_post_reset
1274};
1275
Takashi Iwai877211f2005-11-17 13:59:38 +01001276static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1279}
1280
1281/*
1282 * prepare ioctl
1283 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001284/* we use the second argument for updating f_flags */
1285static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1286 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Takashi Iwai877211f2005-11-17 13:59:38 +01001288 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001289 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1290 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return -EBADFD;
1292 if (snd_pcm_running(substream))
1293 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001294 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return 0;
1296}
1297
Takashi Iwai877211f2005-11-17 13:59:38 +01001298static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 int err;
1301 err = substream->ops->prepare(substream);
1302 if (err < 0)
1303 return err;
1304 return snd_pcm_do_reset(substream, 0);
1305}
1306
Takashi Iwai877211f2005-11-17 13:59:38 +01001307static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Takashi Iwai877211f2005-11-17 13:59:38 +01001309 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 runtime->control->appl_ptr = runtime->status->hw_ptr;
1311 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1312}
1313
1314static struct action_ops snd_pcm_action_prepare = {
1315 .pre_action = snd_pcm_pre_prepare,
1316 .do_action = snd_pcm_do_prepare,
1317 .post_action = snd_pcm_post_prepare
1318};
1319
1320/**
1321 * snd_pcm_prepare
Takashi Iwaidf8db932005-09-07 13:38:19 +02001322 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001323 * @file: file to refer f_flags
Takashi Iwaidf8db932005-09-07 13:38:19 +02001324 *
1325 * Prepare the PCM substream to be triggerable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001327static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1328 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001331 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001332 int f_flags;
1333
1334 if (file)
1335 f_flags = file->f_flags;
1336 else
1337 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001340 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001341 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1342 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 snd_power_unlock(card);
1344 return res;
1345}
1346
1347/*
1348 * drain ioctl
1349 */
1350
Takashi Iwai877211f2005-11-17 13:59:38 +01001351static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001353 if (substream->f_flags & O_NONBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EAGAIN;
1355 substream->runtime->trigger_master = substream;
1356 return 0;
1357}
1358
Takashi Iwai877211f2005-11-17 13:59:38 +01001359static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Takashi Iwai877211f2005-11-17 13:59:38 +01001361 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1363 switch (runtime->status->state) {
1364 case SNDRV_PCM_STATE_PREPARED:
1365 /* start playback stream if possible */
1366 if (! snd_pcm_playback_empty(substream)) {
1367 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1368 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1369 }
1370 break;
1371 case SNDRV_PCM_STATE_RUNNING:
1372 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1373 break;
1374 default:
1375 break;
1376 }
1377 } else {
1378 /* stop running stream */
1379 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001380 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001382 snd_pcm_do_stop(substream, new_state);
1383 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385 }
1386 return 0;
1387}
1388
Takashi Iwai877211f2005-11-17 13:59:38 +01001389static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391}
1392
1393static struct action_ops snd_pcm_action_drain_init = {
1394 .pre_action = snd_pcm_pre_drain_init,
1395 .do_action = snd_pcm_do_drain_init,
1396 .post_action = snd_pcm_post_drain_init
1397};
1398
1399struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001400 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 wait_queue_t wait;
1402 snd_pcm_uframes_t stop_threshold;
1403};
1404
Takashi Iwai877211f2005-11-17 13:59:38 +01001405static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407/*
1408 * Drain the stream(s).
1409 * When the substream is linked, sync until the draining of all playback streams
1410 * is finished.
1411 * After this call, all streams are supposed to be either SETUP or DRAINING
1412 * (capture only) state.
1413 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001414static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Takashi Iwai877211f2005-11-17 13:59:38 +01001416 struct snd_card *card;
1417 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001418 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 int result = 0;
1420 int i, num_drecs;
1421 struct drain_rec *drec, drec_tmp, *d;
1422
1423 snd_assert(substream != NULL, return -ENXIO);
1424 card = substream->pcm->card;
1425 runtime = substream->runtime;
1426
1427 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1428 return -EBADFD;
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 snd_power_lock(card);
1431 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001432 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001433 if (result < 0) {
1434 snd_power_unlock(card);
1435 return result;
1436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438
1439 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001440 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (snd_pcm_stream_linked(substream)) {
1442 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1443 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001444 up_read(&snd_pcm_link_rwsem);
1445 snd_power_unlock(card);
1446 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448 } else
1449 drec = &drec_tmp;
1450
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001451 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 num_drecs = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001453 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1456 d = &drec[num_drecs++];
1457 d->substream = s;
1458 init_waitqueue_entry(&d->wait, current);
1459 add_wait_queue(&runtime->sleep, &d->wait);
1460 /* stop_threshold fixup to avoid endless loop when
1461 * stop_threshold > buffer_size
1462 */
1463 d->stop_threshold = runtime->stop_threshold;
1464 if (runtime->stop_threshold > runtime->buffer_size)
1465 runtime->stop_threshold = runtime->buffer_size;
1466 }
1467 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001468 up_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001469
1470 snd_pcm_stream_lock_irq(substream);
1471 /* resume pause */
Takashi Iwai1a7fa542007-07-06 12:27:25 +02001472 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001473 snd_pcm_pause(substream, 0);
1474
1475 /* pre-start/stop - all running streams are changed to DRAINING state */
1476 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1477 if (result < 0) {
1478 snd_pcm_stream_unlock_irq(substream);
1479 goto _error;
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 for (;;) {
1483 long tout;
1484 if (signal_pending(current)) {
1485 result = -ERESTARTSYS;
1486 break;
1487 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001488 /* all finished? */
1489 for (i = 0; i < num_drecs; i++) {
1490 runtime = drec[i].substream->runtime;
1491 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1492 break;
1493 }
1494 if (i == num_drecs)
1495 break; /* yes, all drained */
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 set_current_state(TASK_INTERRUPTIBLE);
1498 snd_pcm_stream_unlock_irq(substream);
1499 snd_power_unlock(card);
1500 tout = schedule_timeout(10 * HZ);
1501 snd_power_lock(card);
1502 snd_pcm_stream_lock_irq(substream);
1503 if (tout == 0) {
1504 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1505 result = -ESTRPIPE;
1506 else {
1507 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1508 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1509 result = -EIO;
1510 }
1511 break;
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001514
1515 snd_pcm_stream_unlock_irq(substream);
1516
1517 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 for (i = 0; i < num_drecs; i++) {
1519 d = &drec[i];
1520 runtime = d->substream->runtime;
1521 remove_wait_queue(&runtime->sleep, &d->wait);
1522 runtime->stop_threshold = d->stop_threshold;
1523 }
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (drec != &drec_tmp)
1526 kfree(drec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 return result;
1530}
1531
1532/*
1533 * drop ioctl
1534 *
1535 * Immediately put all linked substreams into SETUP state.
1536 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001537static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Takashi Iwai877211f2005-11-17 13:59:38 +01001539 struct snd_pcm_runtime *runtime;
1540 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 int result = 0;
1542
1543 snd_assert(substream != NULL, return -ENXIO);
1544 runtime = substream->runtime;
1545 card = substream->pcm->card;
1546
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001547 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1548 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -EBADFD;
1550
1551 snd_power_lock(card);
1552 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001553 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (result < 0)
1555 goto _unlock;
1556 }
1557
1558 snd_pcm_stream_lock_irq(substream);
1559 /* resume pause */
1560 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1561 snd_pcm_pause(substream, 0);
1562
1563 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1564 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1565 snd_pcm_stream_unlock_irq(substream);
1566 _unlock:
1567 snd_power_unlock(card);
1568 return result;
1569}
1570
1571
1572/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573static struct file *snd_pcm_file_fd(int fd)
1574{
1575 struct file *file;
1576 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001577 unsigned int minor;
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 file = fget(fd);
1580 if (!file)
1581 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001582 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (!S_ISCHR(inode->i_mode) ||
1584 imajor(inode) != snd_major) {
1585 fput(file);
1586 return NULL;
1587 }
1588 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001589 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1590 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 fput(file);
1592 return NULL;
1593 }
1594 return file;
1595}
1596
1597/*
1598 * PCM link handling
1599 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001600static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 int res = 0;
1603 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001604 struct snd_pcm_file *pcm_file;
1605 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 file = snd_pcm_file_fd(fd);
1608 if (!file)
1609 return -EBADFD;
1610 pcm_file = file->private_data;
1611 substream1 = pcm_file->substream;
1612 down_write(&snd_pcm_link_rwsem);
1613 write_lock_irq(&snd_pcm_link_rwlock);
1614 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1615 substream->runtime->status->state != substream1->runtime->status->state) {
1616 res = -EBADFD;
1617 goto _end;
1618 }
1619 if (snd_pcm_stream_linked(substream1)) {
1620 res = -EALREADY;
1621 goto _end;
1622 }
1623 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001624 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (substream->group == NULL) {
1626 res = -ENOMEM;
1627 goto _end;
1628 }
1629 spin_lock_init(&substream->group->lock);
1630 INIT_LIST_HEAD(&substream->group->substreams);
1631 list_add_tail(&substream->link_list, &substream->group->substreams);
1632 substream->group->count = 1;
1633 }
1634 list_add_tail(&substream1->link_list, &substream->group->substreams);
1635 substream->group->count++;
1636 substream1->group = substream->group;
1637 _end:
1638 write_unlock_irq(&snd_pcm_link_rwlock);
1639 up_write(&snd_pcm_link_rwsem);
1640 fput(file);
1641 return res;
1642}
1643
Takashi Iwai877211f2005-11-17 13:59:38 +01001644static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 substream->group = &substream->self_group;
1647 INIT_LIST_HEAD(&substream->self_group.substreams);
1648 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1649}
1650
Takashi Iwai877211f2005-11-17 13:59:38 +01001651static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Takashi Iwaief991b92007-02-22 12:52:53 +01001653 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 int res = 0;
1655
1656 down_write(&snd_pcm_link_rwsem);
1657 write_lock_irq(&snd_pcm_link_rwlock);
1658 if (!snd_pcm_stream_linked(substream)) {
1659 res = -EALREADY;
1660 goto _end;
1661 }
1662 list_del(&substream->link_list);
1663 substream->group->count--;
1664 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001665 snd_pcm_group_for_each_entry(s, substream) {
1666 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 break;
1668 }
1669 kfree(substream->group);
1670 }
1671 relink_to_local(substream);
1672 _end:
1673 write_unlock_irq(&snd_pcm_link_rwlock);
1674 up_write(&snd_pcm_link_rwsem);
1675 return res;
1676}
1677
1678/*
1679 * hw configurator
1680 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001681static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1682 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Takashi Iwai877211f2005-11-17 13:59:38 +01001684 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1686 hw_param_interval_c(params, rule->deps[1]), &t);
1687 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1688}
1689
Takashi Iwai877211f2005-11-17 13:59:38 +01001690static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1691 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Takashi Iwai877211f2005-11-17 13:59:38 +01001693 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1695 hw_param_interval_c(params, rule->deps[1]), &t);
1696 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1697}
1698
Takashi Iwai877211f2005-11-17 13:59:38 +01001699static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1700 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Takashi Iwai877211f2005-11-17 13:59:38 +01001702 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1704 hw_param_interval_c(params, rule->deps[1]),
1705 (unsigned long) rule->private, &t);
1706 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1707}
1708
Takashi Iwai877211f2005-11-17 13:59:38 +01001709static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1710 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Takashi Iwai877211f2005-11-17 13:59:38 +01001712 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1714 (unsigned long) rule->private,
1715 hw_param_interval_c(params, rule->deps[1]), &t);
1716 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1717}
1718
Takashi Iwai877211f2005-11-17 13:59:38 +01001719static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1720 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001723 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1724 struct snd_mask m;
1725 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 snd_mask_any(&m);
1727 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1728 int bits;
1729 if (! snd_mask_test(mask, k))
1730 continue;
1731 bits = snd_pcm_format_physical_width(k);
1732 if (bits <= 0)
1733 continue; /* ignore invalid formats */
1734 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1735 snd_mask_reset(&m, k);
1736 }
1737 return snd_mask_refine(mask, &m);
1738}
1739
Takashi Iwai877211f2005-11-17 13:59:38 +01001740static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1741 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Takashi Iwai877211f2005-11-17 13:59:38 +01001743 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 unsigned int k;
1745 t.min = UINT_MAX;
1746 t.max = 0;
1747 t.openmin = 0;
1748 t.openmax = 0;
1749 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1750 int bits;
1751 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1752 continue;
1753 bits = snd_pcm_format_physical_width(k);
1754 if (bits <= 0)
1755 continue; /* ignore invalid formats */
1756 if (t.min > (unsigned)bits)
1757 t.min = bits;
1758 if (t.max < (unsigned)bits)
1759 t.max = bits;
1760 }
1761 t.integer = 1;
1762 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1763}
1764
1765#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1766#error "Change this table"
1767#endif
1768
1769static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1770 48000, 64000, 88200, 96000, 176400, 192000 };
1771
Clemens Ladisch7653d552007-08-13 17:38:54 +02001772const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1773 .count = ARRAY_SIZE(rates),
1774 .list = rates,
1775};
1776
Takashi Iwai877211f2005-11-17 13:59:38 +01001777static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1778 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Takashi Iwai877211f2005-11-17 13:59:38 +01001780 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001782 snd_pcm_known_rates.count,
1783 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Takashi Iwai877211f2005-11-17 13:59:38 +01001786static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1787 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Takashi Iwai877211f2005-11-17 13:59:38 +01001789 struct snd_interval t;
1790 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 t.min = 0;
1792 t.max = substream->buffer_bytes_max;
1793 t.openmin = 0;
1794 t.openmax = 0;
1795 t.integer = 1;
1796 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1797}
1798
Takashi Iwai877211f2005-11-17 13:59:38 +01001799int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Takashi Iwai877211f2005-11-17 13:59:38 +01001801 struct snd_pcm_runtime *runtime = substream->runtime;
1802 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 int k, err;
1804
1805 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1806 snd_mask_any(constrs_mask(constrs, k));
1807 }
1808
1809 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1810 snd_interval_any(constrs_interval(constrs, k));
1811 }
1812
1813 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1814 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1815 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1816 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1817 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1818
1819 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1820 snd_pcm_hw_rule_format, NULL,
1821 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1822 if (err < 0)
1823 return err;
1824 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1825 snd_pcm_hw_rule_sample_bits, NULL,
1826 SNDRV_PCM_HW_PARAM_FORMAT,
1827 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1828 if (err < 0)
1829 return err;
1830 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1831 snd_pcm_hw_rule_div, NULL,
1832 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1833 if (err < 0)
1834 return err;
1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1836 snd_pcm_hw_rule_mul, NULL,
1837 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1838 if (err < 0)
1839 return err;
1840 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1841 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1842 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1843 if (err < 0)
1844 return err;
1845 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1846 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1847 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1848 if (err < 0)
1849 return err;
1850 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1851 snd_pcm_hw_rule_div, NULL,
1852 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1853 if (err < 0)
1854 return err;
1855 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1856 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1857 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1858 if (err < 0)
1859 return err;
1860 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1861 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1862 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1863 if (err < 0)
1864 return err;
1865 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1866 snd_pcm_hw_rule_div, NULL,
1867 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1868 if (err < 0)
1869 return err;
1870 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1871 snd_pcm_hw_rule_div, NULL,
1872 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1873 if (err < 0)
1874 return err;
1875 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1876 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1877 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1878 if (err < 0)
1879 return err;
1880 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1881 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1882 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1883 if (err < 0)
1884 return err;
1885 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1886 snd_pcm_hw_rule_mul, NULL,
1887 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1888 if (err < 0)
1889 return err;
1890 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1891 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1892 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1893 if (err < 0)
1894 return err;
1895 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1896 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1897 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1898 if (err < 0)
1899 return err;
1900 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1901 snd_pcm_hw_rule_muldivk, (void*) 8,
1902 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1903 if (err < 0)
1904 return err;
1905 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1906 snd_pcm_hw_rule_muldivk, (void*) 8,
1907 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1908 if (err < 0)
1909 return err;
1910 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1911 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1912 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1913 if (err < 0)
1914 return err;
1915 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1916 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1917 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1918 if (err < 0)
1919 return err;
1920 return 0;
1921}
1922
Takashi Iwai877211f2005-11-17 13:59:38 +01001923int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Takashi Iwai877211f2005-11-17 13:59:38 +01001925 struct snd_pcm_runtime *runtime = substream->runtime;
1926 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int err;
1928 unsigned int mask = 0;
1929
1930 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1931 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1932 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1933 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1934 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1935 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1936 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1937 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1938 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1939 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1940 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1941 }
1942 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1943 snd_assert(err >= 0, return -EINVAL);
1944
1945 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1946 snd_assert(err >= 0, return -EINVAL);
1947
1948 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1949 snd_assert(err >= 0, return -EINVAL);
1950
1951 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1952 hw->channels_min, hw->channels_max);
1953 snd_assert(err >= 0, return -EINVAL);
1954
1955 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1956 hw->rate_min, hw->rate_max);
1957 snd_assert(err >= 0, return -EINVAL);
1958
1959 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1960 hw->period_bytes_min, hw->period_bytes_max);
1961 snd_assert(err >= 0, return -EINVAL);
1962
1963 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1964 hw->periods_min, hw->periods_max);
1965 snd_assert(err >= 0, return -EINVAL);
1966
1967 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1968 hw->period_bytes_min, hw->buffer_bytes_max);
1969 snd_assert(err >= 0, return -EINVAL);
1970
1971 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1972 snd_pcm_hw_rule_buffer_bytes_max, substream,
1973 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1974 if (err < 0)
1975 return err;
1976
1977 /* FIXME: remove */
1978 if (runtime->dma_bytes) {
1979 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1980 snd_assert(err >= 0, return -EINVAL);
1981 }
1982
1983 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1984 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1985 snd_pcm_hw_rule_rate, hw,
1986 SNDRV_PCM_HW_PARAM_RATE, -1);
1987 if (err < 0)
1988 return err;
1989 }
1990
1991 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1993
1994 return 0;
1995}
1996
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001997static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002000}
2001
2002void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2003{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002004 substream->ref_count--;
2005 if (substream->ref_count > 0)
2006 return;
2007
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002008 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002009 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (substream->ops->hw_free != NULL)
2011 substream->ops->hw_free(substream);
2012 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002013 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
Takashi Iwai15762742006-04-06 19:47:42 +02002015 if (substream->pcm_release) {
2016 substream->pcm_release(substream);
2017 substream->pcm_release = NULL;
2018 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002019 snd_pcm_detach_substream(substream);
2020}
2021
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002022EXPORT_SYMBOL(snd_pcm_release_substream);
2023
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002024int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2025 struct file *file,
2026 struct snd_pcm_substream **rsubstream)
2027{
2028 struct snd_pcm_substream *substream;
2029 int err;
2030
2031 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2032 if (err < 0)
2033 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002034 if (substream->ref_count > 1) {
2035 *rsubstream = substream;
2036 return 0;
2037 }
2038
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002039 err = snd_pcm_hw_constraints_init(substream);
2040 if (err < 0) {
2041 snd_printd("snd_pcm_hw_constraints_init failed\n");
2042 goto error;
2043 }
2044
2045 if ((err = substream->ops->open(substream)) < 0)
2046 goto error;
2047
2048 substream->hw_opened = 1;
2049
2050 err = snd_pcm_hw_constraints_complete(substream);
2051 if (err < 0) {
2052 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2053 goto error;
2054 }
2055
2056 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002058
2059 error:
2060 snd_pcm_release_substream(substream);
2061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002064EXPORT_SYMBOL(snd_pcm_open_substream);
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002067 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002069 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Takashi Iwai877211f2005-11-17 13:59:38 +01002071 struct snd_pcm_file *pcm_file;
2072 struct snd_pcm_substream *substream;
2073 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002074 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 snd_assert(rpcm_file != NULL, return -EINVAL);
2077 *rpcm_file = NULL;
2078
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002079 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2080 if (err < 0)
2081 return err;
2082
Takashi Iwai548a6482006-07-31 16:51:51 +02002083 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2084 if (pcm_file == NULL) {
2085 snd_pcm_release_substream(substream);
2086 return -ENOMEM;
2087 }
2088 pcm_file->substream = substream;
2089 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002090 str = substream->pstr;
2091 substream->file = pcm_file;
2092 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 file->private_data = pcm_file;
2095 *rpcm_file = pcm_file;
2096 return 0;
2097}
2098
Clemens Ladischf87135f2005-11-20 14:06:59 +01002099static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Takashi Iwai877211f2005-11-17 13:59:38 +01002101 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002102
2103 pcm = snd_lookup_minor_data(iminor(inode),
2104 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2105 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2106}
2107
2108static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2109{
2110 struct snd_pcm *pcm;
2111
2112 pcm = snd_lookup_minor_data(iminor(inode),
2113 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2114 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2115}
2116
2117static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2118{
2119 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002120 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 wait_queue_t wait;
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (pcm == NULL) {
2124 err = -ENODEV;
2125 goto __error1;
2126 }
2127 err = snd_card_file_add(pcm->card, file);
2128 if (err < 0)
2129 goto __error1;
2130 if (!try_module_get(pcm->card->module)) {
2131 err = -EFAULT;
2132 goto __error2;
2133 }
2134 init_waitqueue_entry(&wait, current);
2135 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002136 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002138 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (err >= 0)
2140 break;
2141 if (err == -EAGAIN) {
2142 if (file->f_flags & O_NONBLOCK) {
2143 err = -EBUSY;
2144 break;
2145 }
2146 } else
2147 break;
2148 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002149 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002151 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (signal_pending(current)) {
2153 err = -ERESTARTSYS;
2154 break;
2155 }
2156 }
2157 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002158 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (err < 0)
2160 goto __error;
2161 return err;
2162
2163 __error:
2164 module_put(pcm->card->module);
2165 __error2:
2166 snd_card_file_remove(pcm->card, file);
2167 __error1:
2168 return err;
2169}
2170
2171static int snd_pcm_release(struct inode *inode, struct file *file)
2172{
Takashi Iwai877211f2005-11-17 13:59:38 +01002173 struct snd_pcm *pcm;
2174 struct snd_pcm_substream *substream;
2175 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 pcm_file = file->private_data;
2178 substream = pcm_file->substream;
2179 snd_assert(substream != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 pcm = substream->pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 fasync_helper(-1, file, 0, &substream->runtime->fasync);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002182 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002183 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002184 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002185 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 wake_up(&pcm->open_wait);
2187 module_put(pcm->card->module);
2188 snd_card_file_remove(pcm->card, file);
2189 return 0;
2190}
2191
Takashi Iwai877211f2005-11-17 13:59:38 +01002192static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2193 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Takashi Iwai877211f2005-11-17 13:59:38 +01002195 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 snd_pcm_sframes_t appl_ptr;
2197 snd_pcm_sframes_t ret;
2198 snd_pcm_sframes_t hw_avail;
2199
2200 if (frames == 0)
2201 return 0;
2202
2203 snd_pcm_stream_lock_irq(substream);
2204 switch (runtime->status->state) {
2205 case SNDRV_PCM_STATE_PREPARED:
2206 break;
2207 case SNDRV_PCM_STATE_DRAINING:
2208 case SNDRV_PCM_STATE_RUNNING:
2209 if (snd_pcm_update_hw_ptr(substream) >= 0)
2210 break;
2211 /* Fall through */
2212 case SNDRV_PCM_STATE_XRUN:
2213 ret = -EPIPE;
2214 goto __end;
2215 default:
2216 ret = -EBADFD;
2217 goto __end;
2218 }
2219
2220 hw_avail = snd_pcm_playback_hw_avail(runtime);
2221 if (hw_avail <= 0) {
2222 ret = 0;
2223 goto __end;
2224 }
2225 if (frames > (snd_pcm_uframes_t)hw_avail)
2226 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 appl_ptr = runtime->control->appl_ptr - frames;
2228 if (appl_ptr < 0)
2229 appl_ptr += runtime->boundary;
2230 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 ret = frames;
2232 __end:
2233 snd_pcm_stream_unlock_irq(substream);
2234 return ret;
2235}
2236
Takashi Iwai877211f2005-11-17 13:59:38 +01002237static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2238 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Takashi Iwai877211f2005-11-17 13:59:38 +01002240 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 snd_pcm_sframes_t appl_ptr;
2242 snd_pcm_sframes_t ret;
2243 snd_pcm_sframes_t hw_avail;
2244
2245 if (frames == 0)
2246 return 0;
2247
2248 snd_pcm_stream_lock_irq(substream);
2249 switch (runtime->status->state) {
2250 case SNDRV_PCM_STATE_PREPARED:
2251 case SNDRV_PCM_STATE_DRAINING:
2252 break;
2253 case SNDRV_PCM_STATE_RUNNING:
2254 if (snd_pcm_update_hw_ptr(substream) >= 0)
2255 break;
2256 /* Fall through */
2257 case SNDRV_PCM_STATE_XRUN:
2258 ret = -EPIPE;
2259 goto __end;
2260 default:
2261 ret = -EBADFD;
2262 goto __end;
2263 }
2264
2265 hw_avail = snd_pcm_capture_hw_avail(runtime);
2266 if (hw_avail <= 0) {
2267 ret = 0;
2268 goto __end;
2269 }
2270 if (frames > (snd_pcm_uframes_t)hw_avail)
2271 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 appl_ptr = runtime->control->appl_ptr - frames;
2273 if (appl_ptr < 0)
2274 appl_ptr += runtime->boundary;
2275 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 ret = frames;
2277 __end:
2278 snd_pcm_stream_unlock_irq(substream);
2279 return ret;
2280}
2281
Takashi Iwai877211f2005-11-17 13:59:38 +01002282static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2283 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Takashi Iwai877211f2005-11-17 13:59:38 +01002285 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 snd_pcm_sframes_t appl_ptr;
2287 snd_pcm_sframes_t ret;
2288 snd_pcm_sframes_t avail;
2289
2290 if (frames == 0)
2291 return 0;
2292
2293 snd_pcm_stream_lock_irq(substream);
2294 switch (runtime->status->state) {
2295 case SNDRV_PCM_STATE_PREPARED:
2296 case SNDRV_PCM_STATE_PAUSED:
2297 break;
2298 case SNDRV_PCM_STATE_DRAINING:
2299 case SNDRV_PCM_STATE_RUNNING:
2300 if (snd_pcm_update_hw_ptr(substream) >= 0)
2301 break;
2302 /* Fall through */
2303 case SNDRV_PCM_STATE_XRUN:
2304 ret = -EPIPE;
2305 goto __end;
2306 default:
2307 ret = -EBADFD;
2308 goto __end;
2309 }
2310
2311 avail = snd_pcm_playback_avail(runtime);
2312 if (avail <= 0) {
2313 ret = 0;
2314 goto __end;
2315 }
2316 if (frames > (snd_pcm_uframes_t)avail)
2317 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 appl_ptr = runtime->control->appl_ptr + frames;
2319 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2320 appl_ptr -= runtime->boundary;
2321 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 ret = frames;
2323 __end:
2324 snd_pcm_stream_unlock_irq(substream);
2325 return ret;
2326}
2327
Takashi Iwai877211f2005-11-17 13:59:38 +01002328static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2329 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Takashi Iwai877211f2005-11-17 13:59:38 +01002331 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 snd_pcm_sframes_t appl_ptr;
2333 snd_pcm_sframes_t ret;
2334 snd_pcm_sframes_t avail;
2335
2336 if (frames == 0)
2337 return 0;
2338
2339 snd_pcm_stream_lock_irq(substream);
2340 switch (runtime->status->state) {
2341 case SNDRV_PCM_STATE_PREPARED:
2342 case SNDRV_PCM_STATE_DRAINING:
2343 case SNDRV_PCM_STATE_PAUSED:
2344 break;
2345 case SNDRV_PCM_STATE_RUNNING:
2346 if (snd_pcm_update_hw_ptr(substream) >= 0)
2347 break;
2348 /* Fall through */
2349 case SNDRV_PCM_STATE_XRUN:
2350 ret = -EPIPE;
2351 goto __end;
2352 default:
2353 ret = -EBADFD;
2354 goto __end;
2355 }
2356
2357 avail = snd_pcm_capture_avail(runtime);
2358 if (avail <= 0) {
2359 ret = 0;
2360 goto __end;
2361 }
2362 if (frames > (snd_pcm_uframes_t)avail)
2363 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 appl_ptr = runtime->control->appl_ptr + frames;
2365 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2366 appl_ptr -= runtime->boundary;
2367 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 ret = frames;
2369 __end:
2370 snd_pcm_stream_unlock_irq(substream);
2371 return ret;
2372}
2373
Takashi Iwai877211f2005-11-17 13:59:38 +01002374static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Takashi Iwai877211f2005-11-17 13:59:38 +01002376 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 int err;
2378
2379 snd_pcm_stream_lock_irq(substream);
2380 switch (runtime->status->state) {
2381 case SNDRV_PCM_STATE_DRAINING:
2382 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2383 goto __badfd;
2384 case SNDRV_PCM_STATE_RUNNING:
2385 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2386 break;
2387 /* Fall through */
2388 case SNDRV_PCM_STATE_PREPARED:
2389 case SNDRV_PCM_STATE_SUSPENDED:
2390 err = 0;
2391 break;
2392 case SNDRV_PCM_STATE_XRUN:
2393 err = -EPIPE;
2394 break;
2395 default:
2396 __badfd:
2397 err = -EBADFD;
2398 break;
2399 }
2400 snd_pcm_stream_unlock_irq(substream);
2401 return err;
2402}
2403
Takashi Iwai877211f2005-11-17 13:59:38 +01002404static int snd_pcm_delay(struct snd_pcm_substream *substream,
2405 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Takashi Iwai877211f2005-11-17 13:59:38 +01002407 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int err;
2409 snd_pcm_sframes_t n = 0;
2410
2411 snd_pcm_stream_lock_irq(substream);
2412 switch (runtime->status->state) {
2413 case SNDRV_PCM_STATE_DRAINING:
2414 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2415 goto __badfd;
2416 case SNDRV_PCM_STATE_RUNNING:
2417 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2418 break;
2419 /* Fall through */
2420 case SNDRV_PCM_STATE_PREPARED:
2421 case SNDRV_PCM_STATE_SUSPENDED:
2422 err = 0;
2423 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2424 n = snd_pcm_playback_hw_avail(runtime);
2425 else
2426 n = snd_pcm_capture_avail(runtime);
2427 break;
2428 case SNDRV_PCM_STATE_XRUN:
2429 err = -EPIPE;
2430 break;
2431 default:
2432 __badfd:
2433 err = -EBADFD;
2434 break;
2435 }
2436 snd_pcm_stream_unlock_irq(substream);
2437 if (!err)
2438 if (put_user(n, res))
2439 err = -EFAULT;
2440 return err;
2441}
2442
Takashi Iwai877211f2005-11-17 13:59:38 +01002443static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2444 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
Takashi Iwai877211f2005-11-17 13:59:38 +01002446 struct snd_pcm_runtime *runtime = substream->runtime;
2447 struct snd_pcm_sync_ptr sync_ptr;
2448 volatile struct snd_pcm_mmap_status *status;
2449 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int err;
2451
2452 memset(&sync_ptr, 0, sizeof(sync_ptr));
2453 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2454 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002455 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 -07002456 return -EFAULT;
2457 status = runtime->status;
2458 control = runtime->control;
2459 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2460 err = snd_pcm_hwsync(substream);
2461 if (err < 0)
2462 return err;
2463 }
2464 snd_pcm_stream_lock_irq(substream);
2465 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2466 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2467 else
2468 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2469 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2470 control->avail_min = sync_ptr.c.control.avail_min;
2471 else
2472 sync_ptr.c.control.avail_min = control->avail_min;
2473 sync_ptr.s.status.state = status->state;
2474 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2475 sync_ptr.s.status.tstamp = status->tstamp;
2476 sync_ptr.s.status.suspended_state = status->suspended_state;
2477 snd_pcm_stream_unlock_irq(substream);
2478 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2479 return -EFAULT;
2480 return 0;
2481}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002482
2483static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2484{
2485 struct snd_pcm_runtime *runtime = substream->runtime;
2486 int arg;
2487
2488 if (get_user(arg, _arg))
2489 return -EFAULT;
2490 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2491 return -EINVAL;
2492 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2493 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2494 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2495 return 0;
2496}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Takashi Iwai0df63e42006-04-28 15:13:41 +02002498static int snd_pcm_common_ioctl1(struct file *file,
2499 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 unsigned int cmd, void __user *arg)
2501{
2502 snd_assert(substream != NULL, return -ENXIO);
2503
2504 switch (cmd) {
2505 case SNDRV_PCM_IOCTL_PVERSION:
2506 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2507 case SNDRV_PCM_IOCTL_INFO:
2508 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002509 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2510 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002511 case SNDRV_PCM_IOCTL_TTSTAMP:
2512 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 case SNDRV_PCM_IOCTL_HW_REFINE:
2514 return snd_pcm_hw_refine_user(substream, arg);
2515 case SNDRV_PCM_IOCTL_HW_PARAMS:
2516 return snd_pcm_hw_params_user(substream, arg);
2517 case SNDRV_PCM_IOCTL_HW_FREE:
2518 return snd_pcm_hw_free(substream);
2519 case SNDRV_PCM_IOCTL_SW_PARAMS:
2520 return snd_pcm_sw_params_user(substream, arg);
2521 case SNDRV_PCM_IOCTL_STATUS:
2522 return snd_pcm_status_user(substream, arg);
2523 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2524 return snd_pcm_channel_info_user(substream, arg);
2525 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002526 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 case SNDRV_PCM_IOCTL_RESET:
2528 return snd_pcm_reset(substream);
2529 case SNDRV_PCM_IOCTL_START:
2530 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2531 case SNDRV_PCM_IOCTL_LINK:
2532 return snd_pcm_link(substream, (int)(unsigned long) arg);
2533 case SNDRV_PCM_IOCTL_UNLINK:
2534 return snd_pcm_unlink(substream);
2535 case SNDRV_PCM_IOCTL_RESUME:
2536 return snd_pcm_resume(substream);
2537 case SNDRV_PCM_IOCTL_XRUN:
2538 return snd_pcm_xrun(substream);
2539 case SNDRV_PCM_IOCTL_HWSYNC:
2540 return snd_pcm_hwsync(substream);
2541 case SNDRV_PCM_IOCTL_DELAY:
2542 return snd_pcm_delay(substream, arg);
2543 case SNDRV_PCM_IOCTL_SYNC_PTR:
2544 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002545#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2547 return snd_pcm_hw_refine_old_user(substream, arg);
2548 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2549 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 case SNDRV_PCM_IOCTL_DRAIN:
2552 return snd_pcm_drain(substream);
2553 case SNDRV_PCM_IOCTL_DROP:
2554 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002555 case SNDRV_PCM_IOCTL_PAUSE:
2556 {
2557 int res;
2558 snd_pcm_stream_lock_irq(substream);
2559 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2560 snd_pcm_stream_unlock_irq(substream);
2561 return res;
2562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564 snd_printd("unknown ioctl = 0x%x\n", cmd);
2565 return -ENOTTY;
2566}
2567
Takashi Iwai0df63e42006-04-28 15:13:41 +02002568static int snd_pcm_playback_ioctl1(struct file *file,
2569 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 unsigned int cmd, void __user *arg)
2571{
2572 snd_assert(substream != NULL, return -ENXIO);
2573 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2574 switch (cmd) {
2575 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2576 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002577 struct snd_xferi xferi;
2578 struct snd_xferi __user *_xferi = arg;
2579 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 snd_pcm_sframes_t result;
2581 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2582 return -EBADFD;
2583 if (put_user(0, &_xferi->result))
2584 return -EFAULT;
2585 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2586 return -EFAULT;
2587 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2588 __put_user(result, &_xferi->result);
2589 return result < 0 ? result : 0;
2590 }
2591 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2592 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002593 struct snd_xfern xfern;
2594 struct snd_xfern __user *_xfern = arg;
2595 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 void __user **bufs;
2597 snd_pcm_sframes_t result;
2598 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2599 return -EBADFD;
2600 if (runtime->channels > 128)
2601 return -EINVAL;
2602 if (put_user(0, &_xfern->result))
2603 return -EFAULT;
2604 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2605 return -EFAULT;
2606 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2607 if (bufs == NULL)
2608 return -ENOMEM;
2609 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2610 kfree(bufs);
2611 return -EFAULT;
2612 }
2613 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2614 kfree(bufs);
2615 __put_user(result, &_xfern->result);
2616 return result < 0 ? result : 0;
2617 }
2618 case SNDRV_PCM_IOCTL_REWIND:
2619 {
2620 snd_pcm_uframes_t frames;
2621 snd_pcm_uframes_t __user *_frames = arg;
2622 snd_pcm_sframes_t result;
2623 if (get_user(frames, _frames))
2624 return -EFAULT;
2625 if (put_user(0, _frames))
2626 return -EFAULT;
2627 result = snd_pcm_playback_rewind(substream, frames);
2628 __put_user(result, _frames);
2629 return result < 0 ? result : 0;
2630 }
2631 case SNDRV_PCM_IOCTL_FORWARD:
2632 {
2633 snd_pcm_uframes_t frames;
2634 snd_pcm_uframes_t __user *_frames = arg;
2635 snd_pcm_sframes_t result;
2636 if (get_user(frames, _frames))
2637 return -EFAULT;
2638 if (put_user(0, _frames))
2639 return -EFAULT;
2640 result = snd_pcm_playback_forward(substream, frames);
2641 __put_user(result, _frames);
2642 return result < 0 ? result : 0;
2643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002645 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
Takashi Iwai0df63e42006-04-28 15:13:41 +02002648static int snd_pcm_capture_ioctl1(struct file *file,
2649 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 unsigned int cmd, void __user *arg)
2651{
2652 snd_assert(substream != NULL, return -ENXIO);
2653 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2654 switch (cmd) {
2655 case SNDRV_PCM_IOCTL_READI_FRAMES:
2656 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002657 struct snd_xferi xferi;
2658 struct snd_xferi __user *_xferi = arg;
2659 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 snd_pcm_sframes_t result;
2661 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2662 return -EBADFD;
2663 if (put_user(0, &_xferi->result))
2664 return -EFAULT;
2665 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2666 return -EFAULT;
2667 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2668 __put_user(result, &_xferi->result);
2669 return result < 0 ? result : 0;
2670 }
2671 case SNDRV_PCM_IOCTL_READN_FRAMES:
2672 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002673 struct snd_xfern xfern;
2674 struct snd_xfern __user *_xfern = arg;
2675 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 void *bufs;
2677 snd_pcm_sframes_t result;
2678 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2679 return -EBADFD;
2680 if (runtime->channels > 128)
2681 return -EINVAL;
2682 if (put_user(0, &_xfern->result))
2683 return -EFAULT;
2684 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2685 return -EFAULT;
2686 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2687 if (bufs == NULL)
2688 return -ENOMEM;
2689 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2690 kfree(bufs);
2691 return -EFAULT;
2692 }
2693 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2694 kfree(bufs);
2695 __put_user(result, &_xfern->result);
2696 return result < 0 ? result : 0;
2697 }
2698 case SNDRV_PCM_IOCTL_REWIND:
2699 {
2700 snd_pcm_uframes_t frames;
2701 snd_pcm_uframes_t __user *_frames = arg;
2702 snd_pcm_sframes_t result;
2703 if (get_user(frames, _frames))
2704 return -EFAULT;
2705 if (put_user(0, _frames))
2706 return -EFAULT;
2707 result = snd_pcm_capture_rewind(substream, frames);
2708 __put_user(result, _frames);
2709 return result < 0 ? result : 0;
2710 }
2711 case SNDRV_PCM_IOCTL_FORWARD:
2712 {
2713 snd_pcm_uframes_t frames;
2714 snd_pcm_uframes_t __user *_frames = arg;
2715 snd_pcm_sframes_t result;
2716 if (get_user(frames, _frames))
2717 return -EFAULT;
2718 if (put_user(0, _frames))
2719 return -EFAULT;
2720 result = snd_pcm_capture_forward(substream, frames);
2721 __put_user(result, _frames);
2722 return result < 0 ? result : 0;
2723 }
2724 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002725 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
Takashi Iwai877211f2005-11-17 13:59:38 +01002728static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2729 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Takashi Iwai877211f2005-11-17 13:59:38 +01002731 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 pcm_file = file->private_data;
2734
2735 if (((cmd >> 8) & 0xff) != 'A')
2736 return -ENOTTY;
2737
Takashi Iwai0df63e42006-04-28 15:13:41 +02002738 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2739 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
Takashi Iwai877211f2005-11-17 13:59:38 +01002742static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2743 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
Takashi Iwai877211f2005-11-17 13:59:38 +01002745 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
2747 pcm_file = file->private_data;
2748
2749 if (((cmd >> 8) & 0xff) != 'A')
2750 return -ENOTTY;
2751
Takashi Iwai0df63e42006-04-28 15:13:41 +02002752 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2753 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
Takashi Iwai877211f2005-11-17 13:59:38 +01002756int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 unsigned int cmd, void *arg)
2758{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002759 mm_segment_t fs;
2760 int result;
2761
2762 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 switch (substream->stream) {
2764 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002765 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2766 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002769 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2770 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002771 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002773 result = -EINVAL;
2774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002776 snd_leave_user(fs);
2777 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002780EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2781
Takashi Iwai877211f2005-11-17 13:59:38 +01002782static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2783 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
Takashi Iwai877211f2005-11-17 13:59:38 +01002785 struct snd_pcm_file *pcm_file;
2786 struct snd_pcm_substream *substream;
2787 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 snd_pcm_sframes_t result;
2789
2790 pcm_file = file->private_data;
2791 substream = pcm_file->substream;
2792 snd_assert(substream != NULL, return -ENXIO);
2793 runtime = substream->runtime;
2794 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2795 return -EBADFD;
2796 if (!frame_aligned(runtime, count))
2797 return -EINVAL;
2798 count = bytes_to_frames(runtime, count);
2799 result = snd_pcm_lib_read(substream, buf, count);
2800 if (result > 0)
2801 result = frames_to_bytes(runtime, result);
2802 return result;
2803}
2804
Takashi Iwai877211f2005-11-17 13:59:38 +01002805static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2806 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Takashi Iwai877211f2005-11-17 13:59:38 +01002808 struct snd_pcm_file *pcm_file;
2809 struct snd_pcm_substream *substream;
2810 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 snd_pcm_sframes_t result;
2812
2813 pcm_file = file->private_data;
2814 substream = pcm_file->substream;
2815 snd_assert(substream != NULL, result = -ENXIO; goto end);
2816 runtime = substream->runtime;
2817 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2818 result = -EBADFD;
2819 goto end;
2820 }
2821 if (!frame_aligned(runtime, count)) {
2822 result = -EINVAL;
2823 goto end;
2824 }
2825 count = bytes_to_frames(runtime, count);
2826 result = snd_pcm_lib_write(substream, buf, count);
2827 if (result > 0)
2828 result = frames_to_bytes(runtime, result);
2829 end:
2830 return result;
2831}
2832
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002833static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2834 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836{
Takashi Iwai877211f2005-11-17 13:59:38 +01002837 struct snd_pcm_file *pcm_file;
2838 struct snd_pcm_substream *substream;
2839 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 snd_pcm_sframes_t result;
2841 unsigned long i;
2842 void __user **bufs;
2843 snd_pcm_uframes_t frames;
2844
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002845 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 substream = pcm_file->substream;
2847 snd_assert(substream != NULL, return -ENXIO);
2848 runtime = substream->runtime;
2849 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2850 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002851 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002853 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002855 frames = bytes_to_samples(runtime, iov->iov_len);
2856 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (bufs == NULL)
2858 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002859 for (i = 0; i < nr_segs; ++i)
2860 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 result = snd_pcm_lib_readv(substream, bufs, frames);
2862 if (result > 0)
2863 result = frames_to_bytes(runtime, result);
2864 kfree(bufs);
2865 return result;
2866}
2867
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002868static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2869 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
Takashi Iwai877211f2005-11-17 13:59:38 +01002871 struct snd_pcm_file *pcm_file;
2872 struct snd_pcm_substream *substream;
2873 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 snd_pcm_sframes_t result;
2875 unsigned long i;
2876 void __user **bufs;
2877 snd_pcm_uframes_t frames;
2878
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002879 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 substream = pcm_file->substream;
2881 snd_assert(substream != NULL, result = -ENXIO; goto end);
2882 runtime = substream->runtime;
2883 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2884 result = -EBADFD;
2885 goto end;
2886 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002887 if (nr_segs > 128 || nr_segs != runtime->channels ||
2888 !frame_aligned(runtime, iov->iov_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 result = -EINVAL;
2890 goto end;
2891 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002892 frames = bytes_to_samples(runtime, iov->iov_len);
2893 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (bufs == NULL)
2895 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002896 for (i = 0; i < nr_segs; ++i)
2897 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 result = snd_pcm_lib_writev(substream, bufs, frames);
2899 if (result > 0)
2900 result = frames_to_bytes(runtime, result);
2901 kfree(bufs);
2902 end:
2903 return result;
2904}
2905
2906static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2907{
Takashi Iwai877211f2005-11-17 13:59:38 +01002908 struct snd_pcm_file *pcm_file;
2909 struct snd_pcm_substream *substream;
2910 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 unsigned int mask;
2912 snd_pcm_uframes_t avail;
2913
2914 pcm_file = file->private_data;
2915
2916 substream = pcm_file->substream;
2917 snd_assert(substream != NULL, return -ENXIO);
2918 runtime = substream->runtime;
2919
2920 poll_wait(file, &runtime->sleep, wait);
2921
2922 snd_pcm_stream_lock_irq(substream);
2923 avail = snd_pcm_playback_avail(runtime);
2924 switch (runtime->status->state) {
2925 case SNDRV_PCM_STATE_RUNNING:
2926 case SNDRV_PCM_STATE_PREPARED:
2927 case SNDRV_PCM_STATE_PAUSED:
2928 if (avail >= runtime->control->avail_min) {
2929 mask = POLLOUT | POLLWRNORM;
2930 break;
2931 }
2932 /* Fall through */
2933 case SNDRV_PCM_STATE_DRAINING:
2934 mask = 0;
2935 break;
2936 default:
2937 mask = POLLOUT | POLLWRNORM | POLLERR;
2938 break;
2939 }
2940 snd_pcm_stream_unlock_irq(substream);
2941 return mask;
2942}
2943
2944static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2945{
Takashi Iwai877211f2005-11-17 13:59:38 +01002946 struct snd_pcm_file *pcm_file;
2947 struct snd_pcm_substream *substream;
2948 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 unsigned int mask;
2950 snd_pcm_uframes_t avail;
2951
2952 pcm_file = file->private_data;
2953
2954 substream = pcm_file->substream;
2955 snd_assert(substream != NULL, return -ENXIO);
2956 runtime = substream->runtime;
2957
2958 poll_wait(file, &runtime->sleep, wait);
2959
2960 snd_pcm_stream_lock_irq(substream);
2961 avail = snd_pcm_capture_avail(runtime);
2962 switch (runtime->status->state) {
2963 case SNDRV_PCM_STATE_RUNNING:
2964 case SNDRV_PCM_STATE_PREPARED:
2965 case SNDRV_PCM_STATE_PAUSED:
2966 if (avail >= runtime->control->avail_min) {
2967 mask = POLLIN | POLLRDNORM;
2968 break;
2969 }
2970 mask = 0;
2971 break;
2972 case SNDRV_PCM_STATE_DRAINING:
2973 if (avail > 0) {
2974 mask = POLLIN | POLLRDNORM;
2975 break;
2976 }
2977 /* Fall through */
2978 default:
2979 mask = POLLIN | POLLRDNORM | POLLERR;
2980 break;
2981 }
2982 snd_pcm_stream_unlock_irq(substream);
2983 return mask;
2984}
2985
2986/*
2987 * mmap support
2988 */
2989
2990/*
2991 * Only on coherent architectures, we can mmap the status and the control records
2992 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2993 */
2994#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2995/*
2996 * mmap status record
2997 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002998static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2999 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
Takashi Iwai877211f2005-11-17 13:59:38 +01003001 struct snd_pcm_substream *substream = area->vm_private_data;
3002 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003005 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003007 vmf->page = virt_to_page(runtime->status);
3008 get_page(vmf->page);
3009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
3012static struct vm_operations_struct snd_pcm_vm_ops_status =
3013{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003014 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015};
3016
Takashi Iwai877211f2005-11-17 13:59:38 +01003017static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 struct vm_area_struct *area)
3019{
Takashi Iwai877211f2005-11-17 13:59:38 +01003020 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 long size;
3022 if (!(area->vm_flags & VM_READ))
3023 return -EINVAL;
3024 runtime = substream->runtime;
3025 snd_assert(runtime != NULL, return -EAGAIN);
3026 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003027 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return -EINVAL;
3029 area->vm_ops = &snd_pcm_vm_ops_status;
3030 area->vm_private_data = substream;
3031 area->vm_flags |= VM_RESERVED;
3032 return 0;
3033}
3034
3035/*
3036 * mmap control record
3037 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003038static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3039 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Takashi Iwai877211f2005-11-17 13:59:38 +01003041 struct snd_pcm_substream *substream = area->vm_private_data;
3042 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003045 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003047 vmf->page = virt_to_page(runtime->control);
3048 get_page(vmf->page);
3049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
3052static struct vm_operations_struct snd_pcm_vm_ops_control =
3053{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003054 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055};
3056
Takashi Iwai877211f2005-11-17 13:59:38 +01003057static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct vm_area_struct *area)
3059{
Takashi Iwai877211f2005-11-17 13:59:38 +01003060 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 long size;
3062 if (!(area->vm_flags & VM_READ))
3063 return -EINVAL;
3064 runtime = substream->runtime;
3065 snd_assert(runtime != NULL, return -EAGAIN);
3066 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003067 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return -EINVAL;
3069 area->vm_ops = &snd_pcm_vm_ops_control;
3070 area->vm_private_data = substream;
3071 area->vm_flags |= VM_RESERVED;
3072 return 0;
3073}
3074#else /* ! coherent mmap */
3075/*
3076 * don't support mmap for status and control records.
3077 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003078static int snd_pcm_mmap_status(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}
Takashi Iwai877211f2005-11-17 13:59:38 +01003083static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 struct vm_area_struct *area)
3085{
3086 return -ENXIO;
3087}
3088#endif /* coherent mmap */
3089
3090/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003091 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003093static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3094 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
Takashi Iwai877211f2005-11-17 13:59:38 +01003096 struct snd_pcm_substream *substream = area->vm_private_data;
3097 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 unsigned long offset;
3099 struct page * page;
3100 void *vaddr;
3101 size_t dma_bytes;
3102
3103 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003104 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003106 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3108 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003109 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (substream->ops->page) {
3111 page = substream->ops->page(substream, offset);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003112 if (!page)
3113 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 } else {
3115 vaddr = runtime->dma_area + offset;
3116 page = virt_to_page(vaddr);
3117 }
Nick Pigginb5810032005-10-29 18:16:12 -07003118 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003119 vmf->page = page;
3120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
3123static struct vm_operations_struct snd_pcm_vm_ops_data =
3124{
3125 .open = snd_pcm_mmap_data_open,
3126 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003127 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128};
3129
3130/*
3131 * mmap the DMA buffer on RAM
3132 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003133static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3134 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
3136 area->vm_ops = &snd_pcm_vm_ops_data;
3137 area->vm_private_data = substream;
3138 area->vm_flags |= VM_RESERVED;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003139 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return 0;
3141}
3142
3143/*
3144 * mmap the DMA buffer on I/O memory area
3145 */
3146#if SNDRV_PCM_INFO_MMAP_IOMEM
3147static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3148{
3149 .open = snd_pcm_mmap_data_open,
3150 .close = snd_pcm_mmap_data_close,
3151};
3152
Takashi Iwai877211f2005-11-17 13:59:38 +01003153int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3154 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 long size;
3157 unsigned long offset;
3158
3159#ifdef pgprot_noncached
3160 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3161#endif
3162 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3163 area->vm_private_data = substream;
3164 area->vm_flags |= VM_IO;
3165 size = area->vm_end - area->vm_start;
3166 offset = area->vm_pgoff << PAGE_SHIFT;
3167 if (io_remap_pfn_range(area, area->vm_start,
3168 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3169 size, area->vm_page_prot))
3170 return -EAGAIN;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003171 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return 0;
3173}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003174
3175EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176#endif /* SNDRV_PCM_INFO_MMAP */
3177
3178/*
3179 * mmap DMA buffer
3180 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003181int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 struct vm_area_struct *area)
3183{
Takashi Iwai877211f2005-11-17 13:59:38 +01003184 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 long size;
3186 unsigned long offset;
3187 size_t dma_bytes;
3188
3189 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3190 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3191 return -EINVAL;
3192 } else {
3193 if (!(area->vm_flags & VM_READ))
3194 return -EINVAL;
3195 }
3196 runtime = substream->runtime;
3197 snd_assert(runtime != NULL, return -EAGAIN);
3198 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3199 return -EBADFD;
3200 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3201 return -ENXIO;
3202 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3203 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3204 return -EINVAL;
3205 size = area->vm_end - area->vm_start;
3206 offset = area->vm_pgoff << PAGE_SHIFT;
3207 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3208 if ((size_t)size > dma_bytes)
3209 return -EINVAL;
3210 if (offset > dma_bytes - size)
3211 return -EINVAL;
3212
3213 if (substream->ops->mmap)
3214 return substream->ops->mmap(substream, area);
3215 else
3216 return snd_pcm_default_mmap(substream, area);
3217}
3218
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003219EXPORT_SYMBOL(snd_pcm_mmap_data);
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3222{
Takashi Iwai877211f2005-11-17 13:59:38 +01003223 struct snd_pcm_file * pcm_file;
3224 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 unsigned long offset;
3226
3227 pcm_file = file->private_data;
3228 substream = pcm_file->substream;
3229 snd_assert(substream != NULL, return -ENXIO);
3230
3231 offset = area->vm_pgoff << PAGE_SHIFT;
3232 switch (offset) {
3233 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003234 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return -ENXIO;
3236 return snd_pcm_mmap_status(substream, file, area);
3237 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003238 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return -ENXIO;
3240 return snd_pcm_mmap_control(substream, file, area);
3241 default:
3242 return snd_pcm_mmap_data(substream, file, area);
3243 }
3244 return 0;
3245}
3246
3247static int snd_pcm_fasync(int fd, struct file * file, int on)
3248{
Takashi Iwai877211f2005-11-17 13:59:38 +01003249 struct snd_pcm_file * pcm_file;
3250 struct snd_pcm_substream *substream;
3251 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 int err;
3253
3254 pcm_file = file->private_data;
3255 substream = pcm_file->substream;
3256 snd_assert(substream != NULL, return -ENXIO);
3257 runtime = substream->runtime;
3258
3259 err = fasync_helper(fd, file, on, &runtime->fasync);
3260 if (err < 0)
3261 return err;
3262 return 0;
3263}
3264
3265/*
3266 * ioctl32 compat
3267 */
3268#ifdef CONFIG_COMPAT
3269#include "pcm_compat.c"
3270#else
3271#define snd_pcm_ioctl_compat NULL
3272#endif
3273
3274/*
3275 * To be removed helpers to keep binary compatibility
3276 */
3277
Takashi Iwai59d48582005-12-01 10:51:58 +01003278#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3280#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3281
Takashi Iwai877211f2005-11-17 13:59:38 +01003282static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3283 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
3285 unsigned int i;
3286
3287 memset(params, 0, sizeof(*params));
3288 params->flags = oparams->flags;
3289 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3290 params->masks[i].bits[0] = oparams->masks[i];
3291 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3292 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3293 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3294 params->info = oparams->info;
3295 params->msbits = oparams->msbits;
3296 params->rate_num = oparams->rate_num;
3297 params->rate_den = oparams->rate_den;
3298 params->fifo_size = oparams->fifo_size;
3299}
3300
Takashi Iwai877211f2005-11-17 13:59:38 +01003301static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3302 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
3304 unsigned int i;
3305
3306 memset(oparams, 0, sizeof(*oparams));
3307 oparams->flags = params->flags;
3308 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3309 oparams->masks[i] = params->masks[i].bits[0];
3310 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3311 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3312 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3313 oparams->info = params->info;
3314 oparams->msbits = params->msbits;
3315 oparams->rate_num = params->rate_num;
3316 oparams->rate_den = params->rate_den;
3317 oparams->fifo_size = params->fifo_size;
3318}
3319
Takashi Iwai877211f2005-11-17 13:59:38 +01003320static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3321 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
Takashi Iwai877211f2005-11-17 13:59:38 +01003323 struct snd_pcm_hw_params *params;
3324 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 int err;
3326
3327 params = kmalloc(sizeof(*params), GFP_KERNEL);
3328 if (!params) {
3329 err = -ENOMEM;
3330 goto out;
3331 }
3332 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3333 if (!oparams) {
3334 err = -ENOMEM;
3335 goto out;
3336 }
3337
3338 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3339 err = -EFAULT;
3340 goto out;
3341 }
3342 snd_pcm_hw_convert_from_old_params(params, oparams);
3343 err = snd_pcm_hw_refine(substream, params);
3344 snd_pcm_hw_convert_to_old_params(oparams, params);
3345 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3346 if (!err)
3347 err = -EFAULT;
3348 }
3349out:
3350 kfree(params);
3351 kfree(oparams);
3352 return err;
3353}
3354
Takashi Iwai877211f2005-11-17 13:59:38 +01003355static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3356 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Takashi Iwai877211f2005-11-17 13:59:38 +01003358 struct snd_pcm_hw_params *params;
3359 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 int err;
3361
3362 params = kmalloc(sizeof(*params), GFP_KERNEL);
3363 if (!params) {
3364 err = -ENOMEM;
3365 goto out;
3366 }
3367 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3368 if (!oparams) {
3369 err = -ENOMEM;
3370 goto out;
3371 }
3372 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3373 err = -EFAULT;
3374 goto out;
3375 }
3376 snd_pcm_hw_convert_from_old_params(params, oparams);
3377 err = snd_pcm_hw_params(substream, params);
3378 snd_pcm_hw_convert_to_old_params(oparams, params);
3379 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3380 if (!err)
3381 err = -EFAULT;
3382 }
3383out:
3384 kfree(params);
3385 kfree(oparams);
3386 return err;
3387}
Takashi Iwai59d48582005-12-01 10:51:58 +01003388#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390/*
3391 * Register section
3392 */
3393
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003394const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003396 .owner = THIS_MODULE,
3397 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003398 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003399 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003400 .release = snd_pcm_release,
3401 .poll = snd_pcm_playback_poll,
3402 .unlocked_ioctl = snd_pcm_playback_ioctl,
3403 .compat_ioctl = snd_pcm_ioctl_compat,
3404 .mmap = snd_pcm_mmap,
3405 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 },
3407 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003408 .owner = THIS_MODULE,
3409 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003410 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003411 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003412 .release = snd_pcm_release,
3413 .poll = snd_pcm_capture_poll,
3414 .unlocked_ioctl = snd_pcm_capture_ioctl,
3415 .compat_ioctl = snd_pcm_ioctl_compat,
3416 .mmap = snd_pcm_mmap,
3417 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
3419};