2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
22 #include <sound/driver.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/uio.h>
28 #include <sound/core.h>
29 #include <sound/control.h>
30 #include <sound/info.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/timer.h>
34 #include <sound/minors.h>
40 struct sndrv_pcm_hw_params_old {
42 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
43 SNDRV_PCM_HW_PARAM_ACCESS + 1];
44 struct sndrv_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
45 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
50 unsigned int rate_num;
51 unsigned int rate_den;
52 sndrv_pcm_uframes_t fifo_size;
53 unsigned char reserved[64];
56 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct sndrv_pcm_hw_params_old)
57 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct sndrv_pcm_hw_params_old)
59 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams);
60 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams);
66 rwlock_t snd_pcm_link_rwlock = RW_LOCK_UNLOCKED;
69 static inline mm_segment_t snd_enter_user(void)
71 mm_segment_t fs = get_fs();
76 static inline void snd_leave_user(mm_segment_t fs)
83 int snd_pcm_info(snd_pcm_substream_t * substream, snd_pcm_info_t *info)
85 snd_pcm_runtime_t * runtime;
86 snd_pcm_t *pcm = substream->pcm;
87 snd_pcm_str_t *pstr = substream->pstr;
89 snd_assert(substream != NULL, return -ENXIO);
90 memset(info, 0, sizeof(*info));
91 info->card = pcm->card->number;
92 info->device = pcm->device;
93 info->stream = substream->stream;
94 info->subdevice = substream->number;
95 strlcpy(info->id, pcm->id, sizeof(info->id));
96 strlcpy(info->name, pcm->name, sizeof(info->name));
97 info->dev_class = pcm->dev_class;
98 info->dev_subclass = pcm->dev_subclass;
99 info->subdevices_count = pstr->substream_count;
100 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
101 strlcpy(info->subname, substream->name, sizeof(info->subname));
102 runtime = substream->runtime;
103 /* AB: FIXME!!! This is definitely nonsense */
105 info->sync = runtime->sync;
106 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
111 int snd_pcm_info_user(snd_pcm_substream_t * substream, snd_pcm_info_t * _info)
114 int err = snd_pcm_info(substream, &info);
115 if (copy_to_user(_info, &info, sizeof(info)))
123 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
124 char *snd_pcm_hw_param_names[] = {
128 HW_PARAM(SAMPLE_BITS),
129 HW_PARAM(FRAME_BITS),
132 HW_PARAM(PERIOD_TIME),
133 HW_PARAM(PERIOD_SIZE),
134 HW_PARAM(PERIOD_BYTES),
136 HW_PARAM(BUFFER_TIME),
137 HW_PARAM(BUFFER_SIZE),
138 HW_PARAM(BUFFER_BYTES),
143 int snd_pcm_hw_refine(snd_pcm_substream_t *substream,
144 snd_pcm_hw_params_t *params)
147 snd_pcm_hardware_t *hw;
148 snd_interval_t *i = NULL;
149 snd_mask_t *m = NULL;
150 snd_pcm_hw_constraints_t *constrs = &substream->runtime->hw_constraints;
151 unsigned int rstamps[constrs->rules_num];
152 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
153 unsigned int stamp = 2;
157 params->fifo_size = 0;
158 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
160 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
161 params->rate_num = 0;
162 params->rate_den = 0;
165 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
166 m = hw_param_mask(params, k);
167 if (snd_mask_empty(m))
169 if (!(params->rmask & (1 << k)))
172 printk("%s = ", snd_pcm_hw_param_names[k]);
173 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
175 changed = snd_mask_refine(m, constrs_mask(constrs, k));
177 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
180 params->cmask |= 1 << k;
185 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
186 i = hw_param_interval(params, k);
187 if (snd_interval_empty(i))
189 if (!(params->rmask & (1 << k)))
192 printk("%s = ", snd_pcm_hw_param_names[k]);
197 i->openmin ? '(' : '[', i->min,
198 i->max, i->openmax ? ')' : ']');
201 changed = snd_interval_refine(i, constrs_interval(constrs, k));
206 printk("%c%u %u%c\n",
207 i->openmin ? '(' : '[', i->min,
208 i->max, i->openmax ? ')' : ']');
211 params->cmask |= 1 << k;
216 for (k = 0; k < constrs->rules_num; k++)
218 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
219 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
222 for (k = 0; k < constrs->rules_num; k++) {
223 snd_pcm_hw_rule_t *r = &constrs->rules[k];
226 if (r->cond && !(r->cond & params->flags))
228 for (d = 0; r->deps[d] >= 0; d++) {
229 if (vstamps[r->deps[d]] > rstamps[k]) {
237 printk("Rule %d [%p]: ", k, r->func);
239 printk("%s = ", snd_pcm_hw_param_names[r->var]);
240 if (hw_is_mask(r->var)) {
241 m = hw_param_mask(params, r->var);
242 printk("%x", *m->bits);
244 i = hw_param_interval(params, r->var);
249 i->openmin ? '(' : '[', i->min,
250 i->max, i->openmax ? ')' : ']');
254 changed = r->func(params, r);
258 if (hw_is_mask(r->var))
259 printk("%x", *m->bits);
265 i->openmin ? '(' : '[', i->min,
266 i->max, i->openmax ? ')' : ']');
272 if (changed && r->var >= 0) {
273 params->cmask |= (1 << r->var);
274 vstamps[r->var] = stamp;
282 if (!params->msbits) {
283 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
284 if (snd_interval_single(i))
285 params->msbits = snd_interval_value(i);
288 if (!params->rate_den) {
289 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
290 if (snd_interval_single(i)) {
291 params->rate_num = snd_interval_value(i);
292 params->rate_den = 1;
296 hw = &substream->runtime->hw;
298 params->info = hw->info;
299 if (!params->fifo_size)
300 params->fifo_size = hw->fifo_size;
305 static int snd_pcm_hw_refine_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t * _params)
307 snd_pcm_hw_params_t params;
309 if (copy_from_user(¶ms, _params, sizeof(params)))
311 err = snd_pcm_hw_refine(substream, ¶ms);
312 if (copy_to_user(_params, ¶ms, sizeof(params)))
317 static int snd_pcm_hw_params(snd_pcm_substream_t *substream,
318 snd_pcm_hw_params_t *params)
320 snd_pcm_runtime_t *runtime;
323 snd_pcm_uframes_t frames;
325 snd_assert(substream != NULL, return -ENXIO);
326 runtime = substream->runtime;
327 snd_assert(runtime != NULL, return -ENXIO);
328 switch (runtime->status->state) {
329 case SNDRV_PCM_STATE_OPEN:
330 case SNDRV_PCM_STATE_SETUP:
331 case SNDRV_PCM_STATE_PREPARED:
336 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
337 if (!substream->oss.oss)
339 if (atomic_read(&runtime->mmap_count))
343 err = snd_pcm_hw_refine(substream, params);
347 err = snd_pcm_hw_params_choose(substream, params);
351 if (substream->ops->hw_params != NULL) {
352 err = substream->ops->hw_params(substream, params);
357 runtime->access = params_access(params);
358 runtime->format = params_format(params);
359 runtime->subformat = params_subformat(params);
360 runtime->channels = params_channels(params);
361 runtime->rate = params_rate(params);
362 runtime->period_size = params_period_size(params);
363 runtime->periods = params_periods(params);
364 runtime->buffer_size = params_buffer_size(params);
365 runtime->tick_time = params_tick_time(params);
366 runtime->info = params->info;
367 runtime->rate_num = params->rate_num;
368 runtime->rate_den = params->rate_den;
370 bits = snd_pcm_format_physical_width(runtime->format);
371 runtime->sample_bits = bits;
372 bits *= runtime->channels;
373 runtime->frame_bits = bits;
375 while (bits % 8 != 0) {
379 runtime->byte_align = bits / 8;
380 runtime->min_align = frames;
382 /* Default sw params */
383 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
384 runtime->period_step = 1;
385 runtime->sleep_min = 0;
386 runtime->control->avail_min = runtime->period_size;
387 runtime->xfer_align = runtime->period_size;
388 runtime->start_threshold = 1;
389 runtime->stop_threshold = runtime->buffer_size;
390 runtime->silence_threshold = 0;
391 runtime->silence_size = 0;
392 runtime->boundary = runtime->buffer_size;
393 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
394 runtime->boundary *= 2;
396 snd_pcm_timer_resolution_change(substream);
397 runtime->status->state = SNDRV_PCM_STATE_SETUP;
400 /* hardware might be unuseable from this time,
401 so we force application to retry to set
402 the correct hardware parameter settings */
403 runtime->status->state = SNDRV_PCM_STATE_OPEN;
404 if (substream->ops->hw_free != NULL)
405 substream->ops->hw_free(substream);
409 static int snd_pcm_hw_params_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t * _params)
411 snd_pcm_hw_params_t params;
413 if (copy_from_user(¶ms, _params, sizeof(params)))
415 err = snd_pcm_hw_params(substream, ¶ms);
416 if (copy_to_user(_params, ¶ms, sizeof(params)))
421 static int snd_pcm_hw_free(snd_pcm_substream_t * substream)
423 snd_pcm_runtime_t *runtime;
426 snd_assert(substream != NULL, return -ENXIO);
427 runtime = substream->runtime;
428 snd_assert(runtime != NULL, return -ENXIO);
429 switch (runtime->status->state) {
430 case SNDRV_PCM_STATE_SETUP:
431 case SNDRV_PCM_STATE_PREPARED:
436 if (atomic_read(&runtime->mmap_count))
438 if (substream->ops->hw_free == NULL) {
439 runtime->status->state = SNDRV_PCM_STATE_OPEN;
442 result = substream->ops->hw_free(substream);
443 runtime->status->state = SNDRV_PCM_STATE_OPEN;
447 static int snd_pcm_sw_params(snd_pcm_substream_t * substream, snd_pcm_sw_params_t *params)
449 snd_pcm_runtime_t *runtime;
450 snd_assert(substream != NULL, return -ENXIO);
451 runtime = substream->runtime;
452 snd_assert(runtime != NULL, return -ENXIO);
453 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
456 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
458 if (params->avail_min == 0)
460 if (params->xfer_align == 0 ||
461 params->xfer_align % runtime->min_align != 0)
463 if (params->silence_size >= runtime->boundary) {
464 if (params->silence_threshold != 0)
467 if (params->silence_size > params->silence_threshold)
469 if (params->silence_threshold > runtime->buffer_size)
472 snd_pcm_stream_lock_irq(substream);
473 runtime->tstamp_mode = params->tstamp_mode;
474 runtime->sleep_min = params->sleep_min;
475 runtime->period_step = params->period_step;
476 runtime->control->avail_min = params->avail_min;
477 runtime->start_threshold = params->start_threshold;
478 runtime->stop_threshold = params->stop_threshold;
479 runtime->silence_threshold = params->silence_threshold;
480 runtime->silence_size = params->silence_size;
481 runtime->xfer_align = params->xfer_align;
482 params->boundary = runtime->boundary;
483 if (snd_pcm_running(substream)) {
484 if (runtime->sleep_min)
485 snd_pcm_tick_prepare(substream);
487 snd_pcm_tick_set(substream, 0);
488 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
489 runtime->silence_size > 0)
490 snd_pcm_playback_silence(substream, ULONG_MAX);
491 wake_up(&runtime->sleep);
493 snd_pcm_stream_unlock_irq(substream);
497 static int snd_pcm_sw_params_user(snd_pcm_substream_t * substream, snd_pcm_sw_params_t * _params)
499 snd_pcm_sw_params_t params;
501 if (copy_from_user(¶ms, _params, sizeof(params)))
503 err = snd_pcm_sw_params(substream, ¶ms);
504 if (copy_to_user(_params, ¶ms, sizeof(params)))
509 int snd_pcm_status(snd_pcm_substream_t *substream,
510 snd_pcm_status_t *status)
512 snd_pcm_runtime_t *runtime = substream->runtime;
514 snd_pcm_stream_lock_irq(substream);
515 status->state = runtime->status->state;
516 status->suspended_state = runtime->status->suspended_state;
517 if (status->state == SNDRV_PCM_STATE_OPEN)
519 status->trigger_tstamp = runtime->trigger_tstamp;
520 if (snd_pcm_running(substream)) {
521 snd_pcm_update_hw_ptr(substream);
522 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
523 status->tstamp = runtime->status->tstamp;
525 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
527 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
528 status->appl_ptr = runtime->control->appl_ptr;
529 status->hw_ptr = runtime->status->hw_ptr;
530 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
531 status->avail = snd_pcm_playback_avail(runtime);
532 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
533 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
534 status->delay = runtime->buffer_size - status->avail;
538 status->avail = snd_pcm_capture_avail(runtime);
539 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
540 status->delay = status->avail;
544 status->avail_max = runtime->avail_max;
545 status->overrange = runtime->overrange;
546 runtime->avail_max = 0;
547 runtime->overrange = 0;
549 snd_pcm_stream_unlock_irq(substream);
553 static int snd_pcm_status_user(snd_pcm_substream_t * substream, snd_pcm_status_t * _status)
555 snd_pcm_status_t status;
556 snd_pcm_runtime_t *runtime;
559 snd_assert(substream != NULL, return -ENXIO);
560 runtime = substream->runtime;
561 memset(&status, 0, sizeof(status));
562 res = snd_pcm_status(substream, &status);
565 if (copy_to_user(_status, &status, sizeof(status)))
570 static int snd_pcm_channel_info(snd_pcm_substream_t * substream, snd_pcm_channel_info_t * _info)
572 snd_pcm_channel_info_t info;
573 snd_pcm_runtime_t *runtime;
575 unsigned int channel;
577 snd_assert(substream != NULL, return -ENXIO);
578 if (copy_from_user(&info, _info, sizeof(info)))
580 channel = info.channel;
581 runtime = substream->runtime;
582 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
584 if (channel >= runtime->channels)
586 memset(&info, 0, sizeof(info));
587 info.channel = channel;
588 res = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, &info);
591 if (copy_to_user(_info, &info, sizeof(info)))
596 static void snd_pcm_trigger_tstamp(snd_pcm_substream_t *substream)
598 snd_pcm_runtime_t *runtime = substream->runtime;
599 if (runtime->trigger_master == NULL)
601 if (runtime->trigger_master == substream) {
602 snd_timestamp_now(&runtime->trigger_tstamp, runtime->tstamp_timespec);
604 snd_pcm_trigger_tstamp(runtime->trigger_master);
605 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
607 runtime->trigger_master = NULL;
611 int (*pre_action)(snd_pcm_substream_t *substream, int state);
612 int (*do_action)(snd_pcm_substream_t *substream, int state);
613 void (*post_action)(snd_pcm_substream_t *substream, int state);
617 * this functions is core for handling of linked stream
618 * Note: the stream state might be changed also on failure
619 * Note2: call with calling stream lock + link lock
621 static int snd_pcm_action_group(struct action_ops *ops,
622 snd_pcm_substream_t *substream,
623 int state, int atomic_only)
625 struct list_head *pos;
626 snd_pcm_substream_t *s = NULL;
629 snd_pcm_group_for_each(pos, substream) {
630 s = snd_pcm_group_substream_entry(pos);
631 if (atomic_only && (s->pcm->info_flags & SNDRV_PCM_INFO_NONATOMIC_OPS))
634 spin_lock(&s->self_group.lock);
635 res = ops->pre_action(s, state);
640 snd_pcm_group_for_each(pos, substream) {
641 s = snd_pcm_group_substream_entry(pos);
642 if (atomic_only && (s->pcm->info_flags & SNDRV_PCM_INFO_NONATOMIC_OPS))
644 err = ops->do_action(s, state);
649 ops->post_action(s, state);
652 spin_unlock(&s->self_group.lock);
655 snd_pcm_substream_t *s1;
656 /* unlock all streams */
657 snd_pcm_group_for_each(pos, substream) {
658 s1 = snd_pcm_group_substream_entry(pos);
659 if (atomic_only && (s1->pcm->info_flags & SNDRV_PCM_INFO_NONATOMIC_OPS))
661 else if (s1 != substream)
662 spin_unlock(&s1->self_group.lock);
663 if (s1 == s) /* end */
671 * Note: call with stream lock
673 static int snd_pcm_action_single(struct action_ops *ops,
674 snd_pcm_substream_t *substream,
679 res = ops->pre_action(substream, state);
682 res = ops->do_action(substream, state);
684 ops->post_action(substream, state);
690 * Note: call with stream lock
692 * NB2: this won't handle the non-atomic callbacks
694 static int snd_pcm_action(struct action_ops *ops,
695 snd_pcm_substream_t *substream,
700 if (snd_pcm_stream_linked(substream)) {
701 if (!spin_trylock(&substream->group->lock)) {
702 spin_unlock(&substream->self_group.lock);
703 spin_lock(&substream->group->lock);
704 spin_lock(&substream->self_group.lock);
706 res = snd_pcm_action_group(ops, substream, state, 0);
707 spin_unlock(&substream->group->lock);
709 res = snd_pcm_action_single(ops, substream, state);
715 * Note: don't use any locks before
717 * NB2: this can handle the non-atomic callbacks if allow_nonatomic = 1
718 * when the pcm->info_flags has NONATOMIC_OPS bit, it's handled
719 * ouside the lock to allow sleep in the callback.
721 static int snd_pcm_action_lock_irq(struct action_ops *ops,
722 snd_pcm_substream_t *substream,
723 int state, int allow_nonatomic)
727 read_lock_irq(&snd_pcm_link_rwlock);
728 if (snd_pcm_stream_linked(substream)) {
729 spin_lock(&substream->group->lock);
730 spin_lock(&substream->self_group.lock);
731 res = snd_pcm_action_group(ops, substream, state, allow_nonatomic);
732 spin_unlock(&substream->self_group.lock);
733 spin_unlock(&substream->group->lock);
734 if (res >= 0 && allow_nonatomic) {
735 /* now process the non-atomic substreams separately
738 #define MAX_LINKED_STREAMS 16 /* FIXME: should be variable */
740 struct list_head *pos;
742 snd_pcm_substream_t *s;
743 snd_pcm_substream_t *subs[MAX_LINKED_STREAMS];
744 snd_pcm_group_for_each(pos, substream) {
745 if (num_s >= MAX_LINKED_STREAMS) {
747 num_s = 0; /* don't proceed */
750 s = snd_pcm_group_substream_entry(pos);
751 if (s->pcm->info_flags & SNDRV_PCM_INFO_NONATOMIC_OPS)
755 read_unlock_irq(&snd_pcm_link_rwlock);
756 for (i = 0; i < num_s && res >= 0; i++)
757 res = snd_pcm_action_single(ops, subs[i], state);
762 if (allow_nonatomic &&
763 (substream->pcm->info_flags & SNDRV_PCM_INFO_NONATOMIC_OPS)) {
764 read_unlock_irq(&snd_pcm_link_rwlock);
765 /* process outside the lock */
766 return snd_pcm_action_single(ops, substream, state);
768 spin_lock(&substream->self_group.lock);
769 res = snd_pcm_action_single(ops, substream, state);
770 spin_unlock(&substream->self_group.lock);
772 read_unlock_irq(&snd_pcm_link_rwlock);
776 static int snd_pcm_pre_start(snd_pcm_substream_t *substream, int state)
778 snd_pcm_runtime_t *runtime = substream->runtime;
779 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
781 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
782 !snd_pcm_playback_data(substream))
784 runtime->trigger_master = substream;
788 static int snd_pcm_do_start(snd_pcm_substream_t *substream, int state)
790 if (substream->runtime->trigger_master != substream)
792 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
795 static void snd_pcm_post_start(snd_pcm_substream_t *substream, int state)
797 snd_pcm_runtime_t *runtime = substream->runtime;
798 snd_pcm_trigger_tstamp(substream);
799 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
800 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
801 runtime->silence_size > 0)
802 snd_pcm_playback_silence(substream, ULONG_MAX);
803 if (runtime->sleep_min)
804 snd_pcm_tick_prepare(substream);
805 if (substream->timer)
806 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART, &runtime->trigger_tstamp);
809 static struct action_ops snd_pcm_action_start = {
810 .pre_action = snd_pcm_pre_start,
811 .do_action = snd_pcm_do_start,
812 .post_action = snd_pcm_post_start
818 int snd_pcm_start(snd_pcm_substream_t *substream)
820 return snd_pcm_action(&snd_pcm_action_start, substream, 0);
823 static int snd_pcm_pre_stop(snd_pcm_substream_t *substream, int state)
825 snd_pcm_runtime_t *runtime = substream->runtime;
826 if (substream->runtime->status->state != SNDRV_PCM_STATE_RUNNING &&
827 substream->runtime->status->state != SNDRV_PCM_STATE_DRAINING)
829 runtime->trigger_master = substream;
833 static int snd_pcm_do_stop(snd_pcm_substream_t *substream, int state)
835 if (substream->runtime->trigger_master != substream)
837 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
840 static void snd_pcm_post_stop(snd_pcm_substream_t *substream, int state)
842 snd_pcm_runtime_t *runtime = substream->runtime;
843 snd_pcm_trigger_tstamp(substream);
844 if (substream->timer)
845 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP, &runtime->trigger_tstamp);
846 runtime->status->state = state;
847 snd_pcm_tick_set(substream, 0);
848 wake_up(&runtime->sleep);
851 static struct action_ops snd_pcm_action_stop = {
852 .pre_action = snd_pcm_pre_stop,
853 .do_action = snd_pcm_do_stop,
854 .post_action = snd_pcm_post_stop
860 int snd_pcm_stop(snd_pcm_substream_t *substream, int state)
862 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
865 static int snd_pcm_pre_pause(snd_pcm_substream_t *substream, int push)
867 snd_pcm_runtime_t *runtime = substream->runtime;
868 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
871 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
873 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
875 runtime->trigger_master = substream;
879 static int snd_pcm_do_pause(snd_pcm_substream_t *substream, int push)
881 if (substream->runtime->trigger_master != substream)
883 return substream->ops->trigger(substream,
884 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
885 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
888 static void snd_pcm_post_pause(snd_pcm_substream_t *substream, int push)
890 snd_pcm_runtime_t *runtime = substream->runtime;
891 snd_pcm_trigger_tstamp(substream);
893 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
894 if (substream->timer)
895 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
896 snd_pcm_tick_set(substream, 0);
897 wake_up(&runtime->sleep);
899 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
900 if (runtime->sleep_min)
901 snd_pcm_tick_prepare(substream);
902 if (substream->timer)
903 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
907 static struct action_ops snd_pcm_action_pause = {
908 .pre_action = snd_pcm_pre_pause,
909 .do_action = snd_pcm_do_pause,
910 .post_action = snd_pcm_post_pause
913 static int snd_pcm_pause(snd_pcm_substream_t *substream, int push)
915 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
921 static int snd_pcm_pre_suspend(snd_pcm_substream_t *substream, int state)
923 snd_pcm_runtime_t *runtime = substream->runtime;
924 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
926 runtime->status->suspended_state = runtime->status->state;
927 runtime->trigger_master = substream;
931 static int snd_pcm_do_suspend(snd_pcm_substream_t *substream, int state)
933 snd_pcm_runtime_t *runtime = substream->runtime;
934 if (runtime->trigger_master != substream)
936 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
937 runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING)
939 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
942 static void snd_pcm_post_suspend(snd_pcm_substream_t *substream, int state)
944 snd_pcm_runtime_t *runtime = substream->runtime;
945 snd_pcm_trigger_tstamp(substream);
946 if (substream->timer)
947 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
948 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
949 snd_pcm_tick_set(substream, 0);
950 wake_up(&runtime->sleep);
953 static struct action_ops snd_pcm_action_suspend = {
954 .pre_action = snd_pcm_pre_suspend,
955 .do_action = snd_pcm_do_suspend,
956 .post_action = snd_pcm_post_suspend
962 int snd_pcm_suspend(snd_pcm_substream_t *substream)
964 return snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
968 * snd_pcm_suspend_all
970 int snd_pcm_suspend_all(snd_pcm_t *pcm)
972 snd_pcm_substream_t *substream;
975 for (stream = 0; stream < 2; stream++) {
976 for (substream = pcm->streams[stream].substream; substream; substream = substream->next) {
977 /* FIXME: the open/close code should lock this as well */
978 if (substream->runtime == NULL)
980 snd_pcm_stream_lock(substream);
981 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
982 snd_pcm_stream_unlock(substream);
985 if ((err = snd_pcm_suspend(substream)) < 0) {
986 snd_pcm_stream_unlock(substream);
989 snd_pcm_stream_unlock(substream);
997 static int snd_pcm_pre_resume(snd_pcm_substream_t *substream, int state)
999 snd_pcm_runtime_t *runtime = substream->runtime;
1000 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1002 runtime->trigger_master = substream;
1006 static int snd_pcm_do_resume(snd_pcm_substream_t *substream, int state)
1008 snd_pcm_runtime_t *runtime = substream->runtime;
1009 if (runtime->trigger_master != substream)
1011 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1012 runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING)
1014 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1017 static void snd_pcm_post_resume(snd_pcm_substream_t *substream, int state)
1019 snd_pcm_runtime_t *runtime = substream->runtime;
1020 snd_pcm_trigger_tstamp(substream);
1021 if (substream->timer)
1022 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
1023 runtime->status->state = runtime->status->suspended_state;
1024 if (runtime->sleep_min)
1025 snd_pcm_tick_prepare(substream);
1028 static struct action_ops snd_pcm_action_resume = {
1029 .pre_action = snd_pcm_pre_resume,
1030 .do_action = snd_pcm_do_resume,
1031 .post_action = snd_pcm_post_resume
1034 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1036 snd_card_t *card = substream->pcm->card;
1039 snd_power_lock(card);
1040 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1041 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0, 0);
1042 snd_power_unlock(card);
1048 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1053 #endif /* CONFIG_PM */
1055 static int snd_pcm_xrun(snd_pcm_substream_t *substream)
1057 snd_card_t *card = substream->pcm->card;
1058 snd_pcm_runtime_t *runtime = substream->runtime;
1061 snd_power_lock(card);
1062 snd_pcm_stream_lock_irq(substream);
1064 switch (runtime->status->state) {
1065 case SNDRV_PCM_STATE_XRUN:
1066 result = 0; /* already there */
1068 case SNDRV_PCM_STATE_RUNNING:
1069 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1071 case SNDRV_PCM_STATE_SUSPENDED:
1072 snd_pcm_stream_unlock_irq(substream);
1073 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1074 snd_pcm_stream_lock_irq(substream);
1076 goto _xrun_recovery;
1081 snd_pcm_stream_unlock_irq(substream);
1082 snd_power_unlock(card);
1086 static int snd_pcm_pre_reset(snd_pcm_substream_t * substream, int state)
1088 snd_pcm_runtime_t *runtime = substream->runtime;
1089 switch (runtime->status->state) {
1090 case SNDRV_PCM_STATE_RUNNING:
1091 case SNDRV_PCM_STATE_PREPARED:
1092 case SNDRV_PCM_STATE_PAUSED:
1093 case SNDRV_PCM_STATE_SUSPENDED:
1100 static int snd_pcm_do_reset(snd_pcm_substream_t * substream, int state)
1102 snd_pcm_runtime_t *runtime = substream->runtime;
1103 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, 0);
1106 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1107 runtime->hw_ptr_base = 0;
1108 runtime->hw_ptr_interrupt = runtime->status->hw_ptr - runtime->status->hw_ptr % runtime->period_size;
1109 runtime->silence_start = runtime->status->hw_ptr;
1110 runtime->silence_filled = 0;
1114 static void snd_pcm_post_reset(snd_pcm_substream_t * substream, int state)
1116 snd_pcm_runtime_t *runtime = substream->runtime;
1117 runtime->control->appl_ptr = runtime->status->hw_ptr;
1118 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1119 runtime->silence_size > 0)
1120 snd_pcm_playback_silence(substream, ULONG_MAX);
1123 static struct action_ops snd_pcm_action_reset = {
1124 .pre_action = snd_pcm_pre_reset,
1125 .do_action = snd_pcm_do_reset,
1126 .post_action = snd_pcm_post_reset
1129 static int snd_pcm_reset(snd_pcm_substream_t *substream)
1131 return snd_pcm_action_lock_irq(&snd_pcm_action_reset, substream, 0, 0);
1134 static int snd_pcm_pre_prepare(snd_pcm_substream_t * substream, int state)
1136 snd_pcm_runtime_t *runtime = substream->runtime;
1137 switch (runtime->status->state) {
1138 case SNDRV_PCM_STATE_OPEN:
1140 case SNDRV_PCM_STATE_RUNNING:
1147 static int snd_pcm_do_prepare(snd_pcm_substream_t * substream, int state)
1150 err = substream->ops->prepare(substream);
1153 return snd_pcm_do_reset(substream, 0);
1156 static void snd_pcm_post_prepare(snd_pcm_substream_t * substream, int state)
1158 snd_pcm_runtime_t *runtime = substream->runtime;
1159 runtime->control->appl_ptr = runtime->status->hw_ptr;
1160 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1163 static struct action_ops snd_pcm_action_prepare = {
1164 .pre_action = snd_pcm_pre_prepare,
1165 .do_action = snd_pcm_do_prepare,
1166 .post_action = snd_pcm_post_prepare
1172 int snd_pcm_prepare(snd_pcm_substream_t *substream)
1175 snd_card_t *card = substream->pcm->card;
1177 snd_power_lock(card);
1178 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1179 res = snd_pcm_action_lock_irq(&snd_pcm_action_prepare, substream, 0, 1); /* allow sleep if specified */
1180 snd_power_unlock(card);
1184 static void snd_pcm_change_state(snd_pcm_substream_t *substream, int state)
1186 struct list_head *pos;
1187 snd_pcm_substream_t *s;
1189 if (snd_pcm_stream_linked(substream)) {
1190 if (!spin_trylock(&substream->group->lock)) {
1191 spin_unlock(&substream->self_group.lock);
1192 spin_lock(&substream->group->lock);
1193 spin_lock(&substream->self_group.lock);
1195 snd_pcm_group_for_each(pos, substream) {
1196 s = snd_pcm_group_substream_entry(pos);
1198 spin_lock(&s->self_group.lock);
1199 s->runtime->status->state = state;
1201 spin_unlock(&s->self_group.lock);
1203 spin_unlock(&substream->group->lock);
1205 substream->runtime->status->state = state;
1209 static int snd_pcm_playback_drop(snd_pcm_substream_t *substream);
1211 static int snd_pcm_playback_drain(snd_pcm_substream_t * substream)
1214 snd_pcm_runtime_t *runtime;
1215 int err, result = 0;
1217 enum { READY, EXPIRED, SUSPENDED, SIGNALED } state = READY;
1218 snd_pcm_uframes_t stop_threshold;
1220 snd_assert(substream != NULL, return -ENXIO);
1221 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
1222 runtime = substream->runtime;
1223 card = substream->pcm->card;
1225 snd_power_lock(card);
1226 snd_pcm_stream_lock_irq(substream);
1228 /* stop_threshold fixup to avoid endless loop when */
1229 /* stop_threshold > buffer_size */
1230 stop_threshold = runtime->stop_threshold;
1231 if (runtime->stop_threshold > runtime->buffer_size)
1232 runtime->stop_threshold = runtime->buffer_size;
1234 switch (runtime->status->state) {
1235 case SNDRV_PCM_STATE_PAUSED:
1236 snd_pcm_pause(substream, 0);
1238 case SNDRV_PCM_STATE_RUNNING:
1239 case SNDRV_PCM_STATE_DRAINING:
1241 case SNDRV_PCM_STATE_SUSPENDED:
1242 snd_pcm_stream_unlock_irq(substream);
1243 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1244 snd_pcm_stream_lock_irq(substream);
1246 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1248 case SNDRV_PCM_STATE_OPEN:
1251 case SNDRV_PCM_STATE_PREPARED:
1252 if (!snd_pcm_playback_empty(substream)) {
1253 err = snd_pcm_start(substream);
1261 case SNDRV_PCM_STATE_XRUN:
1262 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1264 case SNDRV_PCM_STATE_SETUP:
1270 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1271 if (snd_pcm_playback_empty(substream)) {
1272 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1275 snd_pcm_change_state(substream, SNDRV_PCM_STATE_DRAINING);
1278 if (substream->ffile->f_flags & O_NONBLOCK) {
1283 init_waitqueue_entry(&wait, current);
1284 add_wait_queue(&runtime->sleep, &wait);
1287 if (signal_pending(current)) {
1291 set_current_state(TASK_INTERRUPTIBLE);
1292 snd_pcm_stream_unlock_irq(substream);
1293 snd_power_unlock(card);
1294 tout = schedule_timeout(10 * HZ);
1295 snd_power_lock(card);
1296 snd_pcm_stream_lock_irq(substream);
1298 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
1301 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING) {
1306 remove_wait_queue(&runtime->sleep, &wait);
1310 result = -ERESTARTSYS;
1316 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1324 runtime->stop_threshold = stop_threshold;
1325 snd_pcm_stream_unlock_irq(substream);
1326 snd_power_unlock(card);
1327 if (state == EXPIRED)
1328 snd_pcm_playback_drop(substream);
1333 static int snd_pcm_playback_drop(snd_pcm_substream_t *substream)
1335 snd_pcm_runtime_t *runtime = substream->runtime;
1336 snd_card_t *card = substream->pcm->card;
1339 snd_power_lock(card);
1340 snd_pcm_stream_lock_irq(substream);
1341 switch (runtime->status->state) {
1342 case SNDRV_PCM_STATE_OPEN:
1345 case SNDRV_PCM_STATE_SETUP:
1347 case SNDRV_PCM_STATE_PAUSED:
1348 snd_pcm_pause(substream, 0);
1350 case SNDRV_PCM_STATE_RUNNING:
1351 case SNDRV_PCM_STATE_DRAINING:
1352 if (snd_pcm_update_hw_ptr(substream) >= 0) {
1353 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1357 case SNDRV_PCM_STATE_PREPARED:
1358 case SNDRV_PCM_STATE_XRUN:
1359 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1361 case SNDRV_PCM_STATE_SUSPENDED:
1362 snd_pcm_stream_unlock_irq(substream);
1363 res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1364 snd_pcm_stream_lock_irq(substream);
1366 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1371 runtime->control->appl_ptr = runtime->status->hw_ptr;
1372 snd_pcm_stream_unlock_irq(substream);
1373 snd_power_unlock(card);
1377 static int snd_pcm_capture_drain(snd_pcm_substream_t * substream)
1379 snd_pcm_runtime_t *runtime = substream->runtime;
1380 snd_card_t *card = substream->pcm->card;
1383 snd_power_lock(card);
1384 snd_pcm_stream_lock_irq(substream);
1385 switch (runtime->status->state) {
1386 case SNDRV_PCM_STATE_OPEN:
1389 case SNDRV_PCM_STATE_PREPARED:
1390 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1392 case SNDRV_PCM_STATE_SETUP:
1393 case SNDRV_PCM_STATE_DRAINING:
1395 case SNDRV_PCM_STATE_PAUSED:
1396 snd_pcm_pause(substream, 0);
1398 case SNDRV_PCM_STATE_RUNNING:
1399 if (snd_pcm_update_hw_ptr(substream) >= 0) {
1400 snd_pcm_stop(substream,
1401 snd_pcm_capture_avail(runtime) > 0 ?
1402 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP);
1406 case SNDRV_PCM_STATE_XRUN:
1408 snd_pcm_change_state(substream,
1409 snd_pcm_capture_avail(runtime) > 0 ?
1410 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP);
1412 case SNDRV_PCM_STATE_SUSPENDED:
1413 snd_pcm_stream_unlock_irq(substream);
1414 res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1415 snd_pcm_stream_lock_irq(substream);
1417 goto _xrun_recovery;
1422 snd_pcm_stream_unlock_irq(substream);
1423 snd_power_unlock(card);
1427 static int snd_pcm_capture_drop(snd_pcm_substream_t * substream)
1429 snd_pcm_runtime_t *runtime = substream->runtime;
1430 snd_card_t *card = substream->pcm->card;
1433 snd_power_lock(card);
1434 snd_pcm_stream_lock_irq(substream);
1435 switch (runtime->status->state) {
1436 case SNDRV_PCM_STATE_OPEN:
1439 case SNDRV_PCM_STATE_SETUP:
1441 case SNDRV_PCM_STATE_PAUSED:
1442 snd_pcm_pause(substream, 0);
1444 case SNDRV_PCM_STATE_RUNNING:
1445 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1447 case SNDRV_PCM_STATE_SUSPENDED:
1448 snd_pcm_stream_unlock_irq(substream);
1449 res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1450 snd_pcm_stream_lock_irq(substream);
1454 case SNDRV_PCM_STATE_PREPARED:
1455 case SNDRV_PCM_STATE_DRAINING:
1456 case SNDRV_PCM_STATE_XRUN:
1457 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1462 runtime->control->appl_ptr = runtime->status->hw_ptr;
1464 snd_pcm_stream_unlock_irq(substream);
1465 snd_power_unlock(card);
1469 /* WARNING: Don't forget to fput back the file */
1470 extern int snd_major;
1471 static struct file *snd_pcm_file_fd(int fd)
1474 struct inode *inode;
1475 unsigned short minor;
1479 inode = file->f_dentry->d_inode;
1480 if (!S_ISCHR(inode->i_mode) ||
1481 imajor(inode) != snd_major) {
1485 minor = iminor(inode);
1487 minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
1494 static int snd_pcm_link(snd_pcm_substream_t *substream, int fd)
1498 snd_pcm_file_t *pcm_file;
1499 snd_pcm_substream_t *substream1;
1501 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
1503 file = snd_pcm_file_fd(fd);
1506 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
1507 substream1 = pcm_file->substream;
1508 write_lock_irq(&snd_pcm_link_rwlock);
1509 if (substream->runtime->status->state != substream1->runtime->status->state) {
1513 if (snd_pcm_stream_linked(substream1)) {
1517 if (!snd_pcm_stream_linked(substream)) {
1518 substream->group = kmalloc(sizeof(snd_pcm_group_t), GFP_ATOMIC);
1519 if (substream->group == NULL) {
1523 spin_lock_init(&substream->group->lock);
1524 INIT_LIST_HEAD(&substream->group->substreams);
1525 list_add_tail(&substream->link_list, &substream->group->substreams);
1527 list_add_tail(&substream1->link_list, &substream->group->substreams);
1528 substream1->group = substream->group;
1530 write_unlock_irq(&snd_pcm_link_rwlock);
1535 static void relink_to_local(snd_pcm_substream_t *substream)
1537 substream->group = &substream->self_group;
1538 INIT_LIST_HEAD(&substream->self_group.substreams);
1539 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1542 static int snd_pcm_unlink(snd_pcm_substream_t *substream)
1544 struct list_head *pos;
1545 int res = 0, count = 0;
1547 write_lock_irq(&snd_pcm_link_rwlock);
1548 if (!snd_pcm_stream_linked(substream)) {
1552 list_del(&substream->link_list);
1553 snd_pcm_group_for_each(pos, substream) {
1557 if (count == 1) { /* detach the last stream, too */
1558 snd_pcm_group_for_each(pos, substream) {
1559 relink_to_local(snd_pcm_group_substream_entry(pos));
1562 kfree(substream->group);
1564 relink_to_local(substream);
1566 write_unlock_irq(&snd_pcm_link_rwlock);
1570 static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t *params,
1571 snd_pcm_hw_rule_t *rule)
1574 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1575 hw_param_interval_c(params, rule->deps[1]), &t);
1576 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1579 static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t *params,
1580 snd_pcm_hw_rule_t *rule)
1583 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1584 hw_param_interval_c(params, rule->deps[1]), &t);
1585 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1588 static int snd_pcm_hw_rule_muldivk(snd_pcm_hw_params_t *params,
1589 snd_pcm_hw_rule_t *rule)
1592 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1593 hw_param_interval_c(params, rule->deps[1]),
1594 (unsigned long) rule->private, &t);
1595 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1598 static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t *params,
1599 snd_pcm_hw_rule_t *rule)
1602 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1603 (unsigned long) rule->private,
1604 hw_param_interval_c(params, rule->deps[1]), &t);
1605 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1608 static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t *params,
1609 snd_pcm_hw_rule_t *rule)
1612 snd_interval_t *i = hw_param_interval(params, rule->deps[0]);
1614 snd_mask_t *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1616 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1618 if (! snd_mask_test(mask, k))
1620 bits = snd_pcm_format_physical_width(k);
1622 continue; /* ignore invalid formats */
1623 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1624 snd_mask_reset(&m, k);
1626 return snd_mask_refine(mask, &m);
1629 static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t *params,
1630 snd_pcm_hw_rule_t *rule)
1638 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1640 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1642 bits = snd_pcm_format_physical_width(k);
1644 continue; /* ignore invalid formats */
1645 if (t.min > (unsigned)bits)
1647 if (t.max < (unsigned)bits)
1651 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1654 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1655 #error "Change this table"
1658 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1659 48000, 64000, 88200, 96000, 176400, 192000 };
1661 #define RATES (sizeof(rates) / sizeof(rates[0]))
1663 static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t *params,
1664 snd_pcm_hw_rule_t *rule)
1666 snd_pcm_hardware_t *hw = rule->private;
1667 return snd_interval_list(hw_param_interval(params, rule->var), RATES, rates, hw->rates);
1670 static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t *params,
1671 snd_pcm_hw_rule_t *rule)
1674 snd_pcm_substream_t *substream = rule->private;
1676 t.max = substream->buffer_bytes_max;
1680 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1683 int snd_pcm_hw_constraints_init(snd_pcm_substream_t *substream)
1685 snd_pcm_runtime_t *runtime = substream->runtime;
1686 snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
1689 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1690 snd_mask_any(constrs_mask(constrs, k));
1693 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1694 snd_interval_any(constrs_interval(constrs, k));
1697 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1698 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1699 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1700 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1701 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1703 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1704 snd_pcm_hw_rule_format, 0,
1705 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1708 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1709 snd_pcm_hw_rule_sample_bits, 0,
1710 SNDRV_PCM_HW_PARAM_FORMAT,
1711 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1714 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1715 snd_pcm_hw_rule_div, 0,
1716 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1719 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1720 snd_pcm_hw_rule_mul, 0,
1721 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1724 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1725 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1726 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1729 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1730 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1731 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1734 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1735 snd_pcm_hw_rule_div, 0,
1736 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1739 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1740 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1741 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1744 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1745 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1746 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1749 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1750 snd_pcm_hw_rule_div, 0,
1751 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1754 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1755 snd_pcm_hw_rule_div, 0,
1756 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1759 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1760 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1761 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1764 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1765 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1766 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1769 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1770 snd_pcm_hw_rule_mul, 0,
1771 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1774 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1775 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1776 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1779 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1780 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1781 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1784 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1785 snd_pcm_hw_rule_muldivk, (void*) 8,
1786 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1789 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1790 snd_pcm_hw_rule_muldivk, (void*) 8,
1791 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1794 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1795 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1796 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1799 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1800 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1801 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1807 int snd_pcm_hw_constraints_complete(snd_pcm_substream_t *substream)
1809 snd_pcm_runtime_t *runtime = substream->runtime;
1810 snd_pcm_hardware_t *hw = &runtime->hw;
1812 unsigned int mask = 0;
1814 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1815 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1816 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1817 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1818 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1819 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1820 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1821 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1822 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1823 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1824 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1826 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1827 snd_assert(err >= 0, return -EINVAL);
1829 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1830 snd_assert(err >= 0, return -EINVAL);
1832 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1833 snd_assert(err >= 0, return -EINVAL);
1835 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1836 hw->channels_min, hw->channels_max);
1837 snd_assert(err >= 0, return -EINVAL);
1839 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1840 hw->rate_min, hw->rate_max);
1841 snd_assert(err >= 0, return -EINVAL);
1843 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1844 hw->period_bytes_min, hw->period_bytes_max);
1845 snd_assert(err >= 0, return -EINVAL);
1847 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1848 hw->periods_min, hw->periods_max);
1849 snd_assert(err >= 0, return -EINVAL);
1851 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1852 hw->period_bytes_min, hw->buffer_bytes_max);
1853 snd_assert(err >= 0, return -EINVAL);
1855 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1856 snd_pcm_hw_rule_buffer_bytes_max, substream,
1857 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1862 if (runtime->dma_bytes) {
1863 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1864 snd_assert(err >= 0, return -EINVAL);
1867 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1869 snd_pcm_hw_rule_rate, hw,
1870 SNDRV_PCM_HW_PARAM_RATE, -1);
1875 /* FIXME: this belong to lowlevel */
1876 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1877 1000000 / HZ, 1000000 / HZ);
1878 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1883 static void snd_pcm_add_file(snd_pcm_str_t *str,
1884 snd_pcm_file_t *pcm_file)
1886 pcm_file->next = str->files;
1887 str->files = pcm_file;
1890 static void snd_pcm_remove_file(snd_pcm_str_t *str,
1891 snd_pcm_file_t *pcm_file)
1893 snd_pcm_file_t * pcm_file1;
1894 if (str->files == pcm_file) {
1895 str->files = pcm_file->next;
1897 pcm_file1 = str->files;
1898 while (pcm_file1 && pcm_file1->next != pcm_file)
1899 pcm_file1 = pcm_file1->next;
1900 if (pcm_file1 != NULL)
1901 pcm_file1->next = pcm_file->next;
1905 static int snd_pcm_release_file(snd_pcm_file_t * pcm_file)
1907 snd_pcm_substream_t *substream;
1908 snd_pcm_runtime_t *runtime;
1909 snd_pcm_str_t * str;
1911 snd_assert(pcm_file != NULL, return -ENXIO);
1912 substream = pcm_file->substream;
1913 snd_assert(substream != NULL, return -ENXIO);
1914 runtime = substream->runtime;
1915 str = substream->pstr;
1916 snd_pcm_unlink(substream);
1917 if (substream->open_flag) {
1918 if (substream->ops->hw_free != NULL)
1919 substream->ops->hw_free(substream);
1920 substream->ops->close(substream);
1921 substream->open_flag = 0;
1923 substream->ffile = NULL;
1924 snd_pcm_remove_file(str, pcm_file);
1925 snd_pcm_release_substream(substream);
1926 snd_magic_kfree(pcm_file);
1930 static int snd_pcm_open_file(struct file *file,
1933 snd_pcm_file_t **rpcm_file)
1936 snd_pcm_file_t *pcm_file;
1937 snd_pcm_substream_t *substream;
1940 snd_assert(rpcm_file != NULL, return -EINVAL);
1943 pcm_file = snd_magic_kcalloc(snd_pcm_file_t, 0, GFP_KERNEL);
1944 if (pcm_file == NULL) {
1948 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
1949 snd_magic_kfree(pcm_file);
1953 str = substream->pstr;
1954 substream->file = pcm_file;
1956 pcm_file->substream = substream;
1958 snd_pcm_add_file(str, pcm_file);
1960 err = snd_pcm_hw_constraints_init(substream);
1962 snd_printd("snd_pcm_hw_constraints_init failed\n");
1963 snd_pcm_release_file(pcm_file);
1967 if ((err = substream->ops->open(substream)) < 0) {
1968 snd_pcm_release_file(pcm_file);
1971 substream->open_flag = 1;
1973 err = snd_pcm_hw_constraints_complete(substream);
1975 snd_printd("snd_pcm_hw_constraints_complete failed\n");
1976 substream->ops->close(substream);
1977 snd_pcm_release_file(pcm_file);
1981 substream->ffile = file;
1983 file->private_data = pcm_file;
1984 *rpcm_file = pcm_file;
1988 int snd_pcm_open(struct inode *inode, struct file *file)
1990 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
1991 int device = SNDRV_MINOR_DEVICE(iminor(inode));
1994 snd_pcm_file_t *pcm_file;
1997 snd_runtime_check(device >= SNDRV_MINOR_PCM_PLAYBACK && device < SNDRV_MINOR_DEVICES, return -ENXIO);
1998 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
2003 err = snd_card_file_add(pcm->card, file);
2006 if (!try_module_get(pcm->card->module)) {
2010 init_waitqueue_entry(&wait, current);
2011 add_wait_queue(&pcm->open_wait, &wait);
2012 down(&pcm->open_mutex);
2014 err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
2017 if (err == -EAGAIN) {
2018 if (file->f_flags & O_NONBLOCK) {
2024 set_current_state(TASK_INTERRUPTIBLE);
2025 up(&pcm->open_mutex);
2027 down(&pcm->open_mutex);
2028 if (signal_pending(current)) {
2033 remove_wait_queue(&pcm->open_wait, &wait);
2034 up(&pcm->open_mutex);
2040 module_put(pcm->card->module);
2042 snd_card_file_remove(pcm->card, file);
2047 int snd_pcm_release(struct inode *inode, struct file *file)
2050 snd_pcm_substream_t *substream;
2051 snd_pcm_file_t *pcm_file;
2053 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2054 substream = pcm_file->substream;
2055 snd_assert(substream != NULL, return -ENXIO);
2056 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2057 pcm = substream->pcm;
2058 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2059 snd_pcm_playback_drop(substream);
2061 snd_pcm_capture_drop(substream);
2062 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2063 down(&pcm->open_mutex);
2064 snd_pcm_release_file(pcm_file);
2065 up(&pcm->open_mutex);
2066 wake_up(&pcm->open_wait);
2067 module_put(pcm->card->module);
2068 snd_card_file_remove(pcm->card, file);
2072 snd_pcm_sframes_t snd_pcm_playback_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2074 snd_pcm_runtime_t *runtime = substream->runtime;
2075 snd_pcm_sframes_t appl_ptr;
2076 snd_pcm_sframes_t ret;
2077 snd_pcm_sframes_t hw_avail;
2082 snd_pcm_stream_lock_irq(substream);
2083 switch (runtime->status->state) {
2084 case SNDRV_PCM_STATE_PREPARED:
2086 case SNDRV_PCM_STATE_DRAINING:
2087 case SNDRV_PCM_STATE_RUNNING:
2088 if (snd_pcm_update_hw_ptr(substream) >= 0)
2091 case SNDRV_PCM_STATE_XRUN:
2099 hw_avail = snd_pcm_playback_hw_avail(runtime);
2100 if (hw_avail <= 0) {
2104 if (frames > (snd_pcm_uframes_t)hw_avail)
2107 frames -= frames % runtime->xfer_align;
2108 appl_ptr = runtime->control->appl_ptr - frames;
2110 appl_ptr += runtime->boundary;
2111 runtime->control->appl_ptr = appl_ptr;
2112 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2114 snd_pcm_tick_prepare(substream);
2117 snd_pcm_stream_unlock_irq(substream);
2121 snd_pcm_sframes_t snd_pcm_capture_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2123 snd_pcm_runtime_t *runtime = substream->runtime;
2124 snd_pcm_sframes_t appl_ptr;
2125 snd_pcm_sframes_t ret;
2126 snd_pcm_sframes_t hw_avail;
2131 snd_pcm_stream_lock_irq(substream);
2132 switch (runtime->status->state) {
2133 case SNDRV_PCM_STATE_PREPARED:
2134 case SNDRV_PCM_STATE_DRAINING:
2136 case SNDRV_PCM_STATE_RUNNING:
2137 if (snd_pcm_update_hw_ptr(substream) >= 0)
2140 case SNDRV_PCM_STATE_XRUN:
2148 hw_avail = snd_pcm_capture_hw_avail(runtime);
2149 if (hw_avail <= 0) {
2153 if (frames > (snd_pcm_uframes_t)hw_avail)
2156 frames -= frames % runtime->xfer_align;
2157 appl_ptr = runtime->control->appl_ptr - frames;
2159 appl_ptr += runtime->boundary;
2160 runtime->control->appl_ptr = appl_ptr;
2161 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2163 snd_pcm_tick_prepare(substream);
2166 snd_pcm_stream_unlock_irq(substream);
2170 snd_pcm_sframes_t snd_pcm_playback_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2172 snd_pcm_runtime_t *runtime = substream->runtime;
2173 snd_pcm_sframes_t appl_ptr;
2174 snd_pcm_sframes_t ret;
2175 snd_pcm_sframes_t avail;
2180 snd_pcm_stream_lock_irq(substream);
2181 switch (runtime->status->state) {
2182 case SNDRV_PCM_STATE_PREPARED:
2183 case SNDRV_PCM_STATE_PAUSED:
2185 case SNDRV_PCM_STATE_DRAINING:
2186 case SNDRV_PCM_STATE_RUNNING:
2187 if (snd_pcm_update_hw_ptr(substream) >= 0)
2190 case SNDRV_PCM_STATE_XRUN:
2198 avail = snd_pcm_playback_avail(runtime);
2203 if (frames > (snd_pcm_uframes_t)avail)
2206 frames -= frames % runtime->xfer_align;
2207 appl_ptr = runtime->control->appl_ptr + frames;
2208 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2209 appl_ptr -= runtime->boundary;
2210 runtime->control->appl_ptr = appl_ptr;
2211 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2213 snd_pcm_tick_prepare(substream);
2216 snd_pcm_stream_unlock_irq(substream);
2220 snd_pcm_sframes_t snd_pcm_capture_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2222 snd_pcm_runtime_t *runtime = substream->runtime;
2223 snd_pcm_sframes_t appl_ptr;
2224 snd_pcm_sframes_t ret;
2225 snd_pcm_sframes_t avail;
2230 snd_pcm_stream_lock_irq(substream);
2231 switch (runtime->status->state) {
2232 case SNDRV_PCM_STATE_PREPARED:
2233 case SNDRV_PCM_STATE_DRAINING:
2234 case SNDRV_PCM_STATE_PAUSED:
2236 case SNDRV_PCM_STATE_RUNNING:
2237 if (snd_pcm_update_hw_ptr(substream) >= 0)
2240 case SNDRV_PCM_STATE_XRUN:
2248 avail = snd_pcm_capture_avail(runtime);
2253 if (frames > (snd_pcm_uframes_t)avail)
2256 frames -= frames % runtime->xfer_align;
2257 appl_ptr = runtime->control->appl_ptr + frames;
2258 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2259 appl_ptr -= runtime->boundary;
2260 runtime->control->appl_ptr = appl_ptr;
2261 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2263 snd_pcm_tick_prepare(substream);
2266 snd_pcm_stream_unlock_irq(substream);
2270 static int snd_pcm_hwsync(snd_pcm_substream_t *substream)
2272 snd_pcm_runtime_t *runtime = substream->runtime;
2275 snd_pcm_stream_lock_irq(substream);
2276 switch (runtime->status->state) {
2277 case SNDRV_PCM_STATE_DRAINING:
2278 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2280 case SNDRV_PCM_STATE_RUNNING:
2281 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2284 case SNDRV_PCM_STATE_PREPARED:
2285 case SNDRV_PCM_STATE_SUSPENDED:
2288 case SNDRV_PCM_STATE_XRUN:
2296 snd_pcm_stream_unlock_irq(substream);
2300 static int snd_pcm_delay(snd_pcm_substream_t *substream, snd_pcm_sframes_t *res)
2302 snd_pcm_runtime_t *runtime = substream->runtime;
2304 snd_pcm_sframes_t n = 0;
2306 snd_pcm_stream_lock_irq(substream);
2307 switch (runtime->status->state) {
2308 case SNDRV_PCM_STATE_DRAINING:
2309 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2311 case SNDRV_PCM_STATE_RUNNING:
2312 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2315 case SNDRV_PCM_STATE_PREPARED:
2316 case SNDRV_PCM_STATE_SUSPENDED:
2318 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2319 n = snd_pcm_playback_hw_avail(runtime);
2321 n = snd_pcm_capture_avail(runtime);
2323 case SNDRV_PCM_STATE_XRUN:
2331 snd_pcm_stream_unlock_irq(substream);
2333 if (put_user(n, res))
2338 static int snd_pcm_sync_ptr(snd_pcm_substream_t *substream, struct sndrv_pcm_sync_ptr *_sync_ptr)
2340 snd_pcm_runtime_t *runtime = substream->runtime;
2341 struct sndrv_pcm_sync_ptr sync_ptr;
2342 volatile struct sndrv_pcm_mmap_status *status;
2343 volatile struct sndrv_pcm_mmap_control *control;
2346 memset(&sync_ptr, 0, sizeof(sync_ptr));
2347 if (get_user(sync_ptr.flags, (unsigned int *) &(_sync_ptr->flags)))
2349 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct sndrv_pcm_mmap_control)))
2351 status = runtime->status;
2352 control = runtime->control;
2353 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2354 err = snd_pcm_hwsync(substream);
2358 snd_pcm_stream_lock_irq(substream);
2359 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2360 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2362 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2363 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2364 control->avail_min = sync_ptr.c.control.avail_min;
2366 sync_ptr.c.control.avail_min = control->avail_min;
2367 sync_ptr.s.status.state = status->state;
2368 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2369 sync_ptr.s.status.tstamp = status->tstamp;
2370 sync_ptr.s.status.suspended_state = status->suspended_state;
2371 snd_pcm_stream_unlock_irq(substream);
2372 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2377 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2378 unsigned int cmd, void *arg);
2379 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2380 unsigned int cmd, void *arg);
2382 static int snd_pcm_common_ioctl1(snd_pcm_substream_t *substream,
2383 unsigned int cmd, void *arg)
2385 snd_assert(substream != NULL, return -ENXIO);
2388 case SNDRV_PCM_IOCTL_PVERSION:
2389 return put_user(SNDRV_PCM_VERSION, (int *)arg) ? -EFAULT : 0;
2390 case SNDRV_PCM_IOCTL_INFO:
2391 return snd_pcm_info_user(substream, (snd_pcm_info_t *) arg);
2392 case SNDRV_PCM_IOCTL_TSTAMP:
2395 if (get_user(xarg, (int *) arg))
2397 substream->runtime->tstamp_timespec = xarg ? 1 : 0;
2400 case SNDRV_PCM_IOCTL_HW_REFINE:
2401 return snd_pcm_hw_refine_user(substream, (snd_pcm_hw_params_t *) arg);
2402 case SNDRV_PCM_IOCTL_HW_PARAMS:
2403 return snd_pcm_hw_params_user(substream, (snd_pcm_hw_params_t *) arg);
2404 case SNDRV_PCM_IOCTL_HW_FREE:
2405 return snd_pcm_hw_free(substream);
2406 case SNDRV_PCM_IOCTL_SW_PARAMS:
2407 return snd_pcm_sw_params_user(substream, (snd_pcm_sw_params_t *) arg);
2408 case SNDRV_PCM_IOCTL_STATUS:
2409 return snd_pcm_status_user(substream, (snd_pcm_status_t *) arg);
2410 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2411 return snd_pcm_channel_info(substream, (snd_pcm_channel_info_t *) arg);
2412 case SNDRV_PCM_IOCTL_PREPARE:
2413 return snd_pcm_prepare(substream);
2414 case SNDRV_PCM_IOCTL_RESET:
2415 return snd_pcm_reset(substream);
2416 case SNDRV_PCM_IOCTL_START:
2417 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, 0, 0);
2418 case SNDRV_PCM_IOCTL_LINK:
2419 return snd_pcm_link(substream, (long) arg);
2420 case SNDRV_PCM_IOCTL_UNLINK:
2421 return snd_pcm_unlink(substream);
2422 case SNDRV_PCM_IOCTL_RESUME:
2423 return snd_pcm_resume(substream);
2424 case SNDRV_PCM_IOCTL_XRUN:
2425 return snd_pcm_xrun(substream);
2426 case SNDRV_PCM_IOCTL_HWSYNC:
2427 return snd_pcm_hwsync(substream);
2428 case SNDRV_PCM_IOCTL_DELAY:
2429 return snd_pcm_delay(substream, (snd_pcm_sframes_t *) arg);
2430 case SNDRV_PCM_IOCTL_SYNC_PTR:
2431 return snd_pcm_sync_ptr(substream, (struct sndrv_pcm_sync_ptr *) arg);
2432 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2433 return snd_pcm_hw_refine_old_user(substream, (struct sndrv_pcm_hw_params_old *) arg);
2434 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2435 return snd_pcm_hw_params_old_user(substream, (struct sndrv_pcm_hw_params_old *) arg);
2437 snd_printd("unknown ioctl = 0x%x\n", cmd);
2441 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2442 unsigned int cmd, void *arg)
2444 snd_assert(substream != NULL, return -ENXIO);
2445 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2447 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2449 snd_xferi_t xferi, *_xferi = arg;
2450 snd_pcm_runtime_t *runtime = substream->runtime;
2451 snd_pcm_sframes_t result;
2452 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2454 if (put_user(0, &_xferi->result))
2456 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2458 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2459 __put_user(result, &_xferi->result);
2460 return result < 0 ? result : 0;
2462 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2464 snd_xfern_t xfern, *_xfern = arg;
2465 snd_pcm_runtime_t *runtime = substream->runtime;
2467 snd_pcm_sframes_t result;
2468 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2470 if (runtime->channels > 128)
2472 if (put_user(0, &_xfern->result))
2474 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2476 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2479 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2483 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2485 __put_user(result, &_xfern->result);
2486 return result < 0 ? result : 0;
2488 case SNDRV_PCM_IOCTL_REWIND:
2490 snd_pcm_uframes_t frames, *_frames = arg;
2491 snd_pcm_sframes_t result;
2492 if (get_user(frames, _frames))
2494 if (put_user(0, _frames))
2496 result = snd_pcm_playback_rewind(substream, frames);
2497 __put_user(result, _frames);
2498 return result < 0 ? result : 0;
2500 case SNDRV_PCM_IOCTL_FORWARD:
2502 snd_pcm_uframes_t frames, *_frames = arg;
2503 snd_pcm_sframes_t result;
2504 if (get_user(frames, _frames))
2506 if (put_user(0, _frames))
2508 result = snd_pcm_playback_forward(substream, frames);
2509 __put_user(result, _frames);
2510 return result < 0 ? result : 0;
2512 case SNDRV_PCM_IOCTL_PAUSE:
2515 snd_pcm_stream_lock_irq(substream);
2516 res = snd_pcm_pause(substream, (long) arg);
2517 snd_pcm_stream_unlock_irq(substream);
2520 case SNDRV_PCM_IOCTL_DRAIN:
2521 return snd_pcm_playback_drain(substream);
2522 case SNDRV_PCM_IOCTL_DROP:
2523 return snd_pcm_playback_drop(substream);
2525 return snd_pcm_common_ioctl1(substream, cmd, arg);
2528 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2529 unsigned int cmd, void *arg)
2531 snd_assert(substream != NULL, return -ENXIO);
2532 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2534 case SNDRV_PCM_IOCTL_READI_FRAMES:
2536 snd_xferi_t xferi, *_xferi = arg;
2537 snd_pcm_runtime_t *runtime = substream->runtime;
2538 snd_pcm_sframes_t result;
2539 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2541 if (put_user(0, &_xferi->result))
2543 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2545 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2546 __put_user(result, &_xferi->result);
2547 return result < 0 ? result : 0;
2549 case SNDRV_PCM_IOCTL_READN_FRAMES:
2551 snd_xfern_t xfern, *_xfern = arg;
2552 snd_pcm_runtime_t *runtime = substream->runtime;
2554 snd_pcm_sframes_t result;
2555 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2557 if (runtime->channels > 128)
2559 if (put_user(0, &_xfern->result))
2561 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2563 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2566 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2570 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2572 __put_user(result, &_xfern->result);
2573 return result < 0 ? result : 0;
2575 case SNDRV_PCM_IOCTL_REWIND:
2577 snd_pcm_uframes_t frames, *_frames = arg;
2578 snd_pcm_sframes_t result;
2579 if (get_user(frames, _frames))
2581 if (put_user(0, _frames))
2583 result = snd_pcm_capture_rewind(substream, frames);
2584 __put_user(result, _frames);
2585 return result < 0 ? result : 0;
2587 case SNDRV_PCM_IOCTL_FORWARD:
2589 snd_pcm_uframes_t frames, *_frames = arg;
2590 snd_pcm_sframes_t result;
2591 if (get_user(frames, _frames))
2593 if (put_user(0, _frames))
2595 result = snd_pcm_capture_forward(substream, frames);
2596 __put_user(result, _frames);
2597 return result < 0 ? result : 0;
2599 case SNDRV_PCM_IOCTL_DRAIN:
2600 return snd_pcm_capture_drain(substream);
2601 case SNDRV_PCM_IOCTL_DROP:
2602 return snd_pcm_capture_drop(substream);
2604 return snd_pcm_common_ioctl1(substream, cmd, arg);
2607 static int snd_pcm_playback_ioctl(struct inode *inode, struct file *file,
2608 unsigned int cmd, unsigned long arg)
2610 snd_pcm_file_t *pcm_file;
2612 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2614 if (((cmd >> 8) & 0xff) != 'A')
2617 return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void *) arg);
2620 static int snd_pcm_capture_ioctl(struct inode *inode, struct file *file,
2621 unsigned int cmd, unsigned long arg)
2623 snd_pcm_file_t *pcm_file;
2625 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2627 if (((cmd >> 8) & 0xff) != 'A')
2630 return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void *) arg);
2633 int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t *substream,
2634 unsigned int cmd, void *arg)
2639 fs = snd_enter_user();
2640 result = snd_pcm_playback_ioctl1(substream, cmd, arg);
2645 int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t *substream,
2646 unsigned int cmd, void *arg)
2651 fs = snd_enter_user();
2652 result = snd_pcm_capture_ioctl1(substream, cmd, arg);
2657 int snd_pcm_kernel_ioctl(snd_pcm_substream_t *substream,
2658 unsigned int cmd, void *arg)
2660 switch (substream->stream) {
2661 case SNDRV_PCM_STREAM_PLAYBACK:
2662 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2663 case SNDRV_PCM_STREAM_CAPTURE:
2664 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2670 static ssize_t snd_pcm_read(struct file *file, char *buf, size_t count, loff_t * offset)
2672 snd_pcm_file_t *pcm_file;
2673 snd_pcm_substream_t *substream;
2674 snd_pcm_runtime_t *runtime;
2675 snd_pcm_sframes_t result;
2677 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2678 substream = pcm_file->substream;
2679 snd_assert(substream != NULL, return -ENXIO);
2680 runtime = substream->runtime;
2681 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2683 if (!frame_aligned(runtime, count))
2685 count = bytes_to_frames(runtime, count);
2686 result = snd_pcm_lib_read(substream, buf, count);
2688 result = frames_to_bytes(runtime, result);
2692 static ssize_t snd_pcm_write(struct file *file, const char *buf, size_t count, loff_t * offset)
2694 snd_pcm_file_t *pcm_file;
2695 snd_pcm_substream_t *substream;
2696 snd_pcm_runtime_t *runtime;
2697 snd_pcm_sframes_t result;
2699 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, result = -ENXIO; goto end);
2700 substream = pcm_file->substream;
2701 snd_assert(substream != NULL, result = -ENXIO; goto end);
2702 runtime = substream->runtime;
2703 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2707 if (!frame_aligned(runtime, count)) {
2711 count = bytes_to_frames(runtime, count);
2712 result = snd_pcm_lib_write(substream, buf, count);
2714 result = frames_to_bytes(runtime, result);
2719 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2720 unsigned long count, loff_t * offset)
2723 snd_pcm_file_t *pcm_file;
2724 snd_pcm_substream_t *substream;
2725 snd_pcm_runtime_t *runtime;
2726 snd_pcm_sframes_t result;
2729 snd_pcm_uframes_t frames;
2731 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2732 substream = pcm_file->substream;
2733 snd_assert(substream != NULL, return -ENXIO);
2734 runtime = substream->runtime;
2735 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2737 if (count > 1024 || count != runtime->channels)
2739 if (!frame_aligned(runtime, _vector->iov_len))
2741 frames = bytes_to_samples(runtime, _vector->iov_len);
2742 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2745 for (i = 0; i < count; ++i)
2746 bufs[i] = _vector[i].iov_base;
2747 result = snd_pcm_lib_readv(substream, bufs, frames);
2749 result = frames_to_bytes(runtime, result);
2754 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2755 unsigned long count, loff_t * offset)
2757 snd_pcm_file_t *pcm_file;
2758 snd_pcm_substream_t *substream;
2759 snd_pcm_runtime_t *runtime;
2760 snd_pcm_sframes_t result;
2763 snd_pcm_uframes_t frames;
2765 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, result = -ENXIO; goto end);
2766 substream = pcm_file->substream;
2767 snd_assert(substream != NULL, result = -ENXIO; goto end);
2768 runtime = substream->runtime;
2769 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2773 if (count > 128 || count != runtime->channels ||
2774 !frame_aligned(runtime, _vector->iov_len)) {
2778 frames = bytes_to_samples(runtime, _vector->iov_len);
2779 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2782 for (i = 0; i < count; ++i)
2783 bufs[i] = _vector[i].iov_base;
2784 result = snd_pcm_lib_writev(substream, bufs, frames);
2786 result = frames_to_bytes(runtime, result);
2792 unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2794 snd_pcm_file_t *pcm_file;
2795 snd_pcm_substream_t *substream;
2796 snd_pcm_runtime_t *runtime;
2798 snd_pcm_uframes_t avail;
2800 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return 0);
2802 substream = pcm_file->substream;
2803 snd_assert(substream != NULL, return -ENXIO);
2804 runtime = substream->runtime;
2806 poll_wait(file, &runtime->sleep, wait);
2808 snd_pcm_stream_lock_irq(substream);
2809 avail = snd_pcm_playback_avail(runtime);
2810 switch (runtime->status->state) {
2811 case SNDRV_PCM_STATE_RUNNING:
2812 case SNDRV_PCM_STATE_PREPARED:
2813 case SNDRV_PCM_STATE_PAUSED:
2814 if (avail >= runtime->control->avail_min) {
2815 mask = POLLOUT | POLLWRNORM;
2819 case SNDRV_PCM_STATE_DRAINING:
2823 mask = POLLOUT | POLLWRNORM | POLLERR;
2826 snd_pcm_stream_unlock_irq(substream);
2830 unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2832 snd_pcm_file_t *pcm_file;
2833 snd_pcm_substream_t *substream;
2834 snd_pcm_runtime_t *runtime;
2836 snd_pcm_uframes_t avail;
2838 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return 0);
2840 substream = pcm_file->substream;
2841 snd_assert(substream != NULL, return -ENXIO);
2842 runtime = substream->runtime;
2844 poll_wait(file, &runtime->sleep, wait);
2846 snd_pcm_stream_lock_irq(substream);
2847 avail = snd_pcm_capture_avail(runtime);
2848 switch (runtime->status->state) {
2849 case SNDRV_PCM_STATE_RUNNING:
2850 case SNDRV_PCM_STATE_PREPARED:
2851 case SNDRV_PCM_STATE_PAUSED:
2852 if (avail >= runtime->control->avail_min) {
2853 mask = POLLIN | POLLRDNORM;
2858 case SNDRV_PCM_STATE_DRAINING:
2860 mask = POLLIN | POLLRDNORM;
2865 mask = POLLIN | POLLRDNORM | POLLERR;
2868 snd_pcm_stream_unlock_irq(substream);
2872 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2874 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2875 snd_pcm_runtime_t *runtime;
2878 if (substream == NULL)
2880 runtime = substream->runtime;
2881 page = virt_to_page(runtime->status);
2882 if (!PageReserved(page))
2885 *type = VM_FAULT_MINOR;
2889 static struct vm_operations_struct snd_pcm_vm_ops_status =
2891 .nopage = snd_pcm_mmap_status_nopage,
2894 int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
2895 struct vm_area_struct *area)
2897 snd_pcm_runtime_t *runtime;
2899 if (!(area->vm_flags & VM_READ))
2901 runtime = substream->runtime;
2902 snd_assert(runtime != NULL, return -EAGAIN);
2903 size = area->vm_end - area->vm_start;
2904 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)))
2906 area->vm_ops = &snd_pcm_vm_ops_status;
2907 area->vm_private_data = substream;
2908 area->vm_flags |= VM_RESERVED;
2912 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2914 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2915 snd_pcm_runtime_t *runtime;
2918 if (substream == NULL)
2920 runtime = substream->runtime;
2921 page = virt_to_page(runtime->control);
2922 if (!PageReserved(page))
2925 *type = VM_FAULT_MINOR;
2929 static struct vm_operations_struct snd_pcm_vm_ops_control =
2931 .nopage = snd_pcm_mmap_control_nopage,
2934 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
2935 struct vm_area_struct *area)
2937 snd_pcm_runtime_t *runtime;
2939 if (!(area->vm_flags & VM_READ))
2941 runtime = substream->runtime;
2942 snd_assert(runtime != NULL, return -EAGAIN);
2943 size = area->vm_end - area->vm_start;
2944 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)))
2946 area->vm_ops = &snd_pcm_vm_ops_control;
2947 area->vm_private_data = substream;
2948 area->vm_flags |= VM_RESERVED;
2952 static void snd_pcm_mmap_data_open(struct vm_area_struct *area)
2954 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2955 atomic_inc(&substream->runtime->mmap_count);
2958 static void snd_pcm_mmap_data_close(struct vm_area_struct *area)
2960 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2961 atomic_dec(&substream->runtime->mmap_count);
2964 static struct page * snd_pcm_mmap_data_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2966 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2967 snd_pcm_runtime_t *runtime;
2968 unsigned long offset;
2973 if (substream == NULL)
2975 runtime = substream->runtime;
2976 offset = area->vm_pgoff << PAGE_SHIFT;
2977 offset += address - area->vm_start;
2978 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
2979 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
2980 if (offset > dma_bytes - PAGE_SIZE)
2981 return NOPAGE_SIGBUS;
2982 if (substream->ops->page) {
2983 page = substream->ops->page(substream, offset);
2987 vaddr = runtime->dma_area + offset;
2988 page = virt_to_page(vaddr);
2990 if (!PageReserved(page))
2993 *type = VM_FAULT_MINOR;
2997 static struct vm_operations_struct snd_pcm_vm_ops_data =
2999 .open = snd_pcm_mmap_data_open,
3000 .close = snd_pcm_mmap_data_close,
3001 .nopage = snd_pcm_mmap_data_nopage,
3004 int snd_pcm_mmap_data(snd_pcm_substream_t *substream, struct file *file,
3005 struct vm_area_struct *area)
3007 snd_pcm_runtime_t *runtime;
3009 unsigned long offset;
3012 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3013 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3016 if (!(area->vm_flags & VM_READ))
3019 runtime = substream->runtime;
3020 snd_assert(runtime != NULL, return -EAGAIN);
3021 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3023 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3025 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3026 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3028 size = area->vm_end - area->vm_start;
3029 offset = area->vm_pgoff << PAGE_SHIFT;
3030 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3031 if ((size_t)size > dma_bytes)
3033 if (offset > dma_bytes - size)
3036 area->vm_ops = &snd_pcm_vm_ops_data;
3037 area->vm_private_data = substream;
3038 area->vm_flags |= VM_RESERVED;
3039 atomic_inc(&runtime->mmap_count);
3043 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3045 snd_pcm_file_t * pcm_file;
3046 snd_pcm_substream_t *substream;
3047 unsigned long offset;
3049 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
3050 substream = pcm_file->substream;
3051 snd_assert(substream != NULL, return -ENXIO);
3053 offset = area->vm_pgoff << PAGE_SHIFT;
3055 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3056 return snd_pcm_mmap_status(substream, file, area);
3057 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3058 return snd_pcm_mmap_control(substream, file, area);
3060 return snd_pcm_mmap_data(substream, file, area);
3065 static int snd_pcm_fasync(int fd, struct file * file, int on)
3067 snd_pcm_file_t * pcm_file;
3068 snd_pcm_substream_t *substream;
3069 snd_pcm_runtime_t *runtime;
3072 pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
3073 substream = pcm_file->substream;
3074 snd_assert(substream != NULL, return -ENXIO);
3075 runtime = substream->runtime;
3077 err = fasync_helper(fd, file, on, &runtime->fasync);
3084 * To be removed helpers to keep binary compatibility
3087 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3088 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3090 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t *params, struct sndrv_pcm_hw_params_old *oparams)
3094 memset(params, 0, sizeof(*params));
3095 params->flags = oparams->flags;
3096 for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++)
3097 params->masks[i].bits[0] = oparams->masks[i];
3098 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3099 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3100 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3101 params->info = oparams->info;
3102 params->msbits = oparams->msbits;
3103 params->rate_num = oparams->rate_num;
3104 params->rate_den = oparams->rate_den;
3105 params->fifo_size = oparams->fifo_size;
3108 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old *oparams, snd_pcm_hw_params_t *params)
3112 memset(oparams, 0, sizeof(*oparams));
3113 oparams->flags = params->flags;
3114 for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++)
3115 oparams->masks[i] = params->masks[i].bits[0];
3116 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3117 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3118 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3119 oparams->info = params->info;
3120 oparams->msbits = params->msbits;
3121 oparams->rate_num = params->rate_num;
3122 oparams->rate_den = params->rate_den;
3123 oparams->fifo_size = params->fifo_size;
3126 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams)
3128 snd_pcm_hw_params_t params;
3129 struct sndrv_pcm_hw_params_old oparams;
3131 if (copy_from_user(&oparams, _oparams, sizeof(oparams)))
3133 snd_pcm_hw_convert_from_old_params(¶ms, &oparams);
3134 err = snd_pcm_hw_refine(substream, ¶ms);
3135 snd_pcm_hw_convert_to_old_params(&oparams, ¶ms);
3136 if (copy_to_user(_oparams, &oparams, sizeof(oparams)))
3141 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams)
3143 snd_pcm_hw_params_t params;
3144 struct sndrv_pcm_hw_params_old oparams;
3146 if (copy_from_user(&oparams, _oparams, sizeof(oparams)))
3148 snd_pcm_hw_convert_from_old_params(¶ms, &oparams);
3149 err = snd_pcm_hw_params(substream, ¶ms);
3150 snd_pcm_hw_convert_to_old_params(&oparams, ¶ms);
3151 if (copy_to_user(_oparams, &oparams, sizeof(oparams)))
3160 static struct file_operations snd_pcm_f_ops_playback = {
3161 .owner = THIS_MODULE,
3162 .write = snd_pcm_write,
3163 .writev = snd_pcm_writev,
3164 .open = snd_pcm_open,
3165 .release = snd_pcm_release,
3166 .poll = snd_pcm_playback_poll,
3167 .ioctl = snd_pcm_playback_ioctl,
3168 .mmap = snd_pcm_mmap,
3169 .fasync = snd_pcm_fasync,
3172 static struct file_operations snd_pcm_f_ops_capture = {
3173 .owner = THIS_MODULE,
3174 .read = snd_pcm_read,
3175 .readv = snd_pcm_readv,
3176 .open = snd_pcm_open,
3177 .release = snd_pcm_release,
3178 .poll = snd_pcm_capture_poll,
3179 .ioctl = snd_pcm_capture_ioctl,
3180 .mmap = snd_pcm_mmap,
3181 .fasync = snd_pcm_fasync,
3184 snd_minor_t snd_pcm_reg[2] =
3187 .comment = "digital audio playback",
3188 .f_ops = &snd_pcm_f_ops_playback,
3191 .comment = "digital audio capture",
3192 .f_ops = &snd_pcm_f_ops_capture,