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 /* clear the buffer for avoiding possible kernel info leaks */
591 if (runtime->dma_area && !substream->ops->copy)
592 memset(runtime->dma_area, 0, runtime->dma_bytes);
594 snd_pcm_timer_resolution_change(substream);
595 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
597 if (pm_qos_request_active(&substream->latency_pm_qos_req))
598 pm_qos_remove_request(&substream->latency_pm_qos_req);
599 if ((usecs = period_to_usecs(runtime)) >= 0)
600 pm_qos_add_request(&substream->latency_pm_qos_req,
601 PM_QOS_CPU_DMA_LATENCY, usecs);
604 /* hardware might be unusable from this time,
605 so we force application to retry to set
606 the correct hardware parameter settings */
607 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
608 if (substream->ops->hw_free != NULL)
609 substream->ops->hw_free(substream);
613 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
614 struct snd_pcm_hw_params __user * _params)
616 struct snd_pcm_hw_params *params;
619 params = memdup_user(_params, sizeof(*params));
621 return PTR_ERR(params);
623 err = snd_pcm_hw_params(substream, params);
624 if (copy_to_user(_params, params, sizeof(*params))) {
633 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
635 struct snd_pcm_runtime *runtime;
638 if (PCM_RUNTIME_CHECK(substream))
640 runtime = substream->runtime;
641 snd_pcm_stream_lock_irq(substream);
642 switch (runtime->status->state) {
643 case SNDRV_PCM_STATE_SETUP:
644 case SNDRV_PCM_STATE_PREPARED:
647 snd_pcm_stream_unlock_irq(substream);
650 snd_pcm_stream_unlock_irq(substream);
651 if (atomic_read(&substream->mmap_count))
653 if (substream->ops->hw_free)
654 result = substream->ops->hw_free(substream);
655 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
656 if (pm_qos_request_active(&substream->latency_pm_qos_req))
657 pm_qos_remove_request(&substream->latency_pm_qos_req);
662 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
663 struct snd_pcm_sw_params *params)
665 struct snd_pcm_runtime *runtime;
668 if (PCM_RUNTIME_CHECK(substream))
670 runtime = substream->runtime;
671 snd_pcm_stream_lock_irq(substream);
672 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
673 snd_pcm_stream_unlock_irq(substream);
676 snd_pcm_stream_unlock_irq(substream);
678 if (params->tstamp_mode < 0 ||
679 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
681 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
682 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
684 if (params->avail_min == 0)
686 if (params->silence_size >= runtime->boundary) {
687 if (params->silence_threshold != 0)
690 if (params->silence_size > params->silence_threshold)
692 if (params->silence_threshold > runtime->buffer_size)
696 snd_pcm_stream_lock_irq(substream);
697 runtime->tstamp_mode = params->tstamp_mode;
698 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
699 runtime->tstamp_type = params->tstamp_type;
700 runtime->period_step = params->period_step;
701 runtime->control->avail_min = params->avail_min;
702 runtime->start_threshold = params->start_threshold;
703 runtime->stop_threshold = params->stop_threshold;
704 runtime->silence_threshold = params->silence_threshold;
705 runtime->silence_size = params->silence_size;
706 params->boundary = runtime->boundary;
707 if (snd_pcm_running(substream)) {
708 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
709 runtime->silence_size > 0)
710 snd_pcm_playback_silence(substream, ULONG_MAX);
711 err = snd_pcm_update_state(substream, runtime);
713 snd_pcm_stream_unlock_irq(substream);
717 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
718 struct snd_pcm_sw_params __user * _params)
720 struct snd_pcm_sw_params params;
722 if (copy_from_user(¶ms, _params, sizeof(params)))
724 err = snd_pcm_sw_params(substream, ¶ms);
725 if (copy_to_user(_params, ¶ms, sizeof(params)))
730 int snd_pcm_status(struct snd_pcm_substream *substream,
731 struct snd_pcm_status *status)
733 struct snd_pcm_runtime *runtime = substream->runtime;
735 snd_pcm_stream_lock_irq(substream);
737 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
738 &runtime->audio_tstamp_config);
740 /* backwards compatible behavior */
741 if (runtime->audio_tstamp_config.type_requested ==
742 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
743 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
744 runtime->audio_tstamp_config.type_requested =
745 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
747 runtime->audio_tstamp_config.type_requested =
748 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
749 runtime->audio_tstamp_report.valid = 0;
751 runtime->audio_tstamp_report.valid = 1;
753 status->state = runtime->status->state;
754 status->suspended_state = runtime->status->suspended_state;
755 if (status->state == SNDRV_PCM_STATE_OPEN)
757 status->trigger_tstamp = runtime->trigger_tstamp;
758 if (snd_pcm_running(substream)) {
759 snd_pcm_update_hw_ptr(substream);
760 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
761 status->tstamp = runtime->status->tstamp;
762 status->driver_tstamp = runtime->driver_tstamp;
763 status->audio_tstamp =
764 runtime->status->audio_tstamp;
765 if (runtime->audio_tstamp_report.valid == 1)
766 /* backwards compatibility, no report provided in COMPAT mode */
767 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
768 &status->audio_tstamp_accuracy,
769 &runtime->audio_tstamp_report);
774 /* get tstamp only in fallback mode and only if enabled */
775 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
776 snd_pcm_gettime(runtime, &status->tstamp);
779 status->appl_ptr = runtime->control->appl_ptr;
780 status->hw_ptr = runtime->status->hw_ptr;
781 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
782 status->avail = snd_pcm_playback_avail(runtime);
783 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
784 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
785 status->delay = runtime->buffer_size - status->avail;
786 status->delay += runtime->delay;
790 status->avail = snd_pcm_capture_avail(runtime);
791 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
792 status->delay = status->avail + runtime->delay;
796 status->avail_max = runtime->avail_max;
797 status->overrange = runtime->overrange;
798 runtime->avail_max = 0;
799 runtime->overrange = 0;
801 snd_pcm_stream_unlock_irq(substream);
805 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
806 struct snd_pcm_status __user * _status,
809 struct snd_pcm_status status;
812 memset(&status, 0, sizeof(status));
814 * with extension, parameters are read/write,
815 * get audio_tstamp_data from user,
816 * ignore rest of status structure
818 if (ext && get_user(status.audio_tstamp_data,
819 (u32 __user *)(&_status->audio_tstamp_data)))
821 res = snd_pcm_status(substream, &status);
824 if (copy_to_user(_status, &status, sizeof(status)))
829 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
830 struct snd_pcm_channel_info * info)
832 struct snd_pcm_runtime *runtime;
833 unsigned int channel;
835 channel = info->channel;
836 runtime = substream->runtime;
837 snd_pcm_stream_lock_irq(substream);
838 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
839 snd_pcm_stream_unlock_irq(substream);
842 snd_pcm_stream_unlock_irq(substream);
843 if (channel >= runtime->channels)
845 memset(info, 0, sizeof(*info));
846 info->channel = channel;
847 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
850 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
851 struct snd_pcm_channel_info __user * _info)
853 struct snd_pcm_channel_info info;
856 if (copy_from_user(&info, _info, sizeof(info)))
858 res = snd_pcm_channel_info(substream, &info);
861 if (copy_to_user(_info, &info, sizeof(info)))
866 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
868 struct snd_pcm_runtime *runtime = substream->runtime;
869 if (runtime->trigger_master == NULL)
871 if (runtime->trigger_master == substream) {
872 if (!runtime->trigger_tstamp_latched)
873 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
875 snd_pcm_trigger_tstamp(runtime->trigger_master);
876 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
878 runtime->trigger_master = NULL;
882 int (*pre_action)(struct snd_pcm_substream *substream, int state);
883 int (*do_action)(struct snd_pcm_substream *substream, int state);
884 void (*undo_action)(struct snd_pcm_substream *substream, int state);
885 void (*post_action)(struct snd_pcm_substream *substream, int state);
889 * this functions is core for handling of linked stream
890 * Note: the stream state might be changed also on failure
891 * Note2: call with calling stream lock + link lock
893 static int snd_pcm_action_group(struct action_ops *ops,
894 struct snd_pcm_substream *substream,
895 int state, int do_lock)
897 struct snd_pcm_substream *s = NULL;
898 struct snd_pcm_substream *s1;
899 int res = 0, depth = 1;
901 snd_pcm_group_for_each_entry(s, substream) {
902 if (do_lock && s != substream) {
903 if (s->pcm->nonatomic)
904 mutex_lock_nested(&s->self_group.mutex, depth);
906 spin_lock_nested(&s->self_group.lock, depth);
909 res = ops->pre_action(s, state);
913 snd_pcm_group_for_each_entry(s, substream) {
914 res = ops->do_action(s, state);
916 if (ops->undo_action) {
917 snd_pcm_group_for_each_entry(s1, substream) {
918 if (s1 == s) /* failed stream */
920 ops->undo_action(s1, state);
923 s = NULL; /* unlock all */
927 snd_pcm_group_for_each_entry(s, substream) {
928 ops->post_action(s, state);
933 snd_pcm_group_for_each_entry(s1, substream) {
934 if (s1 != substream) {
935 if (s1->pcm->nonatomic)
936 mutex_unlock(&s1->self_group.mutex);
938 spin_unlock(&s1->self_group.lock);
940 if (s1 == s) /* end */
948 * Note: call with stream lock
950 static int snd_pcm_action_single(struct action_ops *ops,
951 struct snd_pcm_substream *substream,
956 res = ops->pre_action(substream, state);
959 res = ops->do_action(substream, state);
961 ops->post_action(substream, state);
962 else if (ops->undo_action)
963 ops->undo_action(substream, state);
968 * Note: call with stream lock
970 static int snd_pcm_action(struct action_ops *ops,
971 struct snd_pcm_substream *substream,
976 if (!snd_pcm_stream_linked(substream))
977 return snd_pcm_action_single(ops, substream, state);
979 if (substream->pcm->nonatomic) {
980 if (!mutex_trylock(&substream->group->mutex)) {
981 mutex_unlock(&substream->self_group.mutex);
982 mutex_lock(&substream->group->mutex);
983 mutex_lock(&substream->self_group.mutex);
985 res = snd_pcm_action_group(ops, substream, state, 1);
986 mutex_unlock(&substream->group->mutex);
988 if (!spin_trylock(&substream->group->lock)) {
989 spin_unlock(&substream->self_group.lock);
990 spin_lock(&substream->group->lock);
991 spin_lock(&substream->self_group.lock);
993 res = snd_pcm_action_group(ops, substream, state, 1);
994 spin_unlock(&substream->group->lock);
1000 * Note: don't use any locks before
1002 static int snd_pcm_action_lock_irq(struct action_ops *ops,
1003 struct snd_pcm_substream *substream,
1008 snd_pcm_stream_lock_irq(substream);
1009 res = snd_pcm_action(ops, substream, state);
1010 snd_pcm_stream_unlock_irq(substream);
1016 static int snd_pcm_action_nonatomic(struct action_ops *ops,
1017 struct snd_pcm_substream *substream,
1022 down_read(&snd_pcm_link_rwsem);
1023 if (snd_pcm_stream_linked(substream))
1024 res = snd_pcm_action_group(ops, substream, state, 0);
1026 res = snd_pcm_action_single(ops, substream, state);
1027 up_read(&snd_pcm_link_rwsem);
1034 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1036 struct snd_pcm_runtime *runtime = substream->runtime;
1037 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1039 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1040 !substream->hw_no_buffer &&
1041 !snd_pcm_playback_data(substream))
1043 runtime->trigger_tstamp_latched = false;
1044 runtime->trigger_master = substream;
1048 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1050 if (substream->runtime->trigger_master != substream)
1052 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1055 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1057 if (substream->runtime->trigger_master == substream)
1058 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1061 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1063 struct snd_pcm_runtime *runtime = substream->runtime;
1064 snd_pcm_trigger_tstamp(substream);
1065 runtime->hw_ptr_jiffies = jiffies;
1066 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1068 runtime->status->state = state;
1069 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1070 runtime->silence_size > 0)
1071 snd_pcm_playback_silence(substream, ULONG_MAX);
1072 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1075 static struct action_ops snd_pcm_action_start = {
1076 .pre_action = snd_pcm_pre_start,
1077 .do_action = snd_pcm_do_start,
1078 .undo_action = snd_pcm_undo_start,
1079 .post_action = snd_pcm_post_start
1083 * snd_pcm_start - start all linked streams
1084 * @substream: the PCM substream instance
1086 * Return: Zero if successful, or a negative error code.
1088 int snd_pcm_start(struct snd_pcm_substream *substream)
1090 return snd_pcm_action(&snd_pcm_action_start, substream,
1091 SNDRV_PCM_STATE_RUNNING);
1094 static int snd_compressed_ioctl(struct snd_pcm_substream *substream,
1095 unsigned int cmd, void __user *arg)
1097 struct snd_pcm_runtime *runtime;
1100 if (PCM_RUNTIME_CHECK(substream))
1102 runtime = substream->runtime;
1103 pr_debug("%s called with cmd = %d\n", __func__, cmd);
1104 err = substream->ops->ioctl(substream, cmd, arg);
1108 static int snd_user_ioctl(struct snd_pcm_substream *substream,
1109 unsigned int cmd, void __user *arg)
1111 struct snd_pcm_runtime *runtime;
1114 if (PCM_RUNTIME_CHECK(substream))
1116 runtime = substream->runtime;
1117 err = substream->ops->ioctl(substream, cmd, arg);
1124 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1126 struct snd_pcm_runtime *runtime = substream->runtime;
1127 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1129 runtime->trigger_master = substream;
1133 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1135 if (substream->runtime->trigger_master == substream &&
1136 snd_pcm_running(substream))
1137 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1138 return 0; /* unconditonally stop all substreams */
1141 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1143 struct snd_pcm_runtime *runtime = substream->runtime;
1144 if (runtime->status->state != state) {
1145 snd_pcm_trigger_tstamp(substream);
1146 runtime->status->state = state;
1147 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1149 wake_up(&runtime->sleep);
1150 wake_up(&runtime->tsleep);
1153 static struct action_ops snd_pcm_action_stop = {
1154 .pre_action = snd_pcm_pre_stop,
1155 .do_action = snd_pcm_do_stop,
1156 .post_action = snd_pcm_post_stop
1160 * snd_pcm_stop - try to stop all running streams in the substream group
1161 * @substream: the PCM substream instance
1162 * @state: PCM state after stopping the stream
1164 * The state of each stream is then changed to the given state unconditionally.
1166 * Return: Zero if successful, or a negative error code.
1168 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1170 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1173 EXPORT_SYMBOL(snd_pcm_stop);
1176 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1177 * @substream: the PCM substream
1179 * After stopping, the state is changed to SETUP.
1180 * Unlike snd_pcm_stop(), this affects only the given stream.
1182 * Return: Zero if succesful, or a negative error code.
1184 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1186 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1187 SNDRV_PCM_STATE_SETUP);
1191 * snd_pcm_stop_xrun - stop the running streams as XRUN
1192 * @substream: the PCM substream instance
1194 * This stops the given running substream (and all linked substreams) as XRUN.
1195 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1197 * Return: Zero if successful, or a negative error code.
1199 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1201 unsigned long flags;
1204 snd_pcm_stream_lock_irqsave(substream, flags);
1205 if (snd_pcm_running(substream))
1206 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1207 snd_pcm_stream_unlock_irqrestore(substream, flags);
1210 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1215 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1217 struct snd_pcm_runtime *runtime = substream->runtime;
1218 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1221 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1223 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1225 runtime->trigger_master = substream;
1229 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1231 if (substream->runtime->trigger_master != substream)
1233 /* some drivers might use hw_ptr to recover from the pause -
1234 update the hw_ptr now */
1236 snd_pcm_update_hw_ptr(substream);
1237 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1238 * a delta between the current jiffies, this gives a large enough
1239 * delta, effectively to skip the check once.
1241 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1242 return substream->ops->trigger(substream,
1243 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1244 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1247 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1249 if (substream->runtime->trigger_master == substream)
1250 substream->ops->trigger(substream,
1251 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1252 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1255 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1257 struct snd_pcm_runtime *runtime = substream->runtime;
1258 snd_pcm_trigger_tstamp(substream);
1260 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1261 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1262 wake_up(&runtime->sleep);
1263 wake_up(&runtime->tsleep);
1265 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1266 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1270 static struct action_ops snd_pcm_action_pause = {
1271 .pre_action = snd_pcm_pre_pause,
1272 .do_action = snd_pcm_do_pause,
1273 .undo_action = snd_pcm_undo_pause,
1274 .post_action = snd_pcm_post_pause
1278 * Push/release the pause for all linked streams.
1280 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1282 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1288 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1290 struct snd_pcm_runtime *runtime = substream->runtime;
1291 switch (runtime->status->state) {
1292 case SNDRV_PCM_STATE_SUSPENDED:
1294 /* unresumable PCM state; return -EBUSY for skipping suspend */
1295 case SNDRV_PCM_STATE_OPEN:
1296 case SNDRV_PCM_STATE_SETUP:
1297 case SNDRV_PCM_STATE_DISCONNECTED:
1300 runtime->trigger_master = substream;
1304 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1306 struct snd_pcm_runtime *runtime = substream->runtime;
1307 if (runtime->trigger_master != substream)
1309 if (! snd_pcm_running(substream))
1311 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1312 return 0; /* suspend unconditionally */
1315 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1317 struct snd_pcm_runtime *runtime = substream->runtime;
1318 snd_pcm_trigger_tstamp(substream);
1319 runtime->status->suspended_state = runtime->status->state;
1320 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1321 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1322 wake_up(&runtime->sleep);
1323 wake_up(&runtime->tsleep);
1326 static struct action_ops snd_pcm_action_suspend = {
1327 .pre_action = snd_pcm_pre_suspend,
1328 .do_action = snd_pcm_do_suspend,
1329 .post_action = snd_pcm_post_suspend
1333 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1334 * @substream: the PCM substream
1336 * After this call, all streams are changed to SUSPENDED state.
1338 * Return: Zero if successful (or @substream is %NULL), or a negative error
1341 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1344 unsigned long flags;
1349 snd_pcm_stream_lock_irqsave(substream, flags);
1350 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1351 snd_pcm_stream_unlock_irqrestore(substream, flags);
1355 EXPORT_SYMBOL(snd_pcm_suspend);
1358 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1359 * @pcm: the PCM instance
1361 * After this call, all streams are changed to SUSPENDED state.
1363 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1365 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1367 struct snd_pcm_substream *substream;
1368 int stream, err = 0;
1373 for (stream = 0; stream < 2; stream++) {
1374 for (substream = pcm->streams[stream].substream;
1375 substream; substream = substream->next) {
1376 /* FIXME: the open/close code should lock this as well */
1377 if (substream->runtime == NULL)
1381 * Skip BE dai link PCM's that are internal and may
1382 * not have their substream ops set.
1384 if (!substream->ops)
1387 err = snd_pcm_suspend(substream);
1388 if (err < 0 && err != -EBUSY)
1395 EXPORT_SYMBOL(snd_pcm_suspend_all);
1399 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1401 struct snd_pcm_runtime *runtime = substream->runtime;
1402 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1404 runtime->trigger_master = substream;
1408 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1410 struct snd_pcm_runtime *runtime = substream->runtime;
1411 if (runtime->trigger_master != substream)
1413 /* DMA not running previously? */
1414 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1415 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1416 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1418 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1421 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1423 if (substream->runtime->trigger_master == substream &&
1424 snd_pcm_running(substream))
1425 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1428 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1430 struct snd_pcm_runtime *runtime = substream->runtime;
1431 snd_pcm_trigger_tstamp(substream);
1432 runtime->status->state = runtime->status->suspended_state;
1433 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1436 static struct action_ops snd_pcm_action_resume = {
1437 .pre_action = snd_pcm_pre_resume,
1438 .do_action = snd_pcm_do_resume,
1439 .undo_action = snd_pcm_undo_resume,
1440 .post_action = snd_pcm_post_resume
1443 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1445 struct snd_card *card = substream->pcm->card;
1448 snd_power_lock(card);
1449 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1450 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1451 snd_power_unlock(card);
1457 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1462 #endif /* CONFIG_PM */
1467 * Change the RUNNING stream(s) to XRUN state.
1469 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1471 struct snd_card *card = substream->pcm->card;
1472 struct snd_pcm_runtime *runtime = substream->runtime;
1475 snd_power_lock(card);
1476 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1477 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1482 snd_pcm_stream_lock_irq(substream);
1483 switch (runtime->status->state) {
1484 case SNDRV_PCM_STATE_XRUN:
1485 result = 0; /* already there */
1487 case SNDRV_PCM_STATE_RUNNING:
1488 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1493 snd_pcm_stream_unlock_irq(substream);
1495 snd_power_unlock(card);
1502 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1504 struct snd_pcm_runtime *runtime = substream->runtime;
1505 switch (runtime->status->state) {
1506 case SNDRV_PCM_STATE_RUNNING:
1507 case SNDRV_PCM_STATE_PREPARED:
1508 case SNDRV_PCM_STATE_PAUSED:
1509 case SNDRV_PCM_STATE_SUSPENDED:
1516 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1518 struct snd_pcm_runtime *runtime = substream->runtime;
1519 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1522 runtime->hw_ptr_base = 0;
1523 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1524 runtime->status->hw_ptr % runtime->period_size;
1525 runtime->silence_start = runtime->status->hw_ptr;
1526 runtime->silence_filled = 0;
1530 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1532 struct snd_pcm_runtime *runtime = substream->runtime;
1533 runtime->control->appl_ptr = runtime->status->hw_ptr;
1534 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1535 runtime->silence_size > 0)
1536 snd_pcm_playback_silence(substream, ULONG_MAX);
1539 static struct action_ops snd_pcm_action_reset = {
1540 .pre_action = snd_pcm_pre_reset,
1541 .do_action = snd_pcm_do_reset,
1542 .post_action = snd_pcm_post_reset
1545 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1547 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1553 /* we use the second argument for updating f_flags */
1554 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1557 struct snd_pcm_runtime *runtime = substream->runtime;
1558 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1559 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1561 if (snd_pcm_running(substream))
1563 substream->f_flags = f_flags;
1567 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1570 err = substream->ops->prepare(substream);
1573 return snd_pcm_do_reset(substream, 0);
1576 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1578 struct snd_pcm_runtime *runtime = substream->runtime;
1579 runtime->control->appl_ptr = runtime->status->hw_ptr;
1580 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1583 static struct action_ops snd_pcm_action_prepare = {
1584 .pre_action = snd_pcm_pre_prepare,
1585 .do_action = snd_pcm_do_prepare,
1586 .post_action = snd_pcm_post_prepare
1590 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1591 * @substream: the PCM substream instance
1592 * @file: file to refer f_flags
1594 * Return: Zero if successful, or a negative error code.
1596 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1600 struct snd_card *card = substream->pcm->card;
1604 f_flags = file->f_flags;
1606 f_flags = substream->f_flags;
1608 snd_power_lock(card);
1609 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1610 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1611 substream, f_flags);
1612 snd_power_unlock(card);
1620 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1622 struct snd_pcm_runtime *runtime = substream->runtime;
1623 switch (runtime->status->state) {
1624 case SNDRV_PCM_STATE_OPEN:
1625 case SNDRV_PCM_STATE_DISCONNECTED:
1626 case SNDRV_PCM_STATE_SUSPENDED:
1629 runtime->trigger_master = substream;
1633 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1635 struct snd_pcm_runtime *runtime = substream->runtime;
1636 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1637 switch (runtime->status->state) {
1638 case SNDRV_PCM_STATE_PREPARED:
1639 /* start playback stream if possible */
1640 if (! snd_pcm_playback_empty(substream)) {
1641 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1642 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1644 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1647 case SNDRV_PCM_STATE_RUNNING:
1648 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1650 case SNDRV_PCM_STATE_XRUN:
1651 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1657 /* stop running stream */
1658 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1659 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1660 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1661 snd_pcm_do_stop(substream, new_state);
1662 snd_pcm_post_stop(substream, new_state);
1666 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1667 runtime->trigger_master == substream &&
1668 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1669 return substream->ops->trigger(substream,
1670 SNDRV_PCM_TRIGGER_DRAIN);
1675 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1679 static struct action_ops snd_pcm_action_drain_init = {
1680 .pre_action = snd_pcm_pre_drain_init,
1681 .do_action = snd_pcm_do_drain_init,
1682 .post_action = snd_pcm_post_drain_init
1685 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1688 * Drain the stream(s).
1689 * When the substream is linked, sync until the draining of all playback streams
1691 * After this call, all streams are supposed to be either SETUP or DRAINING
1692 * (capture only) state.
1694 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1697 struct snd_card *card;
1698 struct snd_pcm_runtime *runtime;
1699 struct snd_pcm_substream *s;
1704 card = substream->pcm->card;
1705 runtime = substream->runtime;
1707 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1710 snd_power_lock(card);
1711 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1712 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1714 snd_power_unlock(card);
1720 if (file->f_flags & O_NONBLOCK)
1722 } else if (substream->f_flags & O_NONBLOCK)
1725 down_read(&snd_pcm_link_rwsem);
1726 snd_pcm_stream_lock_irq(substream);
1728 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1729 snd_pcm_pause(substream, 0);
1731 /* pre-start/stop - all running streams are changed to DRAINING state */
1732 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1735 /* in non-blocking, we don't wait in ioctl but let caller poll */
1743 struct snd_pcm_runtime *to_check;
1744 if (signal_pending(current)) {
1745 result = -ERESTARTSYS;
1748 /* find a substream to drain */
1750 snd_pcm_group_for_each_entry(s, substream) {
1751 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1753 runtime = s->runtime;
1754 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1760 break; /* all drained */
1761 init_waitqueue_entry(&wait, current);
1762 add_wait_queue(&to_check->sleep, &wait);
1763 snd_pcm_stream_unlock_irq(substream);
1764 up_read(&snd_pcm_link_rwsem);
1765 snd_power_unlock(card);
1766 if (runtime->no_period_wakeup)
1767 tout = MAX_SCHEDULE_TIMEOUT;
1770 if (runtime->rate) {
1771 long t = runtime->period_size * 2 / runtime->rate;
1772 tout = max(t, tout);
1774 tout = msecs_to_jiffies(tout * 1000);
1776 tout = schedule_timeout_interruptible(tout);
1777 snd_power_lock(card);
1778 down_read(&snd_pcm_link_rwsem);
1779 snd_pcm_stream_lock_irq(substream);
1780 remove_wait_queue(&to_check->sleep, &wait);
1781 if (card->shutdown) {
1786 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1789 dev_dbg(substream->pcm->card->dev,
1790 "playback drain error (DMA or IRQ trouble?)\n");
1791 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1799 snd_pcm_stream_unlock_irq(substream);
1800 up_read(&snd_pcm_link_rwsem);
1801 snd_power_unlock(card);
1809 * Immediately put all linked substreams into SETUP state.
1811 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1813 struct snd_pcm_runtime *runtime;
1816 if (PCM_RUNTIME_CHECK(substream))
1818 runtime = substream->runtime;
1820 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1821 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1822 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1825 snd_pcm_stream_lock_irq(substream);
1827 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1828 snd_pcm_pause(substream, 0);
1830 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1831 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1832 snd_pcm_stream_unlock_irq(substream);
1838 static bool is_pcm_file(struct file *file)
1840 struct inode *inode = file_inode(file);
1843 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1845 minor = iminor(inode);
1846 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1847 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1853 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1856 struct snd_pcm_file *pcm_file;
1857 struct snd_pcm_substream *substream1;
1858 struct snd_pcm_group *group;
1859 struct fd f = fdget(fd);
1863 if (!is_pcm_file(f.file)) {
1867 pcm_file = f.file->private_data;
1868 substream1 = pcm_file->substream;
1869 group = kmalloc(sizeof(*group), GFP_KERNEL);
1874 down_write_nonfifo(&snd_pcm_link_rwsem);
1875 write_lock_irq(&snd_pcm_link_rwlock);
1876 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1877 substream->runtime->status->state != substream1->runtime->status->state ||
1878 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1882 if (snd_pcm_stream_linked(substream1)) {
1886 if (!snd_pcm_stream_linked(substream)) {
1887 substream->group = group;
1889 spin_lock_init(&substream->group->lock);
1890 mutex_init(&substream->group->mutex);
1891 INIT_LIST_HEAD(&substream->group->substreams);
1892 list_add_tail(&substream->link_list, &substream->group->substreams);
1893 substream->group->count = 1;
1895 list_add_tail(&substream1->link_list, &substream->group->substreams);
1896 substream->group->count++;
1897 substream1->group = substream->group;
1899 write_unlock_irq(&snd_pcm_link_rwlock);
1900 up_write(&snd_pcm_link_rwsem);
1902 snd_card_unref(substream1->pcm->card);
1909 static void relink_to_local(struct snd_pcm_substream *substream)
1911 substream->group = &substream->self_group;
1912 INIT_LIST_HEAD(&substream->self_group.substreams);
1913 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1916 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1918 struct snd_pcm_substream *s;
1921 down_write_nonfifo(&snd_pcm_link_rwsem);
1922 write_lock_irq(&snd_pcm_link_rwlock);
1923 if (!snd_pcm_stream_linked(substream)) {
1927 list_del(&substream->link_list);
1928 substream->group->count--;
1929 if (substream->group->count == 1) { /* detach the last stream, too */
1930 snd_pcm_group_for_each_entry(s, substream) {
1934 kfree(substream->group);
1936 relink_to_local(substream);
1938 write_unlock_irq(&snd_pcm_link_rwlock);
1939 up_write(&snd_pcm_link_rwsem);
1946 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1947 struct snd_pcm_hw_rule *rule)
1949 struct snd_interval t;
1950 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1951 hw_param_interval_c(params, rule->deps[1]), &t);
1952 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1955 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1956 struct snd_pcm_hw_rule *rule)
1958 struct snd_interval t;
1959 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1960 hw_param_interval_c(params, rule->deps[1]), &t);
1961 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1964 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1965 struct snd_pcm_hw_rule *rule)
1967 struct snd_interval t;
1968 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1969 hw_param_interval_c(params, rule->deps[1]),
1970 (unsigned long) rule->private, &t);
1971 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1974 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1975 struct snd_pcm_hw_rule *rule)
1977 struct snd_interval t;
1978 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1979 (unsigned long) rule->private,
1980 hw_param_interval_c(params, rule->deps[1]), &t);
1981 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1984 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1985 struct snd_pcm_hw_rule *rule)
1988 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1990 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1992 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1994 if (! snd_mask_test(mask, k))
1996 bits = snd_pcm_format_physical_width(k);
1998 continue; /* ignore invalid formats */
1999 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2000 snd_mask_reset(&m, k);
2002 return snd_mask_refine(mask, &m);
2005 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2006 struct snd_pcm_hw_rule *rule)
2008 struct snd_interval t;
2014 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2016 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2018 bits = snd_pcm_format_physical_width(k);
2020 continue; /* ignore invalid formats */
2021 if (t.min > (unsigned)bits)
2023 if (t.max < (unsigned)bits)
2027 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2030 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2031 #error "Change this table"
2034 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2035 48000, 64000, 88200, 96000, 176400, 192000,
2038 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2039 .count = ARRAY_SIZE(rates),
2043 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2044 struct snd_pcm_hw_rule *rule)
2046 struct snd_pcm_hardware *hw = rule->private;
2047 return snd_interval_list(hw_param_interval(params, rule->var),
2048 snd_pcm_known_rates.count,
2049 snd_pcm_known_rates.list, hw->rates);
2052 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2053 struct snd_pcm_hw_rule *rule)
2055 struct snd_interval t;
2056 struct snd_pcm_substream *substream = rule->private;
2058 t.max = substream->buffer_bytes_max;
2062 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2065 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2067 struct snd_pcm_runtime *runtime = substream->runtime;
2068 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2071 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2072 snd_mask_any(constrs_mask(constrs, k));
2075 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2076 snd_interval_any(constrs_interval(constrs, k));
2079 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2080 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2081 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2082 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2083 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2085 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2086 snd_pcm_hw_rule_format, NULL,
2087 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2090 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2091 snd_pcm_hw_rule_sample_bits, NULL,
2092 SNDRV_PCM_HW_PARAM_FORMAT,
2093 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2096 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2097 snd_pcm_hw_rule_div, NULL,
2098 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2101 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2102 snd_pcm_hw_rule_mul, NULL,
2103 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2106 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2107 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2108 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2111 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2112 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2113 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2116 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2117 snd_pcm_hw_rule_div, NULL,
2118 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2121 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2122 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2123 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2126 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2127 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2128 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2131 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2132 snd_pcm_hw_rule_div, NULL,
2133 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2136 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2137 snd_pcm_hw_rule_div, NULL,
2138 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2141 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2142 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2143 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2146 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2147 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2148 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2151 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2152 snd_pcm_hw_rule_mul, NULL,
2153 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2156 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2157 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2158 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2161 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2162 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2163 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2166 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2167 snd_pcm_hw_rule_muldivk, (void*) 8,
2168 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2171 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2172 snd_pcm_hw_rule_muldivk, (void*) 8,
2173 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2176 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2177 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2178 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2181 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2182 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2183 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2189 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2191 struct snd_pcm_runtime *runtime = substream->runtime;
2192 struct snd_pcm_hardware *hw = &runtime->hw;
2194 unsigned int mask = 0;
2196 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2197 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2198 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2199 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2200 if (hw_support_mmap(substream)) {
2201 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2202 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2203 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2204 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2205 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2206 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2208 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2212 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2216 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2220 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2221 hw->channels_min, hw->channels_max);
2225 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2226 hw->rate_min, hw->rate_max);
2230 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2231 hw->period_bytes_min, hw->period_bytes_max);
2235 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2236 hw->periods_min, hw->periods_max);
2240 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2241 hw->period_bytes_min, hw->buffer_bytes_max);
2245 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2246 snd_pcm_hw_rule_buffer_bytes_max, substream,
2247 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2252 if (runtime->dma_bytes) {
2253 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2258 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2259 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2260 snd_pcm_hw_rule_rate, hw,
2261 SNDRV_PCM_HW_PARAM_RATE, -1);
2266 /* FIXME: this belong to lowlevel */
2267 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2272 static void pcm_release_private(struct snd_pcm_substream *substream)
2274 if (snd_pcm_stream_linked(substream))
2275 snd_pcm_unlink(substream);
2278 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2280 substream->ref_count--;
2281 if (substream->ref_count > 0)
2284 snd_pcm_drop(substream);
2285 if (substream->hw_opened) {
2286 if (substream->ops->hw_free &&
2287 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2288 substream->ops->hw_free(substream);
2289 substream->ops->close(substream);
2290 substream->hw_opened = 0;
2292 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2293 pm_qos_remove_request(&substream->latency_pm_qos_req);
2294 if (substream->pcm_release) {
2295 substream->pcm_release(substream);
2296 substream->pcm_release = NULL;
2298 snd_pcm_detach_substream(substream);
2301 EXPORT_SYMBOL(snd_pcm_release_substream);
2303 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2305 struct snd_pcm_substream **rsubstream)
2307 struct snd_pcm_substream *substream;
2310 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2313 if (substream->ref_count > 1) {
2314 *rsubstream = substream;
2318 err = snd_pcm_hw_constraints_init(substream);
2320 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2324 if ((err = substream->ops->open(substream)) < 0)
2327 substream->hw_opened = 1;
2329 err = snd_pcm_hw_constraints_complete(substream);
2331 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2335 *rsubstream = substream;
2339 snd_pcm_release_substream(substream);
2343 EXPORT_SYMBOL(snd_pcm_open_substream);
2345 static int snd_pcm_open_file(struct file *file,
2346 struct snd_pcm *pcm,
2349 struct snd_pcm_file *pcm_file;
2350 struct snd_pcm_substream *substream;
2353 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2357 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2358 if (pcm_file == NULL) {
2359 snd_pcm_release_substream(substream);
2362 pcm_file->substream = substream;
2363 if (substream->ref_count == 1) {
2364 substream->file = pcm_file;
2365 substream->pcm_release = pcm_release_private;
2367 file->private_data = pcm_file;
2372 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2374 struct snd_pcm *pcm;
2375 int err = nonseekable_open(inode, file);
2378 pcm = snd_lookup_minor_data(iminor(inode),
2379 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2380 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2382 snd_card_unref(pcm->card);
2386 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2388 struct snd_pcm *pcm;
2389 int err = nonseekable_open(inode, file);
2392 pcm = snd_lookup_minor_data(iminor(inode),
2393 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2394 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2396 snd_card_unref(pcm->card);
2400 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2409 err = snd_card_file_add(pcm->card, file);
2412 if (!try_module_get(pcm->card->module)) {
2416 init_waitqueue_entry(&wait, current);
2417 add_wait_queue(&pcm->open_wait, &wait);
2418 mutex_lock(&pcm->open_mutex);
2420 err = snd_pcm_open_file(file, pcm, stream);
2423 if (err == -EAGAIN) {
2424 if (file->f_flags & O_NONBLOCK) {
2430 set_current_state(TASK_INTERRUPTIBLE);
2431 mutex_unlock(&pcm->open_mutex);
2433 mutex_lock(&pcm->open_mutex);
2434 if (pcm->card->shutdown) {
2438 if (signal_pending(current)) {
2443 remove_wait_queue(&pcm->open_wait, &wait);
2444 mutex_unlock(&pcm->open_mutex);
2450 module_put(pcm->card->module);
2452 snd_card_file_remove(pcm->card, file);
2457 static int snd_pcm_release(struct inode *inode, struct file *file)
2459 struct snd_pcm *pcm;
2460 struct snd_pcm_substream *substream;
2461 struct snd_pcm_file *pcm_file;
2463 pcm_file = file->private_data;
2464 substream = pcm_file->substream;
2465 if (snd_BUG_ON(!substream))
2467 pcm = substream->pcm;
2468 mutex_lock(&pcm->open_mutex);
2469 snd_pcm_release_substream(substream);
2471 mutex_unlock(&pcm->open_mutex);
2472 wake_up(&pcm->open_wait);
2473 module_put(pcm->card->module);
2474 snd_card_file_remove(pcm->card, file);
2478 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2479 snd_pcm_uframes_t frames)
2481 struct snd_pcm_runtime *runtime = substream->runtime;
2482 snd_pcm_sframes_t appl_ptr;
2483 snd_pcm_sframes_t ret;
2484 snd_pcm_sframes_t hw_avail;
2489 snd_pcm_stream_lock_irq(substream);
2490 switch (runtime->status->state) {
2491 case SNDRV_PCM_STATE_PREPARED:
2493 case SNDRV_PCM_STATE_DRAINING:
2494 case SNDRV_PCM_STATE_RUNNING:
2495 if (snd_pcm_update_hw_ptr(substream) >= 0)
2498 case SNDRV_PCM_STATE_XRUN:
2501 case SNDRV_PCM_STATE_SUSPENDED:
2509 hw_avail = snd_pcm_playback_hw_avail(runtime);
2510 if (hw_avail <= 0) {
2514 if (frames > (snd_pcm_uframes_t)hw_avail)
2516 appl_ptr = runtime->control->appl_ptr - frames;
2518 appl_ptr += runtime->boundary;
2519 runtime->control->appl_ptr = appl_ptr;
2522 snd_pcm_stream_unlock_irq(substream);
2526 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2527 snd_pcm_uframes_t frames)
2529 struct snd_pcm_runtime *runtime = substream->runtime;
2530 snd_pcm_sframes_t appl_ptr;
2531 snd_pcm_sframes_t ret;
2532 snd_pcm_sframes_t hw_avail;
2537 snd_pcm_stream_lock_irq(substream);
2538 switch (runtime->status->state) {
2539 case SNDRV_PCM_STATE_PREPARED:
2540 case SNDRV_PCM_STATE_DRAINING:
2542 case SNDRV_PCM_STATE_RUNNING:
2543 if (snd_pcm_update_hw_ptr(substream) >= 0)
2546 case SNDRV_PCM_STATE_XRUN:
2549 case SNDRV_PCM_STATE_SUSPENDED:
2557 hw_avail = snd_pcm_capture_hw_avail(runtime);
2558 if (hw_avail <= 0) {
2562 if (frames > (snd_pcm_uframes_t)hw_avail)
2564 appl_ptr = runtime->control->appl_ptr - frames;
2566 appl_ptr += runtime->boundary;
2567 runtime->control->appl_ptr = appl_ptr;
2570 snd_pcm_stream_unlock_irq(substream);
2574 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2575 snd_pcm_uframes_t frames)
2577 struct snd_pcm_runtime *runtime = substream->runtime;
2578 snd_pcm_sframes_t appl_ptr;
2579 snd_pcm_sframes_t ret;
2580 snd_pcm_sframes_t avail;
2585 snd_pcm_stream_lock_irq(substream);
2586 switch (runtime->status->state) {
2587 case SNDRV_PCM_STATE_PREPARED:
2588 case SNDRV_PCM_STATE_PAUSED:
2590 case SNDRV_PCM_STATE_DRAINING:
2591 case SNDRV_PCM_STATE_RUNNING:
2592 if (snd_pcm_update_hw_ptr(substream) >= 0)
2595 case SNDRV_PCM_STATE_XRUN:
2598 case SNDRV_PCM_STATE_SUSPENDED:
2606 avail = snd_pcm_playback_avail(runtime);
2611 if (frames > (snd_pcm_uframes_t)avail)
2613 appl_ptr = runtime->control->appl_ptr + frames;
2614 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2615 appl_ptr -= runtime->boundary;
2616 runtime->control->appl_ptr = appl_ptr;
2619 snd_pcm_stream_unlock_irq(substream);
2623 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2624 snd_pcm_uframes_t frames)
2626 struct snd_pcm_runtime *runtime = substream->runtime;
2627 snd_pcm_sframes_t appl_ptr;
2628 snd_pcm_sframes_t ret;
2629 snd_pcm_sframes_t avail;
2634 snd_pcm_stream_lock_irq(substream);
2635 switch (runtime->status->state) {
2636 case SNDRV_PCM_STATE_PREPARED:
2637 case SNDRV_PCM_STATE_DRAINING:
2638 case SNDRV_PCM_STATE_PAUSED:
2640 case SNDRV_PCM_STATE_RUNNING:
2641 if (snd_pcm_update_hw_ptr(substream) >= 0)
2644 case SNDRV_PCM_STATE_XRUN:
2647 case SNDRV_PCM_STATE_SUSPENDED:
2655 avail = snd_pcm_capture_avail(runtime);
2660 if (frames > (snd_pcm_uframes_t)avail)
2662 appl_ptr = runtime->control->appl_ptr + frames;
2663 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2664 appl_ptr -= runtime->boundary;
2665 runtime->control->appl_ptr = appl_ptr;
2668 snd_pcm_stream_unlock_irq(substream);
2672 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2674 struct snd_pcm_runtime *runtime = substream->runtime;
2677 snd_pcm_stream_lock_irq(substream);
2678 switch (runtime->status->state) {
2679 case SNDRV_PCM_STATE_DRAINING:
2680 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2683 case SNDRV_PCM_STATE_RUNNING:
2684 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2687 case SNDRV_PCM_STATE_PREPARED:
2688 case SNDRV_PCM_STATE_SUSPENDED:
2691 case SNDRV_PCM_STATE_XRUN:
2699 snd_pcm_stream_unlock_irq(substream);
2703 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2704 snd_pcm_sframes_t __user *res)
2706 struct snd_pcm_runtime *runtime = substream->runtime;
2708 snd_pcm_sframes_t n = 0;
2710 snd_pcm_stream_lock_irq(substream);
2711 switch (runtime->status->state) {
2712 case SNDRV_PCM_STATE_DRAINING:
2713 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2716 case SNDRV_PCM_STATE_RUNNING:
2717 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2720 case SNDRV_PCM_STATE_PREPARED:
2721 case SNDRV_PCM_STATE_SUSPENDED:
2723 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2724 n = snd_pcm_playback_hw_avail(runtime);
2726 n = snd_pcm_capture_avail(runtime);
2727 n += runtime->delay;
2729 case SNDRV_PCM_STATE_XRUN:
2737 snd_pcm_stream_unlock_irq(substream);
2739 if (put_user(n, res))
2744 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2745 struct snd_pcm_sync_ptr __user *_sync_ptr)
2747 struct snd_pcm_runtime *runtime = substream->runtime;
2748 struct snd_pcm_sync_ptr sync_ptr;
2749 volatile struct snd_pcm_mmap_status *status;
2750 volatile struct snd_pcm_mmap_control *control;
2752 snd_pcm_uframes_t hw_avail;
2754 memset(&sync_ptr, 0, sizeof(sync_ptr));
2755 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2757 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2759 status = runtime->status;
2760 control = runtime->control;
2761 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2762 err = snd_pcm_hwsync(substream);
2766 snd_pcm_stream_lock_irq(substream);
2767 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2768 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2770 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2771 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2772 control->avail_min = sync_ptr.c.control.avail_min;
2774 sync_ptr.c.control.avail_min = control->avail_min;
2776 if (runtime->render_flag & SNDRV_NON_DMA_MODE) {
2777 hw_avail = snd_pcm_playback_hw_avail(runtime);
2778 if ((hw_avail >= runtime->start_threshold)
2779 && (runtime->render_flag &
2780 SNDRV_RENDER_STOPPED)) {
2781 if (substream->ops->restart)
2782 substream->ops->restart(substream);
2785 sync_ptr.s.status.state = status->state;
2786 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2787 sync_ptr.s.status.tstamp = status->tstamp;
2788 sync_ptr.s.status.suspended_state = status->suspended_state;
2789 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2790 snd_pcm_stream_unlock_irq(substream);
2791 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2796 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2798 struct snd_pcm_runtime *runtime = substream->runtime;
2801 if (get_user(arg, _arg))
2803 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2805 runtime->tstamp_type = arg;
2809 static int snd_pcm_common_ioctl1(struct file *file,
2810 struct snd_pcm_substream *substream,
2811 unsigned int cmd, void __user *arg)
2814 case SNDRV_PCM_IOCTL_PVERSION:
2815 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2816 case SNDRV_PCM_IOCTL_INFO:
2817 return snd_pcm_info_user(substream, arg);
2818 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2820 case SNDRV_PCM_IOCTL_TTSTAMP:
2821 return snd_pcm_tstamp(substream, arg);
2822 case SNDRV_PCM_IOCTL_HW_REFINE:
2823 return snd_pcm_hw_refine_user(substream, arg);
2824 case SNDRV_PCM_IOCTL_HW_PARAMS:
2825 return snd_pcm_hw_params_user(substream, arg);
2826 case SNDRV_PCM_IOCTL_HW_FREE:
2827 return snd_pcm_hw_free(substream);
2828 case SNDRV_PCM_IOCTL_SW_PARAMS:
2829 return snd_pcm_sw_params_user(substream, arg);
2830 case SNDRV_PCM_IOCTL_STATUS:
2831 return snd_pcm_status_user(substream, arg, false);
2832 case SNDRV_PCM_IOCTL_STATUS_EXT:
2833 return snd_pcm_status_user(substream, arg, true);
2834 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2835 return snd_pcm_channel_info_user(substream, arg);
2836 case SNDRV_PCM_IOCTL_PREPARE:
2837 return snd_pcm_prepare(substream, file);
2838 case SNDRV_PCM_IOCTL_RESET:
2839 return snd_pcm_reset(substream);
2840 case SNDRV_PCM_IOCTL_START:
2841 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2842 case SNDRV_PCM_IOCTL_LINK:
2843 return snd_pcm_link(substream, (int)(unsigned long) arg);
2844 case SNDRV_PCM_IOCTL_UNLINK:
2845 return snd_pcm_unlink(substream);
2846 case SNDRV_PCM_IOCTL_RESUME:
2847 return snd_pcm_resume(substream);
2848 case SNDRV_PCM_IOCTL_XRUN:
2849 return snd_pcm_xrun(substream);
2850 case SNDRV_PCM_IOCTL_HWSYNC:
2851 return snd_pcm_hwsync(substream);
2852 case SNDRV_PCM_IOCTL_DELAY:
2853 return snd_pcm_delay(substream, arg);
2854 case SNDRV_PCM_IOCTL_SYNC_PTR:
2855 return snd_pcm_sync_ptr(substream, arg);
2856 #ifdef CONFIG_SND_SUPPORT_OLD_API
2857 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2858 return snd_pcm_hw_refine_old_user(substream, arg);
2859 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2860 return snd_pcm_hw_params_old_user(substream, arg);
2862 case SNDRV_PCM_IOCTL_DRAIN:
2863 return snd_pcm_drain(substream, file);
2864 case SNDRV_PCM_IOCTL_DROP:
2865 return snd_pcm_drop(substream);
2866 case SNDRV_PCM_IOCTL_PAUSE:
2869 snd_pcm_stream_lock_irq(substream);
2870 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2871 snd_pcm_stream_unlock_irq(substream);
2874 case SNDRV_COMPRESS_GET_CAPS:
2875 case SNDRV_COMPRESS_GET_CODEC_CAPS:
2876 case SNDRV_COMPRESS_SET_PARAMS:
2877 case SNDRV_COMPRESS_GET_PARAMS:
2878 case SNDRV_COMPRESS_TSTAMP:
2879 case SNDRV_COMPRESS_DRAIN:
2880 return snd_compressed_ioctl(substream, cmd, arg);
2882 if (((cmd >> 8) & 0xff) == 'U')
2883 return snd_user_ioctl(substream, cmd, arg);
2885 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2889 static int snd_pcm_playback_ioctl1(struct file *file,
2890 struct snd_pcm_substream *substream,
2891 unsigned int cmd, void __user *arg)
2893 if (snd_BUG_ON(!substream))
2895 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2898 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2900 struct snd_xferi xferi;
2901 struct snd_xferi __user *_xferi = arg;
2902 struct snd_pcm_runtime *runtime = substream->runtime;
2903 snd_pcm_sframes_t result;
2904 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2906 if (put_user(0, &_xferi->result))
2908 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2910 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2911 __put_user(result, &_xferi->result);
2912 return result < 0 ? result : 0;
2914 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2916 struct snd_xfern xfern;
2917 struct snd_xfern __user *_xfern = arg;
2918 struct snd_pcm_runtime *runtime = substream->runtime;
2920 snd_pcm_sframes_t result;
2921 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2923 if (runtime->channels > 128)
2925 if (put_user(0, &_xfern->result))
2927 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2930 bufs = memdup_user(xfern.bufs,
2931 sizeof(void *) * runtime->channels);
2933 return PTR_ERR(bufs);
2934 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2936 __put_user(result, &_xfern->result);
2937 return result < 0 ? result : 0;
2939 case SNDRV_PCM_IOCTL_REWIND:
2941 snd_pcm_uframes_t frames;
2942 snd_pcm_uframes_t __user *_frames = arg;
2943 snd_pcm_sframes_t result;
2944 if (get_user(frames, _frames))
2946 if (put_user(0, _frames))
2948 result = snd_pcm_playback_rewind(substream, frames);
2949 __put_user(result, _frames);
2950 return result < 0 ? result : 0;
2952 case SNDRV_PCM_IOCTL_FORWARD:
2954 snd_pcm_uframes_t frames;
2955 snd_pcm_uframes_t __user *_frames = arg;
2956 snd_pcm_sframes_t result;
2957 if (get_user(frames, _frames))
2959 if (put_user(0, _frames))
2961 result = snd_pcm_playback_forward(substream, frames);
2962 __put_user(result, _frames);
2963 return result < 0 ? result : 0;
2966 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2969 static int snd_pcm_capture_ioctl1(struct file *file,
2970 struct snd_pcm_substream *substream,
2971 unsigned int cmd, void __user *arg)
2973 if (snd_BUG_ON(!substream))
2975 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2978 case SNDRV_PCM_IOCTL_READI_FRAMES:
2980 struct snd_xferi xferi;
2981 struct snd_xferi __user *_xferi = arg;
2982 struct snd_pcm_runtime *runtime = substream->runtime;
2983 snd_pcm_sframes_t result;
2984 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2986 if (put_user(0, &_xferi->result))
2988 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2990 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2991 __put_user(result, &_xferi->result);
2992 return result < 0 ? result : 0;
2994 case SNDRV_PCM_IOCTL_READN_FRAMES:
2996 struct snd_xfern xfern;
2997 struct snd_xfern __user *_xfern = arg;
2998 struct snd_pcm_runtime *runtime = substream->runtime;
3000 snd_pcm_sframes_t result;
3001 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3003 if (runtime->channels > 128)
3005 if (put_user(0, &_xfern->result))
3007 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3010 bufs = memdup_user(xfern.bufs,
3011 sizeof(void *) * runtime->channels);
3013 return PTR_ERR(bufs);
3014 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3016 __put_user(result, &_xfern->result);
3017 return result < 0 ? result : 0;
3019 case SNDRV_PCM_IOCTL_REWIND:
3021 snd_pcm_uframes_t frames;
3022 snd_pcm_uframes_t __user *_frames = arg;
3023 snd_pcm_sframes_t result;
3024 if (get_user(frames, _frames))
3026 if (put_user(0, _frames))
3028 result = snd_pcm_capture_rewind(substream, frames);
3029 __put_user(result, _frames);
3030 return result < 0 ? result : 0;
3032 case SNDRV_PCM_IOCTL_FORWARD:
3034 snd_pcm_uframes_t frames;
3035 snd_pcm_uframes_t __user *_frames = arg;
3036 snd_pcm_sframes_t result;
3037 if (get_user(frames, _frames))
3039 if (put_user(0, _frames))
3041 result = snd_pcm_capture_forward(substream, frames);
3042 __put_user(result, _frames);
3043 return result < 0 ? result : 0;
3046 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
3049 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
3052 struct snd_pcm_file *pcm_file;
3053 unsigned char ioctl_magic;
3055 pcm_file = file->private_data;
3056 ioctl_magic = ((cmd >> 8) & 0xff);
3058 if (ioctl_magic != 'A' && ioctl_magic != 'C' && ioctl_magic != 'U')
3061 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3062 (void __user *)arg);
3065 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3068 struct snd_pcm_file *pcm_file;
3069 unsigned char ioctl_magic;
3071 pcm_file = file->private_data;
3072 ioctl_magic = ((cmd >> 8) & 0xff);
3074 if (ioctl_magic != 'A' && ioctl_magic != 'U')
3077 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3078 (void __user *)arg);
3081 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3082 unsigned int cmd, void *arg)
3087 fs = snd_enter_user();
3088 switch (substream->stream) {
3089 case SNDRV_PCM_STREAM_PLAYBACK:
3090 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3091 (void __user *)arg);
3093 case SNDRV_PCM_STREAM_CAPTURE:
3094 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3095 (void __user *)arg);
3105 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3107 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3110 struct snd_pcm_file *pcm_file;
3111 struct snd_pcm_substream *substream;
3112 struct snd_pcm_runtime *runtime;
3113 snd_pcm_sframes_t result;
3115 pcm_file = file->private_data;
3116 substream = pcm_file->substream;
3117 if (PCM_RUNTIME_CHECK(substream))
3119 runtime = substream->runtime;
3120 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3122 if (!frame_aligned(runtime, count))
3124 count = bytes_to_frames(runtime, count);
3125 result = snd_pcm_lib_read(substream, buf, count);
3127 result = frames_to_bytes(runtime, result);
3131 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3132 size_t count, loff_t * offset)
3134 struct snd_pcm_file *pcm_file;
3135 struct snd_pcm_substream *substream;
3136 struct snd_pcm_runtime *runtime;
3137 snd_pcm_sframes_t result;
3139 pcm_file = file->private_data;
3140 substream = pcm_file->substream;
3141 if (PCM_RUNTIME_CHECK(substream))
3143 runtime = substream->runtime;
3144 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3146 if (!frame_aligned(runtime, count))
3148 count = bytes_to_frames(runtime, count);
3149 result = snd_pcm_lib_write(substream, buf, count);
3151 result = frames_to_bytes(runtime, result);
3155 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3157 struct snd_pcm_file *pcm_file;
3158 struct snd_pcm_substream *substream;
3159 struct snd_pcm_runtime *runtime;
3160 snd_pcm_sframes_t result;
3163 snd_pcm_uframes_t frames;
3165 pcm_file = iocb->ki_filp->private_data;
3166 substream = pcm_file->substream;
3167 if (PCM_RUNTIME_CHECK(substream))
3169 runtime = substream->runtime;
3170 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3172 if (!iter_is_iovec(to))
3174 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3176 if (!frame_aligned(runtime, to->iov->iov_len))
3178 frames = bytes_to_samples(runtime, to->iov->iov_len);
3179 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
3182 for (i = 0; i < to->nr_segs; ++i)
3183 bufs[i] = to->iov[i].iov_base;
3184 result = snd_pcm_lib_readv(substream, bufs, frames);
3186 result = frames_to_bytes(runtime, result);
3191 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3193 struct snd_pcm_file *pcm_file;
3194 struct snd_pcm_substream *substream;
3195 struct snd_pcm_runtime *runtime;
3196 snd_pcm_sframes_t result;
3199 snd_pcm_uframes_t frames;
3201 pcm_file = iocb->ki_filp->private_data;
3202 substream = pcm_file->substream;
3203 if (PCM_RUNTIME_CHECK(substream))
3205 runtime = substream->runtime;
3206 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3208 if (!iter_is_iovec(from))
3210 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3211 !frame_aligned(runtime, from->iov->iov_len))
3213 frames = bytes_to_samples(runtime, from->iov->iov_len);
3214 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
3217 for (i = 0; i < from->nr_segs; ++i)
3218 bufs[i] = from->iov[i].iov_base;
3219 result = snd_pcm_lib_writev(substream, bufs, frames);
3221 result = frames_to_bytes(runtime, result);
3226 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3228 struct snd_pcm_file *pcm_file;
3229 struct snd_pcm_substream *substream;
3230 struct snd_pcm_runtime *runtime;
3232 snd_pcm_uframes_t avail;
3234 pcm_file = file->private_data;
3236 substream = pcm_file->substream;
3237 if (PCM_RUNTIME_CHECK(substream))
3239 runtime = substream->runtime;
3241 poll_wait(file, &runtime->sleep, wait);
3243 snd_pcm_stream_lock_irq(substream);
3244 avail = snd_pcm_playback_avail(runtime);
3245 switch (runtime->status->state) {
3246 case SNDRV_PCM_STATE_RUNNING:
3247 case SNDRV_PCM_STATE_PREPARED:
3248 case SNDRV_PCM_STATE_PAUSED:
3249 if (avail >= runtime->control->avail_min) {
3250 mask = POLLOUT | POLLWRNORM;
3254 case SNDRV_PCM_STATE_DRAINING:
3258 mask = POLLOUT | POLLWRNORM | POLLERR;
3261 snd_pcm_stream_unlock_irq(substream);
3265 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3267 struct snd_pcm_file *pcm_file;
3268 struct snd_pcm_substream *substream;
3269 struct snd_pcm_runtime *runtime;
3271 snd_pcm_uframes_t avail;
3273 pcm_file = file->private_data;
3275 substream = pcm_file->substream;
3276 if (PCM_RUNTIME_CHECK(substream))
3278 runtime = substream->runtime;
3280 poll_wait(file, &runtime->sleep, wait);
3282 snd_pcm_stream_lock_irq(substream);
3283 avail = snd_pcm_capture_avail(runtime);
3284 switch (runtime->status->state) {
3285 case SNDRV_PCM_STATE_RUNNING:
3286 case SNDRV_PCM_STATE_PREPARED:
3287 case SNDRV_PCM_STATE_PAUSED:
3288 if (avail >= runtime->control->avail_min) {
3289 mask = POLLIN | POLLRDNORM;
3294 case SNDRV_PCM_STATE_DRAINING:
3296 mask = POLLIN | POLLRDNORM;
3301 mask = POLLIN | POLLRDNORM | POLLERR;
3304 snd_pcm_stream_unlock_irq(substream);
3313 * Only on coherent architectures, we can mmap the status and the control records
3314 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3316 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3318 * mmap status record
3320 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3321 struct vm_fault *vmf)
3323 struct snd_pcm_substream *substream = area->vm_private_data;
3324 struct snd_pcm_runtime *runtime;
3326 if (substream == NULL)
3327 return VM_FAULT_SIGBUS;
3328 runtime = substream->runtime;
3329 vmf->page = virt_to_page(runtime->status);
3330 get_page(vmf->page);
3334 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3336 .fault = snd_pcm_mmap_status_fault,
3339 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3340 struct vm_area_struct *area)
3343 if (!(area->vm_flags & VM_READ))
3345 size = area->vm_end - area->vm_start;
3346 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3348 area->vm_ops = &snd_pcm_vm_ops_status;
3349 area->vm_private_data = substream;
3350 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3355 * mmap control record
3357 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3358 struct vm_fault *vmf)
3360 struct snd_pcm_substream *substream = area->vm_private_data;
3361 struct snd_pcm_runtime *runtime;
3363 if (substream == NULL)
3364 return VM_FAULT_SIGBUS;
3365 runtime = substream->runtime;
3366 vmf->page = virt_to_page(runtime->control);
3367 get_page(vmf->page);
3371 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3373 .fault = snd_pcm_mmap_control_fault,
3376 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3377 struct vm_area_struct *area)
3380 if (!(area->vm_flags & VM_READ))
3382 size = area->vm_end - area->vm_start;
3383 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3385 area->vm_ops = &snd_pcm_vm_ops_control;
3386 area->vm_private_data = substream;
3387 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3390 #else /* ! coherent mmap */
3392 * don't support mmap for status and control records.
3394 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3395 struct vm_area_struct *area)
3399 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3400 struct vm_area_struct *area)
3404 #endif /* coherent mmap */
3406 static inline struct page *
3407 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3409 void *vaddr = substream->runtime->dma_area + ofs;
3410 return virt_to_page(vaddr);
3414 * fault callback for mmapping a RAM page
3416 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3417 struct vm_fault *vmf)
3419 struct snd_pcm_substream *substream = area->vm_private_data;
3420 struct snd_pcm_runtime *runtime;
3421 unsigned long offset;
3425 if (substream == NULL)
3426 return VM_FAULT_SIGBUS;
3427 runtime = substream->runtime;
3428 offset = vmf->pgoff << PAGE_SHIFT;
3429 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3430 if (offset > dma_bytes - PAGE_SIZE)
3431 return VM_FAULT_SIGBUS;
3432 if (substream->ops->page)
3433 page = substream->ops->page(substream, offset);
3435 page = snd_pcm_default_page_ops(substream, offset);
3437 return VM_FAULT_SIGBUS;
3443 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3444 .open = snd_pcm_mmap_data_open,
3445 .close = snd_pcm_mmap_data_close,
3448 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3449 .open = snd_pcm_mmap_data_open,
3450 .close = snd_pcm_mmap_data_close,
3451 .fault = snd_pcm_mmap_data_fault,
3455 * mmap the DMA buffer on RAM
3459 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3460 * @substream: PCM substream
3463 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3464 * this function is invoked implicitly.
3466 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3467 struct vm_area_struct *area)
3469 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3470 #ifdef CONFIG_GENERIC_ALLOCATOR
3471 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3472 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3473 return remap_pfn_range(area, area->vm_start,
3474 substream->dma_buffer.addr >> PAGE_SHIFT,
3475 area->vm_end - area->vm_start, area->vm_page_prot);
3477 #endif /* CONFIG_GENERIC_ALLOCATOR */
3478 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3479 if (!substream->ops->page &&
3480 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3481 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3483 substream->runtime->dma_area,
3484 substream->runtime->dma_addr,
3485 substream->runtime->dma_bytes);
3486 #endif /* CONFIG_X86 */
3487 /* mmap with fault handler */
3488 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3491 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3494 * mmap the DMA buffer on I/O memory area
3496 #if SNDRV_PCM_INFO_MMAP_IOMEM
3498 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3499 * @substream: PCM substream
3502 * When your hardware uses the iomapped pages as the hardware buffer and
3503 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3504 * is supposed to work only on limited architectures.
3506 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3507 struct vm_area_struct *area)
3509 struct snd_pcm_runtime *runtime = substream->runtime;;
3511 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3512 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3515 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3516 #endif /* SNDRV_PCM_INFO_MMAP */
3521 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3522 struct vm_area_struct *area)
3524 struct snd_pcm_runtime *runtime;
3526 unsigned long offset;
3530 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3531 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3534 if (!(area->vm_flags & VM_READ))
3537 runtime = substream->runtime;
3538 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3540 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3542 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3543 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3545 size = area->vm_end - area->vm_start;
3546 offset = area->vm_pgoff << PAGE_SHIFT;
3547 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3548 if ((size_t)size > dma_bytes)
3550 if (offset > dma_bytes - size)
3553 area->vm_ops = &snd_pcm_vm_ops_data;
3554 area->vm_private_data = substream;
3555 if (substream->ops->mmap)
3556 err = substream->ops->mmap(substream, area);
3558 err = snd_pcm_lib_default_mmap(substream, area);
3560 atomic_inc(&substream->mmap_count);
3564 EXPORT_SYMBOL(snd_pcm_mmap_data);
3566 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3568 struct snd_pcm_file * pcm_file;
3569 struct snd_pcm_substream *substream;
3570 unsigned long offset;
3572 pcm_file = file->private_data;
3573 substream = pcm_file->substream;
3574 if (PCM_RUNTIME_CHECK(substream))
3577 offset = area->vm_pgoff << PAGE_SHIFT;
3579 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3580 if (pcm_file->no_compat_mmap)
3582 return snd_pcm_mmap_status(substream, file, area);
3583 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3584 if (pcm_file->no_compat_mmap)
3586 return snd_pcm_mmap_control(substream, file, area);
3588 return snd_pcm_mmap_data(substream, file, area);
3593 static int snd_pcm_fasync(int fd, struct file * file, int on)
3595 struct snd_pcm_file * pcm_file;
3596 struct snd_pcm_substream *substream;
3597 struct snd_pcm_runtime *runtime;
3599 pcm_file = file->private_data;
3600 substream = pcm_file->substream;
3601 if (PCM_RUNTIME_CHECK(substream))
3603 runtime = substream->runtime;
3604 return fasync_helper(fd, file, on, &runtime->fasync);
3610 #ifdef CONFIG_COMPAT
3611 #include "pcm_compat.c"
3613 #define snd_pcm_ioctl_compat NULL
3617 * To be removed helpers to keep binary compatibility
3620 #ifdef CONFIG_SND_SUPPORT_OLD_API
3621 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3622 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3624 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3625 struct snd_pcm_hw_params_old *oparams)
3629 memset(params, 0, sizeof(*params));
3630 params->flags = oparams->flags;
3631 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3632 params->masks[i].bits[0] = oparams->masks[i];
3633 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3634 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3635 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3636 params->info = oparams->info;
3637 params->msbits = oparams->msbits;
3638 params->rate_num = oparams->rate_num;
3639 params->rate_den = oparams->rate_den;
3640 params->fifo_size = oparams->fifo_size;
3643 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3644 struct snd_pcm_hw_params *params)
3648 memset(oparams, 0, sizeof(*oparams));
3649 oparams->flags = params->flags;
3650 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3651 oparams->masks[i] = params->masks[i].bits[0];
3652 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3653 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3654 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3655 oparams->info = params->info;
3656 oparams->msbits = params->msbits;
3657 oparams->rate_num = params->rate_num;
3658 oparams->rate_den = params->rate_den;
3659 oparams->fifo_size = params->fifo_size;
3662 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3663 struct snd_pcm_hw_params_old __user * _oparams)
3665 struct snd_pcm_hw_params *params;
3666 struct snd_pcm_hw_params_old *oparams = NULL;
3669 params = kmalloc(sizeof(*params), GFP_KERNEL);
3673 oparams = memdup_user(_oparams, sizeof(*oparams));
3674 if (IS_ERR(oparams)) {
3675 err = PTR_ERR(oparams);
3678 snd_pcm_hw_convert_from_old_params(params, oparams);
3679 err = snd_pcm_hw_refine(substream, params);
3680 snd_pcm_hw_convert_to_old_params(oparams, params);
3681 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3692 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3693 struct snd_pcm_hw_params_old __user * _oparams)
3695 struct snd_pcm_hw_params *params;
3696 struct snd_pcm_hw_params_old *oparams = NULL;
3699 params = kmalloc(sizeof(*params), GFP_KERNEL);
3703 oparams = memdup_user(_oparams, sizeof(*oparams));
3704 if (IS_ERR(oparams)) {
3705 err = PTR_ERR(oparams);
3708 snd_pcm_hw_convert_from_old_params(params, oparams);
3709 err = snd_pcm_hw_params(substream, params);
3710 snd_pcm_hw_convert_to_old_params(oparams, params);
3711 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3721 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3724 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3727 unsigned long pgoff,
3728 unsigned long flags)
3730 struct snd_pcm_file *pcm_file = file->private_data;
3731 struct snd_pcm_substream *substream = pcm_file->substream;
3732 struct snd_pcm_runtime *runtime = substream->runtime;
3733 unsigned long offset = pgoff << PAGE_SHIFT;
3736 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3737 return (unsigned long)runtime->status;
3738 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3739 return (unsigned long)runtime->control;
3741 return (unsigned long)runtime->dma_area + offset;
3745 # define snd_pcm_get_unmapped_area NULL
3752 const struct file_operations snd_pcm_f_ops[2] = {
3754 .owner = THIS_MODULE,
3755 .write = snd_pcm_write,
3756 .write_iter = snd_pcm_writev,
3757 .open = snd_pcm_playback_open,
3758 .release = snd_pcm_release,
3759 .llseek = no_llseek,
3760 .poll = snd_pcm_playback_poll,
3761 .unlocked_ioctl = snd_pcm_playback_ioctl,
3762 .compat_ioctl = snd_pcm_ioctl_compat,
3763 .mmap = snd_pcm_mmap,
3764 .fasync = snd_pcm_fasync,
3765 .get_unmapped_area = snd_pcm_get_unmapped_area,
3768 .owner = THIS_MODULE,
3769 .read = snd_pcm_read,
3770 .read_iter = snd_pcm_readv,
3771 .open = snd_pcm_capture_open,
3772 .release = snd_pcm_release,
3773 .llseek = no_llseek,
3774 .poll = snd_pcm_capture_poll,
3775 .unlocked_ioctl = snd_pcm_capture_ioctl,
3776 .compat_ioctl = snd_pcm_ioctl_compat,
3777 .mmap = snd_pcm_mmap,
3778 .fasync = snd_pcm_fasync,
3779 .get_unmapped_area = snd_pcm_get_unmapped_area,