2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.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
23 #include <linux/module.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos.h>
29 #include <linux/dma-mapping.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/compress_offload.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
38 #include <linux/uio.h>
39 #include <linux/delay.h>
45 struct snd_pcm_hw_params_old {
47 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
48 SNDRV_PCM_HW_PARAM_ACCESS + 1];
49 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
50 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
55 unsigned int rate_num;
56 unsigned int rate_den;
57 snd_pcm_uframes_t fifo_size;
58 unsigned char reserved[64];
61 #ifdef CONFIG_SND_SUPPORT_OLD_API
62 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
63 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
65 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
67 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
68 struct snd_pcm_hw_params_old __user * _oparams);
70 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
76 static DEFINE_RWLOCK(snd_pcm_link_rwlock);
77 static DECLARE_RWSEM(snd_pcm_link_rwsem);
79 /* Writer in rwsem may block readers even during its waiting in queue,
80 * and this may lead to a deadlock when the code path takes read sem
81 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
82 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
83 * sleep until all the readers are completed without blocking by writer.
85 static inline void down_write_nonfifo(struct rw_semaphore *lock)
87 while (!down_write_trylock(lock))
92 * snd_pcm_stream_lock - Lock the PCM stream
93 * @substream: PCM substream
95 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
96 * flag of the given substream. This also takes the global link rw lock
97 * (or rw sem), too, for avoiding the race with linked streams.
99 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
101 if (substream->pcm->nonatomic) {
102 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
103 mutex_lock(&substream->self_group.mutex);
105 read_lock(&snd_pcm_link_rwlock);
106 spin_lock(&substream->self_group.lock);
109 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
112 * snd_pcm_stream_lock - Unlock the PCM stream
113 * @substream: PCM substream
115 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
117 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
119 if (substream->pcm->nonatomic) {
120 mutex_unlock(&substream->self_group.mutex);
121 up_read(&snd_pcm_link_rwsem);
123 spin_unlock(&substream->self_group.lock);
124 read_unlock(&snd_pcm_link_rwlock);
127 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
130 * snd_pcm_stream_lock_irq - Lock the PCM stream
131 * @substream: PCM substream
133 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
134 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
135 * as snd_pcm_stream_lock().
137 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
139 if (!substream->pcm->nonatomic)
141 snd_pcm_stream_lock(substream);
143 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
146 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
147 * @substream: PCM substream
149 * This is a counter-part of snd_pcm_stream_lock_irq().
151 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
153 snd_pcm_stream_unlock(substream);
154 if (!substream->pcm->nonatomic)
157 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
159 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
161 unsigned long flags = 0;
162 if (!substream->pcm->nonatomic)
163 local_irq_save(flags);
164 snd_pcm_stream_lock(substream);
167 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
170 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
171 * @substream: PCM substream
174 * This is a counter-part of snd_pcm_stream_lock_irqsave().
176 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
179 snd_pcm_stream_unlock(substream);
180 if (!substream->pcm->nonatomic)
181 local_irq_restore(flags);
183 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
185 static inline mm_segment_t snd_enter_user(void)
187 mm_segment_t fs = get_fs();
192 static inline void snd_leave_user(mm_segment_t fs)
199 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
201 struct snd_pcm *pcm = substream->pcm;
202 struct snd_pcm_str *pstr = substream->pstr;
204 memset(info, 0, sizeof(*info));
205 info->card = pcm->card->number;
206 info->device = pcm->device;
207 info->stream = substream->stream;
208 info->subdevice = substream->number;
209 strlcpy(info->id, pcm->id, sizeof(info->id));
210 strlcpy(info->name, pcm->name, sizeof(info->name));
211 info->dev_class = pcm->dev_class;
212 info->dev_subclass = pcm->dev_subclass;
213 info->subdevices_count = pstr->substream_count;
214 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
215 strlcpy(info->subname, substream->name, sizeof(info->subname));
220 int snd_pcm_info_user(struct snd_pcm_substream *substream,
221 struct snd_pcm_info __user * _info)
223 struct snd_pcm_info *info;
226 info = kmalloc(sizeof(*info), GFP_KERNEL);
229 err = snd_pcm_info(substream, info);
231 if (copy_to_user(_info, info, sizeof(*info)))
238 static bool hw_support_mmap(struct snd_pcm_substream *substream)
240 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
242 /* check architectures that return -EINVAL from dma_mmap_coherent() */
243 /* FIXME: this should be some global flag */
244 #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
245 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
246 if (!substream->ops->mmap &&
247 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
256 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
257 static const char * const snd_pcm_hw_param_names[] = {
261 HW_PARAM(SAMPLE_BITS),
262 HW_PARAM(FRAME_BITS),
265 HW_PARAM(PERIOD_TIME),
266 HW_PARAM(PERIOD_SIZE),
267 HW_PARAM(PERIOD_BYTES),
269 HW_PARAM(BUFFER_TIME),
270 HW_PARAM(BUFFER_SIZE),
271 HW_PARAM(BUFFER_BYTES),
276 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
277 struct snd_pcm_hw_params *params)
280 struct snd_pcm_hardware *hw;
281 struct snd_interval *i = NULL;
282 struct snd_mask *m = NULL;
283 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
284 unsigned int rstamps[constrs->rules_num];
285 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
286 unsigned int stamp = 2;
290 params->fifo_size = 0;
291 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
293 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
294 params->rate_num = 0;
295 params->rate_den = 0;
298 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
299 m = hw_param_mask(params, k);
300 if (snd_mask_empty(m))
302 if (!(params->rmask & (1 << k)))
305 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
306 pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
308 changed = snd_mask_refine(m, constrs_mask(constrs, k));
310 pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
313 params->cmask |= 1 << k;
318 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
319 i = hw_param_interval(params, k);
320 if (snd_interval_empty(i))
322 if (!(params->rmask & (1 << k)))
325 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
330 i->openmin ? '(' : '[', i->min,
331 i->max, i->openmax ? ')' : ']');
334 changed = snd_interval_refine(i, constrs_interval(constrs, k));
339 pr_cont("%c%u %u%c\n",
340 i->openmin ? '(' : '[', i->min,
341 i->max, i->openmax ? ')' : ']');
344 params->cmask |= 1 << k;
349 for (k = 0; k < constrs->rules_num; k++)
351 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
352 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
355 for (k = 0; k < constrs->rules_num; k++) {
356 struct snd_pcm_hw_rule *r = &constrs->rules[k];
359 if (r->cond && !(r->cond & params->flags))
361 for (d = 0; r->deps[d] >= 0; d++) {
362 if (vstamps[r->deps[d]] > rstamps[k]) {
370 pr_debug("Rule %d [%p]: ", k, r->func);
372 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
373 if (hw_is_mask(r->var)) {
374 m = hw_param_mask(params, r->var);
375 pr_cont("%x", *m->bits);
377 i = hw_param_interval(params, r->var);
382 i->openmin ? '(' : '[', i->min,
383 i->max, i->openmax ? ')' : ']');
387 changed = r->func(params, r);
391 if (hw_is_mask(r->var))
392 pr_cont("%x", *m->bits);
398 i->openmin ? '(' : '[', i->min,
399 i->max, i->openmax ? ')' : ']');
405 if (changed && r->var >= 0) {
406 params->cmask |= (1 << r->var);
407 vstamps[r->var] = stamp;
415 if (!params->msbits) {
416 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
417 if (snd_interval_single(i))
418 params->msbits = snd_interval_value(i);
421 if (!params->rate_den) {
422 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
423 if (snd_interval_single(i)) {
424 params->rate_num = snd_interval_value(i);
425 params->rate_den = 1;
429 hw = &substream->runtime->hw;
431 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
432 SNDRV_PCM_INFO_DRAIN_TRIGGER);
433 if (!hw_support_mmap(substream))
434 params->info &= ~(SNDRV_PCM_INFO_MMAP |
435 SNDRV_PCM_INFO_MMAP_VALID);
437 if (!params->fifo_size) {
438 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
439 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
440 if (snd_mask_min(m) == snd_mask_max(m) &&
441 snd_interval_min(i) == snd_interval_max(i)) {
442 changed = substream->ops->ioctl(substream,
443 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
452 EXPORT_SYMBOL(snd_pcm_hw_refine);
454 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
455 struct snd_pcm_hw_params __user * _params)
457 struct snd_pcm_hw_params *params;
460 params = memdup_user(_params, sizeof(*params));
462 return PTR_ERR(params);
464 err = snd_pcm_hw_refine(substream, params);
465 if (copy_to_user(_params, params, sizeof(*params))) {
474 static int period_to_usecs(struct snd_pcm_runtime *runtime)
479 return -1; /* invalid */
481 /* take 75% of period time as the deadline */
482 usecs = (750000 / runtime->rate) * runtime->period_size;
483 usecs += ((750000 % runtime->rate) * runtime->period_size) /
489 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
491 snd_pcm_stream_lock_irq(substream);
492 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
493 substream->runtime->status->state = state;
494 snd_pcm_stream_unlock_irq(substream);
497 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
500 #ifdef CONFIG_SND_PCM_TIMER
501 if (substream->timer)
502 snd_timer_notify(substream->timer, event,
503 &substream->runtime->trigger_tstamp);
507 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
508 struct snd_pcm_hw_params *params)
510 struct snd_pcm_runtime *runtime;
513 snd_pcm_uframes_t frames;
515 if (PCM_RUNTIME_CHECK(substream))
517 runtime = substream->runtime;
518 snd_pcm_stream_lock_irq(substream);
519 switch (runtime->status->state) {
520 case SNDRV_PCM_STATE_OPEN:
521 case SNDRV_PCM_STATE_SETUP:
522 case SNDRV_PCM_STATE_PREPARED:
525 snd_pcm_stream_unlock_irq(substream);
528 snd_pcm_stream_unlock_irq(substream);
529 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
530 if (!substream->oss.oss)
532 if (atomic_read(&substream->mmap_count))
536 err = snd_pcm_hw_refine(substream, params);
540 err = snd_pcm_hw_params_choose(substream, params);
544 if (substream->ops->hw_params != NULL) {
545 err = substream->ops->hw_params(substream, params);
550 runtime->access = params_access(params);
551 runtime->format = params_format(params);
552 runtime->subformat = params_subformat(params);
553 runtime->channels = params_channels(params);
554 runtime->rate = params_rate(params);
555 runtime->period_size = params_period_size(params);
556 runtime->periods = params_periods(params);
557 runtime->buffer_size = params_buffer_size(params);
558 runtime->info = params->info;
559 runtime->rate_num = params->rate_num;
560 runtime->rate_den = params->rate_den;
561 runtime->no_period_wakeup =
562 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
563 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
565 bits = snd_pcm_format_physical_width(runtime->format);
566 runtime->sample_bits = bits;
567 bits *= runtime->channels;
568 runtime->frame_bits = bits;
570 while (bits % 8 != 0) {
574 runtime->byte_align = bits / 8;
575 runtime->min_align = frames;
577 /* Default sw params */
578 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
579 runtime->period_step = 1;
580 runtime->control->avail_min = runtime->period_size;
581 runtime->start_threshold = 1;
582 runtime->stop_threshold = runtime->buffer_size;
583 runtime->silence_threshold = 0;
584 runtime->silence_size = 0;
585 runtime->boundary = runtime->buffer_size;
586 while (runtime->boundary * 2 * runtime->channels <=
587 LONG_MAX - runtime->buffer_size)
588 runtime->boundary *= 2;
590 snd_pcm_timer_resolution_change(substream);
591 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
593 if (pm_qos_request_active(&substream->latency_pm_qos_req))
594 pm_qos_remove_request(&substream->latency_pm_qos_req);
595 if ((usecs = period_to_usecs(runtime)) >= 0)
596 pm_qos_add_request(&substream->latency_pm_qos_req,
597 PM_QOS_CPU_DMA_LATENCY, usecs);
600 /* hardware might be unusable from this time,
601 so we force application to retry to set
602 the correct hardware parameter settings */
603 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
604 if (substream->ops->hw_free != NULL)
605 substream->ops->hw_free(substream);
609 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
610 struct snd_pcm_hw_params __user * _params)
612 struct snd_pcm_hw_params *params;
615 params = memdup_user(_params, sizeof(*params));
617 return PTR_ERR(params);
619 err = snd_pcm_hw_params(substream, params);
620 if (copy_to_user(_params, params, sizeof(*params))) {
629 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
631 struct snd_pcm_runtime *runtime;
634 if (PCM_RUNTIME_CHECK(substream))
636 runtime = substream->runtime;
637 snd_pcm_stream_lock_irq(substream);
638 switch (runtime->status->state) {
639 case SNDRV_PCM_STATE_SETUP:
640 case SNDRV_PCM_STATE_PREPARED:
643 snd_pcm_stream_unlock_irq(substream);
646 snd_pcm_stream_unlock_irq(substream);
647 if (atomic_read(&substream->mmap_count))
649 if (substream->ops->hw_free)
650 result = substream->ops->hw_free(substream);
651 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
652 if (pm_qos_request_active(&substream->latency_pm_qos_req))
653 pm_qos_remove_request(&substream->latency_pm_qos_req);
658 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
659 struct snd_pcm_sw_params *params)
661 struct snd_pcm_runtime *runtime;
664 if (PCM_RUNTIME_CHECK(substream))
666 runtime = substream->runtime;
667 snd_pcm_stream_lock_irq(substream);
668 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
669 snd_pcm_stream_unlock_irq(substream);
672 snd_pcm_stream_unlock_irq(substream);
674 if (params->tstamp_mode < 0 ||
675 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
677 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
678 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
680 if (params->avail_min == 0)
682 if (params->silence_size >= runtime->boundary) {
683 if (params->silence_threshold != 0)
686 if (params->silence_size > params->silence_threshold)
688 if (params->silence_threshold > runtime->buffer_size)
692 snd_pcm_stream_lock_irq(substream);
693 runtime->tstamp_mode = params->tstamp_mode;
694 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
695 runtime->tstamp_type = params->tstamp_type;
696 runtime->period_step = params->period_step;
697 runtime->control->avail_min = params->avail_min;
698 runtime->start_threshold = params->start_threshold;
699 runtime->stop_threshold = params->stop_threshold;
700 runtime->silence_threshold = params->silence_threshold;
701 runtime->silence_size = params->silence_size;
702 params->boundary = runtime->boundary;
703 if (snd_pcm_running(substream)) {
704 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
705 runtime->silence_size > 0)
706 snd_pcm_playback_silence(substream, ULONG_MAX);
707 err = snd_pcm_update_state(substream, runtime);
709 snd_pcm_stream_unlock_irq(substream);
713 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
714 struct snd_pcm_sw_params __user * _params)
716 struct snd_pcm_sw_params params;
718 if (copy_from_user(¶ms, _params, sizeof(params)))
720 err = snd_pcm_sw_params(substream, ¶ms);
721 if (copy_to_user(_params, ¶ms, sizeof(params)))
726 int snd_pcm_status(struct snd_pcm_substream *substream,
727 struct snd_pcm_status *status)
729 struct snd_pcm_runtime *runtime = substream->runtime;
731 snd_pcm_stream_lock_irq(substream);
733 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
734 &runtime->audio_tstamp_config);
736 /* backwards compatible behavior */
737 if (runtime->audio_tstamp_config.type_requested ==
738 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
739 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
740 runtime->audio_tstamp_config.type_requested =
741 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
743 runtime->audio_tstamp_config.type_requested =
744 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
745 runtime->audio_tstamp_report.valid = 0;
747 runtime->audio_tstamp_report.valid = 1;
749 status->state = runtime->status->state;
750 status->suspended_state = runtime->status->suspended_state;
751 if (status->state == SNDRV_PCM_STATE_OPEN)
753 status->trigger_tstamp = runtime->trigger_tstamp;
754 if (snd_pcm_running(substream)) {
755 snd_pcm_update_hw_ptr(substream);
756 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
757 status->tstamp = runtime->status->tstamp;
758 status->driver_tstamp = runtime->driver_tstamp;
759 status->audio_tstamp =
760 runtime->status->audio_tstamp;
761 if (runtime->audio_tstamp_report.valid == 1)
762 /* backwards compatibility, no report provided in COMPAT mode */
763 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
764 &status->audio_tstamp_accuracy,
765 &runtime->audio_tstamp_report);
770 /* get tstamp only in fallback mode and only if enabled */
771 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
772 snd_pcm_gettime(runtime, &status->tstamp);
775 status->appl_ptr = runtime->control->appl_ptr;
776 status->hw_ptr = runtime->status->hw_ptr;
777 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
778 status->avail = snd_pcm_playback_avail(runtime);
779 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
780 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
781 status->delay = runtime->buffer_size - status->avail;
782 status->delay += runtime->delay;
786 status->avail = snd_pcm_capture_avail(runtime);
787 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
788 status->delay = status->avail + runtime->delay;
792 status->avail_max = runtime->avail_max;
793 status->overrange = runtime->overrange;
794 runtime->avail_max = 0;
795 runtime->overrange = 0;
797 snd_pcm_stream_unlock_irq(substream);
801 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
802 struct snd_pcm_status __user * _status,
805 struct snd_pcm_status status;
808 memset(&status, 0, sizeof(status));
810 * with extension, parameters are read/write,
811 * get audio_tstamp_data from user,
812 * ignore rest of status structure
814 if (ext && get_user(status.audio_tstamp_data,
815 (u32 __user *)(&_status->audio_tstamp_data)))
817 res = snd_pcm_status(substream, &status);
820 if (copy_to_user(_status, &status, sizeof(status)))
825 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
826 struct snd_pcm_channel_info * info)
828 struct snd_pcm_runtime *runtime;
829 unsigned int channel;
831 channel = info->channel;
832 runtime = substream->runtime;
833 snd_pcm_stream_lock_irq(substream);
834 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
835 snd_pcm_stream_unlock_irq(substream);
838 snd_pcm_stream_unlock_irq(substream);
839 if (channel >= runtime->channels)
841 memset(info, 0, sizeof(*info));
842 info->channel = channel;
843 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
846 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
847 struct snd_pcm_channel_info __user * _info)
849 struct snd_pcm_channel_info info;
852 if (copy_from_user(&info, _info, sizeof(info)))
854 res = snd_pcm_channel_info(substream, &info);
857 if (copy_to_user(_info, &info, sizeof(info)))
862 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
864 struct snd_pcm_runtime *runtime = substream->runtime;
865 if (runtime->trigger_master == NULL)
867 if (runtime->trigger_master == substream) {
868 if (!runtime->trigger_tstamp_latched)
869 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
871 snd_pcm_trigger_tstamp(runtime->trigger_master);
872 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
874 runtime->trigger_master = NULL;
878 int (*pre_action)(struct snd_pcm_substream *substream, int state);
879 int (*do_action)(struct snd_pcm_substream *substream, int state);
880 void (*undo_action)(struct snd_pcm_substream *substream, int state);
881 void (*post_action)(struct snd_pcm_substream *substream, int state);
885 * this functions is core for handling of linked stream
886 * Note: the stream state might be changed also on failure
887 * Note2: call with calling stream lock + link lock
889 static int snd_pcm_action_group(struct action_ops *ops,
890 struct snd_pcm_substream *substream,
891 int state, int do_lock)
893 struct snd_pcm_substream *s = NULL;
894 struct snd_pcm_substream *s1;
895 int res = 0, depth = 1;
897 snd_pcm_group_for_each_entry(s, substream) {
898 if (do_lock && s != substream) {
899 if (s->pcm->nonatomic)
900 mutex_lock_nested(&s->self_group.mutex, depth);
902 spin_lock_nested(&s->self_group.lock, depth);
905 res = ops->pre_action(s, state);
909 snd_pcm_group_for_each_entry(s, substream) {
910 res = ops->do_action(s, state);
912 if (ops->undo_action) {
913 snd_pcm_group_for_each_entry(s1, substream) {
914 if (s1 == s) /* failed stream */
916 ops->undo_action(s1, state);
919 s = NULL; /* unlock all */
923 snd_pcm_group_for_each_entry(s, substream) {
924 ops->post_action(s, state);
929 snd_pcm_group_for_each_entry(s1, substream) {
930 if (s1 != substream) {
931 if (s1->pcm->nonatomic)
932 mutex_unlock(&s1->self_group.mutex);
934 spin_unlock(&s1->self_group.lock);
936 if (s1 == s) /* end */
944 * Note: call with stream lock
946 static int snd_pcm_action_single(struct action_ops *ops,
947 struct snd_pcm_substream *substream,
952 res = ops->pre_action(substream, state);
955 res = ops->do_action(substream, state);
957 ops->post_action(substream, state);
958 else if (ops->undo_action)
959 ops->undo_action(substream, state);
964 * Note: call with stream lock
966 static int snd_pcm_action(struct action_ops *ops,
967 struct snd_pcm_substream *substream,
972 if (!snd_pcm_stream_linked(substream))
973 return snd_pcm_action_single(ops, substream, state);
975 if (substream->pcm->nonatomic) {
976 if (!mutex_trylock(&substream->group->mutex)) {
977 mutex_unlock(&substream->self_group.mutex);
978 mutex_lock(&substream->group->mutex);
979 mutex_lock(&substream->self_group.mutex);
981 res = snd_pcm_action_group(ops, substream, state, 1);
982 mutex_unlock(&substream->group->mutex);
984 if (!spin_trylock(&substream->group->lock)) {
985 spin_unlock(&substream->self_group.lock);
986 spin_lock(&substream->group->lock);
987 spin_lock(&substream->self_group.lock);
989 res = snd_pcm_action_group(ops, substream, state, 1);
990 spin_unlock(&substream->group->lock);
996 * Note: don't use any locks before
998 static int snd_pcm_action_lock_irq(struct action_ops *ops,
999 struct snd_pcm_substream *substream,
1004 snd_pcm_stream_lock_irq(substream);
1005 res = snd_pcm_action(ops, substream, state);
1006 snd_pcm_stream_unlock_irq(substream);
1012 static int snd_pcm_action_nonatomic(struct action_ops *ops,
1013 struct snd_pcm_substream *substream,
1018 down_read(&snd_pcm_link_rwsem);
1019 if (snd_pcm_stream_linked(substream))
1020 res = snd_pcm_action_group(ops, substream, state, 0);
1022 res = snd_pcm_action_single(ops, substream, state);
1023 up_read(&snd_pcm_link_rwsem);
1030 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1032 struct snd_pcm_runtime *runtime = substream->runtime;
1033 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1035 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1036 !substream->hw_no_buffer &&
1037 !snd_pcm_playback_data(substream))
1039 runtime->trigger_tstamp_latched = false;
1040 runtime->trigger_master = substream;
1044 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1046 if (substream->runtime->trigger_master != substream)
1048 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1051 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1053 if (substream->runtime->trigger_master == substream)
1054 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1057 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1059 struct snd_pcm_runtime *runtime = substream->runtime;
1060 snd_pcm_trigger_tstamp(substream);
1061 runtime->hw_ptr_jiffies = jiffies;
1062 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1064 runtime->status->state = state;
1065 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1066 runtime->silence_size > 0)
1067 snd_pcm_playback_silence(substream, ULONG_MAX);
1068 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1071 static struct action_ops snd_pcm_action_start = {
1072 .pre_action = snd_pcm_pre_start,
1073 .do_action = snd_pcm_do_start,
1074 .undo_action = snd_pcm_undo_start,
1075 .post_action = snd_pcm_post_start
1079 * snd_pcm_start - start all linked streams
1080 * @substream: the PCM substream instance
1082 * Return: Zero if successful, or a negative error code.
1084 int snd_pcm_start(struct snd_pcm_substream *substream)
1086 return snd_pcm_action(&snd_pcm_action_start, substream,
1087 SNDRV_PCM_STATE_RUNNING);
1090 static int snd_compressed_ioctl(struct snd_pcm_substream *substream,
1091 unsigned int cmd, void __user *arg)
1093 struct snd_pcm_runtime *runtime;
1096 if (PCM_RUNTIME_CHECK(substream))
1098 runtime = substream->runtime;
1099 pr_debug("%s called with cmd = %d\n", __func__, cmd);
1100 err = substream->ops->ioctl(substream, cmd, arg);
1104 static int snd_user_ioctl(struct snd_pcm_substream *substream,
1105 unsigned int cmd, void __user *arg)
1107 struct snd_pcm_runtime *runtime;
1110 if (PCM_RUNTIME_CHECK(substream))
1112 runtime = substream->runtime;
1113 err = substream->ops->ioctl(substream, cmd, arg);
1120 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1122 struct snd_pcm_runtime *runtime = substream->runtime;
1123 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1125 runtime->trigger_master = substream;
1129 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1131 if (substream->runtime->trigger_master == substream &&
1132 snd_pcm_running(substream))
1133 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1134 return 0; /* unconditonally stop all substreams */
1137 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1139 struct snd_pcm_runtime *runtime = substream->runtime;
1140 if (runtime->status->state != state) {
1141 snd_pcm_trigger_tstamp(substream);
1142 runtime->status->state = state;
1143 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1145 wake_up(&runtime->sleep);
1146 wake_up(&runtime->tsleep);
1149 static struct action_ops snd_pcm_action_stop = {
1150 .pre_action = snd_pcm_pre_stop,
1151 .do_action = snd_pcm_do_stop,
1152 .post_action = snd_pcm_post_stop
1156 * snd_pcm_stop - try to stop all running streams in the substream group
1157 * @substream: the PCM substream instance
1158 * @state: PCM state after stopping the stream
1160 * The state of each stream is then changed to the given state unconditionally.
1162 * Return: Zero if successful, or a negative error code.
1164 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1166 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1169 EXPORT_SYMBOL(snd_pcm_stop);
1172 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1173 * @substream: the PCM substream
1175 * After stopping, the state is changed to SETUP.
1176 * Unlike snd_pcm_stop(), this affects only the given stream.
1178 * Return: Zero if succesful, or a negative error code.
1180 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1182 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1183 SNDRV_PCM_STATE_SETUP);
1187 * snd_pcm_stop_xrun - stop the running streams as XRUN
1188 * @substream: the PCM substream instance
1190 * This stops the given running substream (and all linked substreams) as XRUN.
1191 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1193 * Return: Zero if successful, or a negative error code.
1195 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1197 unsigned long flags;
1200 snd_pcm_stream_lock_irqsave(substream, flags);
1201 if (snd_pcm_running(substream))
1202 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1203 snd_pcm_stream_unlock_irqrestore(substream, flags);
1206 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1211 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1213 struct snd_pcm_runtime *runtime = substream->runtime;
1214 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1217 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1219 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1221 runtime->trigger_master = substream;
1225 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1227 if (substream->runtime->trigger_master != substream)
1229 /* some drivers might use hw_ptr to recover from the pause -
1230 update the hw_ptr now */
1232 snd_pcm_update_hw_ptr(substream);
1233 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1234 * a delta between the current jiffies, this gives a large enough
1235 * delta, effectively to skip the check once.
1237 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1238 return substream->ops->trigger(substream,
1239 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1240 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1243 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1245 if (substream->runtime->trigger_master == substream)
1246 substream->ops->trigger(substream,
1247 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1248 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1251 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1253 struct snd_pcm_runtime *runtime = substream->runtime;
1254 snd_pcm_trigger_tstamp(substream);
1256 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1257 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1258 wake_up(&runtime->sleep);
1259 wake_up(&runtime->tsleep);
1261 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1262 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1266 static struct action_ops snd_pcm_action_pause = {
1267 .pre_action = snd_pcm_pre_pause,
1268 .do_action = snd_pcm_do_pause,
1269 .undo_action = snd_pcm_undo_pause,
1270 .post_action = snd_pcm_post_pause
1274 * Push/release the pause for all linked streams.
1276 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1278 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1284 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1286 struct snd_pcm_runtime *runtime = substream->runtime;
1287 switch (runtime->status->state) {
1288 case SNDRV_PCM_STATE_SUSPENDED:
1290 /* unresumable PCM state; return -EBUSY for skipping suspend */
1291 case SNDRV_PCM_STATE_OPEN:
1292 case SNDRV_PCM_STATE_SETUP:
1293 case SNDRV_PCM_STATE_DISCONNECTED:
1296 runtime->trigger_master = substream;
1300 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1302 struct snd_pcm_runtime *runtime = substream->runtime;
1303 if (runtime->trigger_master != substream)
1305 if (! snd_pcm_running(substream))
1307 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1308 return 0; /* suspend unconditionally */
1311 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1313 struct snd_pcm_runtime *runtime = substream->runtime;
1314 snd_pcm_trigger_tstamp(substream);
1315 runtime->status->suspended_state = runtime->status->state;
1316 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1317 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1318 wake_up(&runtime->sleep);
1319 wake_up(&runtime->tsleep);
1322 static struct action_ops snd_pcm_action_suspend = {
1323 .pre_action = snd_pcm_pre_suspend,
1324 .do_action = snd_pcm_do_suspend,
1325 .post_action = snd_pcm_post_suspend
1329 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1330 * @substream: the PCM substream
1332 * After this call, all streams are changed to SUSPENDED state.
1334 * Return: Zero if successful (or @substream is %NULL), or a negative error
1337 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1340 unsigned long flags;
1345 snd_pcm_stream_lock_irqsave(substream, flags);
1346 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1347 snd_pcm_stream_unlock_irqrestore(substream, flags);
1351 EXPORT_SYMBOL(snd_pcm_suspend);
1354 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1355 * @pcm: the PCM instance
1357 * After this call, all streams are changed to SUSPENDED state.
1359 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1361 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1363 struct snd_pcm_substream *substream;
1364 int stream, err = 0;
1369 for (stream = 0; stream < 2; stream++) {
1370 for (substream = pcm->streams[stream].substream;
1371 substream; substream = substream->next) {
1372 /* FIXME: the open/close code should lock this as well */
1373 if (substream->runtime == NULL)
1377 * Skip BE dai link PCM's that are internal and may
1378 * not have their substream ops set.
1380 if (!substream->ops)
1383 err = snd_pcm_suspend(substream);
1384 if (err < 0 && err != -EBUSY)
1391 EXPORT_SYMBOL(snd_pcm_suspend_all);
1395 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1397 struct snd_pcm_runtime *runtime = substream->runtime;
1398 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1400 runtime->trigger_master = substream;
1404 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1406 struct snd_pcm_runtime *runtime = substream->runtime;
1407 if (runtime->trigger_master != substream)
1409 /* DMA not running previously? */
1410 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1411 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1412 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1414 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1417 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1419 if (substream->runtime->trigger_master == substream &&
1420 snd_pcm_running(substream))
1421 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1424 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1426 struct snd_pcm_runtime *runtime = substream->runtime;
1427 snd_pcm_trigger_tstamp(substream);
1428 runtime->status->state = runtime->status->suspended_state;
1429 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1432 static struct action_ops snd_pcm_action_resume = {
1433 .pre_action = snd_pcm_pre_resume,
1434 .do_action = snd_pcm_do_resume,
1435 .undo_action = snd_pcm_undo_resume,
1436 .post_action = snd_pcm_post_resume
1439 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1441 struct snd_card *card = substream->pcm->card;
1444 snd_power_lock(card);
1445 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1446 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1447 snd_power_unlock(card);
1453 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1458 #endif /* CONFIG_PM */
1463 * Change the RUNNING stream(s) to XRUN state.
1465 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1467 struct snd_card *card = substream->pcm->card;
1468 struct snd_pcm_runtime *runtime = substream->runtime;
1471 snd_power_lock(card);
1472 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1473 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1478 snd_pcm_stream_lock_irq(substream);
1479 switch (runtime->status->state) {
1480 case SNDRV_PCM_STATE_XRUN:
1481 result = 0; /* already there */
1483 case SNDRV_PCM_STATE_RUNNING:
1484 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1489 snd_pcm_stream_unlock_irq(substream);
1491 snd_power_unlock(card);
1498 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1500 struct snd_pcm_runtime *runtime = substream->runtime;
1501 switch (runtime->status->state) {
1502 case SNDRV_PCM_STATE_RUNNING:
1503 case SNDRV_PCM_STATE_PREPARED:
1504 case SNDRV_PCM_STATE_PAUSED:
1505 case SNDRV_PCM_STATE_SUSPENDED:
1512 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1514 struct snd_pcm_runtime *runtime = substream->runtime;
1515 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1518 runtime->hw_ptr_base = 0;
1519 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1520 runtime->status->hw_ptr % runtime->period_size;
1521 runtime->silence_start = runtime->status->hw_ptr;
1522 runtime->silence_filled = 0;
1526 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1528 struct snd_pcm_runtime *runtime = substream->runtime;
1529 runtime->control->appl_ptr = runtime->status->hw_ptr;
1530 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1531 runtime->silence_size > 0)
1532 snd_pcm_playback_silence(substream, ULONG_MAX);
1535 static struct action_ops snd_pcm_action_reset = {
1536 .pre_action = snd_pcm_pre_reset,
1537 .do_action = snd_pcm_do_reset,
1538 .post_action = snd_pcm_post_reset
1541 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1543 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1549 /* we use the second argument for updating f_flags */
1550 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1553 struct snd_pcm_runtime *runtime = substream->runtime;
1554 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1555 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1557 if (snd_pcm_running(substream))
1559 substream->f_flags = f_flags;
1563 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1566 err = substream->ops->prepare(substream);
1569 return snd_pcm_do_reset(substream, 0);
1572 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1574 struct snd_pcm_runtime *runtime = substream->runtime;
1575 runtime->control->appl_ptr = runtime->status->hw_ptr;
1576 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1579 static struct action_ops snd_pcm_action_prepare = {
1580 .pre_action = snd_pcm_pre_prepare,
1581 .do_action = snd_pcm_do_prepare,
1582 .post_action = snd_pcm_post_prepare
1586 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1587 * @substream: the PCM substream instance
1588 * @file: file to refer f_flags
1590 * Return: Zero if successful, or a negative error code.
1592 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1596 struct snd_card *card = substream->pcm->card;
1600 f_flags = file->f_flags;
1602 f_flags = substream->f_flags;
1604 snd_power_lock(card);
1605 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1606 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1607 substream, f_flags);
1608 snd_power_unlock(card);
1616 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1618 struct snd_pcm_runtime *runtime = substream->runtime;
1619 switch (runtime->status->state) {
1620 case SNDRV_PCM_STATE_OPEN:
1621 case SNDRV_PCM_STATE_DISCONNECTED:
1622 case SNDRV_PCM_STATE_SUSPENDED:
1625 runtime->trigger_master = substream;
1629 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1631 struct snd_pcm_runtime *runtime = substream->runtime;
1632 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1633 switch (runtime->status->state) {
1634 case SNDRV_PCM_STATE_PREPARED:
1635 /* start playback stream if possible */
1636 if (! snd_pcm_playback_empty(substream)) {
1637 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1638 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1640 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1643 case SNDRV_PCM_STATE_RUNNING:
1644 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1646 case SNDRV_PCM_STATE_XRUN:
1647 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1653 /* stop running stream */
1654 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1655 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1656 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1657 snd_pcm_do_stop(substream, new_state);
1658 snd_pcm_post_stop(substream, new_state);
1662 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1663 runtime->trigger_master == substream &&
1664 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1665 return substream->ops->trigger(substream,
1666 SNDRV_PCM_TRIGGER_DRAIN);
1671 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1675 static struct action_ops snd_pcm_action_drain_init = {
1676 .pre_action = snd_pcm_pre_drain_init,
1677 .do_action = snd_pcm_do_drain_init,
1678 .post_action = snd_pcm_post_drain_init
1681 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1684 * Drain the stream(s).
1685 * When the substream is linked, sync until the draining of all playback streams
1687 * After this call, all streams are supposed to be either SETUP or DRAINING
1688 * (capture only) state.
1690 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1693 struct snd_card *card;
1694 struct snd_pcm_runtime *runtime;
1695 struct snd_pcm_substream *s;
1700 card = substream->pcm->card;
1701 runtime = substream->runtime;
1703 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1706 snd_power_lock(card);
1707 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1708 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1710 snd_power_unlock(card);
1716 if (file->f_flags & O_NONBLOCK)
1718 } else if (substream->f_flags & O_NONBLOCK)
1721 down_read(&snd_pcm_link_rwsem);
1722 snd_pcm_stream_lock_irq(substream);
1724 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1725 snd_pcm_pause(substream, 0);
1727 /* pre-start/stop - all running streams are changed to DRAINING state */
1728 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1731 /* in non-blocking, we don't wait in ioctl but let caller poll */
1739 struct snd_pcm_runtime *to_check;
1740 if (signal_pending(current)) {
1741 result = -ERESTARTSYS;
1744 /* find a substream to drain */
1746 snd_pcm_group_for_each_entry(s, substream) {
1747 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1749 runtime = s->runtime;
1750 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1756 break; /* all drained */
1757 init_waitqueue_entry(&wait, current);
1758 add_wait_queue(&to_check->sleep, &wait);
1759 snd_pcm_stream_unlock_irq(substream);
1760 up_read(&snd_pcm_link_rwsem);
1761 snd_power_unlock(card);
1762 if (runtime->no_period_wakeup)
1763 tout = MAX_SCHEDULE_TIMEOUT;
1766 if (runtime->rate) {
1767 long t = runtime->period_size * 2 / runtime->rate;
1768 tout = max(t, tout);
1770 tout = msecs_to_jiffies(tout * 1000);
1772 tout = schedule_timeout_interruptible(tout);
1773 snd_power_lock(card);
1774 down_read(&snd_pcm_link_rwsem);
1775 snd_pcm_stream_lock_irq(substream);
1776 remove_wait_queue(&to_check->sleep, &wait);
1777 if (card->shutdown) {
1782 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1785 dev_dbg(substream->pcm->card->dev,
1786 "playback drain error (DMA or IRQ trouble?)\n");
1787 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1795 snd_pcm_stream_unlock_irq(substream);
1796 up_read(&snd_pcm_link_rwsem);
1797 snd_power_unlock(card);
1805 * Immediately put all linked substreams into SETUP state.
1807 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1809 struct snd_pcm_runtime *runtime;
1812 if (PCM_RUNTIME_CHECK(substream))
1814 runtime = substream->runtime;
1816 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1817 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1818 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1821 snd_pcm_stream_lock_irq(substream);
1823 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1824 snd_pcm_pause(substream, 0);
1826 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1827 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1828 snd_pcm_stream_unlock_irq(substream);
1834 static bool is_pcm_file(struct file *file)
1836 struct inode *inode = file_inode(file);
1839 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1841 minor = iminor(inode);
1842 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1843 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1849 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1852 struct snd_pcm_file *pcm_file;
1853 struct snd_pcm_substream *substream1;
1854 struct snd_pcm_group *group;
1855 struct fd f = fdget(fd);
1859 if (!is_pcm_file(f.file)) {
1863 pcm_file = f.file->private_data;
1864 substream1 = pcm_file->substream;
1865 group = kmalloc(sizeof(*group), GFP_KERNEL);
1870 down_write_nonfifo(&snd_pcm_link_rwsem);
1871 write_lock_irq(&snd_pcm_link_rwlock);
1872 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1873 substream->runtime->status->state != substream1->runtime->status->state ||
1874 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1878 if (snd_pcm_stream_linked(substream1)) {
1882 if (!snd_pcm_stream_linked(substream)) {
1883 substream->group = group;
1885 spin_lock_init(&substream->group->lock);
1886 mutex_init(&substream->group->mutex);
1887 INIT_LIST_HEAD(&substream->group->substreams);
1888 list_add_tail(&substream->link_list, &substream->group->substreams);
1889 substream->group->count = 1;
1891 list_add_tail(&substream1->link_list, &substream->group->substreams);
1892 substream->group->count++;
1893 substream1->group = substream->group;
1895 write_unlock_irq(&snd_pcm_link_rwlock);
1896 up_write(&snd_pcm_link_rwsem);
1898 snd_card_unref(substream1->pcm->card);
1905 static void relink_to_local(struct snd_pcm_substream *substream)
1907 substream->group = &substream->self_group;
1908 INIT_LIST_HEAD(&substream->self_group.substreams);
1909 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1912 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1914 struct snd_pcm_substream *s;
1917 down_write_nonfifo(&snd_pcm_link_rwsem);
1918 write_lock_irq(&snd_pcm_link_rwlock);
1919 if (!snd_pcm_stream_linked(substream)) {
1923 list_del(&substream->link_list);
1924 substream->group->count--;
1925 if (substream->group->count == 1) { /* detach the last stream, too */
1926 snd_pcm_group_for_each_entry(s, substream) {
1930 kfree(substream->group);
1932 relink_to_local(substream);
1934 write_unlock_irq(&snd_pcm_link_rwlock);
1935 up_write(&snd_pcm_link_rwsem);
1942 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1943 struct snd_pcm_hw_rule *rule)
1945 struct snd_interval t;
1946 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1947 hw_param_interval_c(params, rule->deps[1]), &t);
1948 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1951 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1952 struct snd_pcm_hw_rule *rule)
1954 struct snd_interval t;
1955 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1956 hw_param_interval_c(params, rule->deps[1]), &t);
1957 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1960 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1961 struct snd_pcm_hw_rule *rule)
1963 struct snd_interval t;
1964 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1965 hw_param_interval_c(params, rule->deps[1]),
1966 (unsigned long) rule->private, &t);
1967 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1970 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1971 struct snd_pcm_hw_rule *rule)
1973 struct snd_interval t;
1974 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1975 (unsigned long) rule->private,
1976 hw_param_interval_c(params, rule->deps[1]), &t);
1977 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1980 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1981 struct snd_pcm_hw_rule *rule)
1984 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1986 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1988 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1990 if (! snd_mask_test(mask, k))
1992 bits = snd_pcm_format_physical_width(k);
1994 continue; /* ignore invalid formats */
1995 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1996 snd_mask_reset(&m, k);
1998 return snd_mask_refine(mask, &m);
2001 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2002 struct snd_pcm_hw_rule *rule)
2004 struct snd_interval t;
2010 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2012 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2014 bits = snd_pcm_format_physical_width(k);
2016 continue; /* ignore invalid formats */
2017 if (t.min > (unsigned)bits)
2019 if (t.max < (unsigned)bits)
2023 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2026 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2027 #error "Change this table"
2030 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2031 48000, 64000, 88200, 96000, 176400, 192000,
2034 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2035 .count = ARRAY_SIZE(rates),
2039 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2040 struct snd_pcm_hw_rule *rule)
2042 struct snd_pcm_hardware *hw = rule->private;
2043 return snd_interval_list(hw_param_interval(params, rule->var),
2044 snd_pcm_known_rates.count,
2045 snd_pcm_known_rates.list, hw->rates);
2048 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2049 struct snd_pcm_hw_rule *rule)
2051 struct snd_interval t;
2052 struct snd_pcm_substream *substream = rule->private;
2054 t.max = substream->buffer_bytes_max;
2058 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2061 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2063 struct snd_pcm_runtime *runtime = substream->runtime;
2064 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2067 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2068 snd_mask_any(constrs_mask(constrs, k));
2071 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2072 snd_interval_any(constrs_interval(constrs, k));
2075 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2076 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2077 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2078 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2079 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2081 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2082 snd_pcm_hw_rule_format, NULL,
2083 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2086 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2087 snd_pcm_hw_rule_sample_bits, NULL,
2088 SNDRV_PCM_HW_PARAM_FORMAT,
2089 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2092 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2093 snd_pcm_hw_rule_div, NULL,
2094 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2097 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2098 snd_pcm_hw_rule_mul, NULL,
2099 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2102 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2103 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2104 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2107 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2108 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2109 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2112 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2113 snd_pcm_hw_rule_div, NULL,
2114 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2117 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2118 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2119 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2122 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2123 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2124 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2127 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2128 snd_pcm_hw_rule_div, NULL,
2129 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2132 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2133 snd_pcm_hw_rule_div, NULL,
2134 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2137 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2138 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2139 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2142 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2143 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2144 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2147 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2148 snd_pcm_hw_rule_mul, NULL,
2149 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2152 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2153 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2154 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2157 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2158 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2159 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2162 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2163 snd_pcm_hw_rule_muldivk, (void*) 8,
2164 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2167 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2168 snd_pcm_hw_rule_muldivk, (void*) 8,
2169 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2172 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2173 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2174 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2177 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2178 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2179 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2185 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2187 struct snd_pcm_runtime *runtime = substream->runtime;
2188 struct snd_pcm_hardware *hw = &runtime->hw;
2190 unsigned int mask = 0;
2192 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2193 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2194 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2195 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2196 if (hw_support_mmap(substream)) {
2197 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2198 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2199 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2200 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2201 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2202 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2204 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2208 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2212 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2216 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2217 hw->channels_min, hw->channels_max);
2221 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2222 hw->rate_min, hw->rate_max);
2226 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2227 hw->period_bytes_min, hw->period_bytes_max);
2231 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2232 hw->periods_min, hw->periods_max);
2236 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2237 hw->period_bytes_min, hw->buffer_bytes_max);
2241 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2242 snd_pcm_hw_rule_buffer_bytes_max, substream,
2243 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2248 if (runtime->dma_bytes) {
2249 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2254 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2255 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2256 snd_pcm_hw_rule_rate, hw,
2257 SNDRV_PCM_HW_PARAM_RATE, -1);
2262 /* FIXME: this belong to lowlevel */
2263 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2268 static void pcm_release_private(struct snd_pcm_substream *substream)
2270 if (snd_pcm_stream_linked(substream))
2271 snd_pcm_unlink(substream);
2274 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2276 substream->ref_count--;
2277 if (substream->ref_count > 0)
2280 snd_pcm_drop(substream);
2281 if (substream->hw_opened) {
2282 if (substream->ops->hw_free &&
2283 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2284 substream->ops->hw_free(substream);
2285 substream->ops->close(substream);
2286 substream->hw_opened = 0;
2288 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2289 pm_qos_remove_request(&substream->latency_pm_qos_req);
2290 if (substream->pcm_release) {
2291 substream->pcm_release(substream);
2292 substream->pcm_release = NULL;
2294 snd_pcm_detach_substream(substream);
2297 EXPORT_SYMBOL(snd_pcm_release_substream);
2299 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2301 struct snd_pcm_substream **rsubstream)
2303 struct snd_pcm_substream *substream;
2306 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2309 if (substream->ref_count > 1) {
2310 *rsubstream = substream;
2314 err = snd_pcm_hw_constraints_init(substream);
2316 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2320 if ((err = substream->ops->open(substream)) < 0)
2323 substream->hw_opened = 1;
2325 err = snd_pcm_hw_constraints_complete(substream);
2327 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2331 *rsubstream = substream;
2335 snd_pcm_release_substream(substream);
2339 EXPORT_SYMBOL(snd_pcm_open_substream);
2341 static int snd_pcm_open_file(struct file *file,
2342 struct snd_pcm *pcm,
2345 struct snd_pcm_file *pcm_file;
2346 struct snd_pcm_substream *substream;
2349 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2353 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2354 if (pcm_file == NULL) {
2355 snd_pcm_release_substream(substream);
2358 pcm_file->substream = substream;
2359 if (substream->ref_count == 1) {
2360 substream->file = pcm_file;
2361 substream->pcm_release = pcm_release_private;
2363 file->private_data = pcm_file;
2368 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2370 struct snd_pcm *pcm;
2371 int err = nonseekable_open(inode, file);
2374 pcm = snd_lookup_minor_data(iminor(inode),
2375 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2376 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2378 snd_card_unref(pcm->card);
2382 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2384 struct snd_pcm *pcm;
2385 int err = nonseekable_open(inode, file);
2388 pcm = snd_lookup_minor_data(iminor(inode),
2389 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2390 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2392 snd_card_unref(pcm->card);
2396 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2405 err = snd_card_file_add(pcm->card, file);
2408 if (!try_module_get(pcm->card->module)) {
2412 init_waitqueue_entry(&wait, current);
2413 add_wait_queue(&pcm->open_wait, &wait);
2414 mutex_lock(&pcm->open_mutex);
2416 err = snd_pcm_open_file(file, pcm, stream);
2419 if (err == -EAGAIN) {
2420 if (file->f_flags & O_NONBLOCK) {
2426 set_current_state(TASK_INTERRUPTIBLE);
2427 mutex_unlock(&pcm->open_mutex);
2429 mutex_lock(&pcm->open_mutex);
2430 if (pcm->card->shutdown) {
2434 if (signal_pending(current)) {
2439 remove_wait_queue(&pcm->open_wait, &wait);
2440 mutex_unlock(&pcm->open_mutex);
2446 module_put(pcm->card->module);
2448 snd_card_file_remove(pcm->card, file);
2453 static int snd_pcm_release(struct inode *inode, struct file *file)
2455 struct snd_pcm *pcm;
2456 struct snd_pcm_substream *substream;
2457 struct snd_pcm_file *pcm_file;
2459 pcm_file = file->private_data;
2460 substream = pcm_file->substream;
2461 if (snd_BUG_ON(!substream))
2463 pcm = substream->pcm;
2464 mutex_lock(&pcm->open_mutex);
2465 snd_pcm_release_substream(substream);
2467 mutex_unlock(&pcm->open_mutex);
2468 wake_up(&pcm->open_wait);
2469 module_put(pcm->card->module);
2470 snd_card_file_remove(pcm->card, file);
2474 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2475 snd_pcm_uframes_t frames)
2477 struct snd_pcm_runtime *runtime = substream->runtime;
2478 snd_pcm_sframes_t appl_ptr;
2479 snd_pcm_sframes_t ret;
2480 snd_pcm_sframes_t hw_avail;
2485 snd_pcm_stream_lock_irq(substream);
2486 switch (runtime->status->state) {
2487 case SNDRV_PCM_STATE_PREPARED:
2489 case SNDRV_PCM_STATE_DRAINING:
2490 case SNDRV_PCM_STATE_RUNNING:
2491 if (snd_pcm_update_hw_ptr(substream) >= 0)
2494 case SNDRV_PCM_STATE_XRUN:
2497 case SNDRV_PCM_STATE_SUSPENDED:
2505 hw_avail = snd_pcm_playback_hw_avail(runtime);
2506 if (hw_avail <= 0) {
2510 if (frames > (snd_pcm_uframes_t)hw_avail)
2512 appl_ptr = runtime->control->appl_ptr - frames;
2514 appl_ptr += runtime->boundary;
2515 runtime->control->appl_ptr = appl_ptr;
2518 snd_pcm_stream_unlock_irq(substream);
2522 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2523 snd_pcm_uframes_t frames)
2525 struct snd_pcm_runtime *runtime = substream->runtime;
2526 snd_pcm_sframes_t appl_ptr;
2527 snd_pcm_sframes_t ret;
2528 snd_pcm_sframes_t hw_avail;
2533 snd_pcm_stream_lock_irq(substream);
2534 switch (runtime->status->state) {
2535 case SNDRV_PCM_STATE_PREPARED:
2536 case SNDRV_PCM_STATE_DRAINING:
2538 case SNDRV_PCM_STATE_RUNNING:
2539 if (snd_pcm_update_hw_ptr(substream) >= 0)
2542 case SNDRV_PCM_STATE_XRUN:
2545 case SNDRV_PCM_STATE_SUSPENDED:
2553 hw_avail = snd_pcm_capture_hw_avail(runtime);
2554 if (hw_avail <= 0) {
2558 if (frames > (snd_pcm_uframes_t)hw_avail)
2560 appl_ptr = runtime->control->appl_ptr - frames;
2562 appl_ptr += runtime->boundary;
2563 runtime->control->appl_ptr = appl_ptr;
2566 snd_pcm_stream_unlock_irq(substream);
2570 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2571 snd_pcm_uframes_t frames)
2573 struct snd_pcm_runtime *runtime = substream->runtime;
2574 snd_pcm_sframes_t appl_ptr;
2575 snd_pcm_sframes_t ret;
2576 snd_pcm_sframes_t avail;
2581 snd_pcm_stream_lock_irq(substream);
2582 switch (runtime->status->state) {
2583 case SNDRV_PCM_STATE_PREPARED:
2584 case SNDRV_PCM_STATE_PAUSED:
2586 case SNDRV_PCM_STATE_DRAINING:
2587 case SNDRV_PCM_STATE_RUNNING:
2588 if (snd_pcm_update_hw_ptr(substream) >= 0)
2591 case SNDRV_PCM_STATE_XRUN:
2594 case SNDRV_PCM_STATE_SUSPENDED:
2602 avail = snd_pcm_playback_avail(runtime);
2607 if (frames > (snd_pcm_uframes_t)avail)
2609 appl_ptr = runtime->control->appl_ptr + frames;
2610 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2611 appl_ptr -= runtime->boundary;
2612 runtime->control->appl_ptr = appl_ptr;
2615 snd_pcm_stream_unlock_irq(substream);
2619 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2620 snd_pcm_uframes_t frames)
2622 struct snd_pcm_runtime *runtime = substream->runtime;
2623 snd_pcm_sframes_t appl_ptr;
2624 snd_pcm_sframes_t ret;
2625 snd_pcm_sframes_t avail;
2630 snd_pcm_stream_lock_irq(substream);
2631 switch (runtime->status->state) {
2632 case SNDRV_PCM_STATE_PREPARED:
2633 case SNDRV_PCM_STATE_DRAINING:
2634 case SNDRV_PCM_STATE_PAUSED:
2636 case SNDRV_PCM_STATE_RUNNING:
2637 if (snd_pcm_update_hw_ptr(substream) >= 0)
2640 case SNDRV_PCM_STATE_XRUN:
2643 case SNDRV_PCM_STATE_SUSPENDED:
2651 avail = snd_pcm_capture_avail(runtime);
2656 if (frames > (snd_pcm_uframes_t)avail)
2658 appl_ptr = runtime->control->appl_ptr + frames;
2659 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2660 appl_ptr -= runtime->boundary;
2661 runtime->control->appl_ptr = appl_ptr;
2664 snd_pcm_stream_unlock_irq(substream);
2668 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2670 struct snd_pcm_runtime *runtime = substream->runtime;
2673 snd_pcm_stream_lock_irq(substream);
2674 switch (runtime->status->state) {
2675 case SNDRV_PCM_STATE_DRAINING:
2676 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2679 case SNDRV_PCM_STATE_RUNNING:
2680 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2683 case SNDRV_PCM_STATE_PREPARED:
2684 case SNDRV_PCM_STATE_SUSPENDED:
2687 case SNDRV_PCM_STATE_XRUN:
2695 snd_pcm_stream_unlock_irq(substream);
2699 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2700 snd_pcm_sframes_t __user *res)
2702 struct snd_pcm_runtime *runtime = substream->runtime;
2704 snd_pcm_sframes_t n = 0;
2706 snd_pcm_stream_lock_irq(substream);
2707 switch (runtime->status->state) {
2708 case SNDRV_PCM_STATE_DRAINING:
2709 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2712 case SNDRV_PCM_STATE_RUNNING:
2713 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2716 case SNDRV_PCM_STATE_PREPARED:
2717 case SNDRV_PCM_STATE_SUSPENDED:
2719 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2720 n = snd_pcm_playback_hw_avail(runtime);
2722 n = snd_pcm_capture_avail(runtime);
2723 n += runtime->delay;
2725 case SNDRV_PCM_STATE_XRUN:
2733 snd_pcm_stream_unlock_irq(substream);
2735 if (put_user(n, res))
2740 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2741 struct snd_pcm_sync_ptr __user *_sync_ptr)
2743 struct snd_pcm_runtime *runtime = substream->runtime;
2744 struct snd_pcm_sync_ptr sync_ptr;
2745 volatile struct snd_pcm_mmap_status *status;
2746 volatile struct snd_pcm_mmap_control *control;
2748 snd_pcm_uframes_t hw_avail;
2750 memset(&sync_ptr, 0, sizeof(sync_ptr));
2751 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2753 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2755 status = runtime->status;
2756 control = runtime->control;
2757 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2758 err = snd_pcm_hwsync(substream);
2762 snd_pcm_stream_lock_irq(substream);
2763 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2764 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2766 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2767 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2768 control->avail_min = sync_ptr.c.control.avail_min;
2770 sync_ptr.c.control.avail_min = control->avail_min;
2772 if (runtime->render_flag & SNDRV_NON_DMA_MODE) {
2773 hw_avail = snd_pcm_playback_hw_avail(runtime);
2774 if ((hw_avail >= runtime->start_threshold)
2775 && (runtime->render_flag &
2776 SNDRV_RENDER_STOPPED)) {
2777 if (substream->ops->restart)
2778 substream->ops->restart(substream);
2781 sync_ptr.s.status.state = status->state;
2782 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2783 sync_ptr.s.status.tstamp = status->tstamp;
2784 sync_ptr.s.status.suspended_state = status->suspended_state;
2785 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2786 snd_pcm_stream_unlock_irq(substream);
2787 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2792 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2794 struct snd_pcm_runtime *runtime = substream->runtime;
2797 if (get_user(arg, _arg))
2799 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2801 runtime->tstamp_type = arg;
2805 static int snd_pcm_common_ioctl1(struct file *file,
2806 struct snd_pcm_substream *substream,
2807 unsigned int cmd, void __user *arg)
2810 case SNDRV_PCM_IOCTL_PVERSION:
2811 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2812 case SNDRV_PCM_IOCTL_INFO:
2813 return snd_pcm_info_user(substream, arg);
2814 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2816 case SNDRV_PCM_IOCTL_TTSTAMP:
2817 return snd_pcm_tstamp(substream, arg);
2818 case SNDRV_PCM_IOCTL_HW_REFINE:
2819 return snd_pcm_hw_refine_user(substream, arg);
2820 case SNDRV_PCM_IOCTL_HW_PARAMS:
2821 return snd_pcm_hw_params_user(substream, arg);
2822 case SNDRV_PCM_IOCTL_HW_FREE:
2823 return snd_pcm_hw_free(substream);
2824 case SNDRV_PCM_IOCTL_SW_PARAMS:
2825 return snd_pcm_sw_params_user(substream, arg);
2826 case SNDRV_PCM_IOCTL_STATUS:
2827 return snd_pcm_status_user(substream, arg, false);
2828 case SNDRV_PCM_IOCTL_STATUS_EXT:
2829 return snd_pcm_status_user(substream, arg, true);
2830 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2831 return snd_pcm_channel_info_user(substream, arg);
2832 case SNDRV_PCM_IOCTL_PREPARE:
2833 return snd_pcm_prepare(substream, file);
2834 case SNDRV_PCM_IOCTL_RESET:
2835 return snd_pcm_reset(substream);
2836 case SNDRV_PCM_IOCTL_START:
2837 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2838 case SNDRV_PCM_IOCTL_LINK:
2839 return snd_pcm_link(substream, (int)(unsigned long) arg);
2840 case SNDRV_PCM_IOCTL_UNLINK:
2841 return snd_pcm_unlink(substream);
2842 case SNDRV_PCM_IOCTL_RESUME:
2843 return snd_pcm_resume(substream);
2844 case SNDRV_PCM_IOCTL_XRUN:
2845 return snd_pcm_xrun(substream);
2846 case SNDRV_PCM_IOCTL_HWSYNC:
2847 return snd_pcm_hwsync(substream);
2848 case SNDRV_PCM_IOCTL_DELAY:
2849 return snd_pcm_delay(substream, arg);
2850 case SNDRV_PCM_IOCTL_SYNC_PTR:
2851 return snd_pcm_sync_ptr(substream, arg);
2852 #ifdef CONFIG_SND_SUPPORT_OLD_API
2853 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2854 return snd_pcm_hw_refine_old_user(substream, arg);
2855 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2856 return snd_pcm_hw_params_old_user(substream, arg);
2858 case SNDRV_PCM_IOCTL_DRAIN:
2859 return snd_pcm_drain(substream, file);
2860 case SNDRV_PCM_IOCTL_DROP:
2861 return snd_pcm_drop(substream);
2862 case SNDRV_PCM_IOCTL_PAUSE:
2865 snd_pcm_stream_lock_irq(substream);
2866 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2867 snd_pcm_stream_unlock_irq(substream);
2870 case SNDRV_COMPRESS_GET_CAPS:
2871 case SNDRV_COMPRESS_GET_CODEC_CAPS:
2872 case SNDRV_COMPRESS_SET_PARAMS:
2873 case SNDRV_COMPRESS_GET_PARAMS:
2874 case SNDRV_COMPRESS_TSTAMP:
2875 case SNDRV_COMPRESS_DRAIN:
2876 return snd_compressed_ioctl(substream, cmd, arg);
2878 if (((cmd >> 8) & 0xff) == 'U')
2879 return snd_user_ioctl(substream, cmd, arg);
2881 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2885 static int snd_pcm_playback_ioctl1(struct file *file,
2886 struct snd_pcm_substream *substream,
2887 unsigned int cmd, void __user *arg)
2889 if (snd_BUG_ON(!substream))
2891 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2894 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2896 struct snd_xferi xferi;
2897 struct snd_xferi __user *_xferi = arg;
2898 struct snd_pcm_runtime *runtime = substream->runtime;
2899 snd_pcm_sframes_t result;
2900 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2902 if (put_user(0, &_xferi->result))
2904 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2906 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2907 __put_user(result, &_xferi->result);
2908 return result < 0 ? result : 0;
2910 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2912 struct snd_xfern xfern;
2913 struct snd_xfern __user *_xfern = arg;
2914 struct snd_pcm_runtime *runtime = substream->runtime;
2916 snd_pcm_sframes_t result;
2917 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2919 if (runtime->channels > 128)
2921 if (put_user(0, &_xfern->result))
2923 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2926 bufs = memdup_user(xfern.bufs,
2927 sizeof(void *) * runtime->channels);
2929 return PTR_ERR(bufs);
2930 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2932 __put_user(result, &_xfern->result);
2933 return result < 0 ? result : 0;
2935 case SNDRV_PCM_IOCTL_REWIND:
2937 snd_pcm_uframes_t frames;
2938 snd_pcm_uframes_t __user *_frames = arg;
2939 snd_pcm_sframes_t result;
2940 if (get_user(frames, _frames))
2942 if (put_user(0, _frames))
2944 result = snd_pcm_playback_rewind(substream, frames);
2945 __put_user(result, _frames);
2946 return result < 0 ? result : 0;
2948 case SNDRV_PCM_IOCTL_FORWARD:
2950 snd_pcm_uframes_t frames;
2951 snd_pcm_uframes_t __user *_frames = arg;
2952 snd_pcm_sframes_t result;
2953 if (get_user(frames, _frames))
2955 if (put_user(0, _frames))
2957 result = snd_pcm_playback_forward(substream, frames);
2958 __put_user(result, _frames);
2959 return result < 0 ? result : 0;
2962 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2965 static int snd_pcm_capture_ioctl1(struct file *file,
2966 struct snd_pcm_substream *substream,
2967 unsigned int cmd, void __user *arg)
2969 if (snd_BUG_ON(!substream))
2971 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2974 case SNDRV_PCM_IOCTL_READI_FRAMES:
2976 struct snd_xferi xferi;
2977 struct snd_xferi __user *_xferi = arg;
2978 struct snd_pcm_runtime *runtime = substream->runtime;
2979 snd_pcm_sframes_t result;
2980 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2982 if (put_user(0, &_xferi->result))
2984 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2986 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2987 __put_user(result, &_xferi->result);
2988 return result < 0 ? result : 0;
2990 case SNDRV_PCM_IOCTL_READN_FRAMES:
2992 struct snd_xfern xfern;
2993 struct snd_xfern __user *_xfern = arg;
2994 struct snd_pcm_runtime *runtime = substream->runtime;
2996 snd_pcm_sframes_t result;
2997 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2999 if (runtime->channels > 128)
3001 if (put_user(0, &_xfern->result))
3003 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3006 bufs = memdup_user(xfern.bufs,
3007 sizeof(void *) * runtime->channels);
3009 return PTR_ERR(bufs);
3010 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3012 __put_user(result, &_xfern->result);
3013 return result < 0 ? result : 0;
3015 case SNDRV_PCM_IOCTL_REWIND:
3017 snd_pcm_uframes_t frames;
3018 snd_pcm_uframes_t __user *_frames = arg;
3019 snd_pcm_sframes_t result;
3020 if (get_user(frames, _frames))
3022 if (put_user(0, _frames))
3024 result = snd_pcm_capture_rewind(substream, frames);
3025 __put_user(result, _frames);
3026 return result < 0 ? result : 0;
3028 case SNDRV_PCM_IOCTL_FORWARD:
3030 snd_pcm_uframes_t frames;
3031 snd_pcm_uframes_t __user *_frames = arg;
3032 snd_pcm_sframes_t result;
3033 if (get_user(frames, _frames))
3035 if (put_user(0, _frames))
3037 result = snd_pcm_capture_forward(substream, frames);
3038 __put_user(result, _frames);
3039 return result < 0 ? result : 0;
3042 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
3045 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
3048 struct snd_pcm_file *pcm_file;
3049 unsigned char ioctl_magic;
3051 pcm_file = file->private_data;
3052 ioctl_magic = ((cmd >> 8) & 0xff);
3054 if (ioctl_magic != 'A' && ioctl_magic != 'C' && ioctl_magic != 'U')
3057 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3058 (void __user *)arg);
3061 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3064 struct snd_pcm_file *pcm_file;
3065 unsigned char ioctl_magic;
3067 pcm_file = file->private_data;
3068 ioctl_magic = ((cmd >> 8) & 0xff);
3070 if (ioctl_magic != 'A' && ioctl_magic != 'U')
3073 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3074 (void __user *)arg);
3077 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3078 unsigned int cmd, void *arg)
3083 fs = snd_enter_user();
3084 switch (substream->stream) {
3085 case SNDRV_PCM_STREAM_PLAYBACK:
3086 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3087 (void __user *)arg);
3089 case SNDRV_PCM_STREAM_CAPTURE:
3090 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3091 (void __user *)arg);
3101 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3103 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3106 struct snd_pcm_file *pcm_file;
3107 struct snd_pcm_substream *substream;
3108 struct snd_pcm_runtime *runtime;
3109 snd_pcm_sframes_t result;
3111 pcm_file = file->private_data;
3112 substream = pcm_file->substream;
3113 if (PCM_RUNTIME_CHECK(substream))
3115 runtime = substream->runtime;
3116 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3118 if (!frame_aligned(runtime, count))
3120 count = bytes_to_frames(runtime, count);
3121 result = snd_pcm_lib_read(substream, buf, count);
3123 result = frames_to_bytes(runtime, result);
3127 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3128 size_t count, loff_t * offset)
3130 struct snd_pcm_file *pcm_file;
3131 struct snd_pcm_substream *substream;
3132 struct snd_pcm_runtime *runtime;
3133 snd_pcm_sframes_t result;
3135 pcm_file = file->private_data;
3136 substream = pcm_file->substream;
3137 if (PCM_RUNTIME_CHECK(substream))
3139 runtime = substream->runtime;
3140 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3142 if (!frame_aligned(runtime, count))
3144 count = bytes_to_frames(runtime, count);
3145 result = snd_pcm_lib_write(substream, buf, count);
3147 result = frames_to_bytes(runtime, result);
3151 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3153 struct snd_pcm_file *pcm_file;
3154 struct snd_pcm_substream *substream;
3155 struct snd_pcm_runtime *runtime;
3156 snd_pcm_sframes_t result;
3159 snd_pcm_uframes_t frames;
3161 pcm_file = iocb->ki_filp->private_data;
3162 substream = pcm_file->substream;
3163 if (PCM_RUNTIME_CHECK(substream))
3165 runtime = substream->runtime;
3166 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3168 if (!iter_is_iovec(to))
3170 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3172 if (!frame_aligned(runtime, to->iov->iov_len))
3174 frames = bytes_to_samples(runtime, to->iov->iov_len);
3175 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
3178 for (i = 0; i < to->nr_segs; ++i)
3179 bufs[i] = to->iov[i].iov_base;
3180 result = snd_pcm_lib_readv(substream, bufs, frames);
3182 result = frames_to_bytes(runtime, result);
3187 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3189 struct snd_pcm_file *pcm_file;
3190 struct snd_pcm_substream *substream;
3191 struct snd_pcm_runtime *runtime;
3192 snd_pcm_sframes_t result;
3195 snd_pcm_uframes_t frames;
3197 pcm_file = iocb->ki_filp->private_data;
3198 substream = pcm_file->substream;
3199 if (PCM_RUNTIME_CHECK(substream))
3201 runtime = substream->runtime;
3202 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3204 if (!iter_is_iovec(from))
3206 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3207 !frame_aligned(runtime, from->iov->iov_len))
3209 frames = bytes_to_samples(runtime, from->iov->iov_len);
3210 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
3213 for (i = 0; i < from->nr_segs; ++i)
3214 bufs[i] = from->iov[i].iov_base;
3215 result = snd_pcm_lib_writev(substream, bufs, frames);
3217 result = frames_to_bytes(runtime, result);
3222 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3224 struct snd_pcm_file *pcm_file;
3225 struct snd_pcm_substream *substream;
3226 struct snd_pcm_runtime *runtime;
3228 snd_pcm_uframes_t avail;
3230 pcm_file = file->private_data;
3232 substream = pcm_file->substream;
3233 if (PCM_RUNTIME_CHECK(substream))
3235 runtime = substream->runtime;
3237 poll_wait(file, &runtime->sleep, wait);
3239 snd_pcm_stream_lock_irq(substream);
3240 avail = snd_pcm_playback_avail(runtime);
3241 switch (runtime->status->state) {
3242 case SNDRV_PCM_STATE_RUNNING:
3243 case SNDRV_PCM_STATE_PREPARED:
3244 case SNDRV_PCM_STATE_PAUSED:
3245 if (avail >= runtime->control->avail_min) {
3246 mask = POLLOUT | POLLWRNORM;
3250 case SNDRV_PCM_STATE_DRAINING:
3254 mask = POLLOUT | POLLWRNORM | POLLERR;
3257 snd_pcm_stream_unlock_irq(substream);
3261 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3263 struct snd_pcm_file *pcm_file;
3264 struct snd_pcm_substream *substream;
3265 struct snd_pcm_runtime *runtime;
3267 snd_pcm_uframes_t avail;
3269 pcm_file = file->private_data;
3271 substream = pcm_file->substream;
3272 if (PCM_RUNTIME_CHECK(substream))
3274 runtime = substream->runtime;
3276 poll_wait(file, &runtime->sleep, wait);
3278 snd_pcm_stream_lock_irq(substream);
3279 avail = snd_pcm_capture_avail(runtime);
3280 switch (runtime->status->state) {
3281 case SNDRV_PCM_STATE_RUNNING:
3282 case SNDRV_PCM_STATE_PREPARED:
3283 case SNDRV_PCM_STATE_PAUSED:
3284 if (avail >= runtime->control->avail_min) {
3285 mask = POLLIN | POLLRDNORM;
3290 case SNDRV_PCM_STATE_DRAINING:
3292 mask = POLLIN | POLLRDNORM;
3297 mask = POLLIN | POLLRDNORM | POLLERR;
3300 snd_pcm_stream_unlock_irq(substream);
3309 * Only on coherent architectures, we can mmap the status and the control records
3310 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3312 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3314 * mmap status record
3316 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3317 struct vm_fault *vmf)
3319 struct snd_pcm_substream *substream = area->vm_private_data;
3320 struct snd_pcm_runtime *runtime;
3322 if (substream == NULL)
3323 return VM_FAULT_SIGBUS;
3324 runtime = substream->runtime;
3325 vmf->page = virt_to_page(runtime->status);
3326 get_page(vmf->page);
3330 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3332 .fault = snd_pcm_mmap_status_fault,
3335 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3336 struct vm_area_struct *area)
3339 if (!(area->vm_flags & VM_READ))
3341 size = area->vm_end - area->vm_start;
3342 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3344 area->vm_ops = &snd_pcm_vm_ops_status;
3345 area->vm_private_data = substream;
3346 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3351 * mmap control record
3353 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3354 struct vm_fault *vmf)
3356 struct snd_pcm_substream *substream = area->vm_private_data;
3357 struct snd_pcm_runtime *runtime;
3359 if (substream == NULL)
3360 return VM_FAULT_SIGBUS;
3361 runtime = substream->runtime;
3362 vmf->page = virt_to_page(runtime->control);
3363 get_page(vmf->page);
3367 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3369 .fault = snd_pcm_mmap_control_fault,
3372 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3373 struct vm_area_struct *area)
3376 if (!(area->vm_flags & VM_READ))
3378 size = area->vm_end - area->vm_start;
3379 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3381 area->vm_ops = &snd_pcm_vm_ops_control;
3382 area->vm_private_data = substream;
3383 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3386 #else /* ! coherent mmap */
3388 * don't support mmap for status and control records.
3390 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3391 struct vm_area_struct *area)
3395 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3396 struct vm_area_struct *area)
3400 #endif /* coherent mmap */
3402 static inline struct page *
3403 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3405 void *vaddr = substream->runtime->dma_area + ofs;
3406 return virt_to_page(vaddr);
3410 * fault callback for mmapping a RAM page
3412 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3413 struct vm_fault *vmf)
3415 struct snd_pcm_substream *substream = area->vm_private_data;
3416 struct snd_pcm_runtime *runtime;
3417 unsigned long offset;
3421 if (substream == NULL)
3422 return VM_FAULT_SIGBUS;
3423 runtime = substream->runtime;
3424 offset = vmf->pgoff << PAGE_SHIFT;
3425 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3426 if (offset > dma_bytes - PAGE_SIZE)
3427 return VM_FAULT_SIGBUS;
3428 if (substream->ops->page)
3429 page = substream->ops->page(substream, offset);
3431 page = snd_pcm_default_page_ops(substream, offset);
3433 return VM_FAULT_SIGBUS;
3439 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3440 .open = snd_pcm_mmap_data_open,
3441 .close = snd_pcm_mmap_data_close,
3444 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3445 .open = snd_pcm_mmap_data_open,
3446 .close = snd_pcm_mmap_data_close,
3447 .fault = snd_pcm_mmap_data_fault,
3451 * mmap the DMA buffer on RAM
3455 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3456 * @substream: PCM substream
3459 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3460 * this function is invoked implicitly.
3462 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3463 struct vm_area_struct *area)
3465 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3466 #ifdef CONFIG_GENERIC_ALLOCATOR
3467 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3468 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3469 return remap_pfn_range(area, area->vm_start,
3470 substream->dma_buffer.addr >> PAGE_SHIFT,
3471 area->vm_end - area->vm_start, area->vm_page_prot);
3473 #endif /* CONFIG_GENERIC_ALLOCATOR */
3474 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3475 if (!substream->ops->page &&
3476 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3477 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3479 substream->runtime->dma_area,
3480 substream->runtime->dma_addr,
3481 substream->runtime->dma_bytes);
3482 #endif /* CONFIG_X86 */
3483 /* mmap with fault handler */
3484 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3487 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3490 * mmap the DMA buffer on I/O memory area
3492 #if SNDRV_PCM_INFO_MMAP_IOMEM
3494 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3495 * @substream: PCM substream
3498 * When your hardware uses the iomapped pages as the hardware buffer and
3499 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3500 * is supposed to work only on limited architectures.
3502 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3503 struct vm_area_struct *area)
3505 struct snd_pcm_runtime *runtime = substream->runtime;;
3507 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3508 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3511 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3512 #endif /* SNDRV_PCM_INFO_MMAP */
3517 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3518 struct vm_area_struct *area)
3520 struct snd_pcm_runtime *runtime;
3522 unsigned long offset;
3526 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3527 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3530 if (!(area->vm_flags & VM_READ))
3533 runtime = substream->runtime;
3534 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3536 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3538 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3539 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3541 size = area->vm_end - area->vm_start;
3542 offset = area->vm_pgoff << PAGE_SHIFT;
3543 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3544 if ((size_t)size > dma_bytes)
3546 if (offset > dma_bytes - size)
3549 area->vm_ops = &snd_pcm_vm_ops_data;
3550 area->vm_private_data = substream;
3551 if (substream->ops->mmap)
3552 err = substream->ops->mmap(substream, area);
3554 err = snd_pcm_lib_default_mmap(substream, area);
3556 atomic_inc(&substream->mmap_count);
3560 EXPORT_SYMBOL(snd_pcm_mmap_data);
3562 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3564 struct snd_pcm_file * pcm_file;
3565 struct snd_pcm_substream *substream;
3566 unsigned long offset;
3568 pcm_file = file->private_data;
3569 substream = pcm_file->substream;
3570 if (PCM_RUNTIME_CHECK(substream))
3573 offset = area->vm_pgoff << PAGE_SHIFT;
3575 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3576 if (pcm_file->no_compat_mmap)
3578 return snd_pcm_mmap_status(substream, file, area);
3579 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3580 if (pcm_file->no_compat_mmap)
3582 return snd_pcm_mmap_control(substream, file, area);
3584 return snd_pcm_mmap_data(substream, file, area);
3589 static int snd_pcm_fasync(int fd, struct file * file, int on)
3591 struct snd_pcm_file * pcm_file;
3592 struct snd_pcm_substream *substream;
3593 struct snd_pcm_runtime *runtime;
3595 pcm_file = file->private_data;
3596 substream = pcm_file->substream;
3597 if (PCM_RUNTIME_CHECK(substream))
3599 runtime = substream->runtime;
3600 return fasync_helper(fd, file, on, &runtime->fasync);
3606 #ifdef CONFIG_COMPAT
3607 #include "pcm_compat.c"
3609 #define snd_pcm_ioctl_compat NULL
3613 * To be removed helpers to keep binary compatibility
3616 #ifdef CONFIG_SND_SUPPORT_OLD_API
3617 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3618 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3620 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3621 struct snd_pcm_hw_params_old *oparams)
3625 memset(params, 0, sizeof(*params));
3626 params->flags = oparams->flags;
3627 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3628 params->masks[i].bits[0] = oparams->masks[i];
3629 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3630 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3631 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3632 params->info = oparams->info;
3633 params->msbits = oparams->msbits;
3634 params->rate_num = oparams->rate_num;
3635 params->rate_den = oparams->rate_den;
3636 params->fifo_size = oparams->fifo_size;
3639 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3640 struct snd_pcm_hw_params *params)
3644 memset(oparams, 0, sizeof(*oparams));
3645 oparams->flags = params->flags;
3646 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3647 oparams->masks[i] = params->masks[i].bits[0];
3648 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3649 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3650 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3651 oparams->info = params->info;
3652 oparams->msbits = params->msbits;
3653 oparams->rate_num = params->rate_num;
3654 oparams->rate_den = params->rate_den;
3655 oparams->fifo_size = params->fifo_size;
3658 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3659 struct snd_pcm_hw_params_old __user * _oparams)
3661 struct snd_pcm_hw_params *params;
3662 struct snd_pcm_hw_params_old *oparams = NULL;
3665 params = kmalloc(sizeof(*params), GFP_KERNEL);
3669 oparams = memdup_user(_oparams, sizeof(*oparams));
3670 if (IS_ERR(oparams)) {
3671 err = PTR_ERR(oparams);
3674 snd_pcm_hw_convert_from_old_params(params, oparams);
3675 err = snd_pcm_hw_refine(substream, params);
3676 snd_pcm_hw_convert_to_old_params(oparams, params);
3677 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3688 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3689 struct snd_pcm_hw_params_old __user * _oparams)
3691 struct snd_pcm_hw_params *params;
3692 struct snd_pcm_hw_params_old *oparams = NULL;
3695 params = kmalloc(sizeof(*params), GFP_KERNEL);
3699 oparams = memdup_user(_oparams, sizeof(*oparams));
3700 if (IS_ERR(oparams)) {
3701 err = PTR_ERR(oparams);
3704 snd_pcm_hw_convert_from_old_params(params, oparams);
3705 err = snd_pcm_hw_params(substream, params);
3706 snd_pcm_hw_convert_to_old_params(oparams, params);
3707 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3717 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3720 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3723 unsigned long pgoff,
3724 unsigned long flags)
3726 struct snd_pcm_file *pcm_file = file->private_data;
3727 struct snd_pcm_substream *substream = pcm_file->substream;
3728 struct snd_pcm_runtime *runtime = substream->runtime;
3729 unsigned long offset = pgoff << PAGE_SHIFT;
3732 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3733 return (unsigned long)runtime->status;
3734 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3735 return (unsigned long)runtime->control;
3737 return (unsigned long)runtime->dma_area + offset;
3741 # define snd_pcm_get_unmapped_area NULL
3748 const struct file_operations snd_pcm_f_ops[2] = {
3750 .owner = THIS_MODULE,
3751 .write = snd_pcm_write,
3752 .write_iter = snd_pcm_writev,
3753 .open = snd_pcm_playback_open,
3754 .release = snd_pcm_release,
3755 .llseek = no_llseek,
3756 .poll = snd_pcm_playback_poll,
3757 .unlocked_ioctl = snd_pcm_playback_ioctl,
3758 .compat_ioctl = snd_pcm_ioctl_compat,
3759 .mmap = snd_pcm_mmap,
3760 .fasync = snd_pcm_fasync,
3761 .get_unmapped_area = snd_pcm_get_unmapped_area,
3764 .owner = THIS_MODULE,
3765 .read = snd_pcm_read,
3766 .read_iter = snd_pcm_readv,
3767 .open = snd_pcm_capture_open,
3768 .release = snd_pcm_release,
3769 .llseek = no_llseek,
3770 .poll = snd_pcm_capture_poll,
3771 .unlocked_ioctl = snd_pcm_capture_ioctl,
3772 .compat_ioctl = snd_pcm_ioctl_compat,
3773 .mmap = snd_pcm_mmap,
3774 .fasync = snd_pcm_fasync,
3775 .get_unmapped_area = snd_pcm_get_unmapped_area,