blob: 439f047929e18e509961214878ffd36233d71371 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
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
22#include <sound/driver.h>
23#include <linux/mm.h>
24#include <linux/smp_lock.h>
25#include <linux/file.h>
26#include <linux/slab.h>
27#include <linux/time.h>
28#include <linux/uio.h>
29#include <sound/core.h>
30#include <sound/control.h>
31#include <sound/info.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/timer.h>
35#include <sound/minors.h>
36#include <asm/io.h>
37
38/*
39 * Compatibility
40 */
41
Takashi Iwai877211f2005-11-17 13:59:38 +010042struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned int flags;
44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
45 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010046 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
48 unsigned int rmask;
49 unsigned int cmask;
50 unsigned int info;
51 unsigned int msbits;
52 unsigned int rate_num;
53 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010054 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 unsigned char reserved[64];
56};
57
Takashi Iwai59d48582005-12-01 10:51:58 +010058#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010059#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Takashi Iwai877211f2005-11-17 13:59:38 +010062static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params_old __user * _oparams);
64static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010066#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010067static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 *
71 */
72
73DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020074EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020076static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static inline mm_segment_t snd_enter_user(void)
79{
80 mm_segment_t fs = get_fs();
81 set_fs(get_ds());
82 return fs;
83}
84
85static inline void snd_leave_user(mm_segment_t fs)
86{
87 set_fs(fs);
88}
89
90
91
Takashi Iwai877211f2005-11-17 13:59:38 +010092int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Takashi Iwai877211f2005-11-17 13:59:38 +010094 struct snd_pcm_runtime *runtime;
95 struct snd_pcm *pcm = substream->pcm;
96 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 snd_assert(substream != NULL, return -ENXIO);
99 memset(info, 0, sizeof(*info));
100 info->card = pcm->card->number;
101 info->device = pcm->device;
102 info->stream = substream->stream;
103 info->subdevice = substream->number;
104 strlcpy(info->id, pcm->id, sizeof(info->id));
105 strlcpy(info->name, pcm->name, sizeof(info->name));
106 info->dev_class = pcm->dev_class;
107 info->dev_subclass = pcm->dev_subclass;
108 info->subdevices_count = pstr->substream_count;
109 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
110 strlcpy(info->subname, substream->name, sizeof(info->subname));
111 runtime = substream->runtime;
112 /* AB: FIXME!!! This is definitely nonsense */
113 if (runtime) {
114 info->sync = runtime->sync;
115 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
116 }
117 return 0;
118}
119
Takashi Iwai877211f2005-11-17 13:59:38 +0100120int snd_pcm_info_user(struct snd_pcm_substream *substream,
121 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Takashi Iwai877211f2005-11-17 13:59:38 +0100123 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int err;
125
126 info = kmalloc(sizeof(*info), GFP_KERNEL);
127 if (! info)
128 return -ENOMEM;
129 err = snd_pcm_info(substream, info);
130 if (err >= 0) {
131 if (copy_to_user(_info, info, sizeof(*info)))
132 err = -EFAULT;
133 }
134 kfree(info);
135 return err;
136}
137
138#undef RULES_DEBUG
139
140#ifdef RULES_DEBUG
141#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
142char *snd_pcm_hw_param_names[] = {
143 HW_PARAM(ACCESS),
144 HW_PARAM(FORMAT),
145 HW_PARAM(SUBFORMAT),
146 HW_PARAM(SAMPLE_BITS),
147 HW_PARAM(FRAME_BITS),
148 HW_PARAM(CHANNELS),
149 HW_PARAM(RATE),
150 HW_PARAM(PERIOD_TIME),
151 HW_PARAM(PERIOD_SIZE),
152 HW_PARAM(PERIOD_BYTES),
153 HW_PARAM(PERIODS),
154 HW_PARAM(BUFFER_TIME),
155 HW_PARAM(BUFFER_SIZE),
156 HW_PARAM(BUFFER_BYTES),
157 HW_PARAM(TICK_TIME),
158};
159#endif
160
Takashi Iwai877211f2005-11-17 13:59:38 +0100161int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
162 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100165 struct snd_pcm_hardware *hw;
166 struct snd_interval *i = NULL;
167 struct snd_mask *m = NULL;
168 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned int rstamps[constrs->rules_num];
170 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
171 unsigned int stamp = 2;
172 int changed, again;
173
174 params->info = 0;
175 params->fifo_size = 0;
176 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
177 params->msbits = 0;
178 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
179 params->rate_num = 0;
180 params->rate_den = 0;
181 }
182
183 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
184 m = hw_param_mask(params, k);
185 if (snd_mask_empty(m))
186 return -EINVAL;
187 if (!(params->rmask & (1 << k)))
188 continue;
189#ifdef RULES_DEBUG
190 printk("%s = ", snd_pcm_hw_param_names[k]);
191 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
192#endif
193 changed = snd_mask_refine(m, constrs_mask(constrs, k));
194#ifdef RULES_DEBUG
195 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
196#endif
197 if (changed)
198 params->cmask |= 1 << k;
199 if (changed < 0)
200 return changed;
201 }
202
203 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
204 i = hw_param_interval(params, k);
205 if (snd_interval_empty(i))
206 return -EINVAL;
207 if (!(params->rmask & (1 << k)))
208 continue;
209#ifdef RULES_DEBUG
210 printk("%s = ", snd_pcm_hw_param_names[k]);
211 if (i->empty)
212 printk("empty");
213 else
214 printk("%c%u %u%c",
215 i->openmin ? '(' : '[', i->min,
216 i->max, i->openmax ? ')' : ']');
217 printk(" -> ");
218#endif
219 changed = snd_interval_refine(i, constrs_interval(constrs, k));
220#ifdef RULES_DEBUG
221 if (i->empty)
222 printk("empty\n");
223 else
224 printk("%c%u %u%c\n",
225 i->openmin ? '(' : '[', i->min,
226 i->max, i->openmax ? ')' : ']');
227#endif
228 if (changed)
229 params->cmask |= 1 << k;
230 if (changed < 0)
231 return changed;
232 }
233
234 for (k = 0; k < constrs->rules_num; k++)
235 rstamps[k] = 0;
236 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
237 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
238 do {
239 again = 0;
240 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100241 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int d;
243 int doit = 0;
244 if (r->cond && !(r->cond & params->flags))
245 continue;
246 for (d = 0; r->deps[d] >= 0; d++) {
247 if (vstamps[r->deps[d]] > rstamps[k]) {
248 doit = 1;
249 break;
250 }
251 }
252 if (!doit)
253 continue;
254#ifdef RULES_DEBUG
255 printk("Rule %d [%p]: ", k, r->func);
256 if (r->var >= 0) {
257 printk("%s = ", snd_pcm_hw_param_names[r->var]);
258 if (hw_is_mask(r->var)) {
259 m = hw_param_mask(params, r->var);
260 printk("%x", *m->bits);
261 } else {
262 i = hw_param_interval(params, r->var);
263 if (i->empty)
264 printk("empty");
265 else
266 printk("%c%u %u%c",
267 i->openmin ? '(' : '[', i->min,
268 i->max, i->openmax ? ')' : ']');
269 }
270 }
271#endif
272 changed = r->func(params, r);
273#ifdef RULES_DEBUG
274 if (r->var >= 0) {
275 printk(" -> ");
276 if (hw_is_mask(r->var))
277 printk("%x", *m->bits);
278 else {
279 if (i->empty)
280 printk("empty");
281 else
282 printk("%c%u %u%c",
283 i->openmin ? '(' : '[', i->min,
284 i->max, i->openmax ? ')' : ']');
285 }
286 }
287 printk("\n");
288#endif
289 rstamps[k] = stamp;
290 if (changed && r->var >= 0) {
291 params->cmask |= (1 << r->var);
292 vstamps[r->var] = stamp;
293 again = 1;
294 }
295 if (changed < 0)
296 return changed;
297 stamp++;
298 }
299 } while (again);
300 if (!params->msbits) {
301 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
302 if (snd_interval_single(i))
303 params->msbits = snd_interval_value(i);
304 }
305
306 if (!params->rate_den) {
307 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
308 if (snd_interval_single(i)) {
309 params->rate_num = snd_interval_value(i);
310 params->rate_den = 1;
311 }
312 }
313
314 hw = &substream->runtime->hw;
315 if (!params->info)
316 params->info = hw->info;
317 if (!params->fifo_size)
318 params->fifo_size = hw->fifo_size;
319 params->rmask = 0;
320 return 0;
321}
322
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200323EXPORT_SYMBOL(snd_pcm_hw_refine);
324
Takashi Iwai877211f2005-11-17 13:59:38 +0100325static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
326 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Takashi Iwai877211f2005-11-17 13:59:38 +0100328 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int err;
330
331 params = kmalloc(sizeof(*params), GFP_KERNEL);
332 if (!params) {
333 err = -ENOMEM;
334 goto out;
335 }
336 if (copy_from_user(params, _params, sizeof(*params))) {
337 err = -EFAULT;
338 goto out;
339 }
340 err = snd_pcm_hw_refine(substream, params);
341 if (copy_to_user(_params, params, sizeof(*params))) {
342 if (!err)
343 err = -EFAULT;
344 }
345out:
346 kfree(params);
347 return err;
348}
349
Takashi Iwai877211f2005-11-17 13:59:38 +0100350static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
351 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Takashi Iwai877211f2005-11-17 13:59:38 +0100353 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int err;
355 unsigned int bits;
356 snd_pcm_uframes_t frames;
357
358 snd_assert(substream != NULL, return -ENXIO);
359 runtime = substream->runtime;
360 snd_assert(runtime != NULL, return -ENXIO);
361 snd_pcm_stream_lock_irq(substream);
362 switch (runtime->status->state) {
363 case SNDRV_PCM_STATE_OPEN:
364 case SNDRV_PCM_STATE_SETUP:
365 case SNDRV_PCM_STATE_PREPARED:
366 break;
367 default:
368 snd_pcm_stream_unlock_irq(substream);
369 return -EBADFD;
370 }
371 snd_pcm_stream_unlock_irq(substream);
372#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
373 if (!substream->oss.oss)
374#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200375 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return -EBADFD;
377
378 params->rmask = ~0U;
379 err = snd_pcm_hw_refine(substream, params);
380 if (err < 0)
381 goto _error;
382
383 err = snd_pcm_hw_params_choose(substream, params);
384 if (err < 0)
385 goto _error;
386
387 if (substream->ops->hw_params != NULL) {
388 err = substream->ops->hw_params(substream, params);
389 if (err < 0)
390 goto _error;
391 }
392
393 runtime->access = params_access(params);
394 runtime->format = params_format(params);
395 runtime->subformat = params_subformat(params);
396 runtime->channels = params_channels(params);
397 runtime->rate = params_rate(params);
398 runtime->period_size = params_period_size(params);
399 runtime->periods = params_periods(params);
400 runtime->buffer_size = params_buffer_size(params);
401 runtime->tick_time = params_tick_time(params);
402 runtime->info = params->info;
403 runtime->rate_num = params->rate_num;
404 runtime->rate_den = params->rate_den;
405
406 bits = snd_pcm_format_physical_width(runtime->format);
407 runtime->sample_bits = bits;
408 bits *= runtime->channels;
409 runtime->frame_bits = bits;
410 frames = 1;
411 while (bits % 8 != 0) {
412 bits *= 2;
413 frames *= 2;
414 }
415 runtime->byte_align = bits / 8;
416 runtime->min_align = frames;
417
418 /* Default sw params */
419 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
420 runtime->period_step = 1;
421 runtime->sleep_min = 0;
422 runtime->control->avail_min = runtime->period_size;
423 runtime->xfer_align = runtime->period_size;
424 runtime->start_threshold = 1;
425 runtime->stop_threshold = runtime->buffer_size;
426 runtime->silence_threshold = 0;
427 runtime->silence_size = 0;
428 runtime->boundary = runtime->buffer_size;
429 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
430 runtime->boundary *= 2;
431
432 snd_pcm_timer_resolution_change(substream);
433 runtime->status->state = SNDRV_PCM_STATE_SETUP;
434 return 0;
435 _error:
436 /* hardware might be unuseable from this time,
437 so we force application to retry to set
438 the correct hardware parameter settings */
439 runtime->status->state = SNDRV_PCM_STATE_OPEN;
440 if (substream->ops->hw_free != NULL)
441 substream->ops->hw_free(substream);
442 return err;
443}
444
Takashi Iwai877211f2005-11-17 13:59:38 +0100445static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
446 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Takashi Iwai877211f2005-11-17 13:59:38 +0100448 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int err;
450
451 params = kmalloc(sizeof(*params), GFP_KERNEL);
452 if (!params) {
453 err = -ENOMEM;
454 goto out;
455 }
456 if (copy_from_user(params, _params, sizeof(*params))) {
457 err = -EFAULT;
458 goto out;
459 }
460 err = snd_pcm_hw_params(substream, params);
461 if (copy_to_user(_params, params, sizeof(*params))) {
462 if (!err)
463 err = -EFAULT;
464 }
465out:
466 kfree(params);
467 return err;
468}
469
Takashi Iwai877211f2005-11-17 13:59:38 +0100470static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Takashi Iwai877211f2005-11-17 13:59:38 +0100472 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 int result = 0;
474
475 snd_assert(substream != NULL, return -ENXIO);
476 runtime = substream->runtime;
477 snd_assert(runtime != NULL, return -ENXIO);
478 snd_pcm_stream_lock_irq(substream);
479 switch (runtime->status->state) {
480 case SNDRV_PCM_STATE_SETUP:
481 case SNDRV_PCM_STATE_PREPARED:
482 break;
483 default:
484 snd_pcm_stream_unlock_irq(substream);
485 return -EBADFD;
486 }
487 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200488 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -EBADFD;
490 if (substream->ops->hw_free)
491 result = substream->ops->hw_free(substream);
492 runtime->status->state = SNDRV_PCM_STATE_OPEN;
493 return result;
494}
495
Takashi Iwai877211f2005-11-17 13:59:38 +0100496static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
497 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Takashi Iwai877211f2005-11-17 13:59:38 +0100499 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 snd_assert(substream != NULL, return -ENXIO);
502 runtime = substream->runtime;
503 snd_assert(runtime != NULL, return -ENXIO);
504 snd_pcm_stream_lock_irq(substream);
505 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
506 snd_pcm_stream_unlock_irq(substream);
507 return -EBADFD;
508 }
509 snd_pcm_stream_unlock_irq(substream);
510
511 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
512 return -EINVAL;
513 if (params->avail_min == 0)
514 return -EINVAL;
515 if (params->xfer_align == 0 ||
516 params->xfer_align % runtime->min_align != 0)
517 return -EINVAL;
518 if (params->silence_size >= runtime->boundary) {
519 if (params->silence_threshold != 0)
520 return -EINVAL;
521 } else {
522 if (params->silence_size > params->silence_threshold)
523 return -EINVAL;
524 if (params->silence_threshold > runtime->buffer_size)
525 return -EINVAL;
526 }
527 snd_pcm_stream_lock_irq(substream);
528 runtime->tstamp_mode = params->tstamp_mode;
529 runtime->sleep_min = params->sleep_min;
530 runtime->period_step = params->period_step;
531 runtime->control->avail_min = params->avail_min;
532 runtime->start_threshold = params->start_threshold;
533 runtime->stop_threshold = params->stop_threshold;
534 runtime->silence_threshold = params->silence_threshold;
535 runtime->silence_size = params->silence_size;
536 runtime->xfer_align = params->xfer_align;
537 params->boundary = runtime->boundary;
538 if (snd_pcm_running(substream)) {
539 if (runtime->sleep_min)
540 snd_pcm_tick_prepare(substream);
541 else
542 snd_pcm_tick_set(substream, 0);
543 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
544 runtime->silence_size > 0)
545 snd_pcm_playback_silence(substream, ULONG_MAX);
546 wake_up(&runtime->sleep);
547 }
548 snd_pcm_stream_unlock_irq(substream);
549 return 0;
550}
551
Takashi Iwai877211f2005-11-17 13:59:38 +0100552static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
553 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Takashi Iwai877211f2005-11-17 13:59:38 +0100555 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int err;
557 if (copy_from_user(&params, _params, sizeof(params)))
558 return -EFAULT;
559 err = snd_pcm_sw_params(substream, &params);
560 if (copy_to_user(_params, &params, sizeof(params)))
561 return -EFAULT;
562 return err;
563}
564
Takashi Iwai877211f2005-11-17 13:59:38 +0100565int snd_pcm_status(struct snd_pcm_substream *substream,
566 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Takashi Iwai877211f2005-11-17 13:59:38 +0100568 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 snd_pcm_stream_lock_irq(substream);
571 status->state = runtime->status->state;
572 status->suspended_state = runtime->status->suspended_state;
573 if (status->state == SNDRV_PCM_STATE_OPEN)
574 goto _end;
575 status->trigger_tstamp = runtime->trigger_tstamp;
576 if (snd_pcm_running(substream)) {
577 snd_pcm_update_hw_ptr(substream);
578 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
579 status->tstamp = runtime->status->tstamp;
580 else
Takashi Iwai07799e72005-10-10 11:49:49 +0200581 getnstimeofday(&status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 } else
Takashi Iwai07799e72005-10-10 11:49:49 +0200583 getnstimeofday(&status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 status->appl_ptr = runtime->control->appl_ptr;
585 status->hw_ptr = runtime->status->hw_ptr;
586 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
587 status->avail = snd_pcm_playback_avail(runtime);
588 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
589 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
590 status->delay = runtime->buffer_size - status->avail;
591 else
592 status->delay = 0;
593 } else {
594 status->avail = snd_pcm_capture_avail(runtime);
595 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
596 status->delay = status->avail;
597 else
598 status->delay = 0;
599 }
600 status->avail_max = runtime->avail_max;
601 status->overrange = runtime->overrange;
602 runtime->avail_max = 0;
603 runtime->overrange = 0;
604 _end:
605 snd_pcm_stream_unlock_irq(substream);
606 return 0;
607}
608
Takashi Iwai877211f2005-11-17 13:59:38 +0100609static int snd_pcm_status_user(struct snd_pcm_substream *substream,
610 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwai877211f2005-11-17 13:59:38 +0100612 struct snd_pcm_status status;
613 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int res;
615
616 snd_assert(substream != NULL, return -ENXIO);
617 runtime = substream->runtime;
618 memset(&status, 0, sizeof(status));
619 res = snd_pcm_status(substream, &status);
620 if (res < 0)
621 return res;
622 if (copy_to_user(_status, &status, sizeof(status)))
623 return -EFAULT;
624 return 0;
625}
626
Takashi Iwai877211f2005-11-17 13:59:38 +0100627static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
628 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Takashi Iwai877211f2005-11-17 13:59:38 +0100630 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 unsigned int channel;
632
633 snd_assert(substream != NULL, return -ENXIO);
634 channel = info->channel;
635 runtime = substream->runtime;
636 snd_pcm_stream_lock_irq(substream);
637 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
638 snd_pcm_stream_unlock_irq(substream);
639 return -EBADFD;
640 }
641 snd_pcm_stream_unlock_irq(substream);
642 if (channel >= runtime->channels)
643 return -EINVAL;
644 memset(info, 0, sizeof(*info));
645 info->channel = channel;
646 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
647}
648
Takashi Iwai877211f2005-11-17 13:59:38 +0100649static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
650 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Takashi Iwai877211f2005-11-17 13:59:38 +0100652 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int res;
654
655 if (copy_from_user(&info, _info, sizeof(info)))
656 return -EFAULT;
657 res = snd_pcm_channel_info(substream, &info);
658 if (res < 0)
659 return res;
660 if (copy_to_user(_info, &info, sizeof(info)))
661 return -EFAULT;
662 return 0;
663}
664
Takashi Iwai877211f2005-11-17 13:59:38 +0100665static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai877211f2005-11-17 13:59:38 +0100667 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (runtime->trigger_master == NULL)
669 return;
670 if (runtime->trigger_master == substream) {
Takashi Iwai07799e72005-10-10 11:49:49 +0200671 getnstimeofday(&runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 } else {
673 snd_pcm_trigger_tstamp(runtime->trigger_master);
674 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
675 }
676 runtime->trigger_master = NULL;
677}
678
679struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100680 int (*pre_action)(struct snd_pcm_substream *substream, int state);
681 int (*do_action)(struct snd_pcm_substream *substream, int state);
682 void (*undo_action)(struct snd_pcm_substream *substream, int state);
683 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684};
685
686/*
687 * this functions is core for handling of linked stream
688 * Note: the stream state might be changed also on failure
689 * Note2: call with calling stream lock + link lock
690 */
691static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100692 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int state, int do_lock)
694{
695 struct list_head *pos;
Takashi Iwai877211f2005-11-17 13:59:38 +0100696 struct snd_pcm_substream *s = NULL;
697 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int res = 0;
699
700 snd_pcm_group_for_each(pos, substream) {
701 s = snd_pcm_group_substream_entry(pos);
702 if (do_lock && s != substream)
703 spin_lock(&s->self_group.lock);
704 res = ops->pre_action(s, state);
705 if (res < 0)
706 goto _unlock;
707 }
708 snd_pcm_group_for_each(pos, substream) {
709 s = snd_pcm_group_substream_entry(pos);
710 res = ops->do_action(s, state);
711 if (res < 0) {
712 if (ops->undo_action) {
713 snd_pcm_group_for_each(pos, substream) {
714 s1 = snd_pcm_group_substream_entry(pos);
715 if (s1 == s) /* failed stream */
716 break;
717 ops->undo_action(s1, state);
718 }
719 }
720 s = NULL; /* unlock all */
721 goto _unlock;
722 }
723 }
724 snd_pcm_group_for_each(pos, substream) {
725 s = snd_pcm_group_substream_entry(pos);
726 ops->post_action(s, state);
727 }
728 _unlock:
729 if (do_lock) {
730 /* unlock streams */
731 snd_pcm_group_for_each(pos, substream) {
732 s1 = snd_pcm_group_substream_entry(pos);
733 if (s1 != substream)
734 spin_unlock(&s1->self_group.lock);
735 if (s1 == s) /* end */
736 break;
737 }
738 }
739 return res;
740}
741
742/*
743 * Note: call with stream lock
744 */
745static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100746 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int state)
748{
749 int res;
750
751 res = ops->pre_action(substream, state);
752 if (res < 0)
753 return res;
754 res = ops->do_action(substream, state);
755 if (res == 0)
756 ops->post_action(substream, state);
757 else if (ops->undo_action)
758 ops->undo_action(substream, state);
759 return res;
760}
761
762/*
763 * Note: call with stream lock
764 */
765static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100766 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int state)
768{
769 int res;
770
771 if (snd_pcm_stream_linked(substream)) {
772 if (!spin_trylock(&substream->group->lock)) {
773 spin_unlock(&substream->self_group.lock);
774 spin_lock(&substream->group->lock);
775 spin_lock(&substream->self_group.lock);
776 }
777 res = snd_pcm_action_group(ops, substream, state, 1);
778 spin_unlock(&substream->group->lock);
779 } else {
780 res = snd_pcm_action_single(ops, substream, state);
781 }
782 return res;
783}
784
785/*
786 * Note: don't use any locks before
787 */
788static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100789 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 int state)
791{
792 int res;
793
794 read_lock_irq(&snd_pcm_link_rwlock);
795 if (snd_pcm_stream_linked(substream)) {
796 spin_lock(&substream->group->lock);
797 spin_lock(&substream->self_group.lock);
798 res = snd_pcm_action_group(ops, substream, state, 1);
799 spin_unlock(&substream->self_group.lock);
800 spin_unlock(&substream->group->lock);
801 } else {
802 spin_lock(&substream->self_group.lock);
803 res = snd_pcm_action_single(ops, substream, state);
804 spin_unlock(&substream->self_group.lock);
805 }
806 read_unlock_irq(&snd_pcm_link_rwlock);
807 return res;
808}
809
810/*
811 */
812static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100813 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int state)
815{
816 int res;
817
818 down_read(&snd_pcm_link_rwsem);
819 if (snd_pcm_stream_linked(substream))
820 res = snd_pcm_action_group(ops, substream, state, 0);
821 else
822 res = snd_pcm_action_single(ops, substream, state);
823 up_read(&snd_pcm_link_rwsem);
824 return res;
825}
826
827/*
828 * start callbacks
829 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100830static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Takashi Iwai877211f2005-11-17 13:59:38 +0100832 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
834 return -EBADFD;
835 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
836 !snd_pcm_playback_data(substream))
837 return -EPIPE;
838 runtime->trigger_master = substream;
839 return 0;
840}
841
Takashi Iwai877211f2005-11-17 13:59:38 +0100842static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 if (substream->runtime->trigger_master != substream)
845 return 0;
846 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
847}
848
Takashi Iwai877211f2005-11-17 13:59:38 +0100849static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 if (substream->runtime->trigger_master == substream)
852 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
853}
854
Takashi Iwai877211f2005-11-17 13:59:38 +0100855static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Takashi Iwai877211f2005-11-17 13:59:38 +0100857 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 snd_pcm_trigger_tstamp(substream);
859 runtime->status->state = state;
860 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
861 runtime->silence_size > 0)
862 snd_pcm_playback_silence(substream, ULONG_MAX);
863 if (runtime->sleep_min)
864 snd_pcm_tick_prepare(substream);
865 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100866 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
867 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870static struct action_ops snd_pcm_action_start = {
871 .pre_action = snd_pcm_pre_start,
872 .do_action = snd_pcm_do_start,
873 .undo_action = snd_pcm_undo_start,
874 .post_action = snd_pcm_post_start
875};
876
877/**
878 * snd_pcm_start
Takashi Iwaidf8db932005-09-07 13:38:19 +0200879 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 *
881 * Start all linked streams.
882 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100883int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Takashi Iwai877211f2005-11-17 13:59:38 +0100885 return snd_pcm_action(&snd_pcm_action_start, substream,
886 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889/*
890 * stop callbacks
891 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100892static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Takashi Iwai877211f2005-11-17 13:59:38 +0100894 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
896 return -EBADFD;
897 runtime->trigger_master = substream;
898 return 0;
899}
900
Takashi Iwai877211f2005-11-17 13:59:38 +0100901static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 if (substream->runtime->trigger_master == substream &&
904 snd_pcm_running(substream))
905 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
906 return 0; /* unconditonally stop all substreams */
907}
908
Takashi Iwai877211f2005-11-17 13:59:38 +0100909static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwai877211f2005-11-17 13:59:38 +0100911 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (runtime->status->state != state) {
913 snd_pcm_trigger_tstamp(substream);
914 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100915 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
916 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 runtime->status->state = state;
918 snd_pcm_tick_set(substream, 0);
919 }
920 wake_up(&runtime->sleep);
921}
922
923static struct action_ops snd_pcm_action_stop = {
924 .pre_action = snd_pcm_pre_stop,
925 .do_action = snd_pcm_do_stop,
926 .post_action = snd_pcm_post_stop
927};
928
929/**
930 * snd_pcm_stop
Takashi Iwaidf8db932005-09-07 13:38:19 +0200931 * @substream: the PCM substream instance
932 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 *
934 * Try to stop all running streams in the substream group.
935 * The state of each stream is changed to the given value after that unconditionally.
936 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100937int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
940}
941
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200942EXPORT_SYMBOL(snd_pcm_stop);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/**
945 * snd_pcm_drain_done
Takashi Iwaidf8db932005-09-07 13:38:19 +0200946 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 *
948 * Stop the DMA only when the given stream is playback.
949 * The state is changed to SETUP.
950 * Unlike snd_pcm_stop(), this affects only the given stream.
951 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100952int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Takashi Iwai877211f2005-11-17 13:59:38 +0100954 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
955 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958/*
959 * pause callbacks
960 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100961static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Takashi Iwai877211f2005-11-17 13:59:38 +0100963 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
965 return -ENOSYS;
966 if (push) {
967 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
968 return -EBADFD;
969 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
970 return -EBADFD;
971 runtime->trigger_master = substream;
972 return 0;
973}
974
Takashi Iwai877211f2005-11-17 13:59:38 +0100975static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 if (substream->runtime->trigger_master != substream)
978 return 0;
979 return substream->ops->trigger(substream,
980 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
981 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
982}
983
Takashi Iwai877211f2005-11-17 13:59:38 +0100984static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 if (substream->runtime->trigger_master == substream)
987 substream->ops->trigger(substream,
988 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
989 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
990}
991
Takashi Iwai877211f2005-11-17 13:59:38 +0100992static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Takashi Iwai877211f2005-11-17 13:59:38 +0100994 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 snd_pcm_trigger_tstamp(substream);
996 if (push) {
997 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
998 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100999 snd_timer_notify(substream->timer,
1000 SNDRV_TIMER_EVENT_MPAUSE,
1001 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 snd_pcm_tick_set(substream, 0);
1003 wake_up(&runtime->sleep);
1004 } else {
1005 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1006 if (runtime->sleep_min)
1007 snd_pcm_tick_prepare(substream);
1008 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;
1062 snd_pcm_tick_set(substream, 0);
1063 wake_up(&runtime->sleep);
1064}
1065
1066static struct action_ops snd_pcm_action_suspend = {
1067 .pre_action = snd_pcm_pre_suspend,
1068 .do_action = snd_pcm_do_suspend,
1069 .post_action = snd_pcm_post_suspend
1070};
1071
1072/**
1073 * snd_pcm_suspend
Takashi Iwaidf8db932005-09-07 13:38:19 +02001074 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
1076 * Trigger SUSPEND to all linked streams.
1077 * After this call, all streams are changed to SUSPENDED state.
1078 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001079int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 int err;
1082 unsigned long flags;
1083
Takashi Iwai603bf522005-11-17 15:59:14 +01001084 if (! substream)
1085 return 0;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 snd_pcm_stream_lock_irqsave(substream, flags);
1088 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1089 snd_pcm_stream_unlock_irqrestore(substream, flags);
1090 return err;
1091}
1092
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001093EXPORT_SYMBOL(snd_pcm_suspend);
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095/**
1096 * snd_pcm_suspend_all
Takashi Iwaidf8db932005-09-07 13:38:19 +02001097 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 *
1099 * Trigger SUSPEND to all substreams in the given pcm.
1100 * After this call, all streams are changed to SUSPENDED state.
1101 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001102int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Takashi Iwai877211f2005-11-17 13:59:38 +01001104 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int stream, err = 0;
1106
Takashi Iwai603bf522005-11-17 15:59:14 +01001107 if (! pcm)
1108 return 0;
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001111 for (substream = pcm->streams[stream].substream;
1112 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /* FIXME: the open/close code should lock this as well */
1114 if (substream->runtime == NULL)
1115 continue;
1116 err = snd_pcm_suspend(substream);
1117 if (err < 0 && err != -EBUSY)
1118 return err;
1119 }
1120 }
1121 return 0;
1122}
1123
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001124EXPORT_SYMBOL(snd_pcm_suspend_all);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/* resume */
1127
Takashi Iwai877211f2005-11-17 13:59:38 +01001128static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Takashi Iwai877211f2005-11-17 13:59:38 +01001130 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1132 return -ENOSYS;
1133 runtime->trigger_master = substream;
1134 return 0;
1135}
1136
Takashi Iwai877211f2005-11-17 13:59:38 +01001137static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Takashi Iwai877211f2005-11-17 13:59:38 +01001139 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (runtime->trigger_master != substream)
1141 return 0;
1142 /* DMA not running previously? */
1143 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1144 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1145 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1146 return 0;
1147 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1148}
1149
Takashi Iwai877211f2005-11-17 13:59:38 +01001150static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 if (substream->runtime->trigger_master == substream &&
1153 snd_pcm_running(substream))
1154 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1155}
1156
Takashi Iwai877211f2005-11-17 13:59:38 +01001157static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Takashi Iwai877211f2005-11-17 13:59:38 +01001159 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 snd_pcm_trigger_tstamp(substream);
1161 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001162 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1163 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 runtime->status->state = runtime->status->suspended_state;
1165 if (runtime->sleep_min)
1166 snd_pcm_tick_prepare(substream);
1167}
1168
1169static struct action_ops snd_pcm_action_resume = {
1170 .pre_action = snd_pcm_pre_resume,
1171 .do_action = snd_pcm_do_resume,
1172 .undo_action = snd_pcm_undo_resume,
1173 .post_action = snd_pcm_post_resume
1174};
1175
Takashi Iwai877211f2005-11-17 13:59:38 +01001176static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Takashi Iwai877211f2005-11-17 13:59:38 +01001178 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int res;
1180
1181 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001182 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1184 snd_power_unlock(card);
1185 return res;
1186}
1187
1188#else
1189
Takashi Iwai877211f2005-11-17 13:59:38 +01001190static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 return -ENOSYS;
1193}
1194
1195#endif /* CONFIG_PM */
1196
1197/*
1198 * xrun ioctl
1199 *
1200 * Change the RUNNING stream(s) to XRUN state.
1201 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001202static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Takashi Iwai877211f2005-11-17 13:59:38 +01001204 struct snd_card *card = substream->pcm->card;
1205 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int result;
1207
1208 snd_power_lock(card);
1209 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001210 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (result < 0)
1212 goto _unlock;
1213 }
1214
1215 snd_pcm_stream_lock_irq(substream);
1216 switch (runtime->status->state) {
1217 case SNDRV_PCM_STATE_XRUN:
1218 result = 0; /* already there */
1219 break;
1220 case SNDRV_PCM_STATE_RUNNING:
1221 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1222 break;
1223 default:
1224 result = -EBADFD;
1225 }
1226 snd_pcm_stream_unlock_irq(substream);
1227 _unlock:
1228 snd_power_unlock(card);
1229 return result;
1230}
1231
1232/*
1233 * reset ioctl
1234 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001235static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Takashi Iwai877211f2005-11-17 13:59:38 +01001237 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 switch (runtime->status->state) {
1239 case SNDRV_PCM_STATE_RUNNING:
1240 case SNDRV_PCM_STATE_PREPARED:
1241 case SNDRV_PCM_STATE_PAUSED:
1242 case SNDRV_PCM_STATE_SUSPENDED:
1243 return 0;
1244 default:
1245 return -EBADFD;
1246 }
1247}
1248
Takashi Iwai877211f2005-11-17 13:59:38 +01001249static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Takashi Iwai877211f2005-11-17 13:59:38 +01001251 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1253 if (err < 0)
1254 return err;
1255 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1256 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001257 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1258 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 runtime->silence_start = runtime->status->hw_ptr;
1260 runtime->silence_filled = 0;
1261 return 0;
1262}
1263
Takashi Iwai877211f2005-11-17 13:59:38 +01001264static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Takashi Iwai877211f2005-11-17 13:59:38 +01001266 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 runtime->control->appl_ptr = runtime->status->hw_ptr;
1268 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1269 runtime->silence_size > 0)
1270 snd_pcm_playback_silence(substream, ULONG_MAX);
1271}
1272
1273static struct action_ops snd_pcm_action_reset = {
1274 .pre_action = snd_pcm_pre_reset,
1275 .do_action = snd_pcm_do_reset,
1276 .post_action = snd_pcm_post_reset
1277};
1278
Takashi Iwai877211f2005-11-17 13:59:38 +01001279static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1282}
1283
1284/*
1285 * prepare ioctl
1286 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001287/* we use the second argument for updating f_flags */
1288static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1289 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Takashi Iwai877211f2005-11-17 13:59:38 +01001291 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1293 return -EBADFD;
1294 if (snd_pcm_running(substream))
1295 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001296 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return 0;
1298}
1299
Takashi Iwai877211f2005-11-17 13:59:38 +01001300static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 int err;
1303 err = substream->ops->prepare(substream);
1304 if (err < 0)
1305 return err;
1306 return snd_pcm_do_reset(substream, 0);
1307}
1308
Takashi Iwai877211f2005-11-17 13:59:38 +01001309static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Takashi Iwai877211f2005-11-17 13:59:38 +01001311 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 runtime->control->appl_ptr = runtime->status->hw_ptr;
1313 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1314}
1315
1316static struct action_ops snd_pcm_action_prepare = {
1317 .pre_action = snd_pcm_pre_prepare,
1318 .do_action = snd_pcm_do_prepare,
1319 .post_action = snd_pcm_post_prepare
1320};
1321
1322/**
1323 * snd_pcm_prepare
Takashi Iwaidf8db932005-09-07 13:38:19 +02001324 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001325 * @file: file to refer f_flags
Takashi Iwaidf8db932005-09-07 13:38:19 +02001326 *
1327 * Prepare the PCM substream to be triggerable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001329static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1330 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001333 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001334 int f_flags;
1335
1336 if (file)
1337 f_flags = file->f_flags;
1338 else
1339 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001342 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001343 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1344 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 snd_power_unlock(card);
1346 return res;
1347}
1348
1349/*
1350 * drain ioctl
1351 */
1352
Takashi Iwai877211f2005-11-17 13:59:38 +01001353static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001355 if (substream->f_flags & O_NONBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EAGAIN;
1357 substream->runtime->trigger_master = substream;
1358 return 0;
1359}
1360
Takashi Iwai877211f2005-11-17 13:59:38 +01001361static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Takashi Iwai877211f2005-11-17 13:59:38 +01001363 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1365 switch (runtime->status->state) {
1366 case SNDRV_PCM_STATE_PREPARED:
1367 /* start playback stream if possible */
1368 if (! snd_pcm_playback_empty(substream)) {
1369 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1370 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1371 }
1372 break;
1373 case SNDRV_PCM_STATE_RUNNING:
1374 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1375 break;
1376 default:
1377 break;
1378 }
1379 } else {
1380 /* stop running stream */
1381 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1382 int state = snd_pcm_capture_avail(runtime) > 0 ?
1383 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1384 snd_pcm_do_stop(substream, state);
1385 snd_pcm_post_stop(substream, state);
1386 }
1387 }
1388 return 0;
1389}
1390
Takashi Iwai877211f2005-11-17 13:59:38 +01001391static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393}
1394
1395static struct action_ops snd_pcm_action_drain_init = {
1396 .pre_action = snd_pcm_pre_drain_init,
1397 .do_action = snd_pcm_do_drain_init,
1398 .post_action = snd_pcm_post_drain_init
1399};
1400
1401struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001402 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 wait_queue_t wait;
1404 snd_pcm_uframes_t stop_threshold;
1405};
1406
Takashi Iwai877211f2005-11-17 13:59:38 +01001407static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409/*
1410 * Drain the stream(s).
1411 * When the substream is linked, sync until the draining of all playback streams
1412 * is finished.
1413 * After this call, all streams are supposed to be either SETUP or DRAINING
1414 * (capture only) state.
1415 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001416static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Takashi Iwai877211f2005-11-17 13:59:38 +01001418 struct snd_card *card;
1419 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct list_head *pos;
1421 int result = 0;
1422 int i, num_drecs;
1423 struct drain_rec *drec, drec_tmp, *d;
1424
1425 snd_assert(substream != NULL, return -ENXIO);
1426 card = substream->pcm->card;
1427 runtime = substream->runtime;
1428
1429 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1430 return -EBADFD;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 snd_power_lock(card);
1433 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001434 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001435 if (result < 0) {
1436 snd_power_unlock(card);
1437 return result;
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
1441 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001442 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (snd_pcm_stream_linked(substream)) {
1444 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1445 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001446 up_read(&snd_pcm_link_rwsem);
1447 snd_power_unlock(card);
1448 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 } else
1451 drec = &drec_tmp;
1452
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001453 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 num_drecs = 0;
1455 snd_pcm_group_for_each(pos, substream) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001456 struct snd_pcm_substream *s = snd_pcm_group_substream_entry(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1459 d = &drec[num_drecs++];
1460 d->substream = s;
1461 init_waitqueue_entry(&d->wait, current);
1462 add_wait_queue(&runtime->sleep, &d->wait);
1463 /* stop_threshold fixup to avoid endless loop when
1464 * stop_threshold > buffer_size
1465 */
1466 d->stop_threshold = runtime->stop_threshold;
1467 if (runtime->stop_threshold > runtime->buffer_size)
1468 runtime->stop_threshold = runtime->buffer_size;
1469 }
1470 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001471 up_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001472
1473 snd_pcm_stream_lock_irq(substream);
1474 /* resume pause */
1475 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1476 snd_pcm_pause(substream, 0);
1477
1478 /* pre-start/stop - all running streams are changed to DRAINING state */
1479 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1480 if (result < 0) {
1481 snd_pcm_stream_unlock_irq(substream);
1482 goto _error;
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 for (;;) {
1486 long tout;
1487 if (signal_pending(current)) {
1488 result = -ERESTARTSYS;
1489 break;
1490 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001491 /* all finished? */
1492 for (i = 0; i < num_drecs; i++) {
1493 runtime = drec[i].substream->runtime;
1494 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1495 break;
1496 }
1497 if (i == num_drecs)
1498 break; /* yes, all drained */
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 set_current_state(TASK_INTERRUPTIBLE);
1501 snd_pcm_stream_unlock_irq(substream);
1502 snd_power_unlock(card);
1503 tout = schedule_timeout(10 * HZ);
1504 snd_power_lock(card);
1505 snd_pcm_stream_lock_irq(substream);
1506 if (tout == 0) {
1507 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1508 result = -ESTRPIPE;
1509 else {
1510 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1511 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1512 result = -EIO;
1513 }
1514 break;
1515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001517
1518 snd_pcm_stream_unlock_irq(substream);
1519
1520 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 for (i = 0; i < num_drecs; i++) {
1522 d = &drec[i];
1523 runtime = d->substream->runtime;
1524 remove_wait_queue(&runtime->sleep, &d->wait);
1525 runtime->stop_threshold = d->stop_threshold;
1526 }
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (drec != &drec_tmp)
1529 kfree(drec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 return result;
1533}
1534
1535/*
1536 * drop ioctl
1537 *
1538 * Immediately put all linked substreams into SETUP state.
1539 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001540static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Takashi Iwai877211f2005-11-17 13:59:38 +01001542 struct snd_pcm_runtime *runtime;
1543 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int result = 0;
1545
1546 snd_assert(substream != NULL, return -ENXIO);
1547 runtime = substream->runtime;
1548 card = substream->pcm->card;
1549
1550 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1551 return -EBADFD;
1552
1553 snd_power_lock(card);
1554 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001555 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (result < 0)
1557 goto _unlock;
1558 }
1559
1560 snd_pcm_stream_lock_irq(substream);
1561 /* resume pause */
1562 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1563 snd_pcm_pause(substream, 0);
1564
1565 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1566 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1567 snd_pcm_stream_unlock_irq(substream);
1568 _unlock:
1569 snd_power_unlock(card);
1570 return result;
1571}
1572
1573
1574/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575static struct file *snd_pcm_file_fd(int fd)
1576{
1577 struct file *file;
1578 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001579 unsigned int minor;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 file = fget(fd);
1582 if (!file)
1583 return NULL;
1584 inode = file->f_dentry->d_inode;
1585 if (!S_ISCHR(inode->i_mode) ||
1586 imajor(inode) != snd_major) {
1587 fput(file);
1588 return NULL;
1589 }
1590 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001591 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1592 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 fput(file);
1594 return NULL;
1595 }
1596 return file;
1597}
1598
1599/*
1600 * PCM link handling
1601 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001602static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 int res = 0;
1605 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001606 struct snd_pcm_file *pcm_file;
1607 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 file = snd_pcm_file_fd(fd);
1610 if (!file)
1611 return -EBADFD;
1612 pcm_file = file->private_data;
1613 substream1 = pcm_file->substream;
1614 down_write(&snd_pcm_link_rwsem);
1615 write_lock_irq(&snd_pcm_link_rwlock);
1616 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1617 substream->runtime->status->state != substream1->runtime->status->state) {
1618 res = -EBADFD;
1619 goto _end;
1620 }
1621 if (snd_pcm_stream_linked(substream1)) {
1622 res = -EALREADY;
1623 goto _end;
1624 }
1625 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001626 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (substream->group == NULL) {
1628 res = -ENOMEM;
1629 goto _end;
1630 }
1631 spin_lock_init(&substream->group->lock);
1632 INIT_LIST_HEAD(&substream->group->substreams);
1633 list_add_tail(&substream->link_list, &substream->group->substreams);
1634 substream->group->count = 1;
1635 }
1636 list_add_tail(&substream1->link_list, &substream->group->substreams);
1637 substream->group->count++;
1638 substream1->group = substream->group;
1639 _end:
1640 write_unlock_irq(&snd_pcm_link_rwlock);
1641 up_write(&snd_pcm_link_rwsem);
1642 fput(file);
1643 return res;
1644}
1645
Takashi Iwai877211f2005-11-17 13:59:38 +01001646static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 substream->group = &substream->self_group;
1649 INIT_LIST_HEAD(&substream->self_group.substreams);
1650 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1651}
1652
Takashi Iwai877211f2005-11-17 13:59:38 +01001653static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 struct list_head *pos;
1656 int res = 0;
1657
1658 down_write(&snd_pcm_link_rwsem);
1659 write_lock_irq(&snd_pcm_link_rwlock);
1660 if (!snd_pcm_stream_linked(substream)) {
1661 res = -EALREADY;
1662 goto _end;
1663 }
1664 list_del(&substream->link_list);
1665 substream->group->count--;
1666 if (substream->group->count == 1) { /* detach the last stream, too */
1667 snd_pcm_group_for_each(pos, substream) {
1668 relink_to_local(snd_pcm_group_substream_entry(pos));
1669 break;
1670 }
1671 kfree(substream->group);
1672 }
1673 relink_to_local(substream);
1674 _end:
1675 write_unlock_irq(&snd_pcm_link_rwlock);
1676 up_write(&snd_pcm_link_rwsem);
1677 return res;
1678}
1679
1680/*
1681 * hw configurator
1682 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001683static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1684 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Takashi Iwai877211f2005-11-17 13:59:38 +01001686 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1688 hw_param_interval_c(params, rule->deps[1]), &t);
1689 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1690}
1691
Takashi Iwai877211f2005-11-17 13:59:38 +01001692static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1693 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Takashi Iwai877211f2005-11-17 13:59:38 +01001695 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1697 hw_param_interval_c(params, rule->deps[1]), &t);
1698 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1699}
1700
Takashi Iwai877211f2005-11-17 13:59:38 +01001701static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1702 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Takashi Iwai877211f2005-11-17 13:59:38 +01001704 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1706 hw_param_interval_c(params, rule->deps[1]),
1707 (unsigned long) rule->private, &t);
1708 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1709}
1710
Takashi Iwai877211f2005-11-17 13:59:38 +01001711static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1712 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Takashi Iwai877211f2005-11-17 13:59:38 +01001714 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1716 (unsigned long) rule->private,
1717 hw_param_interval_c(params, rule->deps[1]), &t);
1718 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1719}
1720
Takashi Iwai877211f2005-11-17 13:59:38 +01001721static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1722 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001725 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1726 struct snd_mask m;
1727 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 snd_mask_any(&m);
1729 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1730 int bits;
1731 if (! snd_mask_test(mask, k))
1732 continue;
1733 bits = snd_pcm_format_physical_width(k);
1734 if (bits <= 0)
1735 continue; /* ignore invalid formats */
1736 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1737 snd_mask_reset(&m, k);
1738 }
1739 return snd_mask_refine(mask, &m);
1740}
1741
Takashi Iwai877211f2005-11-17 13:59:38 +01001742static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1743 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Takashi Iwai877211f2005-11-17 13:59:38 +01001745 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 unsigned int k;
1747 t.min = UINT_MAX;
1748 t.max = 0;
1749 t.openmin = 0;
1750 t.openmax = 0;
1751 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1752 int bits;
1753 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1754 continue;
1755 bits = snd_pcm_format_physical_width(k);
1756 if (bits <= 0)
1757 continue; /* ignore invalid formats */
1758 if (t.min > (unsigned)bits)
1759 t.min = bits;
1760 if (t.max < (unsigned)bits)
1761 t.max = bits;
1762 }
1763 t.integer = 1;
1764 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1765}
1766
1767#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1768#error "Change this table"
1769#endif
1770
1771static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1772 48000, 64000, 88200, 96000, 176400, 192000 };
1773
Takashi Iwai877211f2005-11-17 13:59:38 +01001774static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1775 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwai877211f2005-11-17 13:59:38 +01001777 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return snd_interval_list(hw_param_interval(params, rule->var),
1779 ARRAY_SIZE(rates), rates, hw->rates);
1780}
1781
Takashi Iwai877211f2005-11-17 13:59:38 +01001782static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1783 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Takashi Iwai877211f2005-11-17 13:59:38 +01001785 struct snd_interval t;
1786 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 t.min = 0;
1788 t.max = substream->buffer_bytes_max;
1789 t.openmin = 0;
1790 t.openmax = 0;
1791 t.integer = 1;
1792 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1793}
1794
Takashi Iwai877211f2005-11-17 13:59:38 +01001795int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Takashi Iwai877211f2005-11-17 13:59:38 +01001797 struct snd_pcm_runtime *runtime = substream->runtime;
1798 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 int k, err;
1800
1801 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1802 snd_mask_any(constrs_mask(constrs, k));
1803 }
1804
1805 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1806 snd_interval_any(constrs_interval(constrs, k));
1807 }
1808
1809 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1810 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1811 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1812 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1813 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1814
1815 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1816 snd_pcm_hw_rule_format, NULL,
1817 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1818 if (err < 0)
1819 return err;
1820 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1821 snd_pcm_hw_rule_sample_bits, NULL,
1822 SNDRV_PCM_HW_PARAM_FORMAT,
1823 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1824 if (err < 0)
1825 return err;
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1827 snd_pcm_hw_rule_div, NULL,
1828 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1829 if (err < 0)
1830 return err;
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1832 snd_pcm_hw_rule_mul, NULL,
1833 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1834 if (err < 0)
1835 return err;
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1837 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1838 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1839 if (err < 0)
1840 return err;
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1842 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1843 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1844 if (err < 0)
1845 return err;
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1847 snd_pcm_hw_rule_div, NULL,
1848 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1849 if (err < 0)
1850 return err;
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1852 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1853 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1857 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1858 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1859 if (err < 0)
1860 return err;
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1862 snd_pcm_hw_rule_div, NULL,
1863 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1864 if (err < 0)
1865 return err;
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1867 snd_pcm_hw_rule_div, NULL,
1868 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1872 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1873 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1877 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1878 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1879 if (err < 0)
1880 return err;
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1882 snd_pcm_hw_rule_mul, NULL,
1883 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1887 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1888 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1889 if (err < 0)
1890 return err;
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1892 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1893 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1894 if (err < 0)
1895 return err;
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1897 snd_pcm_hw_rule_muldivk, (void*) 8,
1898 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1899 if (err < 0)
1900 return err;
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1902 snd_pcm_hw_rule_muldivk, (void*) 8,
1903 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1904 if (err < 0)
1905 return err;
1906 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1907 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1908 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1909 if (err < 0)
1910 return err;
1911 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1912 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1913 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1914 if (err < 0)
1915 return err;
1916 return 0;
1917}
1918
Takashi Iwai877211f2005-11-17 13:59:38 +01001919int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Takashi Iwai877211f2005-11-17 13:59:38 +01001921 struct snd_pcm_runtime *runtime = substream->runtime;
1922 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 int err;
1924 unsigned int mask = 0;
1925
1926 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1927 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1928 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1929 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1930 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1931 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1932 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1933 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1934 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1935 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1936 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1937 }
1938 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1939 snd_assert(err >= 0, return -EINVAL);
1940
1941 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1942 snd_assert(err >= 0, return -EINVAL);
1943
1944 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1945 snd_assert(err >= 0, return -EINVAL);
1946
1947 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1948 hw->channels_min, hw->channels_max);
1949 snd_assert(err >= 0, return -EINVAL);
1950
1951 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1952 hw->rate_min, hw->rate_max);
1953 snd_assert(err >= 0, return -EINVAL);
1954
1955 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1956 hw->period_bytes_min, hw->period_bytes_max);
1957 snd_assert(err >= 0, return -EINVAL);
1958
1959 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1960 hw->periods_min, hw->periods_max);
1961 snd_assert(err >= 0, return -EINVAL);
1962
1963 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1964 hw->period_bytes_min, hw->buffer_bytes_max);
1965 snd_assert(err >= 0, return -EINVAL);
1966
1967 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1968 snd_pcm_hw_rule_buffer_bytes_max, substream,
1969 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1970 if (err < 0)
1971 return err;
1972
1973 /* FIXME: remove */
1974 if (runtime->dma_bytes) {
1975 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1976 snd_assert(err >= 0, return -EINVAL);
1977 }
1978
1979 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1980 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1981 snd_pcm_hw_rule_rate, hw,
1982 SNDRV_PCM_HW_PARAM_RATE, -1);
1983 if (err < 0)
1984 return err;
1985 }
1986
1987 /* FIXME: this belong to lowlevel */
1988 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1989 1000000 / HZ, 1000000 / HZ);
1990 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1991
1992 return 0;
1993}
1994
Takashi Iwai877211f2005-11-17 13:59:38 +01001995static void snd_pcm_add_file(struct snd_pcm_str *str,
1996 struct snd_pcm_file *pcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 pcm_file->next = str->files;
1999 str->files = pcm_file;
2000}
2001
Takashi Iwai877211f2005-11-17 13:59:38 +01002002static void snd_pcm_remove_file(struct snd_pcm_str *str,
2003 struct snd_pcm_file *pcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Takashi Iwai877211f2005-11-17 13:59:38 +01002005 struct snd_pcm_file * pcm_file1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 if (str->files == pcm_file) {
2007 str->files = pcm_file->next;
2008 } else {
2009 pcm_file1 = str->files;
2010 while (pcm_file1 && pcm_file1->next != pcm_file)
2011 pcm_file1 = pcm_file1->next;
2012 if (pcm_file1 != NULL)
2013 pcm_file1->next = pcm_file->next;
2014 }
2015}
2016
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002017static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002019 struct snd_pcm_file *pcm_file = substream->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002022 snd_pcm_remove_file(substream->pstr, pcm_file);
2023 kfree(pcm_file);
2024}
2025
2026void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2027{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002028 substream->ref_count--;
2029 if (substream->ref_count > 0)
2030 return;
2031
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002032 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002033 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (substream->ops->hw_free != NULL)
2035 substream->ops->hw_free(substream);
2036 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002037 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
Takashi Iwai15762742006-04-06 19:47:42 +02002039 if (substream->pcm_release) {
2040 substream->pcm_release(substream);
2041 substream->pcm_release = NULL;
2042 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002043 snd_pcm_detach_substream(substream);
2044}
2045
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002046EXPORT_SYMBOL(snd_pcm_release_substream);
2047
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002048int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2049 struct file *file,
2050 struct snd_pcm_substream **rsubstream)
2051{
2052 struct snd_pcm_substream *substream;
2053 int err;
2054
2055 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2056 if (err < 0)
2057 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002058 if (substream->ref_count > 1) {
2059 *rsubstream = substream;
2060 return 0;
2061 }
2062
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002063 substream->no_mmap_ctrl = 0;
2064 err = snd_pcm_hw_constraints_init(substream);
2065 if (err < 0) {
2066 snd_printd("snd_pcm_hw_constraints_init failed\n");
2067 goto error;
2068 }
2069
2070 if ((err = substream->ops->open(substream)) < 0)
2071 goto error;
2072
2073 substream->hw_opened = 1;
2074
2075 err = snd_pcm_hw_constraints_complete(substream);
2076 if (err < 0) {
2077 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2078 goto error;
2079 }
2080
2081 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002083
2084 error:
2085 snd_pcm_release_substream(substream);
2086 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002089EXPORT_SYMBOL(snd_pcm_open_substream);
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002092 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002094 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Takashi Iwai877211f2005-11-17 13:59:38 +01002096 struct snd_pcm_file *pcm_file;
2097 struct snd_pcm_substream *substream;
2098 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002099 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 snd_assert(rpcm_file != NULL, return -EINVAL);
2102 *rpcm_file = NULL;
2103
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002104 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2105 if (err < 0)
2106 return err;
2107
Takashi Iwai0df63e42006-04-28 15:13:41 +02002108 if (substream->ref_count > 1)
2109 pcm_file = substream->file;
2110 else {
2111 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2112 if (pcm_file == NULL) {
2113 snd_pcm_release_substream(substream);
2114 return -ENOMEM;
2115 }
2116 str = substream->pstr;
2117 substream->file = pcm_file;
2118 substream->pcm_release = pcm_release_private;
2119 pcm_file->substream = substream;
2120 snd_pcm_add_file(str, pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 file->private_data = pcm_file;
2123 *rpcm_file = pcm_file;
2124 return 0;
2125}
2126
Clemens Ladischf87135f2005-11-20 14:06:59 +01002127static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Takashi Iwai877211f2005-11-17 13:59:38 +01002129 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002130
2131 pcm = snd_lookup_minor_data(iminor(inode),
2132 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2133 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2134}
2135
2136static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2137{
2138 struct snd_pcm *pcm;
2139
2140 pcm = snd_lookup_minor_data(iminor(inode),
2141 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2142 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2143}
2144
2145static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2146{
2147 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002148 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 wait_queue_t wait;
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (pcm == NULL) {
2152 err = -ENODEV;
2153 goto __error1;
2154 }
2155 err = snd_card_file_add(pcm->card, file);
2156 if (err < 0)
2157 goto __error1;
2158 if (!try_module_get(pcm->card->module)) {
2159 err = -EFAULT;
2160 goto __error2;
2161 }
2162 init_waitqueue_entry(&wait, current);
2163 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002164 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002166 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (err >= 0)
2168 break;
2169 if (err == -EAGAIN) {
2170 if (file->f_flags & O_NONBLOCK) {
2171 err = -EBUSY;
2172 break;
2173 }
2174 } else
2175 break;
2176 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002177 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002179 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (signal_pending(current)) {
2181 err = -ERESTARTSYS;
2182 break;
2183 }
2184 }
2185 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002186 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (err < 0)
2188 goto __error;
2189 return err;
2190
2191 __error:
2192 module_put(pcm->card->module);
2193 __error2:
2194 snd_card_file_remove(pcm->card, file);
2195 __error1:
2196 return err;
2197}
2198
2199static int snd_pcm_release(struct inode *inode, struct file *file)
2200{
Takashi Iwai877211f2005-11-17 13:59:38 +01002201 struct snd_pcm *pcm;
2202 struct snd_pcm_substream *substream;
2203 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 pcm_file = file->private_data;
2206 substream = pcm_file->substream;
2207 snd_assert(substream != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 pcm = substream->pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 fasync_helper(-1, file, 0, &substream->runtime->fasync);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002210 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002211 snd_pcm_release_substream(substream);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002212 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 wake_up(&pcm->open_wait);
2214 module_put(pcm->card->module);
2215 snd_card_file_remove(pcm->card, file);
2216 return 0;
2217}
2218
Takashi Iwai877211f2005-11-17 13:59:38 +01002219static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2220 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Takashi Iwai877211f2005-11-17 13:59:38 +01002222 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 snd_pcm_sframes_t appl_ptr;
2224 snd_pcm_sframes_t ret;
2225 snd_pcm_sframes_t hw_avail;
2226
2227 if (frames == 0)
2228 return 0;
2229
2230 snd_pcm_stream_lock_irq(substream);
2231 switch (runtime->status->state) {
2232 case SNDRV_PCM_STATE_PREPARED:
2233 break;
2234 case SNDRV_PCM_STATE_DRAINING:
2235 case SNDRV_PCM_STATE_RUNNING:
2236 if (snd_pcm_update_hw_ptr(substream) >= 0)
2237 break;
2238 /* Fall through */
2239 case SNDRV_PCM_STATE_XRUN:
2240 ret = -EPIPE;
2241 goto __end;
2242 default:
2243 ret = -EBADFD;
2244 goto __end;
2245 }
2246
2247 hw_avail = snd_pcm_playback_hw_avail(runtime);
2248 if (hw_avail <= 0) {
2249 ret = 0;
2250 goto __end;
2251 }
2252 if (frames > (snd_pcm_uframes_t)hw_avail)
2253 frames = hw_avail;
2254 else
2255 frames -= frames % runtime->xfer_align;
2256 appl_ptr = runtime->control->appl_ptr - frames;
2257 if (appl_ptr < 0)
2258 appl_ptr += runtime->boundary;
2259 runtime->control->appl_ptr = appl_ptr;
2260 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2261 runtime->sleep_min)
2262 snd_pcm_tick_prepare(substream);
2263 ret = frames;
2264 __end:
2265 snd_pcm_stream_unlock_irq(substream);
2266 return ret;
2267}
2268
Takashi Iwai877211f2005-11-17 13:59:38 +01002269static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2270 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Takashi Iwai877211f2005-11-17 13:59:38 +01002272 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 snd_pcm_sframes_t appl_ptr;
2274 snd_pcm_sframes_t ret;
2275 snd_pcm_sframes_t hw_avail;
2276
2277 if (frames == 0)
2278 return 0;
2279
2280 snd_pcm_stream_lock_irq(substream);
2281 switch (runtime->status->state) {
2282 case SNDRV_PCM_STATE_PREPARED:
2283 case SNDRV_PCM_STATE_DRAINING:
2284 break;
2285 case SNDRV_PCM_STATE_RUNNING:
2286 if (snd_pcm_update_hw_ptr(substream) >= 0)
2287 break;
2288 /* Fall through */
2289 case SNDRV_PCM_STATE_XRUN:
2290 ret = -EPIPE;
2291 goto __end;
2292 default:
2293 ret = -EBADFD;
2294 goto __end;
2295 }
2296
2297 hw_avail = snd_pcm_capture_hw_avail(runtime);
2298 if (hw_avail <= 0) {
2299 ret = 0;
2300 goto __end;
2301 }
2302 if (frames > (snd_pcm_uframes_t)hw_avail)
2303 frames = hw_avail;
2304 else
2305 frames -= frames % runtime->xfer_align;
2306 appl_ptr = runtime->control->appl_ptr - frames;
2307 if (appl_ptr < 0)
2308 appl_ptr += runtime->boundary;
2309 runtime->control->appl_ptr = appl_ptr;
2310 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2311 runtime->sleep_min)
2312 snd_pcm_tick_prepare(substream);
2313 ret = frames;
2314 __end:
2315 snd_pcm_stream_unlock_irq(substream);
2316 return ret;
2317}
2318
Takashi Iwai877211f2005-11-17 13:59:38 +01002319static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2320 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Takashi Iwai877211f2005-11-17 13:59:38 +01002322 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 snd_pcm_sframes_t appl_ptr;
2324 snd_pcm_sframes_t ret;
2325 snd_pcm_sframes_t avail;
2326
2327 if (frames == 0)
2328 return 0;
2329
2330 snd_pcm_stream_lock_irq(substream);
2331 switch (runtime->status->state) {
2332 case SNDRV_PCM_STATE_PREPARED:
2333 case SNDRV_PCM_STATE_PAUSED:
2334 break;
2335 case SNDRV_PCM_STATE_DRAINING:
2336 case SNDRV_PCM_STATE_RUNNING:
2337 if (snd_pcm_update_hw_ptr(substream) >= 0)
2338 break;
2339 /* Fall through */
2340 case SNDRV_PCM_STATE_XRUN:
2341 ret = -EPIPE;
2342 goto __end;
2343 default:
2344 ret = -EBADFD;
2345 goto __end;
2346 }
2347
2348 avail = snd_pcm_playback_avail(runtime);
2349 if (avail <= 0) {
2350 ret = 0;
2351 goto __end;
2352 }
2353 if (frames > (snd_pcm_uframes_t)avail)
2354 frames = avail;
2355 else
2356 frames -= frames % runtime->xfer_align;
2357 appl_ptr = runtime->control->appl_ptr + frames;
2358 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2359 appl_ptr -= runtime->boundary;
2360 runtime->control->appl_ptr = appl_ptr;
2361 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2362 runtime->sleep_min)
2363 snd_pcm_tick_prepare(substream);
2364 ret = frames;
2365 __end:
2366 snd_pcm_stream_unlock_irq(substream);
2367 return ret;
2368}
2369
Takashi Iwai877211f2005-11-17 13:59:38 +01002370static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2371 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Takashi Iwai877211f2005-11-17 13:59:38 +01002373 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 snd_pcm_sframes_t appl_ptr;
2375 snd_pcm_sframes_t ret;
2376 snd_pcm_sframes_t avail;
2377
2378 if (frames == 0)
2379 return 0;
2380
2381 snd_pcm_stream_lock_irq(substream);
2382 switch (runtime->status->state) {
2383 case SNDRV_PCM_STATE_PREPARED:
2384 case SNDRV_PCM_STATE_DRAINING:
2385 case SNDRV_PCM_STATE_PAUSED:
2386 break;
2387 case SNDRV_PCM_STATE_RUNNING:
2388 if (snd_pcm_update_hw_ptr(substream) >= 0)
2389 break;
2390 /* Fall through */
2391 case SNDRV_PCM_STATE_XRUN:
2392 ret = -EPIPE;
2393 goto __end;
2394 default:
2395 ret = -EBADFD;
2396 goto __end;
2397 }
2398
2399 avail = snd_pcm_capture_avail(runtime);
2400 if (avail <= 0) {
2401 ret = 0;
2402 goto __end;
2403 }
2404 if (frames > (snd_pcm_uframes_t)avail)
2405 frames = avail;
2406 else
2407 frames -= frames % runtime->xfer_align;
2408 appl_ptr = runtime->control->appl_ptr + frames;
2409 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2410 appl_ptr -= runtime->boundary;
2411 runtime->control->appl_ptr = appl_ptr;
2412 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2413 runtime->sleep_min)
2414 snd_pcm_tick_prepare(substream);
2415 ret = frames;
2416 __end:
2417 snd_pcm_stream_unlock_irq(substream);
2418 return ret;
2419}
2420
Takashi Iwai877211f2005-11-17 13:59:38 +01002421static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Takashi Iwai877211f2005-11-17 13:59:38 +01002423 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int err;
2425
2426 snd_pcm_stream_lock_irq(substream);
2427 switch (runtime->status->state) {
2428 case SNDRV_PCM_STATE_DRAINING:
2429 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2430 goto __badfd;
2431 case SNDRV_PCM_STATE_RUNNING:
2432 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2433 break;
2434 /* Fall through */
2435 case SNDRV_PCM_STATE_PREPARED:
2436 case SNDRV_PCM_STATE_SUSPENDED:
2437 err = 0;
2438 break;
2439 case SNDRV_PCM_STATE_XRUN:
2440 err = -EPIPE;
2441 break;
2442 default:
2443 __badfd:
2444 err = -EBADFD;
2445 break;
2446 }
2447 snd_pcm_stream_unlock_irq(substream);
2448 return err;
2449}
2450
Takashi Iwai877211f2005-11-17 13:59:38 +01002451static int snd_pcm_delay(struct snd_pcm_substream *substream,
2452 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Takashi Iwai877211f2005-11-17 13:59:38 +01002454 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 int err;
2456 snd_pcm_sframes_t n = 0;
2457
2458 snd_pcm_stream_lock_irq(substream);
2459 switch (runtime->status->state) {
2460 case SNDRV_PCM_STATE_DRAINING:
2461 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2462 goto __badfd;
2463 case SNDRV_PCM_STATE_RUNNING:
2464 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2465 break;
2466 /* Fall through */
2467 case SNDRV_PCM_STATE_PREPARED:
2468 case SNDRV_PCM_STATE_SUSPENDED:
2469 err = 0;
2470 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2471 n = snd_pcm_playback_hw_avail(runtime);
2472 else
2473 n = snd_pcm_capture_avail(runtime);
2474 break;
2475 case SNDRV_PCM_STATE_XRUN:
2476 err = -EPIPE;
2477 break;
2478 default:
2479 __badfd:
2480 err = -EBADFD;
2481 break;
2482 }
2483 snd_pcm_stream_unlock_irq(substream);
2484 if (!err)
2485 if (put_user(n, res))
2486 err = -EFAULT;
2487 return err;
2488}
2489
Takashi Iwai877211f2005-11-17 13:59:38 +01002490static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2491 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Takashi Iwai877211f2005-11-17 13:59:38 +01002493 struct snd_pcm_runtime *runtime = substream->runtime;
2494 struct snd_pcm_sync_ptr sync_ptr;
2495 volatile struct snd_pcm_mmap_status *status;
2496 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 int err;
2498
2499 memset(&sync_ptr, 0, sizeof(sync_ptr));
2500 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2501 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002502 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 -07002503 return -EFAULT;
2504 status = runtime->status;
2505 control = runtime->control;
2506 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2507 err = snd_pcm_hwsync(substream);
2508 if (err < 0)
2509 return err;
2510 }
2511 snd_pcm_stream_lock_irq(substream);
2512 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2513 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2514 else
2515 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2516 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2517 control->avail_min = sync_ptr.c.control.avail_min;
2518 else
2519 sync_ptr.c.control.avail_min = control->avail_min;
2520 sync_ptr.s.status.state = status->state;
2521 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2522 sync_ptr.s.status.tstamp = status->tstamp;
2523 sync_ptr.s.status.suspended_state = status->suspended_state;
2524 snd_pcm_stream_unlock_irq(substream);
2525 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2526 return -EFAULT;
2527 return 0;
2528}
2529
Takashi Iwai0df63e42006-04-28 15:13:41 +02002530static int snd_pcm_common_ioctl1(struct file *file,
2531 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 unsigned int cmd, void __user *arg)
2533{
2534 snd_assert(substream != NULL, return -ENXIO);
2535
2536 switch (cmd) {
2537 case SNDRV_PCM_IOCTL_PVERSION:
2538 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2539 case SNDRV_PCM_IOCTL_INFO:
2540 return snd_pcm_info_user(substream, arg);
Takashi Iwai07799e72005-10-10 11:49:49 +02002541 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 case SNDRV_PCM_IOCTL_HW_REFINE:
2544 return snd_pcm_hw_refine_user(substream, arg);
2545 case SNDRV_PCM_IOCTL_HW_PARAMS:
2546 return snd_pcm_hw_params_user(substream, arg);
2547 case SNDRV_PCM_IOCTL_HW_FREE:
2548 return snd_pcm_hw_free(substream);
2549 case SNDRV_PCM_IOCTL_SW_PARAMS:
2550 return snd_pcm_sw_params_user(substream, arg);
2551 case SNDRV_PCM_IOCTL_STATUS:
2552 return snd_pcm_status_user(substream, arg);
2553 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2554 return snd_pcm_channel_info_user(substream, arg);
2555 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002556 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 case SNDRV_PCM_IOCTL_RESET:
2558 return snd_pcm_reset(substream);
2559 case SNDRV_PCM_IOCTL_START:
2560 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2561 case SNDRV_PCM_IOCTL_LINK:
2562 return snd_pcm_link(substream, (int)(unsigned long) arg);
2563 case SNDRV_PCM_IOCTL_UNLINK:
2564 return snd_pcm_unlink(substream);
2565 case SNDRV_PCM_IOCTL_RESUME:
2566 return snd_pcm_resume(substream);
2567 case SNDRV_PCM_IOCTL_XRUN:
2568 return snd_pcm_xrun(substream);
2569 case SNDRV_PCM_IOCTL_HWSYNC:
2570 return snd_pcm_hwsync(substream);
2571 case SNDRV_PCM_IOCTL_DELAY:
2572 return snd_pcm_delay(substream, arg);
2573 case SNDRV_PCM_IOCTL_SYNC_PTR:
2574 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002575#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2577 return snd_pcm_hw_refine_old_user(substream, arg);
2578 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2579 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002580#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 case SNDRV_PCM_IOCTL_DRAIN:
2582 return snd_pcm_drain(substream);
2583 case SNDRV_PCM_IOCTL_DROP:
2584 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002585 case SNDRV_PCM_IOCTL_PAUSE:
2586 {
2587 int res;
2588 snd_pcm_stream_lock_irq(substream);
2589 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2590 snd_pcm_stream_unlock_irq(substream);
2591 return res;
2592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594 snd_printd("unknown ioctl = 0x%x\n", cmd);
2595 return -ENOTTY;
2596}
2597
Takashi Iwai0df63e42006-04-28 15:13:41 +02002598static int snd_pcm_playback_ioctl1(struct file *file,
2599 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 unsigned int cmd, void __user *arg)
2601{
2602 snd_assert(substream != NULL, return -ENXIO);
2603 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2604 switch (cmd) {
2605 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2606 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002607 struct snd_xferi xferi;
2608 struct snd_xferi __user *_xferi = arg;
2609 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 snd_pcm_sframes_t result;
2611 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2612 return -EBADFD;
2613 if (put_user(0, &_xferi->result))
2614 return -EFAULT;
2615 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2616 return -EFAULT;
2617 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2618 __put_user(result, &_xferi->result);
2619 return result < 0 ? result : 0;
2620 }
2621 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2622 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002623 struct snd_xfern xfern;
2624 struct snd_xfern __user *_xfern = arg;
2625 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 void __user **bufs;
2627 snd_pcm_sframes_t result;
2628 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2629 return -EBADFD;
2630 if (runtime->channels > 128)
2631 return -EINVAL;
2632 if (put_user(0, &_xfern->result))
2633 return -EFAULT;
2634 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2635 return -EFAULT;
2636 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2637 if (bufs == NULL)
2638 return -ENOMEM;
2639 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2640 kfree(bufs);
2641 return -EFAULT;
2642 }
2643 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2644 kfree(bufs);
2645 __put_user(result, &_xfern->result);
2646 return result < 0 ? result : 0;
2647 }
2648 case SNDRV_PCM_IOCTL_REWIND:
2649 {
2650 snd_pcm_uframes_t frames;
2651 snd_pcm_uframes_t __user *_frames = arg;
2652 snd_pcm_sframes_t result;
2653 if (get_user(frames, _frames))
2654 return -EFAULT;
2655 if (put_user(0, _frames))
2656 return -EFAULT;
2657 result = snd_pcm_playback_rewind(substream, frames);
2658 __put_user(result, _frames);
2659 return result < 0 ? result : 0;
2660 }
2661 case SNDRV_PCM_IOCTL_FORWARD:
2662 {
2663 snd_pcm_uframes_t frames;
2664 snd_pcm_uframes_t __user *_frames = arg;
2665 snd_pcm_sframes_t result;
2666 if (get_user(frames, _frames))
2667 return -EFAULT;
2668 if (put_user(0, _frames))
2669 return -EFAULT;
2670 result = snd_pcm_playback_forward(substream, frames);
2671 __put_user(result, _frames);
2672 return result < 0 ? result : 0;
2673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002675 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
Takashi Iwai0df63e42006-04-28 15:13:41 +02002678static int snd_pcm_capture_ioctl1(struct file *file,
2679 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 unsigned int cmd, void __user *arg)
2681{
2682 snd_assert(substream != NULL, return -ENXIO);
2683 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2684 switch (cmd) {
2685 case SNDRV_PCM_IOCTL_READI_FRAMES:
2686 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002687 struct snd_xferi xferi;
2688 struct snd_xferi __user *_xferi = arg;
2689 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 snd_pcm_sframes_t result;
2691 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2692 return -EBADFD;
2693 if (put_user(0, &_xferi->result))
2694 return -EFAULT;
2695 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2696 return -EFAULT;
2697 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2698 __put_user(result, &_xferi->result);
2699 return result < 0 ? result : 0;
2700 }
2701 case SNDRV_PCM_IOCTL_READN_FRAMES:
2702 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002703 struct snd_xfern xfern;
2704 struct snd_xfern __user *_xfern = arg;
2705 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 void *bufs;
2707 snd_pcm_sframes_t result;
2708 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2709 return -EBADFD;
2710 if (runtime->channels > 128)
2711 return -EINVAL;
2712 if (put_user(0, &_xfern->result))
2713 return -EFAULT;
2714 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2715 return -EFAULT;
2716 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2717 if (bufs == NULL)
2718 return -ENOMEM;
2719 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2720 kfree(bufs);
2721 return -EFAULT;
2722 }
2723 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2724 kfree(bufs);
2725 __put_user(result, &_xfern->result);
2726 return result < 0 ? result : 0;
2727 }
2728 case SNDRV_PCM_IOCTL_REWIND:
2729 {
2730 snd_pcm_uframes_t frames;
2731 snd_pcm_uframes_t __user *_frames = arg;
2732 snd_pcm_sframes_t result;
2733 if (get_user(frames, _frames))
2734 return -EFAULT;
2735 if (put_user(0, _frames))
2736 return -EFAULT;
2737 result = snd_pcm_capture_rewind(substream, frames);
2738 __put_user(result, _frames);
2739 return result < 0 ? result : 0;
2740 }
2741 case SNDRV_PCM_IOCTL_FORWARD:
2742 {
2743 snd_pcm_uframes_t frames;
2744 snd_pcm_uframes_t __user *_frames = arg;
2745 snd_pcm_sframes_t result;
2746 if (get_user(frames, _frames))
2747 return -EFAULT;
2748 if (put_user(0, _frames))
2749 return -EFAULT;
2750 result = snd_pcm_capture_forward(substream, frames);
2751 __put_user(result, _frames);
2752 return result < 0 ? result : 0;
2753 }
2754 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002755 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
2757
Takashi Iwai877211f2005-11-17 13:59:38 +01002758static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2759 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Takashi Iwai877211f2005-11-17 13:59:38 +01002761 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 pcm_file = file->private_data;
2764
2765 if (((cmd >> 8) & 0xff) != 'A')
2766 return -ENOTTY;
2767
Takashi Iwai0df63e42006-04-28 15:13:41 +02002768 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2769 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
Takashi Iwai877211f2005-11-17 13:59:38 +01002772static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2773 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Takashi Iwai877211f2005-11-17 13:59:38 +01002775 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 pcm_file = file->private_data;
2778
2779 if (((cmd >> 8) & 0xff) != 'A')
2780 return -ENOTTY;
2781
Takashi Iwai0df63e42006-04-28 15:13:41 +02002782 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2783 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
Takashi Iwai877211f2005-11-17 13:59:38 +01002786int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 unsigned int cmd, void *arg)
2788{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002789 mm_segment_t fs;
2790 int result;
2791
2792 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 switch (substream->stream) {
2794 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002795 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2796 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002797 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002799 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2800 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002803 result = -EINVAL;
2804 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002806 snd_leave_user(fs);
2807 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
2809
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002810EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2811
Takashi Iwai877211f2005-11-17 13:59:38 +01002812static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2813 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
Takashi Iwai877211f2005-11-17 13:59:38 +01002815 struct snd_pcm_file *pcm_file;
2816 struct snd_pcm_substream *substream;
2817 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 snd_pcm_sframes_t result;
2819
2820 pcm_file = file->private_data;
2821 substream = pcm_file->substream;
2822 snd_assert(substream != NULL, return -ENXIO);
2823 runtime = substream->runtime;
2824 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2825 return -EBADFD;
2826 if (!frame_aligned(runtime, count))
2827 return -EINVAL;
2828 count = bytes_to_frames(runtime, count);
2829 result = snd_pcm_lib_read(substream, buf, count);
2830 if (result > 0)
2831 result = frames_to_bytes(runtime, result);
2832 return result;
2833}
2834
Takashi Iwai877211f2005-11-17 13:59:38 +01002835static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2836 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
Takashi Iwai877211f2005-11-17 13:59:38 +01002838 struct snd_pcm_file *pcm_file;
2839 struct snd_pcm_substream *substream;
2840 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 snd_pcm_sframes_t result;
2842
2843 pcm_file = file->private_data;
2844 substream = pcm_file->substream;
2845 snd_assert(substream != NULL, result = -ENXIO; goto end);
2846 runtime = substream->runtime;
2847 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2848 result = -EBADFD;
2849 goto end;
2850 }
2851 if (!frame_aligned(runtime, count)) {
2852 result = -EINVAL;
2853 goto end;
2854 }
2855 count = bytes_to_frames(runtime, count);
2856 result = snd_pcm_lib_write(substream, buf, count);
2857 if (result > 0)
2858 result = frames_to_bytes(runtime, result);
2859 end:
2860 return result;
2861}
2862
2863static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2864 unsigned long count, loff_t * offset)
2865
2866{
Takashi Iwai877211f2005-11-17 13:59:38 +01002867 struct snd_pcm_file *pcm_file;
2868 struct snd_pcm_substream *substream;
2869 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 snd_pcm_sframes_t result;
2871 unsigned long i;
2872 void __user **bufs;
2873 snd_pcm_uframes_t frames;
2874
2875 pcm_file = file->private_data;
2876 substream = pcm_file->substream;
2877 snd_assert(substream != NULL, return -ENXIO);
2878 runtime = substream->runtime;
2879 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2880 return -EBADFD;
2881 if (count > 1024 || count != runtime->channels)
2882 return -EINVAL;
2883 if (!frame_aligned(runtime, _vector->iov_len))
2884 return -EINVAL;
2885 frames = bytes_to_samples(runtime, _vector->iov_len);
2886 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2887 if (bufs == NULL)
2888 return -ENOMEM;
2889 for (i = 0; i < count; ++i)
2890 bufs[i] = _vector[i].iov_base;
2891 result = snd_pcm_lib_readv(substream, bufs, frames);
2892 if (result > 0)
2893 result = frames_to_bytes(runtime, result);
2894 kfree(bufs);
2895 return result;
2896}
2897
2898static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2899 unsigned long count, loff_t * offset)
2900{
Takashi Iwai877211f2005-11-17 13:59:38 +01002901 struct snd_pcm_file *pcm_file;
2902 struct snd_pcm_substream *substream;
2903 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 snd_pcm_sframes_t result;
2905 unsigned long i;
2906 void __user **bufs;
2907 snd_pcm_uframes_t frames;
2908
2909 pcm_file = file->private_data;
2910 substream = pcm_file->substream;
2911 snd_assert(substream != NULL, result = -ENXIO; goto end);
2912 runtime = substream->runtime;
2913 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2914 result = -EBADFD;
2915 goto end;
2916 }
2917 if (count > 128 || count != runtime->channels ||
2918 !frame_aligned(runtime, _vector->iov_len)) {
2919 result = -EINVAL;
2920 goto end;
2921 }
2922 frames = bytes_to_samples(runtime, _vector->iov_len);
2923 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2924 if (bufs == NULL)
2925 return -ENOMEM;
2926 for (i = 0; i < count; ++i)
2927 bufs[i] = _vector[i].iov_base;
2928 result = snd_pcm_lib_writev(substream, bufs, frames);
2929 if (result > 0)
2930 result = frames_to_bytes(runtime, result);
2931 kfree(bufs);
2932 end:
2933 return result;
2934}
2935
2936static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2937{
Takashi Iwai877211f2005-11-17 13:59:38 +01002938 struct snd_pcm_file *pcm_file;
2939 struct snd_pcm_substream *substream;
2940 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 unsigned int mask;
2942 snd_pcm_uframes_t avail;
2943
2944 pcm_file = file->private_data;
2945
2946 substream = pcm_file->substream;
2947 snd_assert(substream != NULL, return -ENXIO);
2948 runtime = substream->runtime;
2949
2950 poll_wait(file, &runtime->sleep, wait);
2951
2952 snd_pcm_stream_lock_irq(substream);
2953 avail = snd_pcm_playback_avail(runtime);
2954 switch (runtime->status->state) {
2955 case SNDRV_PCM_STATE_RUNNING:
2956 case SNDRV_PCM_STATE_PREPARED:
2957 case SNDRV_PCM_STATE_PAUSED:
2958 if (avail >= runtime->control->avail_min) {
2959 mask = POLLOUT | POLLWRNORM;
2960 break;
2961 }
2962 /* Fall through */
2963 case SNDRV_PCM_STATE_DRAINING:
2964 mask = 0;
2965 break;
2966 default:
2967 mask = POLLOUT | POLLWRNORM | POLLERR;
2968 break;
2969 }
2970 snd_pcm_stream_unlock_irq(substream);
2971 return mask;
2972}
2973
2974static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2975{
Takashi Iwai877211f2005-11-17 13:59:38 +01002976 struct snd_pcm_file *pcm_file;
2977 struct snd_pcm_substream *substream;
2978 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 unsigned int mask;
2980 snd_pcm_uframes_t avail;
2981
2982 pcm_file = file->private_data;
2983
2984 substream = pcm_file->substream;
2985 snd_assert(substream != NULL, return -ENXIO);
2986 runtime = substream->runtime;
2987
2988 poll_wait(file, &runtime->sleep, wait);
2989
2990 snd_pcm_stream_lock_irq(substream);
2991 avail = snd_pcm_capture_avail(runtime);
2992 switch (runtime->status->state) {
2993 case SNDRV_PCM_STATE_RUNNING:
2994 case SNDRV_PCM_STATE_PREPARED:
2995 case SNDRV_PCM_STATE_PAUSED:
2996 if (avail >= runtime->control->avail_min) {
2997 mask = POLLIN | POLLRDNORM;
2998 break;
2999 }
3000 mask = 0;
3001 break;
3002 case SNDRV_PCM_STATE_DRAINING:
3003 if (avail > 0) {
3004 mask = POLLIN | POLLRDNORM;
3005 break;
3006 }
3007 /* Fall through */
3008 default:
3009 mask = POLLIN | POLLRDNORM | POLLERR;
3010 break;
3011 }
3012 snd_pcm_stream_unlock_irq(substream);
3013 return mask;
3014}
3015
3016/*
3017 * mmap support
3018 */
3019
3020/*
3021 * Only on coherent architectures, we can mmap the status and the control records
3022 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3023 */
3024#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3025/*
3026 * mmap status record
3027 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003028static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
3029 unsigned long address, int *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Takashi Iwai877211f2005-11-17 13:59:38 +01003031 struct snd_pcm_substream *substream = area->vm_private_data;
3032 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct page * page;
3034
3035 if (substream == NULL)
3036 return NOPAGE_OOM;
3037 runtime = substream->runtime;
3038 page = virt_to_page(runtime->status);
Nick Pigginb5810032005-10-29 18:16:12 -07003039 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (type)
3041 *type = VM_FAULT_MINOR;
3042 return page;
3043}
3044
3045static struct vm_operations_struct snd_pcm_vm_ops_status =
3046{
3047 .nopage = snd_pcm_mmap_status_nopage,
3048};
3049
Takashi Iwai877211f2005-11-17 13:59:38 +01003050static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 struct vm_area_struct *area)
3052{
Takashi Iwai877211f2005-11-17 13:59:38 +01003053 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 long size;
3055 if (!(area->vm_flags & VM_READ))
3056 return -EINVAL;
3057 runtime = substream->runtime;
3058 snd_assert(runtime != NULL, return -EAGAIN);
3059 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003060 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return -EINVAL;
3062 area->vm_ops = &snd_pcm_vm_ops_status;
3063 area->vm_private_data = substream;
3064 area->vm_flags |= VM_RESERVED;
3065 return 0;
3066}
3067
3068/*
3069 * mmap control record
3070 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003071static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3072 unsigned long address, int *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Takashi Iwai877211f2005-11-17 13:59:38 +01003074 struct snd_pcm_substream *substream = area->vm_private_data;
3075 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 struct page * page;
3077
3078 if (substream == NULL)
3079 return NOPAGE_OOM;
3080 runtime = substream->runtime;
3081 page = virt_to_page(runtime->control);
Nick Pigginb5810032005-10-29 18:16:12 -07003082 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (type)
3084 *type = VM_FAULT_MINOR;
3085 return page;
3086}
3087
3088static struct vm_operations_struct snd_pcm_vm_ops_control =
3089{
3090 .nopage = snd_pcm_mmap_control_nopage,
3091};
3092
Takashi Iwai877211f2005-11-17 13:59:38 +01003093static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 struct vm_area_struct *area)
3095{
Takashi Iwai877211f2005-11-17 13:59:38 +01003096 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 long size;
3098 if (!(area->vm_flags & VM_READ))
3099 return -EINVAL;
3100 runtime = substream->runtime;
3101 snd_assert(runtime != NULL, return -EAGAIN);
3102 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003103 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return -EINVAL;
3105 area->vm_ops = &snd_pcm_vm_ops_control;
3106 area->vm_private_data = substream;
3107 area->vm_flags |= VM_RESERVED;
3108 return 0;
3109}
3110#else /* ! coherent mmap */
3111/*
3112 * don't support mmap for status and control records.
3113 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003114static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 struct vm_area_struct *area)
3116{
3117 return -ENXIO;
3118}
Takashi Iwai877211f2005-11-17 13:59:38 +01003119static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 struct vm_area_struct *area)
3121{
3122 return -ENXIO;
3123}
3124#endif /* coherent mmap */
3125
3126/*
3127 * nopage callback for mmapping a RAM page
3128 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003129static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3130 unsigned long address, int *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Takashi Iwai877211f2005-11-17 13:59:38 +01003132 struct snd_pcm_substream *substream = area->vm_private_data;
3133 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 unsigned long offset;
3135 struct page * page;
3136 void *vaddr;
3137 size_t dma_bytes;
3138
3139 if (substream == NULL)
3140 return NOPAGE_OOM;
3141 runtime = substream->runtime;
3142 offset = area->vm_pgoff << PAGE_SHIFT;
3143 offset += address - area->vm_start;
3144 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3145 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3146 if (offset > dma_bytes - PAGE_SIZE)
3147 return NOPAGE_SIGBUS;
3148 if (substream->ops->page) {
3149 page = substream->ops->page(substream, offset);
3150 if (! page)
3151 return NOPAGE_OOM;
3152 } else {
3153 vaddr = runtime->dma_area + offset;
3154 page = virt_to_page(vaddr);
3155 }
Nick Pigginb5810032005-10-29 18:16:12 -07003156 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if (type)
3158 *type = VM_FAULT_MINOR;
3159 return page;
3160}
3161
3162static struct vm_operations_struct snd_pcm_vm_ops_data =
3163{
3164 .open = snd_pcm_mmap_data_open,
3165 .close = snd_pcm_mmap_data_close,
3166 .nopage = snd_pcm_mmap_data_nopage,
3167};
3168
3169/*
3170 * mmap the DMA buffer on RAM
3171 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003172static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3173 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
3175 area->vm_ops = &snd_pcm_vm_ops_data;
3176 area->vm_private_data = substream;
3177 area->vm_flags |= VM_RESERVED;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003178 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 return 0;
3180}
3181
3182/*
3183 * mmap the DMA buffer on I/O memory area
3184 */
3185#if SNDRV_PCM_INFO_MMAP_IOMEM
3186static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3187{
3188 .open = snd_pcm_mmap_data_open,
3189 .close = snd_pcm_mmap_data_close,
3190};
3191
Takashi Iwai877211f2005-11-17 13:59:38 +01003192int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3193 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
3195 long size;
3196 unsigned long offset;
3197
3198#ifdef pgprot_noncached
3199 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3200#endif
3201 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3202 area->vm_private_data = substream;
3203 area->vm_flags |= VM_IO;
3204 size = area->vm_end - area->vm_start;
3205 offset = area->vm_pgoff << PAGE_SHIFT;
3206 if (io_remap_pfn_range(area, area->vm_start,
3207 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3208 size, area->vm_page_prot))
3209 return -EAGAIN;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003210 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return 0;
3212}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003213
3214EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215#endif /* SNDRV_PCM_INFO_MMAP */
3216
3217/*
3218 * mmap DMA buffer
3219 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003220int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 struct vm_area_struct *area)
3222{
Takashi Iwai877211f2005-11-17 13:59:38 +01003223 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 long size;
3225 unsigned long offset;
3226 size_t dma_bytes;
3227
3228 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3229 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3230 return -EINVAL;
3231 } else {
3232 if (!(area->vm_flags & VM_READ))
3233 return -EINVAL;
3234 }
3235 runtime = substream->runtime;
3236 snd_assert(runtime != NULL, return -EAGAIN);
3237 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3238 return -EBADFD;
3239 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3240 return -ENXIO;
3241 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3242 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3243 return -EINVAL;
3244 size = area->vm_end - area->vm_start;
3245 offset = area->vm_pgoff << PAGE_SHIFT;
3246 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3247 if ((size_t)size > dma_bytes)
3248 return -EINVAL;
3249 if (offset > dma_bytes - size)
3250 return -EINVAL;
3251
3252 if (substream->ops->mmap)
3253 return substream->ops->mmap(substream, area);
3254 else
3255 return snd_pcm_default_mmap(substream, area);
3256}
3257
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003258EXPORT_SYMBOL(snd_pcm_mmap_data);
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3261{
Takashi Iwai877211f2005-11-17 13:59:38 +01003262 struct snd_pcm_file * pcm_file;
3263 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 unsigned long offset;
3265
3266 pcm_file = file->private_data;
3267 substream = pcm_file->substream;
3268 snd_assert(substream != NULL, return -ENXIO);
3269
3270 offset = area->vm_pgoff << PAGE_SHIFT;
3271 switch (offset) {
3272 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3273 if (substream->no_mmap_ctrl)
3274 return -ENXIO;
3275 return snd_pcm_mmap_status(substream, file, area);
3276 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3277 if (substream->no_mmap_ctrl)
3278 return -ENXIO;
3279 return snd_pcm_mmap_control(substream, file, area);
3280 default:
3281 return snd_pcm_mmap_data(substream, file, area);
3282 }
3283 return 0;
3284}
3285
3286static int snd_pcm_fasync(int fd, struct file * file, int on)
3287{
Takashi Iwai877211f2005-11-17 13:59:38 +01003288 struct snd_pcm_file * pcm_file;
3289 struct snd_pcm_substream *substream;
3290 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 int err;
3292
3293 pcm_file = file->private_data;
3294 substream = pcm_file->substream;
3295 snd_assert(substream != NULL, return -ENXIO);
3296 runtime = substream->runtime;
3297
3298 err = fasync_helper(fd, file, on, &runtime->fasync);
3299 if (err < 0)
3300 return err;
3301 return 0;
3302}
3303
3304/*
3305 * ioctl32 compat
3306 */
3307#ifdef CONFIG_COMPAT
3308#include "pcm_compat.c"
3309#else
3310#define snd_pcm_ioctl_compat NULL
3311#endif
3312
3313/*
3314 * To be removed helpers to keep binary compatibility
3315 */
3316
Takashi Iwai59d48582005-12-01 10:51:58 +01003317#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3319#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3320
Takashi Iwai877211f2005-11-17 13:59:38 +01003321static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3322 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
3324 unsigned int i;
3325
3326 memset(params, 0, sizeof(*params));
3327 params->flags = oparams->flags;
3328 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3329 params->masks[i].bits[0] = oparams->masks[i];
3330 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3331 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3332 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3333 params->info = oparams->info;
3334 params->msbits = oparams->msbits;
3335 params->rate_num = oparams->rate_num;
3336 params->rate_den = oparams->rate_den;
3337 params->fifo_size = oparams->fifo_size;
3338}
3339
Takashi Iwai877211f2005-11-17 13:59:38 +01003340static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3341 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
3343 unsigned int i;
3344
3345 memset(oparams, 0, sizeof(*oparams));
3346 oparams->flags = params->flags;
3347 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3348 oparams->masks[i] = params->masks[i].bits[0];
3349 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3350 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3351 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3352 oparams->info = params->info;
3353 oparams->msbits = params->msbits;
3354 oparams->rate_num = params->rate_num;
3355 oparams->rate_den = params->rate_den;
3356 oparams->fifo_size = params->fifo_size;
3357}
3358
Takashi Iwai877211f2005-11-17 13:59:38 +01003359static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3360 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
Takashi Iwai877211f2005-11-17 13:59:38 +01003362 struct snd_pcm_hw_params *params;
3363 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 int err;
3365
3366 params = kmalloc(sizeof(*params), GFP_KERNEL);
3367 if (!params) {
3368 err = -ENOMEM;
3369 goto out;
3370 }
3371 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3372 if (!oparams) {
3373 err = -ENOMEM;
3374 goto out;
3375 }
3376
3377 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3378 err = -EFAULT;
3379 goto out;
3380 }
3381 snd_pcm_hw_convert_from_old_params(params, oparams);
3382 err = snd_pcm_hw_refine(substream, params);
3383 snd_pcm_hw_convert_to_old_params(oparams, params);
3384 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3385 if (!err)
3386 err = -EFAULT;
3387 }
3388out:
3389 kfree(params);
3390 kfree(oparams);
3391 return err;
3392}
3393
Takashi Iwai877211f2005-11-17 13:59:38 +01003394static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3395 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
Takashi Iwai877211f2005-11-17 13:59:38 +01003397 struct snd_pcm_hw_params *params;
3398 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 int err;
3400
3401 params = kmalloc(sizeof(*params), GFP_KERNEL);
3402 if (!params) {
3403 err = -ENOMEM;
3404 goto out;
3405 }
3406 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3407 if (!oparams) {
3408 err = -ENOMEM;
3409 goto out;
3410 }
3411 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3412 err = -EFAULT;
3413 goto out;
3414 }
3415 snd_pcm_hw_convert_from_old_params(params, oparams);
3416 err = snd_pcm_hw_params(substream, params);
3417 snd_pcm_hw_convert_to_old_params(oparams, params);
3418 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3419 if (!err)
3420 err = -EFAULT;
3421 }
3422out:
3423 kfree(params);
3424 kfree(oparams);
3425 return err;
3426}
Takashi Iwai59d48582005-12-01 10:51:58 +01003427#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
3429/*
3430 * Register section
3431 */
3432
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003433struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003435 .owner = THIS_MODULE,
3436 .write = snd_pcm_write,
3437 .writev = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003438 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003439 .release = snd_pcm_release,
3440 .poll = snd_pcm_playback_poll,
3441 .unlocked_ioctl = snd_pcm_playback_ioctl,
3442 .compat_ioctl = snd_pcm_ioctl_compat,
3443 .mmap = snd_pcm_mmap,
3444 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 },
3446 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003447 .owner = THIS_MODULE,
3448 .read = snd_pcm_read,
3449 .readv = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003450 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003451 .release = snd_pcm_release,
3452 .poll = snd_pcm_capture_poll,
3453 .unlocked_ioctl = snd_pcm_capture_ioctl,
3454 .compat_ioctl = snd_pcm_ioctl_compat,
3455 .mmap = snd_pcm_mmap,
3456 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
3458};