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/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
37 #include <linux/uio.h>
38 #include <linux/delay.h>
44 struct snd_pcm_hw_params_old
{
46 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
47 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
48 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
49 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
54 unsigned int rate_num
;
55 unsigned int rate_den
;
56 snd_pcm_uframes_t fifo_size
;
57 unsigned char reserved
[64];
60 #ifdef CONFIG_SND_SUPPORT_OLD_API
61 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
64 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
65 struct snd_pcm_hw_params_old __user
* _oparams
);
66 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
67 struct snd_pcm_hw_params_old __user
* _oparams
);
69 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
75 static DEFINE_RWLOCK(snd_pcm_link_rwlock
);
76 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
78 /* Writer in rwsem may block readers even during its waiting in queue,
79 * and this may lead to a deadlock when the code path takes read sem
80 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
81 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
82 * sleep until all the readers are completed without blocking by writer.
84 static inline void down_write_nonfifo(struct rw_semaphore
*lock
)
86 while (!down_write_trylock(lock
))
91 * snd_pcm_stream_lock - Lock the PCM stream
92 * @substream: PCM substream
94 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
95 * flag of the given substream. This also takes the global link rw lock
96 * (or rw sem), too, for avoiding the race with linked streams.
98 void snd_pcm_stream_lock(struct snd_pcm_substream
*substream
)
100 if (substream
->pcm
->nonatomic
) {
101 down_read_nested(&snd_pcm_link_rwsem
, SINGLE_DEPTH_NESTING
);
102 mutex_lock(&substream
->self_group
.mutex
);
104 read_lock(&snd_pcm_link_rwlock
);
105 spin_lock(&substream
->self_group
.lock
);
108 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock
);
111 * snd_pcm_stream_lock - Unlock the PCM stream
112 * @substream: PCM substream
114 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
116 void snd_pcm_stream_unlock(struct snd_pcm_substream
*substream
)
118 if (substream
->pcm
->nonatomic
) {
119 mutex_unlock(&substream
->self_group
.mutex
);
120 up_read(&snd_pcm_link_rwsem
);
122 spin_unlock(&substream
->self_group
.lock
);
123 read_unlock(&snd_pcm_link_rwlock
);
126 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock
);
129 * snd_pcm_stream_lock_irq - Lock the PCM stream
130 * @substream: PCM substream
132 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
133 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
134 * as snd_pcm_stream_lock().
136 void snd_pcm_stream_lock_irq(struct snd_pcm_substream
*substream
)
138 if (!substream
->pcm
->nonatomic
)
140 snd_pcm_stream_lock(substream
);
142 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq
);
145 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
146 * @substream: PCM substream
148 * This is a counter-part of snd_pcm_stream_lock_irq().
150 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream
*substream
)
152 snd_pcm_stream_unlock(substream
);
153 if (!substream
->pcm
->nonatomic
)
156 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq
);
158 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream
*substream
)
160 unsigned long flags
= 0;
161 if (!substream
->pcm
->nonatomic
)
162 local_irq_save(flags
);
163 snd_pcm_stream_lock(substream
);
166 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave
);
169 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
170 * @substream: PCM substream
173 * This is a counter-part of snd_pcm_stream_lock_irqsave().
175 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream
*substream
,
178 snd_pcm_stream_unlock(substream
);
179 if (!substream
->pcm
->nonatomic
)
180 local_irq_restore(flags
);
182 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore
);
184 static inline mm_segment_t
snd_enter_user(void)
186 mm_segment_t fs
= get_fs();
191 static inline void snd_leave_user(mm_segment_t fs
)
198 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
200 struct snd_pcm_runtime
*runtime
;
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
));
216 runtime
= substream
->runtime
;
221 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
222 struct snd_pcm_info __user
* _info
)
224 struct snd_pcm_info
*info
;
227 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
230 err
= snd_pcm_info(substream
, info
);
232 if (copy_to_user(_info
, info
, sizeof(*info
)))
239 static bool hw_support_mmap(struct snd_pcm_substream
*substream
)
241 if (!(substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_MMAP
))
243 /* check architectures that return -EINVAL from dma_mmap_coherent() */
244 /* FIXME: this should be some global flag */
245 #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
246 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
247 if (!substream
->ops
->mmap
&&
248 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
257 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
258 static const char * const snd_pcm_hw_param_names
[] = {
262 HW_PARAM(SAMPLE_BITS
),
263 HW_PARAM(FRAME_BITS
),
266 HW_PARAM(PERIOD_TIME
),
267 HW_PARAM(PERIOD_SIZE
),
268 HW_PARAM(PERIOD_BYTES
),
270 HW_PARAM(BUFFER_TIME
),
271 HW_PARAM(BUFFER_SIZE
),
272 HW_PARAM(BUFFER_BYTES
),
277 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
278 struct snd_pcm_hw_params
*params
)
281 struct snd_pcm_hardware
*hw
;
282 struct snd_interval
*i
= NULL
;
283 struct snd_mask
*m
= NULL
;
284 struct snd_pcm_hw_constraints
*constrs
= &substream
->runtime
->hw_constraints
;
285 unsigned int rstamps
[constrs
->rules_num
];
286 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
287 unsigned int stamp
= 2;
291 params
->fifo_size
= 0;
292 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
294 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
295 params
->rate_num
= 0;
296 params
->rate_den
= 0;
299 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
300 m
= hw_param_mask(params
, k
);
301 if (snd_mask_empty(m
))
303 if (!(params
->rmask
& (1 << k
)))
306 pr_debug("%s = ", snd_pcm_hw_param_names
[k
]);
307 pr_cont("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
309 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
311 pr_cont("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
314 params
->cmask
|= 1 << k
;
319 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
320 i
= hw_param_interval(params
, k
);
321 if (snd_interval_empty(i
))
323 if (!(params
->rmask
& (1 << k
)))
326 pr_debug("%s = ", snd_pcm_hw_param_names
[k
]);
331 i
->openmin
? '(' : '[', i
->min
,
332 i
->max
, i
->openmax
? ')' : ']');
335 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
340 pr_cont("%c%u %u%c\n",
341 i
->openmin
? '(' : '[', i
->min
,
342 i
->max
, i
->openmax
? ')' : ']');
345 params
->cmask
|= 1 << k
;
350 for (k
= 0; k
< constrs
->rules_num
; k
++)
352 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
353 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
356 for (k
= 0; k
< constrs
->rules_num
; k
++) {
357 struct snd_pcm_hw_rule
*r
= &constrs
->rules
[k
];
360 if (r
->cond
&& !(r
->cond
& params
->flags
))
362 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
363 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
371 pr_debug("Rule %d [%p]: ", k
, r
->func
);
373 pr_cont("%s = ", snd_pcm_hw_param_names
[r
->var
]);
374 if (hw_is_mask(r
->var
)) {
375 m
= hw_param_mask(params
, r
->var
);
376 pr_cont("%x", *m
->bits
);
378 i
= hw_param_interval(params
, r
->var
);
383 i
->openmin
? '(' : '[', i
->min
,
384 i
->max
, i
->openmax
? ')' : ']');
388 changed
= r
->func(params
, r
);
392 if (hw_is_mask(r
->var
))
393 pr_cont("%x", *m
->bits
);
399 i
->openmin
? '(' : '[', i
->min
,
400 i
->max
, i
->openmax
? ')' : ']');
406 if (changed
&& r
->var
>= 0) {
407 params
->cmask
|= (1 << r
->var
);
408 vstamps
[r
->var
] = stamp
;
416 if (!params
->msbits
) {
417 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
418 if (snd_interval_single(i
))
419 params
->msbits
= snd_interval_value(i
);
422 if (!params
->rate_den
) {
423 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
424 if (snd_interval_single(i
)) {
425 params
->rate_num
= snd_interval_value(i
);
426 params
->rate_den
= 1;
430 hw
= &substream
->runtime
->hw
;
432 params
->info
= hw
->info
& ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES
|
433 SNDRV_PCM_INFO_DRAIN_TRIGGER
);
434 if (!hw_support_mmap(substream
))
435 params
->info
&= ~(SNDRV_PCM_INFO_MMAP
|
436 SNDRV_PCM_INFO_MMAP_VALID
);
438 if (!params
->fifo_size
) {
439 m
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
440 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
441 if (snd_mask_min(m
) == snd_mask_max(m
) &&
442 snd_interval_min(i
) == snd_interval_max(i
)) {
443 changed
= substream
->ops
->ioctl(substream
,
444 SNDRV_PCM_IOCTL1_FIFO_SIZE
, params
);
453 EXPORT_SYMBOL(snd_pcm_hw_refine
);
455 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
456 struct snd_pcm_hw_params __user
* _params
)
458 struct snd_pcm_hw_params
*params
;
461 params
= memdup_user(_params
, sizeof(*params
));
463 return PTR_ERR(params
);
465 err
= snd_pcm_hw_refine(substream
, params
);
466 if (copy_to_user(_params
, params
, sizeof(*params
))) {
475 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
480 return -1; /* invalid */
482 /* take 75% of period time as the deadline */
483 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
484 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
490 static void snd_pcm_set_state(struct snd_pcm_substream
*substream
, int state
)
492 snd_pcm_stream_lock_irq(substream
);
493 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_DISCONNECTED
)
494 substream
->runtime
->status
->state
= state
;
495 snd_pcm_stream_unlock_irq(substream
);
498 static inline void snd_pcm_timer_notify(struct snd_pcm_substream
*substream
,
501 #ifdef CONFIG_SND_PCM_TIMER
502 if (substream
->timer
)
503 snd_timer_notify(substream
->timer
, event
,
504 &substream
->runtime
->trigger_tstamp
);
508 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
509 struct snd_pcm_hw_params
*params
)
511 struct snd_pcm_runtime
*runtime
;
514 snd_pcm_uframes_t frames
;
516 if (PCM_RUNTIME_CHECK(substream
))
518 runtime
= substream
->runtime
;
519 snd_pcm_stream_lock_irq(substream
);
520 switch (runtime
->status
->state
) {
521 case SNDRV_PCM_STATE_OPEN
:
522 case SNDRV_PCM_STATE_SETUP
:
523 case SNDRV_PCM_STATE_PREPARED
:
526 snd_pcm_stream_unlock_irq(substream
);
529 snd_pcm_stream_unlock_irq(substream
);
530 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
531 if (!substream
->oss
.oss
)
533 if (atomic_read(&substream
->mmap_count
))
537 err
= snd_pcm_hw_refine(substream
, params
);
541 err
= snd_pcm_hw_params_choose(substream
, params
);
545 if (substream
->ops
->hw_params
!= NULL
) {
546 err
= substream
->ops
->hw_params(substream
, params
);
551 runtime
->access
= params_access(params
);
552 runtime
->format
= params_format(params
);
553 runtime
->subformat
= params_subformat(params
);
554 runtime
->channels
= params_channels(params
);
555 runtime
->rate
= params_rate(params
);
556 runtime
->period_size
= params_period_size(params
);
557 runtime
->periods
= params_periods(params
);
558 runtime
->buffer_size
= params_buffer_size(params
);
559 runtime
->info
= params
->info
;
560 runtime
->rate_num
= params
->rate_num
;
561 runtime
->rate_den
= params
->rate_den
;
562 runtime
->no_period_wakeup
=
563 (params
->info
& SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
) &&
564 (params
->flags
& SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
);
566 bits
= snd_pcm_format_physical_width(runtime
->format
);
567 runtime
->sample_bits
= bits
;
568 bits
*= runtime
->channels
;
569 runtime
->frame_bits
= bits
;
571 while (bits
% 8 != 0) {
575 runtime
->byte_align
= bits
/ 8;
576 runtime
->min_align
= frames
;
578 /* Default sw params */
579 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
580 runtime
->period_step
= 1;
581 runtime
->control
->avail_min
= runtime
->period_size
;
582 runtime
->start_threshold
= 1;
583 runtime
->stop_threshold
= runtime
->buffer_size
;
584 runtime
->silence_threshold
= 0;
585 runtime
->silence_size
= 0;
586 runtime
->boundary
= runtime
->buffer_size
;
587 while (runtime
->boundary
* 2 <= 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 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
660 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
661 struct snd_pcm_sw_params
*params
)
663 struct snd_pcm_runtime
*runtime
;
666 if (PCM_RUNTIME_CHECK(substream
))
668 runtime
= substream
->runtime
;
669 snd_pcm_stream_lock_irq(substream
);
670 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
671 snd_pcm_stream_unlock_irq(substream
);
674 snd_pcm_stream_unlock_irq(substream
);
676 if (params
->tstamp_mode
< 0 ||
677 params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
679 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
680 params
->tstamp_type
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
682 if (params
->avail_min
== 0)
684 if (params
->silence_size
>= runtime
->boundary
) {
685 if (params
->silence_threshold
!= 0)
688 if (params
->silence_size
> params
->silence_threshold
)
690 if (params
->silence_threshold
> runtime
->buffer_size
)
694 snd_pcm_stream_lock_irq(substream
);
695 runtime
->tstamp_mode
= params
->tstamp_mode
;
696 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12))
697 runtime
->tstamp_type
= params
->tstamp_type
;
698 runtime
->period_step
= params
->period_step
;
699 runtime
->control
->avail_min
= params
->avail_min
;
700 runtime
->start_threshold
= params
->start_threshold
;
701 runtime
->stop_threshold
= params
->stop_threshold
;
702 runtime
->silence_threshold
= params
->silence_threshold
;
703 runtime
->silence_size
= params
->silence_size
;
704 params
->boundary
= runtime
->boundary
;
705 if (snd_pcm_running(substream
)) {
706 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
707 runtime
->silence_size
> 0)
708 snd_pcm_playback_silence(substream
, ULONG_MAX
);
709 err
= snd_pcm_update_state(substream
, runtime
);
711 snd_pcm_stream_unlock_irq(substream
);
715 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
716 struct snd_pcm_sw_params __user
* _params
)
718 struct snd_pcm_sw_params params
;
720 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
722 err
= snd_pcm_sw_params(substream
, ¶ms
);
723 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
728 int snd_pcm_status(struct snd_pcm_substream
*substream
,
729 struct snd_pcm_status
*status
)
731 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
733 snd_pcm_stream_lock_irq(substream
);
735 snd_pcm_unpack_audio_tstamp_config(status
->audio_tstamp_data
,
736 &runtime
->audio_tstamp_config
);
738 /* backwards compatible behavior */
739 if (runtime
->audio_tstamp_config
.type_requested
==
740 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT
) {
741 if (runtime
->hw
.info
& SNDRV_PCM_INFO_HAS_WALL_CLOCK
)
742 runtime
->audio_tstamp_config
.type_requested
=
743 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK
;
745 runtime
->audio_tstamp_config
.type_requested
=
746 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
;
747 runtime
->audio_tstamp_report
.valid
= 0;
749 runtime
->audio_tstamp_report
.valid
= 1;
751 status
->state
= runtime
->status
->state
;
752 status
->suspended_state
= runtime
->status
->suspended_state
;
753 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
755 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
756 if (snd_pcm_running(substream
)) {
757 snd_pcm_update_hw_ptr(substream
);
758 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
759 status
->tstamp
= runtime
->status
->tstamp
;
760 status
->driver_tstamp
= runtime
->driver_tstamp
;
761 status
->audio_tstamp
=
762 runtime
->status
->audio_tstamp
;
763 if (runtime
->audio_tstamp_report
.valid
== 1)
764 /* backwards compatibility, no report provided in COMPAT mode */
765 snd_pcm_pack_audio_tstamp_report(&status
->audio_tstamp_data
,
766 &status
->audio_tstamp_accuracy
,
767 &runtime
->audio_tstamp_report
);
772 /* get tstamp only in fallback mode and only if enabled */
773 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
774 snd_pcm_gettime(runtime
, &status
->tstamp
);
777 status
->appl_ptr
= runtime
->control
->appl_ptr
;
778 status
->hw_ptr
= runtime
->status
->hw_ptr
;
779 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
780 status
->avail
= snd_pcm_playback_avail(runtime
);
781 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
782 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
783 status
->delay
= runtime
->buffer_size
- status
->avail
;
784 status
->delay
+= runtime
->delay
;
788 status
->avail
= snd_pcm_capture_avail(runtime
);
789 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
790 status
->delay
= status
->avail
+ runtime
->delay
;
794 status
->avail_max
= runtime
->avail_max
;
795 status
->overrange
= runtime
->overrange
;
796 runtime
->avail_max
= 0;
797 runtime
->overrange
= 0;
799 snd_pcm_stream_unlock_irq(substream
);
803 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
804 struct snd_pcm_status __user
* _status
,
807 struct snd_pcm_status status
;
810 memset(&status
, 0, sizeof(status
));
812 * with extension, parameters are read/write,
813 * get audio_tstamp_data from user,
814 * ignore rest of status structure
816 if (ext
&& get_user(status
.audio_tstamp_data
,
817 (u32 __user
*)(&_status
->audio_tstamp_data
)))
819 res
= snd_pcm_status(substream
, &status
);
822 if (copy_to_user(_status
, &status
, sizeof(status
)))
827 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
828 struct snd_pcm_channel_info
* info
)
830 struct snd_pcm_runtime
*runtime
;
831 unsigned int channel
;
833 channel
= info
->channel
;
834 runtime
= substream
->runtime
;
835 snd_pcm_stream_lock_irq(substream
);
836 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
837 snd_pcm_stream_unlock_irq(substream
);
840 snd_pcm_stream_unlock_irq(substream
);
841 if (channel
>= runtime
->channels
)
843 memset(info
, 0, sizeof(*info
));
844 info
->channel
= channel
;
845 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
848 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
849 struct snd_pcm_channel_info __user
* _info
)
851 struct snd_pcm_channel_info info
;
854 if (copy_from_user(&info
, _info
, sizeof(info
)))
856 res
= snd_pcm_channel_info(substream
, &info
);
859 if (copy_to_user(_info
, &info
, sizeof(info
)))
864 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
866 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
867 if (runtime
->trigger_master
== NULL
)
869 if (runtime
->trigger_master
== substream
) {
870 if (!runtime
->trigger_tstamp_latched
)
871 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
873 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
874 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
876 runtime
->trigger_master
= NULL
;
880 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
881 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
882 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
883 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
887 * this functions is core for handling of linked stream
888 * Note: the stream state might be changed also on failure
889 * Note2: call with calling stream lock + link lock
891 static int snd_pcm_action_group(const struct action_ops
*ops
,
892 struct snd_pcm_substream
*substream
,
893 int state
, int do_lock
)
895 struct snd_pcm_substream
*s
= NULL
;
896 struct snd_pcm_substream
*s1
;
897 int res
= 0, depth
= 1;
899 snd_pcm_group_for_each_entry(s
, substream
) {
900 if (do_lock
&& s
!= substream
) {
901 if (s
->pcm
->nonatomic
)
902 mutex_lock_nested(&s
->self_group
.mutex
, depth
);
904 spin_lock_nested(&s
->self_group
.lock
, depth
);
907 res
= ops
->pre_action(s
, state
);
911 snd_pcm_group_for_each_entry(s
, substream
) {
912 res
= ops
->do_action(s
, state
);
914 if (ops
->undo_action
) {
915 snd_pcm_group_for_each_entry(s1
, substream
) {
916 if (s1
== s
) /* failed stream */
918 ops
->undo_action(s1
, state
);
921 s
= NULL
; /* unlock all */
925 snd_pcm_group_for_each_entry(s
, substream
) {
926 ops
->post_action(s
, state
);
931 snd_pcm_group_for_each_entry(s1
, substream
) {
932 if (s1
!= substream
) {
933 if (s1
->pcm
->nonatomic
)
934 mutex_unlock(&s1
->self_group
.mutex
);
936 spin_unlock(&s1
->self_group
.lock
);
938 if (s1
== s
) /* end */
946 * Note: call with stream lock
948 static int snd_pcm_action_single(const struct action_ops
*ops
,
949 struct snd_pcm_substream
*substream
,
954 res
= ops
->pre_action(substream
, state
);
957 res
= ops
->do_action(substream
, state
);
959 ops
->post_action(substream
, state
);
960 else if (ops
->undo_action
)
961 ops
->undo_action(substream
, state
);
966 * Note: call with stream lock
968 static int snd_pcm_action(const struct action_ops
*ops
,
969 struct snd_pcm_substream
*substream
,
974 if (!snd_pcm_stream_linked(substream
))
975 return snd_pcm_action_single(ops
, substream
, state
);
977 if (substream
->pcm
->nonatomic
) {
978 if (!mutex_trylock(&substream
->group
->mutex
)) {
979 mutex_unlock(&substream
->self_group
.mutex
);
980 mutex_lock(&substream
->group
->mutex
);
981 mutex_lock(&substream
->self_group
.mutex
);
983 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
984 mutex_unlock(&substream
->group
->mutex
);
986 if (!spin_trylock(&substream
->group
->lock
)) {
987 spin_unlock(&substream
->self_group
.lock
);
988 spin_lock(&substream
->group
->lock
);
989 spin_lock(&substream
->self_group
.lock
);
991 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
992 spin_unlock(&substream
->group
->lock
);
998 * Note: don't use any locks before
1000 static int snd_pcm_action_lock_irq(const struct action_ops
*ops
,
1001 struct snd_pcm_substream
*substream
,
1006 snd_pcm_stream_lock_irq(substream
);
1007 res
= snd_pcm_action(ops
, substream
, state
);
1008 snd_pcm_stream_unlock_irq(substream
);
1014 static int snd_pcm_action_nonatomic(const struct action_ops
*ops
,
1015 struct snd_pcm_substream
*substream
,
1020 down_read(&snd_pcm_link_rwsem
);
1021 if (snd_pcm_stream_linked(substream
))
1022 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
1024 res
= snd_pcm_action_single(ops
, substream
, state
);
1025 up_read(&snd_pcm_link_rwsem
);
1032 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
1034 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1035 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
1037 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1038 !snd_pcm_playback_data(substream
))
1040 runtime
->trigger_tstamp_latched
= false;
1041 runtime
->trigger_master
= substream
;
1045 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
1047 if (substream
->runtime
->trigger_master
!= substream
)
1049 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
1052 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
1054 if (substream
->runtime
->trigger_master
== substream
)
1055 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1058 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
1060 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1061 snd_pcm_trigger_tstamp(substream
);
1062 runtime
->hw_ptr_jiffies
= jiffies
;
1063 runtime
->hw_ptr_buffer_jiffies
= (runtime
->buffer_size
* HZ
) /
1065 runtime
->status
->state
= state
;
1066 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1067 runtime
->silence_size
> 0)
1068 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1069 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTART
);
1072 static const struct action_ops snd_pcm_action_start
= {
1073 .pre_action
= snd_pcm_pre_start
,
1074 .do_action
= snd_pcm_do_start
,
1075 .undo_action
= snd_pcm_undo_start
,
1076 .post_action
= snd_pcm_post_start
1080 * snd_pcm_start - start all linked streams
1081 * @substream: the PCM substream instance
1083 * Return: Zero if successful, or a negative error code.
1085 int snd_pcm_start(struct snd_pcm_substream
*substream
)
1087 return snd_pcm_action(&snd_pcm_action_start
, substream
,
1088 SNDRV_PCM_STATE_RUNNING
);
1094 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
1096 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1097 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1099 runtime
->trigger_master
= substream
;
1103 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
1105 if (substream
->runtime
->trigger_master
== substream
&&
1106 snd_pcm_running(substream
))
1107 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1108 return 0; /* unconditonally stop all substreams */
1111 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
1113 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1114 if (runtime
->status
->state
!= state
) {
1115 snd_pcm_trigger_tstamp(substream
);
1116 runtime
->status
->state
= state
;
1117 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTOP
);
1119 wake_up(&runtime
->sleep
);
1120 wake_up(&runtime
->tsleep
);
1123 static const struct action_ops snd_pcm_action_stop
= {
1124 .pre_action
= snd_pcm_pre_stop
,
1125 .do_action
= snd_pcm_do_stop
,
1126 .post_action
= snd_pcm_post_stop
1130 * snd_pcm_stop - try to stop all running streams in the substream group
1131 * @substream: the PCM substream instance
1132 * @state: PCM state after stopping the stream
1134 * The state of each stream is then changed to the given state unconditionally.
1136 * Return: Zero if successful, or a negative error code.
1138 int snd_pcm_stop(struct snd_pcm_substream
*substream
, snd_pcm_state_t state
)
1140 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
1143 EXPORT_SYMBOL(snd_pcm_stop
);
1146 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1147 * @substream: the PCM substream
1149 * After stopping, the state is changed to SETUP.
1150 * Unlike snd_pcm_stop(), this affects only the given stream.
1152 * Return: Zero if succesful, or a negative error code.
1154 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
1156 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
1157 SNDRV_PCM_STATE_SETUP
);
1161 * snd_pcm_stop_xrun - stop the running streams as XRUN
1162 * @substream: the PCM substream instance
1164 * This stops the given running substream (and all linked substreams) as XRUN.
1165 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1167 * Return: Zero if successful, or a negative error code.
1169 int snd_pcm_stop_xrun(struct snd_pcm_substream
*substream
)
1171 unsigned long flags
;
1174 snd_pcm_stream_lock_irqsave(substream
, flags
);
1175 if (snd_pcm_running(substream
))
1176 ret
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1177 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1180 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun
);
1185 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
1187 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1188 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
1191 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
1193 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
1195 runtime
->trigger_master
= substream
;
1199 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
1201 if (substream
->runtime
->trigger_master
!= substream
)
1203 /* some drivers might use hw_ptr to recover from the pause -
1204 update the hw_ptr now */
1206 snd_pcm_update_hw_ptr(substream
);
1207 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1208 * a delta between the current jiffies, this gives a large enough
1209 * delta, effectively to skip the check once.
1211 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
1212 return substream
->ops
->trigger(substream
,
1213 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1214 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1217 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1219 if (substream
->runtime
->trigger_master
== substream
)
1220 substream
->ops
->trigger(substream
,
1221 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1222 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1225 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1227 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1228 snd_pcm_trigger_tstamp(substream
);
1230 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1231 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MPAUSE
);
1232 wake_up(&runtime
->sleep
);
1233 wake_up(&runtime
->tsleep
);
1235 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1236 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MCONTINUE
);
1240 static const struct action_ops snd_pcm_action_pause
= {
1241 .pre_action
= snd_pcm_pre_pause
,
1242 .do_action
= snd_pcm_do_pause
,
1243 .undo_action
= snd_pcm_undo_pause
,
1244 .post_action
= snd_pcm_post_pause
1248 * Push/release the pause for all linked streams.
1250 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1252 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1258 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1260 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1261 switch (runtime
->status
->state
) {
1262 case SNDRV_PCM_STATE_SUSPENDED
:
1264 /* unresumable PCM state; return -EBUSY for skipping suspend */
1265 case SNDRV_PCM_STATE_OPEN
:
1266 case SNDRV_PCM_STATE_SETUP
:
1267 case SNDRV_PCM_STATE_DISCONNECTED
:
1270 runtime
->trigger_master
= substream
;
1274 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1276 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1277 if (runtime
->trigger_master
!= substream
)
1279 if (! snd_pcm_running(substream
))
1281 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1282 return 0; /* suspend unconditionally */
1285 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1287 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1288 snd_pcm_trigger_tstamp(substream
);
1289 runtime
->status
->suspended_state
= runtime
->status
->state
;
1290 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1291 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSUSPEND
);
1292 wake_up(&runtime
->sleep
);
1293 wake_up(&runtime
->tsleep
);
1296 static const struct action_ops snd_pcm_action_suspend
= {
1297 .pre_action
= snd_pcm_pre_suspend
,
1298 .do_action
= snd_pcm_do_suspend
,
1299 .post_action
= snd_pcm_post_suspend
1303 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1304 * @substream: the PCM substream
1306 * After this call, all streams are changed to SUSPENDED state.
1308 * Return: Zero if successful (or @substream is %NULL), or a negative error
1311 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1314 unsigned long flags
;
1319 snd_pcm_stream_lock_irqsave(substream
, flags
);
1320 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1321 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1325 EXPORT_SYMBOL(snd_pcm_suspend
);
1328 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1329 * @pcm: the PCM instance
1331 * After this call, all streams are changed to SUSPENDED state.
1333 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1335 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1337 struct snd_pcm_substream
*substream
;
1338 int stream
, err
= 0;
1343 for (stream
= 0; stream
< 2; stream
++) {
1344 for (substream
= pcm
->streams
[stream
].substream
;
1345 substream
; substream
= substream
->next
) {
1346 /* FIXME: the open/close code should lock this as well */
1347 if (substream
->runtime
== NULL
)
1351 * Skip BE dai link PCM's that are internal and may
1352 * not have their substream ops set.
1354 if (!substream
->ops
)
1357 err
= snd_pcm_suspend(substream
);
1358 if (err
< 0 && err
!= -EBUSY
)
1365 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1369 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1371 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1372 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1374 runtime
->trigger_master
= substream
;
1378 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1380 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1381 if (runtime
->trigger_master
!= substream
)
1383 /* DMA not running previously? */
1384 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1385 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1386 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1388 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1391 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1393 if (substream
->runtime
->trigger_master
== substream
&&
1394 snd_pcm_running(substream
))
1395 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1398 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1400 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1401 snd_pcm_trigger_tstamp(substream
);
1402 runtime
->status
->state
= runtime
->status
->suspended_state
;
1403 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MRESUME
);
1406 static const struct action_ops snd_pcm_action_resume
= {
1407 .pre_action
= snd_pcm_pre_resume
,
1408 .do_action
= snd_pcm_do_resume
,
1409 .undo_action
= snd_pcm_undo_resume
,
1410 .post_action
= snd_pcm_post_resume
1413 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1415 struct snd_card
*card
= substream
->pcm
->card
;
1418 snd_power_lock(card
);
1419 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1420 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1421 snd_power_unlock(card
);
1427 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1432 #endif /* CONFIG_PM */
1437 * Change the RUNNING stream(s) to XRUN state.
1439 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1441 struct snd_card
*card
= substream
->pcm
->card
;
1442 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1445 snd_power_lock(card
);
1446 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1447 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1452 snd_pcm_stream_lock_irq(substream
);
1453 switch (runtime
->status
->state
) {
1454 case SNDRV_PCM_STATE_XRUN
:
1455 result
= 0; /* already there */
1457 case SNDRV_PCM_STATE_RUNNING
:
1458 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1463 snd_pcm_stream_unlock_irq(substream
);
1465 snd_power_unlock(card
);
1472 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1474 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1475 switch (runtime
->status
->state
) {
1476 case SNDRV_PCM_STATE_RUNNING
:
1477 case SNDRV_PCM_STATE_PREPARED
:
1478 case SNDRV_PCM_STATE_PAUSED
:
1479 case SNDRV_PCM_STATE_SUSPENDED
:
1486 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1488 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1489 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1492 runtime
->hw_ptr_base
= 0;
1493 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1494 runtime
->status
->hw_ptr
% runtime
->period_size
;
1495 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1496 runtime
->silence_filled
= 0;
1500 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1502 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1503 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1504 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1505 runtime
->silence_size
> 0)
1506 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1509 static const struct action_ops snd_pcm_action_reset
= {
1510 .pre_action
= snd_pcm_pre_reset
,
1511 .do_action
= snd_pcm_do_reset
,
1512 .post_action
= snd_pcm_post_reset
1515 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1517 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1523 /* we use the second argument for updating f_flags */
1524 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1527 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1528 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1529 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1531 if (snd_pcm_running(substream
))
1533 substream
->f_flags
= f_flags
;
1537 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1540 err
= substream
->ops
->prepare(substream
);
1543 return snd_pcm_do_reset(substream
, 0);
1546 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1548 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1549 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1550 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_PREPARED
);
1553 static const struct action_ops snd_pcm_action_prepare
= {
1554 .pre_action
= snd_pcm_pre_prepare
,
1555 .do_action
= snd_pcm_do_prepare
,
1556 .post_action
= snd_pcm_post_prepare
1560 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1561 * @substream: the PCM substream instance
1562 * @file: file to refer f_flags
1564 * Return: Zero if successful, or a negative error code.
1566 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1570 struct snd_card
*card
= substream
->pcm
->card
;
1574 f_flags
= file
->f_flags
;
1576 f_flags
= substream
->f_flags
;
1578 snd_power_lock(card
);
1579 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1580 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1581 substream
, f_flags
);
1582 snd_power_unlock(card
);
1590 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1592 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1593 switch (runtime
->status
->state
) {
1594 case SNDRV_PCM_STATE_OPEN
:
1595 case SNDRV_PCM_STATE_DISCONNECTED
:
1596 case SNDRV_PCM_STATE_SUSPENDED
:
1599 runtime
->trigger_master
= substream
;
1603 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1605 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1606 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1607 switch (runtime
->status
->state
) {
1608 case SNDRV_PCM_STATE_PREPARED
:
1609 /* start playback stream if possible */
1610 if (! snd_pcm_playback_empty(substream
)) {
1611 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1612 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1614 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1617 case SNDRV_PCM_STATE_RUNNING
:
1618 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1620 case SNDRV_PCM_STATE_XRUN
:
1621 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1627 /* stop running stream */
1628 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1629 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1630 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1631 snd_pcm_do_stop(substream
, new_state
);
1632 snd_pcm_post_stop(substream
, new_state
);
1636 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
1637 runtime
->trigger_master
== substream
&&
1638 (runtime
->hw
.info
& SNDRV_PCM_INFO_DRAIN_TRIGGER
))
1639 return substream
->ops
->trigger(substream
,
1640 SNDRV_PCM_TRIGGER_DRAIN
);
1645 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1649 static const struct action_ops snd_pcm_action_drain_init
= {
1650 .pre_action
= snd_pcm_pre_drain_init
,
1651 .do_action
= snd_pcm_do_drain_init
,
1652 .post_action
= snd_pcm_post_drain_init
1655 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1658 * Drain the stream(s).
1659 * When the substream is linked, sync until the draining of all playback streams
1661 * After this call, all streams are supposed to be either SETUP or DRAINING
1662 * (capture only) state.
1664 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1667 struct snd_card
*card
;
1668 struct snd_pcm_runtime
*runtime
;
1669 struct snd_pcm_substream
*s
;
1674 card
= substream
->pcm
->card
;
1675 runtime
= substream
->runtime
;
1677 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1680 snd_power_lock(card
);
1681 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1682 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1684 snd_power_unlock(card
);
1690 if (file
->f_flags
& O_NONBLOCK
)
1692 } else if (substream
->f_flags
& O_NONBLOCK
)
1695 down_read(&snd_pcm_link_rwsem
);
1696 snd_pcm_stream_lock_irq(substream
);
1698 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1699 snd_pcm_pause(substream
, 0);
1701 /* pre-start/stop - all running streams are changed to DRAINING state */
1702 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1705 /* in non-blocking, we don't wait in ioctl but let caller poll */
1713 struct snd_pcm_runtime
*to_check
;
1714 if (signal_pending(current
)) {
1715 result
= -ERESTARTSYS
;
1718 /* find a substream to drain */
1720 snd_pcm_group_for_each_entry(s
, substream
) {
1721 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1723 runtime
= s
->runtime
;
1724 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1730 break; /* all drained */
1731 init_waitqueue_entry(&wait
, current
);
1732 add_wait_queue(&to_check
->sleep
, &wait
);
1733 snd_pcm_stream_unlock_irq(substream
);
1734 up_read(&snd_pcm_link_rwsem
);
1735 snd_power_unlock(card
);
1736 if (runtime
->no_period_wakeup
)
1737 tout
= MAX_SCHEDULE_TIMEOUT
;
1740 if (runtime
->rate
) {
1741 long t
= runtime
->period_size
* 2 / runtime
->rate
;
1742 tout
= max(t
, tout
);
1744 tout
= msecs_to_jiffies(tout
* 1000);
1746 tout
= schedule_timeout_interruptible(tout
);
1747 snd_power_lock(card
);
1748 down_read(&snd_pcm_link_rwsem
);
1749 snd_pcm_stream_lock_irq(substream
);
1750 remove_wait_queue(&to_check
->sleep
, &wait
);
1751 if (card
->shutdown
) {
1756 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1759 dev_dbg(substream
->pcm
->card
->dev
,
1760 "playback drain error (DMA or IRQ trouble?)\n");
1761 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1769 snd_pcm_stream_unlock_irq(substream
);
1770 up_read(&snd_pcm_link_rwsem
);
1771 snd_power_unlock(card
);
1779 * Immediately put all linked substreams into SETUP state.
1781 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1783 struct snd_pcm_runtime
*runtime
;
1786 if (PCM_RUNTIME_CHECK(substream
))
1788 runtime
= substream
->runtime
;
1790 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1791 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
||
1792 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1795 snd_pcm_stream_lock_irq(substream
);
1797 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1798 snd_pcm_pause(substream
, 0);
1800 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1801 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1802 snd_pcm_stream_unlock_irq(substream
);
1808 static bool is_pcm_file(struct file
*file
)
1810 struct inode
*inode
= file_inode(file
);
1813 if (!S_ISCHR(inode
->i_mode
) || imajor(inode
) != snd_major
)
1815 minor
= iminor(inode
);
1816 return snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) ||
1817 snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
1823 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1826 struct snd_pcm_file
*pcm_file
;
1827 struct snd_pcm_substream
*substream1
;
1828 struct snd_pcm_group
*group
;
1829 struct fd f
= fdget(fd
);
1833 if (!is_pcm_file(f
.file
)) {
1837 pcm_file
= f
.file
->private_data
;
1838 substream1
= pcm_file
->substream
;
1839 if (substream
== substream1
) {
1844 group
= kmalloc(sizeof(*group
), GFP_KERNEL
);
1849 down_write_nonfifo(&snd_pcm_link_rwsem
);
1850 write_lock_irq(&snd_pcm_link_rwlock
);
1851 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1852 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
||
1853 substream
->pcm
->nonatomic
!= substream1
->pcm
->nonatomic
) {
1857 if (snd_pcm_stream_linked(substream1
)) {
1861 if (!snd_pcm_stream_linked(substream
)) {
1862 substream
->group
= group
;
1864 spin_lock_init(&substream
->group
->lock
);
1865 mutex_init(&substream
->group
->mutex
);
1866 INIT_LIST_HEAD(&substream
->group
->substreams
);
1867 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1868 substream
->group
->count
= 1;
1870 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1871 substream
->group
->count
++;
1872 substream1
->group
= substream
->group
;
1874 write_unlock_irq(&snd_pcm_link_rwlock
);
1875 up_write(&snd_pcm_link_rwsem
);
1877 snd_card_unref(substream1
->pcm
->card
);
1884 static void relink_to_local(struct snd_pcm_substream
*substream
)
1886 substream
->group
= &substream
->self_group
;
1887 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1888 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1891 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1893 struct snd_pcm_substream
*s
;
1896 down_write_nonfifo(&snd_pcm_link_rwsem
);
1897 write_lock_irq(&snd_pcm_link_rwlock
);
1898 if (!snd_pcm_stream_linked(substream
)) {
1902 list_del(&substream
->link_list
);
1903 substream
->group
->count
--;
1904 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1905 snd_pcm_group_for_each_entry(s
, substream
) {
1909 kfree(substream
->group
);
1911 relink_to_local(substream
);
1913 write_unlock_irq(&snd_pcm_link_rwlock
);
1914 up_write(&snd_pcm_link_rwsem
);
1921 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1922 struct snd_pcm_hw_rule
*rule
)
1924 struct snd_interval t
;
1925 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1926 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1927 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1930 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1931 struct snd_pcm_hw_rule
*rule
)
1933 struct snd_interval t
;
1934 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1935 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1936 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1939 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1940 struct snd_pcm_hw_rule
*rule
)
1942 struct snd_interval t
;
1943 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1944 hw_param_interval_c(params
, rule
->deps
[1]),
1945 (unsigned long) rule
->private, &t
);
1946 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1949 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1950 struct snd_pcm_hw_rule
*rule
)
1952 struct snd_interval t
;
1953 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1954 (unsigned long) rule
->private,
1955 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1956 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1959 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1960 struct snd_pcm_hw_rule
*rule
)
1963 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1965 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1967 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1969 if (! snd_mask_test(mask
, k
))
1971 bits
= snd_pcm_format_physical_width(k
);
1973 continue; /* ignore invalid formats */
1974 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1975 snd_mask_reset(&m
, k
);
1977 return snd_mask_refine(mask
, &m
);
1980 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1981 struct snd_pcm_hw_rule
*rule
)
1983 struct snd_interval t
;
1989 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1991 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1993 bits
= snd_pcm_format_physical_width(k
);
1995 continue; /* ignore invalid formats */
1996 if (t
.min
> (unsigned)bits
)
1998 if (t
.max
< (unsigned)bits
)
2002 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2005 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2006 #error "Change this table"
2009 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2010 48000, 64000, 88200, 96000, 176400, 192000 };
2012 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
2013 .count
= ARRAY_SIZE(rates
),
2017 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
2018 struct snd_pcm_hw_rule
*rule
)
2020 struct snd_pcm_hardware
*hw
= rule
->private;
2021 return snd_interval_list(hw_param_interval(params
, rule
->var
),
2022 snd_pcm_known_rates
.count
,
2023 snd_pcm_known_rates
.list
, hw
->rates
);
2026 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
2027 struct snd_pcm_hw_rule
*rule
)
2029 struct snd_interval t
;
2030 struct snd_pcm_substream
*substream
= rule
->private;
2032 t
.max
= substream
->buffer_bytes_max
;
2036 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2039 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
2041 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2042 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
2045 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
2046 snd_mask_any(constrs_mask(constrs
, k
));
2049 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
2050 snd_interval_any(constrs_interval(constrs
, k
));
2053 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
2054 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
2055 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
2056 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
2057 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
2059 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
2060 snd_pcm_hw_rule_format
, NULL
,
2061 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2064 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2065 snd_pcm_hw_rule_sample_bits
, NULL
,
2066 SNDRV_PCM_HW_PARAM_FORMAT
,
2067 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2070 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2071 snd_pcm_hw_rule_div
, NULL
,
2072 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2075 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2076 snd_pcm_hw_rule_mul
, NULL
,
2077 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2080 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2081 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2082 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2085 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2086 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2087 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
2090 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
2091 snd_pcm_hw_rule_div
, NULL
,
2092 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2095 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2096 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2097 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
2100 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2101 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2102 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
2105 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
2106 snd_pcm_hw_rule_div
, NULL
,
2107 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2110 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2111 snd_pcm_hw_rule_div
, NULL
,
2112 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2115 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2116 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2117 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2120 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2121 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2122 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2125 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2126 snd_pcm_hw_rule_mul
, NULL
,
2127 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2130 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2131 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2132 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2135 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2136 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2137 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2140 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2141 snd_pcm_hw_rule_muldivk
, (void*) 8,
2142 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2145 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2146 snd_pcm_hw_rule_muldivk
, (void*) 8,
2147 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2150 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
2151 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2152 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2155 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
2156 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2157 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2163 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
2165 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2166 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
2168 unsigned int mask
= 0;
2170 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2171 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
2172 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2173 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
2174 if (hw_support_mmap(substream
)) {
2175 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2176 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2177 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2178 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
2179 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
2180 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
2182 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
2186 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
2190 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
2194 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
2195 hw
->channels_min
, hw
->channels_max
);
2199 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
2200 hw
->rate_min
, hw
->rate_max
);
2204 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2205 hw
->period_bytes_min
, hw
->period_bytes_max
);
2209 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
2210 hw
->periods_min
, hw
->periods_max
);
2214 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2215 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
2219 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2220 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
2221 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
2226 if (runtime
->dma_bytes
) {
2227 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
2232 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2233 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2234 snd_pcm_hw_rule_rate
, hw
,
2235 SNDRV_PCM_HW_PARAM_RATE
, -1);
2240 /* FIXME: this belong to lowlevel */
2241 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2246 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2248 if (snd_pcm_stream_linked(substream
))
2249 snd_pcm_unlink(substream
);
2252 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2254 substream
->ref_count
--;
2255 if (substream
->ref_count
> 0)
2258 snd_pcm_drop(substream
);
2259 if (substream
->hw_opened
) {
2260 if (substream
->ops
->hw_free
&&
2261 substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
)
2262 substream
->ops
->hw_free(substream
);
2263 substream
->ops
->close(substream
);
2264 substream
->hw_opened
= 0;
2266 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
2267 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
2268 if (substream
->pcm_release
) {
2269 substream
->pcm_release(substream
);
2270 substream
->pcm_release
= NULL
;
2272 snd_pcm_detach_substream(substream
);
2275 EXPORT_SYMBOL(snd_pcm_release_substream
);
2277 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2279 struct snd_pcm_substream
**rsubstream
)
2281 struct snd_pcm_substream
*substream
;
2284 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2287 if (substream
->ref_count
> 1) {
2288 *rsubstream
= substream
;
2292 err
= snd_pcm_hw_constraints_init(substream
);
2294 pcm_dbg(pcm
, "snd_pcm_hw_constraints_init failed\n");
2298 if ((err
= substream
->ops
->open(substream
)) < 0)
2301 substream
->hw_opened
= 1;
2303 err
= snd_pcm_hw_constraints_complete(substream
);
2305 pcm_dbg(pcm
, "snd_pcm_hw_constraints_complete failed\n");
2309 *rsubstream
= substream
;
2313 snd_pcm_release_substream(substream
);
2317 EXPORT_SYMBOL(snd_pcm_open_substream
);
2319 static int snd_pcm_open_file(struct file
*file
,
2320 struct snd_pcm
*pcm
,
2323 struct snd_pcm_file
*pcm_file
;
2324 struct snd_pcm_substream
*substream
;
2327 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2331 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2332 if (pcm_file
== NULL
) {
2333 snd_pcm_release_substream(substream
);
2336 pcm_file
->substream
= substream
;
2337 if (substream
->ref_count
== 1) {
2338 substream
->file
= pcm_file
;
2339 substream
->pcm_release
= pcm_release_private
;
2341 file
->private_data
= pcm_file
;
2346 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2348 struct snd_pcm
*pcm
;
2349 int err
= nonseekable_open(inode
, file
);
2352 pcm
= snd_lookup_minor_data(iminor(inode
),
2353 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2354 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2356 snd_card_unref(pcm
->card
);
2360 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2362 struct snd_pcm
*pcm
;
2363 int err
= nonseekable_open(inode
, file
);
2366 pcm
= snd_lookup_minor_data(iminor(inode
),
2367 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2368 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2370 snd_card_unref(pcm
->card
);
2374 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2383 err
= snd_card_file_add(pcm
->card
, file
);
2386 if (!try_module_get(pcm
->card
->module
)) {
2390 init_waitqueue_entry(&wait
, current
);
2391 add_wait_queue(&pcm
->open_wait
, &wait
);
2392 mutex_lock(&pcm
->open_mutex
);
2394 err
= snd_pcm_open_file(file
, pcm
, stream
);
2397 if (err
== -EAGAIN
) {
2398 if (file
->f_flags
& O_NONBLOCK
) {
2404 set_current_state(TASK_INTERRUPTIBLE
);
2405 mutex_unlock(&pcm
->open_mutex
);
2407 mutex_lock(&pcm
->open_mutex
);
2408 if (pcm
->card
->shutdown
) {
2412 if (signal_pending(current
)) {
2417 remove_wait_queue(&pcm
->open_wait
, &wait
);
2418 mutex_unlock(&pcm
->open_mutex
);
2424 module_put(pcm
->card
->module
);
2426 snd_card_file_remove(pcm
->card
, file
);
2431 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2433 struct snd_pcm
*pcm
;
2434 struct snd_pcm_substream
*substream
;
2435 struct snd_pcm_file
*pcm_file
;
2437 pcm_file
= file
->private_data
;
2438 substream
= pcm_file
->substream
;
2439 if (snd_BUG_ON(!substream
))
2441 pcm
= substream
->pcm
;
2442 mutex_lock(&pcm
->open_mutex
);
2443 snd_pcm_release_substream(substream
);
2445 mutex_unlock(&pcm
->open_mutex
);
2446 wake_up(&pcm
->open_wait
);
2447 module_put(pcm
->card
->module
);
2448 snd_card_file_remove(pcm
->card
, file
);
2452 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2453 snd_pcm_uframes_t frames
)
2455 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2456 snd_pcm_sframes_t appl_ptr
;
2457 snd_pcm_sframes_t ret
;
2458 snd_pcm_sframes_t hw_avail
;
2463 snd_pcm_stream_lock_irq(substream
);
2464 switch (runtime
->status
->state
) {
2465 case SNDRV_PCM_STATE_PREPARED
:
2467 case SNDRV_PCM_STATE_DRAINING
:
2468 case SNDRV_PCM_STATE_RUNNING
:
2469 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2472 case SNDRV_PCM_STATE_XRUN
:
2475 case SNDRV_PCM_STATE_SUSPENDED
:
2483 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2484 if (hw_avail
<= 0) {
2488 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2490 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2492 appl_ptr
+= runtime
->boundary
;
2493 runtime
->control
->appl_ptr
= appl_ptr
;
2496 snd_pcm_stream_unlock_irq(substream
);
2500 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2501 snd_pcm_uframes_t frames
)
2503 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2504 snd_pcm_sframes_t appl_ptr
;
2505 snd_pcm_sframes_t ret
;
2506 snd_pcm_sframes_t hw_avail
;
2511 snd_pcm_stream_lock_irq(substream
);
2512 switch (runtime
->status
->state
) {
2513 case SNDRV_PCM_STATE_PREPARED
:
2514 case SNDRV_PCM_STATE_DRAINING
:
2516 case SNDRV_PCM_STATE_RUNNING
:
2517 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2520 case SNDRV_PCM_STATE_XRUN
:
2523 case SNDRV_PCM_STATE_SUSPENDED
:
2531 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2532 if (hw_avail
<= 0) {
2536 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2538 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2540 appl_ptr
+= runtime
->boundary
;
2541 runtime
->control
->appl_ptr
= appl_ptr
;
2544 snd_pcm_stream_unlock_irq(substream
);
2548 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2549 snd_pcm_uframes_t frames
)
2551 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2552 snd_pcm_sframes_t appl_ptr
;
2553 snd_pcm_sframes_t ret
;
2554 snd_pcm_sframes_t avail
;
2559 snd_pcm_stream_lock_irq(substream
);
2560 switch (runtime
->status
->state
) {
2561 case SNDRV_PCM_STATE_PREPARED
:
2562 case SNDRV_PCM_STATE_PAUSED
:
2564 case SNDRV_PCM_STATE_DRAINING
:
2565 case SNDRV_PCM_STATE_RUNNING
:
2566 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2569 case SNDRV_PCM_STATE_XRUN
:
2572 case SNDRV_PCM_STATE_SUSPENDED
:
2580 avail
= snd_pcm_playback_avail(runtime
);
2585 if (frames
> (snd_pcm_uframes_t
)avail
)
2587 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2588 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2589 appl_ptr
-= runtime
->boundary
;
2590 runtime
->control
->appl_ptr
= appl_ptr
;
2593 snd_pcm_stream_unlock_irq(substream
);
2597 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2598 snd_pcm_uframes_t frames
)
2600 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2601 snd_pcm_sframes_t appl_ptr
;
2602 snd_pcm_sframes_t ret
;
2603 snd_pcm_sframes_t avail
;
2608 snd_pcm_stream_lock_irq(substream
);
2609 switch (runtime
->status
->state
) {
2610 case SNDRV_PCM_STATE_PREPARED
:
2611 case SNDRV_PCM_STATE_DRAINING
:
2612 case SNDRV_PCM_STATE_PAUSED
:
2614 case SNDRV_PCM_STATE_RUNNING
:
2615 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2618 case SNDRV_PCM_STATE_XRUN
:
2621 case SNDRV_PCM_STATE_SUSPENDED
:
2629 avail
= snd_pcm_capture_avail(runtime
);
2634 if (frames
> (snd_pcm_uframes_t
)avail
)
2636 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2637 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2638 appl_ptr
-= runtime
->boundary
;
2639 runtime
->control
->appl_ptr
= appl_ptr
;
2642 snd_pcm_stream_unlock_irq(substream
);
2646 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2648 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2651 snd_pcm_stream_lock_irq(substream
);
2652 switch (runtime
->status
->state
) {
2653 case SNDRV_PCM_STATE_DRAINING
:
2654 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2657 case SNDRV_PCM_STATE_RUNNING
:
2658 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2661 case SNDRV_PCM_STATE_PREPARED
:
2664 case SNDRV_PCM_STATE_SUSPENDED
:
2667 case SNDRV_PCM_STATE_XRUN
:
2675 snd_pcm_stream_unlock_irq(substream
);
2679 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2680 snd_pcm_sframes_t __user
*res
)
2682 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2684 snd_pcm_sframes_t n
= 0;
2686 snd_pcm_stream_lock_irq(substream
);
2687 switch (runtime
->status
->state
) {
2688 case SNDRV_PCM_STATE_DRAINING
:
2689 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2692 case SNDRV_PCM_STATE_RUNNING
:
2693 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2696 case SNDRV_PCM_STATE_PREPARED
:
2697 case SNDRV_PCM_STATE_SUSPENDED
:
2699 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2700 n
= snd_pcm_playback_hw_avail(runtime
);
2702 n
= snd_pcm_capture_avail(runtime
);
2703 n
+= runtime
->delay
;
2705 case SNDRV_PCM_STATE_XRUN
:
2713 snd_pcm_stream_unlock_irq(substream
);
2715 if (put_user(n
, res
))
2720 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2721 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2723 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2724 struct snd_pcm_sync_ptr sync_ptr
;
2725 volatile struct snd_pcm_mmap_status
*status
;
2726 volatile struct snd_pcm_mmap_control
*control
;
2729 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2730 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2732 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2734 status
= runtime
->status
;
2735 control
= runtime
->control
;
2736 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2737 err
= snd_pcm_hwsync(substream
);
2741 snd_pcm_stream_lock_irq(substream
);
2742 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2743 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2745 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2746 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2747 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2749 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2750 sync_ptr
.s
.status
.state
= status
->state
;
2751 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2752 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2753 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2754 sync_ptr
.s
.status
.audio_tstamp
= status
->audio_tstamp
;
2755 snd_pcm_stream_unlock_irq(substream
);
2756 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2761 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2763 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2766 if (get_user(arg
, _arg
))
2768 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2770 runtime
->tstamp_type
= arg
;
2774 static int snd_pcm_common_ioctl1(struct file
*file
,
2775 struct snd_pcm_substream
*substream
,
2776 unsigned int cmd
, void __user
*arg
)
2779 case SNDRV_PCM_IOCTL_PVERSION
:
2780 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2781 case SNDRV_PCM_IOCTL_INFO
:
2782 return snd_pcm_info_user(substream
, arg
);
2783 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2785 case SNDRV_PCM_IOCTL_TTSTAMP
:
2786 return snd_pcm_tstamp(substream
, arg
);
2787 case SNDRV_PCM_IOCTL_HW_REFINE
:
2788 return snd_pcm_hw_refine_user(substream
, arg
);
2789 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2790 return snd_pcm_hw_params_user(substream
, arg
);
2791 case SNDRV_PCM_IOCTL_HW_FREE
:
2792 return snd_pcm_hw_free(substream
);
2793 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2794 return snd_pcm_sw_params_user(substream
, arg
);
2795 case SNDRV_PCM_IOCTL_STATUS
:
2796 return snd_pcm_status_user(substream
, arg
, false);
2797 case SNDRV_PCM_IOCTL_STATUS_EXT
:
2798 return snd_pcm_status_user(substream
, arg
, true);
2799 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2800 return snd_pcm_channel_info_user(substream
, arg
);
2801 case SNDRV_PCM_IOCTL_PREPARE
:
2802 return snd_pcm_prepare(substream
, file
);
2803 case SNDRV_PCM_IOCTL_RESET
:
2804 return snd_pcm_reset(substream
);
2805 case SNDRV_PCM_IOCTL_START
:
2806 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2807 case SNDRV_PCM_IOCTL_LINK
:
2808 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2809 case SNDRV_PCM_IOCTL_UNLINK
:
2810 return snd_pcm_unlink(substream
);
2811 case SNDRV_PCM_IOCTL_RESUME
:
2812 return snd_pcm_resume(substream
);
2813 case SNDRV_PCM_IOCTL_XRUN
:
2814 return snd_pcm_xrun(substream
);
2815 case SNDRV_PCM_IOCTL_HWSYNC
:
2816 return snd_pcm_hwsync(substream
);
2817 case SNDRV_PCM_IOCTL_DELAY
:
2818 return snd_pcm_delay(substream
, arg
);
2819 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2820 return snd_pcm_sync_ptr(substream
, arg
);
2821 #ifdef CONFIG_SND_SUPPORT_OLD_API
2822 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2823 return snd_pcm_hw_refine_old_user(substream
, arg
);
2824 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2825 return snd_pcm_hw_params_old_user(substream
, arg
);
2827 case SNDRV_PCM_IOCTL_DRAIN
:
2828 return snd_pcm_drain(substream
, file
);
2829 case SNDRV_PCM_IOCTL_DROP
:
2830 return snd_pcm_drop(substream
);
2831 case SNDRV_PCM_IOCTL_PAUSE
:
2834 snd_pcm_stream_lock_irq(substream
);
2835 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2836 snd_pcm_stream_unlock_irq(substream
);
2840 pcm_dbg(substream
->pcm
, "unknown ioctl = 0x%x\n", cmd
);
2844 static int snd_pcm_playback_ioctl1(struct file
*file
,
2845 struct snd_pcm_substream
*substream
,
2846 unsigned int cmd
, void __user
*arg
)
2848 if (snd_BUG_ON(!substream
))
2850 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
2853 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2855 struct snd_xferi xferi
;
2856 struct snd_xferi __user
*_xferi
= arg
;
2857 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2858 snd_pcm_sframes_t result
;
2859 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2861 if (put_user(0, &_xferi
->result
))
2863 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2865 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2866 __put_user(result
, &_xferi
->result
);
2867 return result
< 0 ? result
: 0;
2869 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2871 struct snd_xfern xfern
;
2872 struct snd_xfern __user
*_xfern
= arg
;
2873 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2875 snd_pcm_sframes_t result
;
2876 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2878 if (runtime
->channels
> 128)
2880 if (put_user(0, &_xfern
->result
))
2882 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2885 bufs
= memdup_user(xfern
.bufs
,
2886 sizeof(void *) * runtime
->channels
);
2888 return PTR_ERR(bufs
);
2889 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2891 __put_user(result
, &_xfern
->result
);
2892 return result
< 0 ? result
: 0;
2894 case SNDRV_PCM_IOCTL_REWIND
:
2896 snd_pcm_uframes_t frames
;
2897 snd_pcm_uframes_t __user
*_frames
= arg
;
2898 snd_pcm_sframes_t result
;
2899 if (get_user(frames
, _frames
))
2901 if (put_user(0, _frames
))
2903 result
= snd_pcm_playback_rewind(substream
, frames
);
2904 __put_user(result
, _frames
);
2905 return result
< 0 ? result
: 0;
2907 case SNDRV_PCM_IOCTL_FORWARD
:
2909 snd_pcm_uframes_t frames
;
2910 snd_pcm_uframes_t __user
*_frames
= arg
;
2911 snd_pcm_sframes_t result
;
2912 if (get_user(frames
, _frames
))
2914 if (put_user(0, _frames
))
2916 result
= snd_pcm_playback_forward(substream
, frames
);
2917 __put_user(result
, _frames
);
2918 return result
< 0 ? result
: 0;
2921 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2924 static int snd_pcm_capture_ioctl1(struct file
*file
,
2925 struct snd_pcm_substream
*substream
,
2926 unsigned int cmd
, void __user
*arg
)
2928 if (snd_BUG_ON(!substream
))
2930 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
))
2933 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2935 struct snd_xferi xferi
;
2936 struct snd_xferi __user
*_xferi
= arg
;
2937 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2938 snd_pcm_sframes_t result
;
2939 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2941 if (put_user(0, &_xferi
->result
))
2943 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2945 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2946 __put_user(result
, &_xferi
->result
);
2947 return result
< 0 ? result
: 0;
2949 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2951 struct snd_xfern xfern
;
2952 struct snd_xfern __user
*_xfern
= arg
;
2953 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2955 snd_pcm_sframes_t result
;
2956 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2958 if (runtime
->channels
> 128)
2960 if (put_user(0, &_xfern
->result
))
2962 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2965 bufs
= memdup_user(xfern
.bufs
,
2966 sizeof(void *) * runtime
->channels
);
2968 return PTR_ERR(bufs
);
2969 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2971 __put_user(result
, &_xfern
->result
);
2972 return result
< 0 ? result
: 0;
2974 case SNDRV_PCM_IOCTL_REWIND
:
2976 snd_pcm_uframes_t frames
;
2977 snd_pcm_uframes_t __user
*_frames
= arg
;
2978 snd_pcm_sframes_t result
;
2979 if (get_user(frames
, _frames
))
2981 if (put_user(0, _frames
))
2983 result
= snd_pcm_capture_rewind(substream
, frames
);
2984 __put_user(result
, _frames
);
2985 return result
< 0 ? result
: 0;
2987 case SNDRV_PCM_IOCTL_FORWARD
:
2989 snd_pcm_uframes_t frames
;
2990 snd_pcm_uframes_t __user
*_frames
= arg
;
2991 snd_pcm_sframes_t result
;
2992 if (get_user(frames
, _frames
))
2994 if (put_user(0, _frames
))
2996 result
= snd_pcm_capture_forward(substream
, frames
);
2997 __put_user(result
, _frames
);
2998 return result
< 0 ? result
: 0;
3001 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
3004 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
3007 struct snd_pcm_file
*pcm_file
;
3009 pcm_file
= file
->private_data
;
3011 if (((cmd
>> 8) & 0xff) != 'A')
3014 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
3015 (void __user
*)arg
);
3018 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
3021 struct snd_pcm_file
*pcm_file
;
3023 pcm_file
= file
->private_data
;
3025 if (((cmd
>> 8) & 0xff) != 'A')
3028 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
3029 (void __user
*)arg
);
3032 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
3033 unsigned int cmd
, void *arg
)
3038 fs
= snd_enter_user();
3039 switch (substream
->stream
) {
3040 case SNDRV_PCM_STREAM_PLAYBACK
:
3041 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
3042 (void __user
*)arg
);
3044 case SNDRV_PCM_STREAM_CAPTURE
:
3045 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
3046 (void __user
*)arg
);
3056 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
3058 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
3061 struct snd_pcm_file
*pcm_file
;
3062 struct snd_pcm_substream
*substream
;
3063 struct snd_pcm_runtime
*runtime
;
3064 snd_pcm_sframes_t result
;
3066 pcm_file
= file
->private_data
;
3067 substream
= pcm_file
->substream
;
3068 if (PCM_RUNTIME_CHECK(substream
))
3070 runtime
= substream
->runtime
;
3071 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3073 if (!frame_aligned(runtime
, count
))
3075 count
= bytes_to_frames(runtime
, count
);
3076 result
= snd_pcm_lib_read(substream
, buf
, count
);
3078 result
= frames_to_bytes(runtime
, result
);
3082 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
3083 size_t count
, loff_t
* offset
)
3085 struct snd_pcm_file
*pcm_file
;
3086 struct snd_pcm_substream
*substream
;
3087 struct snd_pcm_runtime
*runtime
;
3088 snd_pcm_sframes_t result
;
3090 pcm_file
= file
->private_data
;
3091 substream
= pcm_file
->substream
;
3092 if (PCM_RUNTIME_CHECK(substream
))
3094 runtime
= substream
->runtime
;
3095 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3097 if (!frame_aligned(runtime
, count
))
3099 count
= bytes_to_frames(runtime
, count
);
3100 result
= snd_pcm_lib_write(substream
, buf
, count
);
3102 result
= frames_to_bytes(runtime
, result
);
3106 static ssize_t
snd_pcm_readv(struct kiocb
*iocb
, struct iov_iter
*to
)
3108 struct snd_pcm_file
*pcm_file
;
3109 struct snd_pcm_substream
*substream
;
3110 struct snd_pcm_runtime
*runtime
;
3111 snd_pcm_sframes_t result
;
3114 snd_pcm_uframes_t frames
;
3116 pcm_file
= iocb
->ki_filp
->private_data
;
3117 substream
= pcm_file
->substream
;
3118 if (PCM_RUNTIME_CHECK(substream
))
3120 runtime
= substream
->runtime
;
3121 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3123 if (!iter_is_iovec(to
))
3125 if (to
->nr_segs
> 1024 || to
->nr_segs
!= runtime
->channels
)
3127 if (!frame_aligned(runtime
, to
->iov
->iov_len
))
3129 frames
= bytes_to_samples(runtime
, to
->iov
->iov_len
);
3130 bufs
= kmalloc(sizeof(void *) * to
->nr_segs
, GFP_KERNEL
);
3133 for (i
= 0; i
< to
->nr_segs
; ++i
)
3134 bufs
[i
] = to
->iov
[i
].iov_base
;
3135 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
3137 result
= frames_to_bytes(runtime
, result
);
3142 static ssize_t
snd_pcm_writev(struct kiocb
*iocb
, struct iov_iter
*from
)
3144 struct snd_pcm_file
*pcm_file
;
3145 struct snd_pcm_substream
*substream
;
3146 struct snd_pcm_runtime
*runtime
;
3147 snd_pcm_sframes_t result
;
3150 snd_pcm_uframes_t frames
;
3152 pcm_file
= iocb
->ki_filp
->private_data
;
3153 substream
= pcm_file
->substream
;
3154 if (PCM_RUNTIME_CHECK(substream
))
3156 runtime
= substream
->runtime
;
3157 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3159 if (!iter_is_iovec(from
))
3161 if (from
->nr_segs
> 128 || from
->nr_segs
!= runtime
->channels
||
3162 !frame_aligned(runtime
, from
->iov
->iov_len
))
3164 frames
= bytes_to_samples(runtime
, from
->iov
->iov_len
);
3165 bufs
= kmalloc(sizeof(void *) * from
->nr_segs
, GFP_KERNEL
);
3168 for (i
= 0; i
< from
->nr_segs
; ++i
)
3169 bufs
[i
] = from
->iov
[i
].iov_base
;
3170 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
3172 result
= frames_to_bytes(runtime
, result
);
3177 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
3179 struct snd_pcm_file
*pcm_file
;
3180 struct snd_pcm_substream
*substream
;
3181 struct snd_pcm_runtime
*runtime
;
3183 snd_pcm_uframes_t avail
;
3185 pcm_file
= file
->private_data
;
3187 substream
= pcm_file
->substream
;
3188 if (PCM_RUNTIME_CHECK(substream
))
3189 return POLLOUT
| POLLWRNORM
| POLLERR
;
3190 runtime
= substream
->runtime
;
3192 poll_wait(file
, &runtime
->sleep
, wait
);
3194 snd_pcm_stream_lock_irq(substream
);
3195 avail
= snd_pcm_playback_avail(runtime
);
3196 switch (runtime
->status
->state
) {
3197 case SNDRV_PCM_STATE_RUNNING
:
3198 case SNDRV_PCM_STATE_PREPARED
:
3199 case SNDRV_PCM_STATE_PAUSED
:
3200 if (avail
>= runtime
->control
->avail_min
) {
3201 mask
= POLLOUT
| POLLWRNORM
;
3205 case SNDRV_PCM_STATE_DRAINING
:
3209 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
3212 snd_pcm_stream_unlock_irq(substream
);
3216 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
3218 struct snd_pcm_file
*pcm_file
;
3219 struct snd_pcm_substream
*substream
;
3220 struct snd_pcm_runtime
*runtime
;
3222 snd_pcm_uframes_t avail
;
3224 pcm_file
= file
->private_data
;
3226 substream
= pcm_file
->substream
;
3227 if (PCM_RUNTIME_CHECK(substream
))
3228 return POLLIN
| POLLRDNORM
| POLLERR
;
3229 runtime
= substream
->runtime
;
3231 poll_wait(file
, &runtime
->sleep
, wait
);
3233 snd_pcm_stream_lock_irq(substream
);
3234 avail
= snd_pcm_capture_avail(runtime
);
3235 switch (runtime
->status
->state
) {
3236 case SNDRV_PCM_STATE_RUNNING
:
3237 case SNDRV_PCM_STATE_PREPARED
:
3238 case SNDRV_PCM_STATE_PAUSED
:
3239 if (avail
>= runtime
->control
->avail_min
) {
3240 mask
= POLLIN
| POLLRDNORM
;
3245 case SNDRV_PCM_STATE_DRAINING
:
3247 mask
= POLLIN
| POLLRDNORM
;
3252 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3255 snd_pcm_stream_unlock_irq(substream
);
3264 * Only on coherent architectures, we can mmap the status and the control records
3265 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3267 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3269 * mmap status record
3271 static int snd_pcm_mmap_status_fault(struct vm_area_struct
*area
,
3272 struct vm_fault
*vmf
)
3274 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3275 struct snd_pcm_runtime
*runtime
;
3277 if (substream
== NULL
)
3278 return VM_FAULT_SIGBUS
;
3279 runtime
= substream
->runtime
;
3280 vmf
->page
= virt_to_page(runtime
->status
);
3281 get_page(vmf
->page
);
3285 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
3287 .fault
= snd_pcm_mmap_status_fault
,
3290 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3291 struct vm_area_struct
*area
)
3294 if (!(area
->vm_flags
& VM_READ
))
3296 size
= area
->vm_end
- area
->vm_start
;
3297 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3299 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3300 area
->vm_private_data
= substream
;
3301 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3306 * mmap control record
3308 static int snd_pcm_mmap_control_fault(struct vm_area_struct
*area
,
3309 struct vm_fault
*vmf
)
3311 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3312 struct snd_pcm_runtime
*runtime
;
3314 if (substream
== NULL
)
3315 return VM_FAULT_SIGBUS
;
3316 runtime
= substream
->runtime
;
3317 vmf
->page
= virt_to_page(runtime
->control
);
3318 get_page(vmf
->page
);
3322 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3324 .fault
= snd_pcm_mmap_control_fault
,
3327 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3328 struct vm_area_struct
*area
)
3331 if (!(area
->vm_flags
& VM_READ
))
3333 size
= area
->vm_end
- area
->vm_start
;
3334 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3336 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3337 area
->vm_private_data
= substream
;
3338 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3341 #else /* ! coherent mmap */
3343 * don't support mmap for status and control records.
3345 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3346 struct vm_area_struct
*area
)
3350 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3351 struct vm_area_struct
*area
)
3355 #endif /* coherent mmap */
3357 static inline struct page
*
3358 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3360 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3361 return virt_to_page(vaddr
);
3365 * fault callback for mmapping a RAM page
3367 static int snd_pcm_mmap_data_fault(struct vm_area_struct
*area
,
3368 struct vm_fault
*vmf
)
3370 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3371 struct snd_pcm_runtime
*runtime
;
3372 unsigned long offset
;
3376 if (substream
== NULL
)
3377 return VM_FAULT_SIGBUS
;
3378 runtime
= substream
->runtime
;
3379 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3380 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3381 if (offset
> dma_bytes
- PAGE_SIZE
)
3382 return VM_FAULT_SIGBUS
;
3383 if (substream
->ops
->page
)
3384 page
= substream
->ops
->page(substream
, offset
);
3386 page
= snd_pcm_default_page_ops(substream
, offset
);
3388 return VM_FAULT_SIGBUS
;
3394 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3395 .open
= snd_pcm_mmap_data_open
,
3396 .close
= snd_pcm_mmap_data_close
,
3399 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3400 .open
= snd_pcm_mmap_data_open
,
3401 .close
= snd_pcm_mmap_data_close
,
3402 .fault
= snd_pcm_mmap_data_fault
,
3406 * mmap the DMA buffer on RAM
3410 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3411 * @substream: PCM substream
3414 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3415 * this function is invoked implicitly.
3417 int snd_pcm_lib_default_mmap(struct snd_pcm_substream
*substream
,
3418 struct vm_area_struct
*area
)
3420 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3421 #ifdef CONFIG_GENERIC_ALLOCATOR
3422 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_IRAM
) {
3423 area
->vm_page_prot
= pgprot_writecombine(area
->vm_page_prot
);
3424 return remap_pfn_range(area
, area
->vm_start
,
3425 substream
->dma_buffer
.addr
>> PAGE_SHIFT
,
3426 area
->vm_end
- area
->vm_start
, area
->vm_page_prot
);
3428 #endif /* CONFIG_GENERIC_ALLOCATOR */
3429 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3430 if (!substream
->ops
->page
&&
3431 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3432 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3434 substream
->runtime
->dma_area
,
3435 substream
->runtime
->dma_addr
,
3436 substream
->runtime
->dma_bytes
);
3437 #endif /* CONFIG_X86 */
3438 /* mmap with fault handler */
3439 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3442 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap
);
3445 * mmap the DMA buffer on I/O memory area
3447 #if SNDRV_PCM_INFO_MMAP_IOMEM
3449 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3450 * @substream: PCM substream
3453 * When your hardware uses the iomapped pages as the hardware buffer and
3454 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3455 * is supposed to work only on limited architectures.
3457 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3458 struct vm_area_struct
*area
)
3460 struct snd_pcm_runtime
*runtime
= substream
->runtime
;;
3462 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3463 return vm_iomap_memory(area
, runtime
->dma_addr
, runtime
->dma_bytes
);
3466 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3467 #endif /* SNDRV_PCM_INFO_MMAP */
3472 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3473 struct vm_area_struct
*area
)
3475 struct snd_pcm_runtime
*runtime
;
3477 unsigned long offset
;
3481 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3482 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3485 if (!(area
->vm_flags
& VM_READ
))
3488 runtime
= substream
->runtime
;
3489 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3491 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3493 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3494 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3496 size
= area
->vm_end
- area
->vm_start
;
3497 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3498 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3499 if ((size_t)size
> dma_bytes
)
3501 if (offset
> dma_bytes
- size
)
3504 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3505 area
->vm_private_data
= substream
;
3506 if (substream
->ops
->mmap
)
3507 err
= substream
->ops
->mmap(substream
, area
);
3509 err
= snd_pcm_lib_default_mmap(substream
, area
);
3511 atomic_inc(&substream
->mmap_count
);
3515 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3517 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3519 struct snd_pcm_file
* pcm_file
;
3520 struct snd_pcm_substream
*substream
;
3521 unsigned long offset
;
3523 pcm_file
= file
->private_data
;
3524 substream
= pcm_file
->substream
;
3525 if (PCM_RUNTIME_CHECK(substream
))
3528 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3530 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3531 if (pcm_file
->no_compat_mmap
)
3533 return snd_pcm_mmap_status(substream
, file
, area
);
3534 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3535 if (pcm_file
->no_compat_mmap
)
3537 return snd_pcm_mmap_control(substream
, file
, area
);
3539 return snd_pcm_mmap_data(substream
, file
, area
);
3544 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3546 struct snd_pcm_file
* pcm_file
;
3547 struct snd_pcm_substream
*substream
;
3548 struct snd_pcm_runtime
*runtime
;
3550 pcm_file
= file
->private_data
;
3551 substream
= pcm_file
->substream
;
3552 if (PCM_RUNTIME_CHECK(substream
))
3554 runtime
= substream
->runtime
;
3555 return fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3561 #ifdef CONFIG_COMPAT
3562 #include "pcm_compat.c"
3564 #define snd_pcm_ioctl_compat NULL
3568 * To be removed helpers to keep binary compatibility
3571 #ifdef CONFIG_SND_SUPPORT_OLD_API
3572 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3573 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3575 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3576 struct snd_pcm_hw_params_old
*oparams
)
3580 memset(params
, 0, sizeof(*params
));
3581 params
->flags
= oparams
->flags
;
3582 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3583 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3584 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3585 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3586 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3587 params
->info
= oparams
->info
;
3588 params
->msbits
= oparams
->msbits
;
3589 params
->rate_num
= oparams
->rate_num
;
3590 params
->rate_den
= oparams
->rate_den
;
3591 params
->fifo_size
= oparams
->fifo_size
;
3594 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3595 struct snd_pcm_hw_params
*params
)
3599 memset(oparams
, 0, sizeof(*oparams
));
3600 oparams
->flags
= params
->flags
;
3601 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3602 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3603 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3604 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3605 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3606 oparams
->info
= params
->info
;
3607 oparams
->msbits
= params
->msbits
;
3608 oparams
->rate_num
= params
->rate_num
;
3609 oparams
->rate_den
= params
->rate_den
;
3610 oparams
->fifo_size
= params
->fifo_size
;
3613 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3614 struct snd_pcm_hw_params_old __user
* _oparams
)
3616 struct snd_pcm_hw_params
*params
;
3617 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3620 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3624 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3625 if (IS_ERR(oparams
)) {
3626 err
= PTR_ERR(oparams
);
3629 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3630 err
= snd_pcm_hw_refine(substream
, params
);
3631 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3632 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3643 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3644 struct snd_pcm_hw_params_old __user
* _oparams
)
3646 struct snd_pcm_hw_params
*params
;
3647 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3650 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3654 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3655 if (IS_ERR(oparams
)) {
3656 err
= PTR_ERR(oparams
);
3659 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3660 err
= snd_pcm_hw_params(substream
, params
);
3661 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3662 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3672 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3675 static unsigned long snd_pcm_get_unmapped_area(struct file
*file
,
3678 unsigned long pgoff
,
3679 unsigned long flags
)
3681 struct snd_pcm_file
*pcm_file
= file
->private_data
;
3682 struct snd_pcm_substream
*substream
= pcm_file
->substream
;
3683 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3684 unsigned long offset
= pgoff
<< PAGE_SHIFT
;
3687 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3688 return (unsigned long)runtime
->status
;
3689 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3690 return (unsigned long)runtime
->control
;
3692 return (unsigned long)runtime
->dma_area
+ offset
;
3696 # define snd_pcm_get_unmapped_area NULL
3703 const struct file_operations snd_pcm_f_ops
[2] = {
3705 .owner
= THIS_MODULE
,
3706 .write
= snd_pcm_write
,
3707 .write_iter
= snd_pcm_writev
,
3708 .open
= snd_pcm_playback_open
,
3709 .release
= snd_pcm_release
,
3710 .llseek
= no_llseek
,
3711 .poll
= snd_pcm_playback_poll
,
3712 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3713 .compat_ioctl
= snd_pcm_ioctl_compat
,
3714 .mmap
= snd_pcm_mmap
,
3715 .fasync
= snd_pcm_fasync
,
3716 .get_unmapped_area
= snd_pcm_get_unmapped_area
,
3719 .owner
= THIS_MODULE
,
3720 .read
= snd_pcm_read
,
3721 .read_iter
= snd_pcm_readv
,
3722 .open
= snd_pcm_capture_open
,
3723 .release
= snd_pcm_release
,
3724 .llseek
= no_llseek
,
3725 .poll
= snd_pcm_capture_poll
,
3726 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3727 .compat_ioctl
= snd_pcm_ioctl_compat
,
3728 .mmap
= snd_pcm_mmap
,
3729 .fasync
= snd_pcm_fasync
,
3730 .get_unmapped_area
= snd_pcm_get_unmapped_area
,