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>
43 struct snd_pcm_hw_params_old
{
45 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
46 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
47 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
53 unsigned int rate_num
;
54 unsigned int rate_den
;
55 snd_pcm_uframes_t fifo_size
;
56 unsigned char reserved
[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
64 struct snd_pcm_hw_params_old __user
* _oparams
);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
66 struct snd_pcm_hw_params_old __user
* _oparams
);
68 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
74 static DEFINE_RWLOCK(snd_pcm_link_rwlock
);
75 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
78 * snd_pcm_stream_lock - Lock the PCM stream
79 * @substream: PCM substream
81 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
82 * flag of the given substream. This also takes the global link rw lock
83 * (or rw sem), too, for avoiding the race with linked streams.
85 void snd_pcm_stream_lock(struct snd_pcm_substream
*substream
)
87 if (substream
->pcm
->nonatomic
) {
88 down_read_nested(&snd_pcm_link_rwsem
, SINGLE_DEPTH_NESTING
);
89 mutex_lock(&substream
->self_group
.mutex
);
91 read_lock(&snd_pcm_link_rwlock
);
92 spin_lock(&substream
->self_group
.lock
);
95 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock
);
98 * snd_pcm_stream_lock - Unlock the PCM stream
99 * @substream: PCM substream
101 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
103 void snd_pcm_stream_unlock(struct snd_pcm_substream
*substream
)
105 if (substream
->pcm
->nonatomic
) {
106 mutex_unlock(&substream
->self_group
.mutex
);
107 up_read(&snd_pcm_link_rwsem
);
109 spin_unlock(&substream
->self_group
.lock
);
110 read_unlock(&snd_pcm_link_rwlock
);
113 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock
);
116 * snd_pcm_stream_lock_irq - Lock the PCM stream
117 * @substream: PCM substream
119 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
120 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
121 * as snd_pcm_stream_lock().
123 void snd_pcm_stream_lock_irq(struct snd_pcm_substream
*substream
)
125 if (!substream
->pcm
->nonatomic
)
127 snd_pcm_stream_lock(substream
);
129 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq
);
132 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
133 * @substream: PCM substream
135 * This is a counter-part of snd_pcm_stream_lock_irq().
137 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream
*substream
)
139 snd_pcm_stream_unlock(substream
);
140 if (!substream
->pcm
->nonatomic
)
143 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq
);
145 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream
*substream
)
147 unsigned long flags
= 0;
148 if (!substream
->pcm
->nonatomic
)
149 local_irq_save(flags
);
150 snd_pcm_stream_lock(substream
);
153 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave
);
156 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
157 * @substream: PCM substream
160 * This is a counter-part of snd_pcm_stream_lock_irqsave().
162 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream
*substream
,
165 snd_pcm_stream_unlock(substream
);
166 if (!substream
->pcm
->nonatomic
)
167 local_irq_restore(flags
);
169 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore
);
171 static inline mm_segment_t
snd_enter_user(void)
173 mm_segment_t fs
= get_fs();
178 static inline void snd_leave_user(mm_segment_t fs
)
185 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
187 struct snd_pcm_runtime
*runtime
;
188 struct snd_pcm
*pcm
= substream
->pcm
;
189 struct snd_pcm_str
*pstr
= substream
->pstr
;
191 memset(info
, 0, sizeof(*info
));
192 info
->card
= pcm
->card
->number
;
193 info
->device
= pcm
->device
;
194 info
->stream
= substream
->stream
;
195 info
->subdevice
= substream
->number
;
196 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
197 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
198 info
->dev_class
= pcm
->dev_class
;
199 info
->dev_subclass
= pcm
->dev_subclass
;
200 info
->subdevices_count
= pstr
->substream_count
;
201 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
202 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
203 runtime
= substream
->runtime
;
204 /* AB: FIXME!!! This is definitely nonsense */
206 info
->sync
= runtime
->sync
;
207 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
212 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
213 struct snd_pcm_info __user
* _info
)
215 struct snd_pcm_info
*info
;
218 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
221 err
= snd_pcm_info(substream
, info
);
223 if (copy_to_user(_info
, info
, sizeof(*info
)))
230 static bool hw_support_mmap(struct snd_pcm_substream
*substream
)
232 if (!(substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_MMAP
))
234 /* check architectures that return -EINVAL from dma_mmap_coherent() */
235 /* FIXME: this should be some global flag */
236 #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
237 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
238 if (!substream
->ops
->mmap
&&
239 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
248 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
249 static const char * const snd_pcm_hw_param_names
[] = {
253 HW_PARAM(SAMPLE_BITS
),
254 HW_PARAM(FRAME_BITS
),
257 HW_PARAM(PERIOD_TIME
),
258 HW_PARAM(PERIOD_SIZE
),
259 HW_PARAM(PERIOD_BYTES
),
261 HW_PARAM(BUFFER_TIME
),
262 HW_PARAM(BUFFER_SIZE
),
263 HW_PARAM(BUFFER_BYTES
),
268 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
269 struct snd_pcm_hw_params
*params
)
272 struct snd_pcm_hardware
*hw
;
273 struct snd_interval
*i
= NULL
;
274 struct snd_mask
*m
= NULL
;
275 struct snd_pcm_hw_constraints
*constrs
= &substream
->runtime
->hw_constraints
;
276 unsigned int rstamps
[constrs
->rules_num
];
277 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
278 unsigned int stamp
= 2;
282 params
->fifo_size
= 0;
283 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
285 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
286 params
->rate_num
= 0;
287 params
->rate_den
= 0;
290 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
291 m
= hw_param_mask(params
, k
);
292 if (snd_mask_empty(m
))
294 if (!(params
->rmask
& (1 << k
)))
297 pr_debug("%s = ", snd_pcm_hw_param_names
[k
]);
298 pr_cont("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
300 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
302 pr_cont("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
305 params
->cmask
|= 1 << k
;
310 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
311 i
= hw_param_interval(params
, k
);
312 if (snd_interval_empty(i
))
314 if (!(params
->rmask
& (1 << k
)))
317 pr_debug("%s = ", snd_pcm_hw_param_names
[k
]);
322 i
->openmin
? '(' : '[', i
->min
,
323 i
->max
, i
->openmax
? ')' : ']');
326 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
331 pr_cont("%c%u %u%c\n",
332 i
->openmin
? '(' : '[', i
->min
,
333 i
->max
, i
->openmax
? ')' : ']');
336 params
->cmask
|= 1 << k
;
341 for (k
= 0; k
< constrs
->rules_num
; k
++)
343 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
344 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
347 for (k
= 0; k
< constrs
->rules_num
; k
++) {
348 struct snd_pcm_hw_rule
*r
= &constrs
->rules
[k
];
351 if (r
->cond
&& !(r
->cond
& params
->flags
))
353 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
354 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
362 pr_debug("Rule %d [%p]: ", k
, r
->func
);
364 pr_cont("%s = ", snd_pcm_hw_param_names
[r
->var
]);
365 if (hw_is_mask(r
->var
)) {
366 m
= hw_param_mask(params
, r
->var
);
367 pr_cont("%x", *m
->bits
);
369 i
= hw_param_interval(params
, r
->var
);
374 i
->openmin
? '(' : '[', i
->min
,
375 i
->max
, i
->openmax
? ')' : ']');
379 changed
= r
->func(params
, r
);
383 if (hw_is_mask(r
->var
))
384 pr_cont("%x", *m
->bits
);
390 i
->openmin
? '(' : '[', i
->min
,
391 i
->max
, i
->openmax
? ')' : ']');
397 if (changed
&& r
->var
>= 0) {
398 params
->cmask
|= (1 << r
->var
);
399 vstamps
[r
->var
] = stamp
;
407 if (!params
->msbits
) {
408 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
409 if (snd_interval_single(i
))
410 params
->msbits
= snd_interval_value(i
);
413 if (!params
->rate_den
) {
414 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
415 if (snd_interval_single(i
)) {
416 params
->rate_num
= snd_interval_value(i
);
417 params
->rate_den
= 1;
421 hw
= &substream
->runtime
->hw
;
423 params
->info
= hw
->info
& ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES
|
424 SNDRV_PCM_INFO_DRAIN_TRIGGER
);
425 if (!hw_support_mmap(substream
))
426 params
->info
&= ~(SNDRV_PCM_INFO_MMAP
|
427 SNDRV_PCM_INFO_MMAP_VALID
);
429 if (!params
->fifo_size
) {
430 m
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
431 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
432 if (snd_mask_min(m
) == snd_mask_max(m
) &&
433 snd_interval_min(i
) == snd_interval_max(i
)) {
434 changed
= substream
->ops
->ioctl(substream
,
435 SNDRV_PCM_IOCTL1_FIFO_SIZE
, params
);
444 EXPORT_SYMBOL(snd_pcm_hw_refine
);
446 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
447 struct snd_pcm_hw_params __user
* _params
)
449 struct snd_pcm_hw_params
*params
;
452 params
= memdup_user(_params
, sizeof(*params
));
454 return PTR_ERR(params
);
456 err
= snd_pcm_hw_refine(substream
, params
);
457 if (copy_to_user(_params
, params
, sizeof(*params
))) {
466 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
471 return -1; /* invalid */
473 /* take 75% of period time as the deadline */
474 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
475 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
481 static void snd_pcm_set_state(struct snd_pcm_substream
*substream
, int state
)
483 snd_pcm_stream_lock_irq(substream
);
484 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_DISCONNECTED
)
485 substream
->runtime
->status
->state
= state
;
486 snd_pcm_stream_unlock_irq(substream
);
489 static inline void snd_pcm_timer_notify(struct snd_pcm_substream
*substream
,
492 #ifdef CONFIG_SND_PCM_TIMER
493 if (substream
->timer
)
494 snd_timer_notify(substream
->timer
, event
,
495 &substream
->runtime
->trigger_tstamp
);
499 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
500 struct snd_pcm_hw_params
*params
)
502 struct snd_pcm_runtime
*runtime
;
505 snd_pcm_uframes_t frames
;
507 if (PCM_RUNTIME_CHECK(substream
))
509 runtime
= substream
->runtime
;
510 snd_pcm_stream_lock_irq(substream
);
511 switch (runtime
->status
->state
) {
512 case SNDRV_PCM_STATE_OPEN
:
513 case SNDRV_PCM_STATE_SETUP
:
514 case SNDRV_PCM_STATE_PREPARED
:
517 snd_pcm_stream_unlock_irq(substream
);
520 snd_pcm_stream_unlock_irq(substream
);
521 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
522 if (!substream
->oss
.oss
)
524 if (atomic_read(&substream
->mmap_count
))
528 err
= snd_pcm_hw_refine(substream
, params
);
532 err
= snd_pcm_hw_params_choose(substream
, params
);
536 if (substream
->ops
->hw_params
!= NULL
) {
537 err
= substream
->ops
->hw_params(substream
, params
);
542 runtime
->access
= params_access(params
);
543 runtime
->format
= params_format(params
);
544 runtime
->subformat
= params_subformat(params
);
545 runtime
->channels
= params_channels(params
);
546 runtime
->rate
= params_rate(params
);
547 runtime
->period_size
= params_period_size(params
);
548 runtime
->periods
= params_periods(params
);
549 runtime
->buffer_size
= params_buffer_size(params
);
550 runtime
->info
= params
->info
;
551 runtime
->rate_num
= params
->rate_num
;
552 runtime
->rate_den
= params
->rate_den
;
553 runtime
->no_period_wakeup
=
554 (params
->info
& SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
) &&
555 (params
->flags
& SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
);
557 bits
= snd_pcm_format_physical_width(runtime
->format
);
558 runtime
->sample_bits
= bits
;
559 bits
*= runtime
->channels
;
560 runtime
->frame_bits
= bits
;
562 while (bits
% 8 != 0) {
566 runtime
->byte_align
= bits
/ 8;
567 runtime
->min_align
= frames
;
569 /* Default sw params */
570 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
571 runtime
->period_step
= 1;
572 runtime
->control
->avail_min
= runtime
->period_size
;
573 runtime
->start_threshold
= 1;
574 runtime
->stop_threshold
= runtime
->buffer_size
;
575 runtime
->silence_threshold
= 0;
576 runtime
->silence_size
= 0;
577 runtime
->boundary
= runtime
->buffer_size
;
578 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
579 runtime
->boundary
*= 2;
581 snd_pcm_timer_resolution_change(substream
);
582 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_SETUP
);
584 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
585 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
586 if ((usecs
= period_to_usecs(runtime
)) >= 0)
587 pm_qos_add_request(&substream
->latency_pm_qos_req
,
588 PM_QOS_CPU_DMA_LATENCY
, usecs
);
591 /* hardware might be unusable from this time,
592 so we force application to retry to set
593 the correct hardware parameter settings */
594 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
595 if (substream
->ops
->hw_free
!= NULL
)
596 substream
->ops
->hw_free(substream
);
600 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
601 struct snd_pcm_hw_params __user
* _params
)
603 struct snd_pcm_hw_params
*params
;
606 params
= memdup_user(_params
, sizeof(*params
));
608 return PTR_ERR(params
);
610 err
= snd_pcm_hw_params(substream
, params
);
611 if (copy_to_user(_params
, params
, sizeof(*params
))) {
620 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
622 struct snd_pcm_runtime
*runtime
;
625 if (PCM_RUNTIME_CHECK(substream
))
627 runtime
= substream
->runtime
;
628 snd_pcm_stream_lock_irq(substream
);
629 switch (runtime
->status
->state
) {
630 case SNDRV_PCM_STATE_SETUP
:
631 case SNDRV_PCM_STATE_PREPARED
:
634 snd_pcm_stream_unlock_irq(substream
);
637 snd_pcm_stream_unlock_irq(substream
);
638 if (atomic_read(&substream
->mmap_count
))
640 if (substream
->ops
->hw_free
)
641 result
= substream
->ops
->hw_free(substream
);
642 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
643 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
647 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
648 struct snd_pcm_sw_params
*params
)
650 struct snd_pcm_runtime
*runtime
;
653 if (PCM_RUNTIME_CHECK(substream
))
655 runtime
= substream
->runtime
;
656 snd_pcm_stream_lock_irq(substream
);
657 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
658 snd_pcm_stream_unlock_irq(substream
);
661 snd_pcm_stream_unlock_irq(substream
);
663 if (params
->tstamp_mode
< 0 ||
664 params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
666 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
667 params
->tstamp_type
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
669 if (params
->avail_min
== 0)
671 if (params
->silence_size
>= runtime
->boundary
) {
672 if (params
->silence_threshold
!= 0)
675 if (params
->silence_size
> params
->silence_threshold
)
677 if (params
->silence_threshold
> runtime
->buffer_size
)
681 snd_pcm_stream_lock_irq(substream
);
682 runtime
->tstamp_mode
= params
->tstamp_mode
;
683 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12))
684 runtime
->tstamp_type
= params
->tstamp_type
;
685 runtime
->period_step
= params
->period_step
;
686 runtime
->control
->avail_min
= params
->avail_min
;
687 runtime
->start_threshold
= params
->start_threshold
;
688 runtime
->stop_threshold
= params
->stop_threshold
;
689 runtime
->silence_threshold
= params
->silence_threshold
;
690 runtime
->silence_size
= params
->silence_size
;
691 params
->boundary
= runtime
->boundary
;
692 if (snd_pcm_running(substream
)) {
693 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
694 runtime
->silence_size
> 0)
695 snd_pcm_playback_silence(substream
, ULONG_MAX
);
696 err
= snd_pcm_update_state(substream
, runtime
);
698 snd_pcm_stream_unlock_irq(substream
);
702 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
703 struct snd_pcm_sw_params __user
* _params
)
705 struct snd_pcm_sw_params params
;
707 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
709 err
= snd_pcm_sw_params(substream
, ¶ms
);
710 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
715 int snd_pcm_status(struct snd_pcm_substream
*substream
,
716 struct snd_pcm_status
*status
)
718 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
720 snd_pcm_stream_lock_irq(substream
);
722 snd_pcm_unpack_audio_tstamp_config(status
->audio_tstamp_data
,
723 &runtime
->audio_tstamp_config
);
725 /* backwards compatible behavior */
726 if (runtime
->audio_tstamp_config
.type_requested
==
727 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT
) {
728 if (runtime
->hw
.info
& SNDRV_PCM_INFO_HAS_WALL_CLOCK
)
729 runtime
->audio_tstamp_config
.type_requested
=
730 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK
;
732 runtime
->audio_tstamp_config
.type_requested
=
733 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
;
734 runtime
->audio_tstamp_report
.valid
= 0;
736 runtime
->audio_tstamp_report
.valid
= 1;
738 status
->state
= runtime
->status
->state
;
739 status
->suspended_state
= runtime
->status
->suspended_state
;
740 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
742 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
743 if (snd_pcm_running(substream
)) {
744 snd_pcm_update_hw_ptr(substream
);
745 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
746 status
->tstamp
= runtime
->status
->tstamp
;
747 status
->driver_tstamp
= runtime
->driver_tstamp
;
748 status
->audio_tstamp
=
749 runtime
->status
->audio_tstamp
;
750 if (runtime
->audio_tstamp_report
.valid
== 1)
751 /* backwards compatibility, no report provided in COMPAT mode */
752 snd_pcm_pack_audio_tstamp_report(&status
->audio_tstamp_data
,
753 &status
->audio_tstamp_accuracy
,
754 &runtime
->audio_tstamp_report
);
759 /* get tstamp only in fallback mode and only if enabled */
760 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
761 snd_pcm_gettime(runtime
, &status
->tstamp
);
764 status
->appl_ptr
= runtime
->control
->appl_ptr
;
765 status
->hw_ptr
= runtime
->status
->hw_ptr
;
766 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
767 status
->avail
= snd_pcm_playback_avail(runtime
);
768 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
769 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
770 status
->delay
= runtime
->buffer_size
- status
->avail
;
771 status
->delay
+= runtime
->delay
;
775 status
->avail
= snd_pcm_capture_avail(runtime
);
776 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
777 status
->delay
= status
->avail
+ runtime
->delay
;
781 status
->avail_max
= runtime
->avail_max
;
782 status
->overrange
= runtime
->overrange
;
783 runtime
->avail_max
= 0;
784 runtime
->overrange
= 0;
786 snd_pcm_stream_unlock_irq(substream
);
790 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
791 struct snd_pcm_status __user
* _status
,
794 struct snd_pcm_status status
;
797 memset(&status
, 0, sizeof(status
));
799 * with extension, parameters are read/write,
800 * get audio_tstamp_data from user,
801 * ignore rest of status structure
803 if (ext
&& get_user(status
.audio_tstamp_data
,
804 (u32 __user
*)(&_status
->audio_tstamp_data
)))
806 res
= snd_pcm_status(substream
, &status
);
809 if (copy_to_user(_status
, &status
, sizeof(status
)))
814 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
815 struct snd_pcm_channel_info
* info
)
817 struct snd_pcm_runtime
*runtime
;
818 unsigned int channel
;
820 channel
= info
->channel
;
821 runtime
= substream
->runtime
;
822 snd_pcm_stream_lock_irq(substream
);
823 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
824 snd_pcm_stream_unlock_irq(substream
);
827 snd_pcm_stream_unlock_irq(substream
);
828 if (channel
>= runtime
->channels
)
830 memset(info
, 0, sizeof(*info
));
831 info
->channel
= channel
;
832 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
835 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
836 struct snd_pcm_channel_info __user
* _info
)
838 struct snd_pcm_channel_info info
;
841 if (copy_from_user(&info
, _info
, sizeof(info
)))
843 res
= snd_pcm_channel_info(substream
, &info
);
846 if (copy_to_user(_info
, &info
, sizeof(info
)))
851 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
853 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
854 if (runtime
->trigger_master
== NULL
)
856 if (runtime
->trigger_master
== substream
) {
857 if (!runtime
->trigger_tstamp_latched
)
858 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
860 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
861 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
863 runtime
->trigger_master
= NULL
;
867 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
868 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
869 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
870 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
874 * this functions is core for handling of linked stream
875 * Note: the stream state might be changed also on failure
876 * Note2: call with calling stream lock + link lock
878 static int snd_pcm_action_group(struct action_ops
*ops
,
879 struct snd_pcm_substream
*substream
,
880 int state
, int do_lock
)
882 struct snd_pcm_substream
*s
= NULL
;
883 struct snd_pcm_substream
*s1
;
884 int res
= 0, depth
= 1;
886 snd_pcm_group_for_each_entry(s
, substream
) {
887 if (do_lock
&& s
!= substream
) {
888 if (s
->pcm
->nonatomic
)
889 mutex_lock_nested(&s
->self_group
.mutex
, depth
);
891 spin_lock_nested(&s
->self_group
.lock
, depth
);
894 res
= ops
->pre_action(s
, state
);
898 snd_pcm_group_for_each_entry(s
, substream
) {
899 res
= ops
->do_action(s
, state
);
901 if (ops
->undo_action
) {
902 snd_pcm_group_for_each_entry(s1
, substream
) {
903 if (s1
== s
) /* failed stream */
905 ops
->undo_action(s1
, state
);
908 s
= NULL
; /* unlock all */
912 snd_pcm_group_for_each_entry(s
, substream
) {
913 ops
->post_action(s
, state
);
918 snd_pcm_group_for_each_entry(s1
, substream
) {
919 if (s1
!= substream
) {
920 if (s1
->pcm
->nonatomic
)
921 mutex_unlock(&s1
->self_group
.mutex
);
923 spin_unlock(&s1
->self_group
.lock
);
925 if (s1
== s
) /* end */
933 * Note: call with stream lock
935 static int snd_pcm_action_single(struct action_ops
*ops
,
936 struct snd_pcm_substream
*substream
,
941 res
= ops
->pre_action(substream
, state
);
944 res
= ops
->do_action(substream
, state
);
946 ops
->post_action(substream
, state
);
947 else if (ops
->undo_action
)
948 ops
->undo_action(substream
, state
);
953 * Note: call with stream lock
955 static int snd_pcm_action(struct action_ops
*ops
,
956 struct snd_pcm_substream
*substream
,
961 if (!snd_pcm_stream_linked(substream
))
962 return snd_pcm_action_single(ops
, substream
, state
);
964 if (substream
->pcm
->nonatomic
) {
965 if (!mutex_trylock(&substream
->group
->mutex
)) {
966 mutex_unlock(&substream
->self_group
.mutex
);
967 mutex_lock(&substream
->group
->mutex
);
968 mutex_lock(&substream
->self_group
.mutex
);
970 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
971 mutex_unlock(&substream
->group
->mutex
);
973 if (!spin_trylock(&substream
->group
->lock
)) {
974 spin_unlock(&substream
->self_group
.lock
);
975 spin_lock(&substream
->group
->lock
);
976 spin_lock(&substream
->self_group
.lock
);
978 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
979 spin_unlock(&substream
->group
->lock
);
985 * Note: don't use any locks before
987 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
988 struct snd_pcm_substream
*substream
,
993 snd_pcm_stream_lock_irq(substream
);
994 res
= snd_pcm_action(ops
, substream
, state
);
995 snd_pcm_stream_unlock_irq(substream
);
1001 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
1002 struct snd_pcm_substream
*substream
,
1007 down_read(&snd_pcm_link_rwsem
);
1008 if (snd_pcm_stream_linked(substream
))
1009 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
1011 res
= snd_pcm_action_single(ops
, substream
, state
);
1012 up_read(&snd_pcm_link_rwsem
);
1019 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
1021 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1022 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
1024 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1025 !snd_pcm_playback_data(substream
))
1027 runtime
->trigger_tstamp_latched
= false;
1028 runtime
->trigger_master
= substream
;
1032 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
1034 if (substream
->runtime
->trigger_master
!= substream
)
1036 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
1039 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
1041 if (substream
->runtime
->trigger_master
== substream
)
1042 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1045 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
1047 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1048 snd_pcm_trigger_tstamp(substream
);
1049 runtime
->hw_ptr_jiffies
= jiffies
;
1050 runtime
->hw_ptr_buffer_jiffies
= (runtime
->buffer_size
* HZ
) /
1052 runtime
->status
->state
= state
;
1053 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1054 runtime
->silence_size
> 0)
1055 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1056 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTART
);
1059 static struct action_ops snd_pcm_action_start
= {
1060 .pre_action
= snd_pcm_pre_start
,
1061 .do_action
= snd_pcm_do_start
,
1062 .undo_action
= snd_pcm_undo_start
,
1063 .post_action
= snd_pcm_post_start
1067 * snd_pcm_start - start all linked streams
1068 * @substream: the PCM substream instance
1070 * Return: Zero if successful, or a negative error code.
1072 int snd_pcm_start(struct snd_pcm_substream
*substream
)
1074 return snd_pcm_action(&snd_pcm_action_start
, substream
,
1075 SNDRV_PCM_STATE_RUNNING
);
1081 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
1083 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1084 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1086 runtime
->trigger_master
= substream
;
1090 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
1092 if (substream
->runtime
->trigger_master
== substream
&&
1093 snd_pcm_running(substream
))
1094 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1095 return 0; /* unconditonally stop all substreams */
1098 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
1100 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1101 if (runtime
->status
->state
!= state
) {
1102 snd_pcm_trigger_tstamp(substream
);
1103 runtime
->status
->state
= state
;
1104 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTOP
);
1106 wake_up(&runtime
->sleep
);
1107 wake_up(&runtime
->tsleep
);
1110 static struct action_ops snd_pcm_action_stop
= {
1111 .pre_action
= snd_pcm_pre_stop
,
1112 .do_action
= snd_pcm_do_stop
,
1113 .post_action
= snd_pcm_post_stop
1117 * snd_pcm_stop - try to stop all running streams in the substream group
1118 * @substream: the PCM substream instance
1119 * @state: PCM state after stopping the stream
1121 * The state of each stream is then changed to the given state unconditionally.
1123 * Return: Zero if successful, or a negative error code.
1125 int snd_pcm_stop(struct snd_pcm_substream
*substream
, snd_pcm_state_t state
)
1127 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
1130 EXPORT_SYMBOL(snd_pcm_stop
);
1133 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1134 * @substream: the PCM substream
1136 * After stopping, the state is changed to SETUP.
1137 * Unlike snd_pcm_stop(), this affects only the given stream.
1139 * Return: Zero if succesful, or a negative error code.
1141 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
1143 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
1144 SNDRV_PCM_STATE_SETUP
);
1148 * snd_pcm_stop_xrun - stop the running streams as XRUN
1149 * @substream: the PCM substream instance
1151 * This stops the given running substream (and all linked substreams) as XRUN.
1152 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1154 * Return: Zero if successful, or a negative error code.
1156 int snd_pcm_stop_xrun(struct snd_pcm_substream
*substream
)
1158 unsigned long flags
;
1161 snd_pcm_stream_lock_irqsave(substream
, flags
);
1162 if (snd_pcm_running(substream
))
1163 ret
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1164 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1167 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun
);
1172 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
1174 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1175 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
1178 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
1180 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
1182 runtime
->trigger_master
= substream
;
1186 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
1188 if (substream
->runtime
->trigger_master
!= substream
)
1190 /* some drivers might use hw_ptr to recover from the pause -
1191 update the hw_ptr now */
1193 snd_pcm_update_hw_ptr(substream
);
1194 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1195 * a delta between the current jiffies, this gives a large enough
1196 * delta, effectively to skip the check once.
1198 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
1199 return substream
->ops
->trigger(substream
,
1200 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1201 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1204 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1206 if (substream
->runtime
->trigger_master
== substream
)
1207 substream
->ops
->trigger(substream
,
1208 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1209 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1212 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1214 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1215 snd_pcm_trigger_tstamp(substream
);
1217 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1218 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MPAUSE
);
1219 wake_up(&runtime
->sleep
);
1220 wake_up(&runtime
->tsleep
);
1222 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1223 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MCONTINUE
);
1227 static struct action_ops snd_pcm_action_pause
= {
1228 .pre_action
= snd_pcm_pre_pause
,
1229 .do_action
= snd_pcm_do_pause
,
1230 .undo_action
= snd_pcm_undo_pause
,
1231 .post_action
= snd_pcm_post_pause
1235 * Push/release the pause for all linked streams.
1237 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1239 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1245 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1247 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1248 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1250 runtime
->trigger_master
= substream
;
1254 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1256 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1257 if (runtime
->trigger_master
!= substream
)
1259 if (! snd_pcm_running(substream
))
1261 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1262 return 0; /* suspend unconditionally */
1265 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1267 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1268 snd_pcm_trigger_tstamp(substream
);
1269 runtime
->status
->suspended_state
= runtime
->status
->state
;
1270 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1271 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSUSPEND
);
1272 wake_up(&runtime
->sleep
);
1273 wake_up(&runtime
->tsleep
);
1276 static struct action_ops snd_pcm_action_suspend
= {
1277 .pre_action
= snd_pcm_pre_suspend
,
1278 .do_action
= snd_pcm_do_suspend
,
1279 .post_action
= snd_pcm_post_suspend
1283 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1284 * @substream: the PCM substream
1286 * After this call, all streams are changed to SUSPENDED state.
1288 * Return: Zero if successful (or @substream is %NULL), or a negative error
1291 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1294 unsigned long flags
;
1299 snd_pcm_stream_lock_irqsave(substream
, flags
);
1300 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1301 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1305 EXPORT_SYMBOL(snd_pcm_suspend
);
1308 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1309 * @pcm: the PCM instance
1311 * After this call, all streams are changed to SUSPENDED state.
1313 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1315 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1317 struct snd_pcm_substream
*substream
;
1318 int stream
, err
= 0;
1323 for (stream
= 0; stream
< 2; stream
++) {
1324 for (substream
= pcm
->streams
[stream
].substream
;
1325 substream
; substream
= substream
->next
) {
1326 /* FIXME: the open/close code should lock this as well */
1327 if (substream
->runtime
== NULL
)
1329 err
= snd_pcm_suspend(substream
);
1330 if (err
< 0 && err
!= -EBUSY
)
1337 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1341 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1343 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1344 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1346 runtime
->trigger_master
= substream
;
1350 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1352 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1353 if (runtime
->trigger_master
!= substream
)
1355 /* DMA not running previously? */
1356 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1357 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1358 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1360 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1363 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1365 if (substream
->runtime
->trigger_master
== substream
&&
1366 snd_pcm_running(substream
))
1367 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1370 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1372 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1373 snd_pcm_trigger_tstamp(substream
);
1374 runtime
->status
->state
= runtime
->status
->suspended_state
;
1375 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MRESUME
);
1378 static struct action_ops snd_pcm_action_resume
= {
1379 .pre_action
= snd_pcm_pre_resume
,
1380 .do_action
= snd_pcm_do_resume
,
1381 .undo_action
= snd_pcm_undo_resume
,
1382 .post_action
= snd_pcm_post_resume
1385 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1387 struct snd_card
*card
= substream
->pcm
->card
;
1390 snd_power_lock(card
);
1391 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1392 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1393 snd_power_unlock(card
);
1399 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1404 #endif /* CONFIG_PM */
1409 * Change the RUNNING stream(s) to XRUN state.
1411 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1413 struct snd_card
*card
= substream
->pcm
->card
;
1414 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1417 snd_power_lock(card
);
1418 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1419 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1424 snd_pcm_stream_lock_irq(substream
);
1425 switch (runtime
->status
->state
) {
1426 case SNDRV_PCM_STATE_XRUN
:
1427 result
= 0; /* already there */
1429 case SNDRV_PCM_STATE_RUNNING
:
1430 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1435 snd_pcm_stream_unlock_irq(substream
);
1437 snd_power_unlock(card
);
1444 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1446 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1447 switch (runtime
->status
->state
) {
1448 case SNDRV_PCM_STATE_RUNNING
:
1449 case SNDRV_PCM_STATE_PREPARED
:
1450 case SNDRV_PCM_STATE_PAUSED
:
1451 case SNDRV_PCM_STATE_SUSPENDED
:
1458 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1460 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1461 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1464 runtime
->hw_ptr_base
= 0;
1465 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1466 runtime
->status
->hw_ptr
% runtime
->period_size
;
1467 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1468 runtime
->silence_filled
= 0;
1472 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1474 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1475 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1476 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1477 runtime
->silence_size
> 0)
1478 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1481 static struct action_ops snd_pcm_action_reset
= {
1482 .pre_action
= snd_pcm_pre_reset
,
1483 .do_action
= snd_pcm_do_reset
,
1484 .post_action
= snd_pcm_post_reset
1487 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1489 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1495 /* we use the second argument for updating f_flags */
1496 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1499 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1500 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1501 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1503 if (snd_pcm_running(substream
))
1505 substream
->f_flags
= f_flags
;
1509 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1512 err
= substream
->ops
->prepare(substream
);
1515 return snd_pcm_do_reset(substream
, 0);
1518 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1520 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1521 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1522 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_PREPARED
);
1525 static struct action_ops snd_pcm_action_prepare
= {
1526 .pre_action
= snd_pcm_pre_prepare
,
1527 .do_action
= snd_pcm_do_prepare
,
1528 .post_action
= snd_pcm_post_prepare
1532 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1533 * @substream: the PCM substream instance
1534 * @file: file to refer f_flags
1536 * Return: Zero if successful, or a negative error code.
1538 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1542 struct snd_card
*card
= substream
->pcm
->card
;
1546 f_flags
= file
->f_flags
;
1548 f_flags
= substream
->f_flags
;
1550 snd_power_lock(card
);
1551 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1552 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1553 substream
, f_flags
);
1554 snd_power_unlock(card
);
1562 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1564 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1565 switch (runtime
->status
->state
) {
1566 case SNDRV_PCM_STATE_OPEN
:
1567 case SNDRV_PCM_STATE_DISCONNECTED
:
1568 case SNDRV_PCM_STATE_SUSPENDED
:
1571 runtime
->trigger_master
= substream
;
1575 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1577 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1578 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1579 switch (runtime
->status
->state
) {
1580 case SNDRV_PCM_STATE_PREPARED
:
1581 /* start playback stream if possible */
1582 if (! snd_pcm_playback_empty(substream
)) {
1583 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1584 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1586 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1589 case SNDRV_PCM_STATE_RUNNING
:
1590 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1592 case SNDRV_PCM_STATE_XRUN
:
1593 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1599 /* stop running stream */
1600 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1601 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1602 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1603 snd_pcm_do_stop(substream
, new_state
);
1604 snd_pcm_post_stop(substream
, new_state
);
1608 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
1609 runtime
->trigger_master
== substream
&&
1610 (runtime
->hw
.info
& SNDRV_PCM_INFO_DRAIN_TRIGGER
))
1611 return substream
->ops
->trigger(substream
,
1612 SNDRV_PCM_TRIGGER_DRAIN
);
1617 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1621 static struct action_ops snd_pcm_action_drain_init
= {
1622 .pre_action
= snd_pcm_pre_drain_init
,
1623 .do_action
= snd_pcm_do_drain_init
,
1624 .post_action
= snd_pcm_post_drain_init
1627 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1630 * Drain the stream(s).
1631 * When the substream is linked, sync until the draining of all playback streams
1633 * After this call, all streams are supposed to be either SETUP or DRAINING
1634 * (capture only) state.
1636 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1639 struct snd_card
*card
;
1640 struct snd_pcm_runtime
*runtime
;
1641 struct snd_pcm_substream
*s
;
1646 card
= substream
->pcm
->card
;
1647 runtime
= substream
->runtime
;
1649 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1652 snd_power_lock(card
);
1653 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1654 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1656 snd_power_unlock(card
);
1662 if (file
->f_flags
& O_NONBLOCK
)
1664 } else if (substream
->f_flags
& O_NONBLOCK
)
1667 down_read(&snd_pcm_link_rwsem
);
1668 snd_pcm_stream_lock_irq(substream
);
1670 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1671 snd_pcm_pause(substream
, 0);
1673 /* pre-start/stop - all running streams are changed to DRAINING state */
1674 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1677 /* in non-blocking, we don't wait in ioctl but let caller poll */
1685 struct snd_pcm_runtime
*to_check
;
1686 if (signal_pending(current
)) {
1687 result
= -ERESTARTSYS
;
1690 /* find a substream to drain */
1692 snd_pcm_group_for_each_entry(s
, substream
) {
1693 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1695 runtime
= s
->runtime
;
1696 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1702 break; /* all drained */
1703 init_waitqueue_entry(&wait
, current
);
1704 add_wait_queue(&to_check
->sleep
, &wait
);
1705 snd_pcm_stream_unlock_irq(substream
);
1706 up_read(&snd_pcm_link_rwsem
);
1707 snd_power_unlock(card
);
1708 if (runtime
->no_period_wakeup
)
1709 tout
= MAX_SCHEDULE_TIMEOUT
;
1712 if (runtime
->rate
) {
1713 long t
= runtime
->period_size
* 2 / runtime
->rate
;
1714 tout
= max(t
, tout
);
1716 tout
= msecs_to_jiffies(tout
* 1000);
1718 tout
= schedule_timeout_interruptible(tout
);
1719 snd_power_lock(card
);
1720 down_read(&snd_pcm_link_rwsem
);
1721 snd_pcm_stream_lock_irq(substream
);
1722 remove_wait_queue(&to_check
->sleep
, &wait
);
1723 if (card
->shutdown
) {
1728 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1731 dev_dbg(substream
->pcm
->card
->dev
,
1732 "playback drain error (DMA or IRQ trouble?)\n");
1733 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1741 snd_pcm_stream_unlock_irq(substream
);
1742 up_read(&snd_pcm_link_rwsem
);
1743 snd_power_unlock(card
);
1751 * Immediately put all linked substreams into SETUP state.
1753 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1755 struct snd_pcm_runtime
*runtime
;
1758 if (PCM_RUNTIME_CHECK(substream
))
1760 runtime
= substream
->runtime
;
1762 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1763 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
||
1764 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1767 snd_pcm_stream_lock_irq(substream
);
1769 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1770 snd_pcm_pause(substream
, 0);
1772 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1773 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1774 snd_pcm_stream_unlock_irq(substream
);
1780 static bool is_pcm_file(struct file
*file
)
1782 struct inode
*inode
= file_inode(file
);
1785 if (!S_ISCHR(inode
->i_mode
) || imajor(inode
) != snd_major
)
1787 minor
= iminor(inode
);
1788 return snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) ||
1789 snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
1795 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1798 struct snd_pcm_file
*pcm_file
;
1799 struct snd_pcm_substream
*substream1
;
1800 struct snd_pcm_group
*group
;
1801 struct fd f
= fdget(fd
);
1805 if (!is_pcm_file(f
.file
)) {
1809 pcm_file
= f
.file
->private_data
;
1810 substream1
= pcm_file
->substream
;
1811 group
= kmalloc(sizeof(*group
), GFP_KERNEL
);
1816 down_write(&snd_pcm_link_rwsem
);
1817 write_lock_irq(&snd_pcm_link_rwlock
);
1818 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1819 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
||
1820 substream
->pcm
->nonatomic
!= substream1
->pcm
->nonatomic
) {
1824 if (snd_pcm_stream_linked(substream1
)) {
1828 if (!snd_pcm_stream_linked(substream
)) {
1829 substream
->group
= group
;
1831 spin_lock_init(&substream
->group
->lock
);
1832 mutex_init(&substream
->group
->mutex
);
1833 INIT_LIST_HEAD(&substream
->group
->substreams
);
1834 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1835 substream
->group
->count
= 1;
1837 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1838 substream
->group
->count
++;
1839 substream1
->group
= substream
->group
;
1841 write_unlock_irq(&snd_pcm_link_rwlock
);
1842 up_write(&snd_pcm_link_rwsem
);
1844 snd_card_unref(substream1
->pcm
->card
);
1851 static void relink_to_local(struct snd_pcm_substream
*substream
)
1853 substream
->group
= &substream
->self_group
;
1854 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1855 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1858 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1860 struct snd_pcm_substream
*s
;
1863 down_write(&snd_pcm_link_rwsem
);
1864 write_lock_irq(&snd_pcm_link_rwlock
);
1865 if (!snd_pcm_stream_linked(substream
)) {
1869 list_del(&substream
->link_list
);
1870 substream
->group
->count
--;
1871 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1872 snd_pcm_group_for_each_entry(s
, substream
) {
1876 kfree(substream
->group
);
1878 relink_to_local(substream
);
1880 write_unlock_irq(&snd_pcm_link_rwlock
);
1881 up_write(&snd_pcm_link_rwsem
);
1888 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1889 struct snd_pcm_hw_rule
*rule
)
1891 struct snd_interval t
;
1892 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1893 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1894 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1897 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1898 struct snd_pcm_hw_rule
*rule
)
1900 struct snd_interval t
;
1901 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1902 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1903 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1906 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1907 struct snd_pcm_hw_rule
*rule
)
1909 struct snd_interval t
;
1910 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1911 hw_param_interval_c(params
, rule
->deps
[1]),
1912 (unsigned long) rule
->private, &t
);
1913 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1916 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1917 struct snd_pcm_hw_rule
*rule
)
1919 struct snd_interval t
;
1920 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1921 (unsigned long) rule
->private,
1922 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1923 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1926 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1927 struct snd_pcm_hw_rule
*rule
)
1930 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1932 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1934 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1936 if (! snd_mask_test(mask
, k
))
1938 bits
= snd_pcm_format_physical_width(k
);
1940 continue; /* ignore invalid formats */
1941 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1942 snd_mask_reset(&m
, k
);
1944 return snd_mask_refine(mask
, &m
);
1947 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1948 struct snd_pcm_hw_rule
*rule
)
1950 struct snd_interval t
;
1956 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1958 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1960 bits
= snd_pcm_format_physical_width(k
);
1962 continue; /* ignore invalid formats */
1963 if (t
.min
> (unsigned)bits
)
1965 if (t
.max
< (unsigned)bits
)
1969 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1972 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1973 #error "Change this table"
1976 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1977 48000, 64000, 88200, 96000, 176400, 192000 };
1979 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
1980 .count
= ARRAY_SIZE(rates
),
1984 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1985 struct snd_pcm_hw_rule
*rule
)
1987 struct snd_pcm_hardware
*hw
= rule
->private;
1988 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1989 snd_pcm_known_rates
.count
,
1990 snd_pcm_known_rates
.list
, hw
->rates
);
1993 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1994 struct snd_pcm_hw_rule
*rule
)
1996 struct snd_interval t
;
1997 struct snd_pcm_substream
*substream
= rule
->private;
1999 t
.max
= substream
->buffer_bytes_max
;
2003 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2006 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
2008 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2009 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
2012 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
2013 snd_mask_any(constrs_mask(constrs
, k
));
2016 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
2017 snd_interval_any(constrs_interval(constrs
, k
));
2020 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
2021 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
2022 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
2023 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
2024 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
2026 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
2027 snd_pcm_hw_rule_format
, NULL
,
2028 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2031 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2032 snd_pcm_hw_rule_sample_bits
, NULL
,
2033 SNDRV_PCM_HW_PARAM_FORMAT
,
2034 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2037 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2038 snd_pcm_hw_rule_div
, NULL
,
2039 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2042 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2043 snd_pcm_hw_rule_mul
, NULL
,
2044 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2047 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2048 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2049 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2052 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2053 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2054 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
2057 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
2058 snd_pcm_hw_rule_div
, NULL
,
2059 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2062 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2063 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2064 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
2067 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2068 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2069 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
2072 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
2073 snd_pcm_hw_rule_div
, NULL
,
2074 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2077 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2078 snd_pcm_hw_rule_div
, NULL
,
2079 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2082 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2083 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2084 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2087 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2088 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2089 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2092 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2093 snd_pcm_hw_rule_mul
, NULL
,
2094 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2097 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2098 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2099 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2102 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2103 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2104 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2107 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2108 snd_pcm_hw_rule_muldivk
, (void*) 8,
2109 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2112 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2113 snd_pcm_hw_rule_muldivk
, (void*) 8,
2114 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2117 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
2118 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2119 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2122 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
2123 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2124 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2130 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
2132 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2133 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
2135 unsigned int mask
= 0;
2137 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2138 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
2139 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2140 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
2141 if (hw_support_mmap(substream
)) {
2142 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2143 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2144 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2145 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
2146 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
2147 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
2149 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
2153 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
2157 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
2161 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
2162 hw
->channels_min
, hw
->channels_max
);
2166 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
2167 hw
->rate_min
, hw
->rate_max
);
2171 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2172 hw
->period_bytes_min
, hw
->period_bytes_max
);
2176 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
2177 hw
->periods_min
, hw
->periods_max
);
2181 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2182 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
2186 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2187 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
2188 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
2193 if (runtime
->dma_bytes
) {
2194 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
2199 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2200 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2201 snd_pcm_hw_rule_rate
, hw
,
2202 SNDRV_PCM_HW_PARAM_RATE
, -1);
2207 /* FIXME: this belong to lowlevel */
2208 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2213 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2215 snd_pcm_unlink(substream
);
2218 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2220 substream
->ref_count
--;
2221 if (substream
->ref_count
> 0)
2224 snd_pcm_drop(substream
);
2225 if (substream
->hw_opened
) {
2226 if (substream
->ops
->hw_free
&&
2227 substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
)
2228 substream
->ops
->hw_free(substream
);
2229 substream
->ops
->close(substream
);
2230 substream
->hw_opened
= 0;
2232 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
2233 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
2234 if (substream
->pcm_release
) {
2235 substream
->pcm_release(substream
);
2236 substream
->pcm_release
= NULL
;
2238 snd_pcm_detach_substream(substream
);
2241 EXPORT_SYMBOL(snd_pcm_release_substream
);
2243 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2245 struct snd_pcm_substream
**rsubstream
)
2247 struct snd_pcm_substream
*substream
;
2250 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2253 if (substream
->ref_count
> 1) {
2254 *rsubstream
= substream
;
2258 err
= snd_pcm_hw_constraints_init(substream
);
2260 pcm_dbg(pcm
, "snd_pcm_hw_constraints_init failed\n");
2264 if ((err
= substream
->ops
->open(substream
)) < 0)
2267 substream
->hw_opened
= 1;
2269 err
= snd_pcm_hw_constraints_complete(substream
);
2271 pcm_dbg(pcm
, "snd_pcm_hw_constraints_complete failed\n");
2275 *rsubstream
= substream
;
2279 snd_pcm_release_substream(substream
);
2283 EXPORT_SYMBOL(snd_pcm_open_substream
);
2285 static int snd_pcm_open_file(struct file
*file
,
2286 struct snd_pcm
*pcm
,
2289 struct snd_pcm_file
*pcm_file
;
2290 struct snd_pcm_substream
*substream
;
2293 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2297 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2298 if (pcm_file
== NULL
) {
2299 snd_pcm_release_substream(substream
);
2302 pcm_file
->substream
= substream
;
2303 if (substream
->ref_count
== 1) {
2304 substream
->file
= pcm_file
;
2305 substream
->pcm_release
= pcm_release_private
;
2307 file
->private_data
= pcm_file
;
2312 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2314 struct snd_pcm
*pcm
;
2315 int err
= nonseekable_open(inode
, file
);
2318 pcm
= snd_lookup_minor_data(iminor(inode
),
2319 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2320 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2322 snd_card_unref(pcm
->card
);
2326 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2328 struct snd_pcm
*pcm
;
2329 int err
= nonseekable_open(inode
, file
);
2332 pcm
= snd_lookup_minor_data(iminor(inode
),
2333 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2334 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2336 snd_card_unref(pcm
->card
);
2340 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2349 err
= snd_card_file_add(pcm
->card
, file
);
2352 if (!try_module_get(pcm
->card
->module
)) {
2356 init_waitqueue_entry(&wait
, current
);
2357 add_wait_queue(&pcm
->open_wait
, &wait
);
2358 mutex_lock(&pcm
->open_mutex
);
2360 err
= snd_pcm_open_file(file
, pcm
, stream
);
2363 if (err
== -EAGAIN
) {
2364 if (file
->f_flags
& O_NONBLOCK
) {
2370 set_current_state(TASK_INTERRUPTIBLE
);
2371 mutex_unlock(&pcm
->open_mutex
);
2373 mutex_lock(&pcm
->open_mutex
);
2374 if (pcm
->card
->shutdown
) {
2378 if (signal_pending(current
)) {
2383 remove_wait_queue(&pcm
->open_wait
, &wait
);
2384 mutex_unlock(&pcm
->open_mutex
);
2390 module_put(pcm
->card
->module
);
2392 snd_card_file_remove(pcm
->card
, file
);
2397 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2399 struct snd_pcm
*pcm
;
2400 struct snd_pcm_substream
*substream
;
2401 struct snd_pcm_file
*pcm_file
;
2403 pcm_file
= file
->private_data
;
2404 substream
= pcm_file
->substream
;
2405 if (snd_BUG_ON(!substream
))
2407 pcm
= substream
->pcm
;
2408 mutex_lock(&pcm
->open_mutex
);
2409 snd_pcm_release_substream(substream
);
2411 mutex_unlock(&pcm
->open_mutex
);
2412 wake_up(&pcm
->open_wait
);
2413 module_put(pcm
->card
->module
);
2414 snd_card_file_remove(pcm
->card
, file
);
2418 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2419 snd_pcm_uframes_t frames
)
2421 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2422 snd_pcm_sframes_t appl_ptr
;
2423 snd_pcm_sframes_t ret
;
2424 snd_pcm_sframes_t hw_avail
;
2429 snd_pcm_stream_lock_irq(substream
);
2430 switch (runtime
->status
->state
) {
2431 case SNDRV_PCM_STATE_PREPARED
:
2433 case SNDRV_PCM_STATE_DRAINING
:
2434 case SNDRV_PCM_STATE_RUNNING
:
2435 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2438 case SNDRV_PCM_STATE_XRUN
:
2441 case SNDRV_PCM_STATE_SUSPENDED
:
2449 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2450 if (hw_avail
<= 0) {
2454 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2456 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2458 appl_ptr
+= runtime
->boundary
;
2459 runtime
->control
->appl_ptr
= appl_ptr
;
2462 snd_pcm_stream_unlock_irq(substream
);
2466 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2467 snd_pcm_uframes_t frames
)
2469 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2470 snd_pcm_sframes_t appl_ptr
;
2471 snd_pcm_sframes_t ret
;
2472 snd_pcm_sframes_t hw_avail
;
2477 snd_pcm_stream_lock_irq(substream
);
2478 switch (runtime
->status
->state
) {
2479 case SNDRV_PCM_STATE_PREPARED
:
2480 case SNDRV_PCM_STATE_DRAINING
:
2482 case SNDRV_PCM_STATE_RUNNING
:
2483 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2486 case SNDRV_PCM_STATE_XRUN
:
2489 case SNDRV_PCM_STATE_SUSPENDED
:
2497 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2498 if (hw_avail
<= 0) {
2502 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2504 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2506 appl_ptr
+= runtime
->boundary
;
2507 runtime
->control
->appl_ptr
= appl_ptr
;
2510 snd_pcm_stream_unlock_irq(substream
);
2514 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2515 snd_pcm_uframes_t frames
)
2517 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2518 snd_pcm_sframes_t appl_ptr
;
2519 snd_pcm_sframes_t ret
;
2520 snd_pcm_sframes_t avail
;
2525 snd_pcm_stream_lock_irq(substream
);
2526 switch (runtime
->status
->state
) {
2527 case SNDRV_PCM_STATE_PREPARED
:
2528 case SNDRV_PCM_STATE_PAUSED
:
2530 case SNDRV_PCM_STATE_DRAINING
:
2531 case SNDRV_PCM_STATE_RUNNING
:
2532 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2535 case SNDRV_PCM_STATE_XRUN
:
2538 case SNDRV_PCM_STATE_SUSPENDED
:
2546 avail
= snd_pcm_playback_avail(runtime
);
2551 if (frames
> (snd_pcm_uframes_t
)avail
)
2553 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2554 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2555 appl_ptr
-= runtime
->boundary
;
2556 runtime
->control
->appl_ptr
= appl_ptr
;
2559 snd_pcm_stream_unlock_irq(substream
);
2563 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2564 snd_pcm_uframes_t frames
)
2566 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2567 snd_pcm_sframes_t appl_ptr
;
2568 snd_pcm_sframes_t ret
;
2569 snd_pcm_sframes_t avail
;
2574 snd_pcm_stream_lock_irq(substream
);
2575 switch (runtime
->status
->state
) {
2576 case SNDRV_PCM_STATE_PREPARED
:
2577 case SNDRV_PCM_STATE_DRAINING
:
2578 case SNDRV_PCM_STATE_PAUSED
:
2580 case SNDRV_PCM_STATE_RUNNING
:
2581 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2584 case SNDRV_PCM_STATE_XRUN
:
2587 case SNDRV_PCM_STATE_SUSPENDED
:
2595 avail
= snd_pcm_capture_avail(runtime
);
2600 if (frames
> (snd_pcm_uframes_t
)avail
)
2602 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2603 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2604 appl_ptr
-= runtime
->boundary
;
2605 runtime
->control
->appl_ptr
= appl_ptr
;
2608 snd_pcm_stream_unlock_irq(substream
);
2612 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2614 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2617 snd_pcm_stream_lock_irq(substream
);
2618 switch (runtime
->status
->state
) {
2619 case SNDRV_PCM_STATE_DRAINING
:
2620 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2623 case SNDRV_PCM_STATE_RUNNING
:
2624 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2627 case SNDRV_PCM_STATE_PREPARED
:
2628 case SNDRV_PCM_STATE_SUSPENDED
:
2631 case SNDRV_PCM_STATE_XRUN
:
2639 snd_pcm_stream_unlock_irq(substream
);
2643 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2644 snd_pcm_sframes_t __user
*res
)
2646 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2648 snd_pcm_sframes_t n
= 0;
2650 snd_pcm_stream_lock_irq(substream
);
2651 switch (runtime
->status
->state
) {
2652 case SNDRV_PCM_STATE_DRAINING
:
2653 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2656 case SNDRV_PCM_STATE_RUNNING
:
2657 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2660 case SNDRV_PCM_STATE_PREPARED
:
2661 case SNDRV_PCM_STATE_SUSPENDED
:
2663 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2664 n
= snd_pcm_playback_hw_avail(runtime
);
2666 n
= snd_pcm_capture_avail(runtime
);
2667 n
+= runtime
->delay
;
2669 case SNDRV_PCM_STATE_XRUN
:
2677 snd_pcm_stream_unlock_irq(substream
);
2679 if (put_user(n
, res
))
2684 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2685 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2687 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2688 struct snd_pcm_sync_ptr sync_ptr
;
2689 volatile struct snd_pcm_mmap_status
*status
;
2690 volatile struct snd_pcm_mmap_control
*control
;
2693 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2694 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2696 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2698 status
= runtime
->status
;
2699 control
= runtime
->control
;
2700 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2701 err
= snd_pcm_hwsync(substream
);
2705 snd_pcm_stream_lock_irq(substream
);
2706 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2707 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2709 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2710 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2711 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2713 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2714 sync_ptr
.s
.status
.state
= status
->state
;
2715 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2716 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2717 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2718 snd_pcm_stream_unlock_irq(substream
);
2719 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2724 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2726 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2729 if (get_user(arg
, _arg
))
2731 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2733 runtime
->tstamp_type
= arg
;
2737 static int snd_pcm_common_ioctl1(struct file
*file
,
2738 struct snd_pcm_substream
*substream
,
2739 unsigned int cmd
, void __user
*arg
)
2742 case SNDRV_PCM_IOCTL_PVERSION
:
2743 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2744 case SNDRV_PCM_IOCTL_INFO
:
2745 return snd_pcm_info_user(substream
, arg
);
2746 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2748 case SNDRV_PCM_IOCTL_TTSTAMP
:
2749 return snd_pcm_tstamp(substream
, arg
);
2750 case SNDRV_PCM_IOCTL_HW_REFINE
:
2751 return snd_pcm_hw_refine_user(substream
, arg
);
2752 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2753 return snd_pcm_hw_params_user(substream
, arg
);
2754 case SNDRV_PCM_IOCTL_HW_FREE
:
2755 return snd_pcm_hw_free(substream
);
2756 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2757 return snd_pcm_sw_params_user(substream
, arg
);
2758 case SNDRV_PCM_IOCTL_STATUS
:
2759 return snd_pcm_status_user(substream
, arg
, false);
2760 case SNDRV_PCM_IOCTL_STATUS_EXT
:
2761 return snd_pcm_status_user(substream
, arg
, true);
2762 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2763 return snd_pcm_channel_info_user(substream
, arg
);
2764 case SNDRV_PCM_IOCTL_PREPARE
:
2765 return snd_pcm_prepare(substream
, file
);
2766 case SNDRV_PCM_IOCTL_RESET
:
2767 return snd_pcm_reset(substream
);
2768 case SNDRV_PCM_IOCTL_START
:
2769 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2770 case SNDRV_PCM_IOCTL_LINK
:
2771 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2772 case SNDRV_PCM_IOCTL_UNLINK
:
2773 return snd_pcm_unlink(substream
);
2774 case SNDRV_PCM_IOCTL_RESUME
:
2775 return snd_pcm_resume(substream
);
2776 case SNDRV_PCM_IOCTL_XRUN
:
2777 return snd_pcm_xrun(substream
);
2778 case SNDRV_PCM_IOCTL_HWSYNC
:
2779 return snd_pcm_hwsync(substream
);
2780 case SNDRV_PCM_IOCTL_DELAY
:
2781 return snd_pcm_delay(substream
, arg
);
2782 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2783 return snd_pcm_sync_ptr(substream
, arg
);
2784 #ifdef CONFIG_SND_SUPPORT_OLD_API
2785 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2786 return snd_pcm_hw_refine_old_user(substream
, arg
);
2787 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2788 return snd_pcm_hw_params_old_user(substream
, arg
);
2790 case SNDRV_PCM_IOCTL_DRAIN
:
2791 return snd_pcm_drain(substream
, file
);
2792 case SNDRV_PCM_IOCTL_DROP
:
2793 return snd_pcm_drop(substream
);
2794 case SNDRV_PCM_IOCTL_PAUSE
:
2797 snd_pcm_stream_lock_irq(substream
);
2798 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2799 snd_pcm_stream_unlock_irq(substream
);
2803 pcm_dbg(substream
->pcm
, "unknown ioctl = 0x%x\n", cmd
);
2807 static int snd_pcm_playback_ioctl1(struct file
*file
,
2808 struct snd_pcm_substream
*substream
,
2809 unsigned int cmd
, void __user
*arg
)
2811 if (snd_BUG_ON(!substream
))
2813 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
2816 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2818 struct snd_xferi xferi
;
2819 struct snd_xferi __user
*_xferi
= arg
;
2820 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2821 snd_pcm_sframes_t result
;
2822 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2824 if (put_user(0, &_xferi
->result
))
2826 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2828 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2829 __put_user(result
, &_xferi
->result
);
2830 return result
< 0 ? result
: 0;
2832 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2834 struct snd_xfern xfern
;
2835 struct snd_xfern __user
*_xfern
= arg
;
2836 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2838 snd_pcm_sframes_t result
;
2839 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2841 if (runtime
->channels
> 128)
2843 if (put_user(0, &_xfern
->result
))
2845 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2848 bufs
= memdup_user(xfern
.bufs
,
2849 sizeof(void *) * runtime
->channels
);
2851 return PTR_ERR(bufs
);
2852 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2854 __put_user(result
, &_xfern
->result
);
2855 return result
< 0 ? result
: 0;
2857 case SNDRV_PCM_IOCTL_REWIND
:
2859 snd_pcm_uframes_t frames
;
2860 snd_pcm_uframes_t __user
*_frames
= arg
;
2861 snd_pcm_sframes_t result
;
2862 if (get_user(frames
, _frames
))
2864 if (put_user(0, _frames
))
2866 result
= snd_pcm_playback_rewind(substream
, frames
);
2867 __put_user(result
, _frames
);
2868 return result
< 0 ? result
: 0;
2870 case SNDRV_PCM_IOCTL_FORWARD
:
2872 snd_pcm_uframes_t frames
;
2873 snd_pcm_uframes_t __user
*_frames
= arg
;
2874 snd_pcm_sframes_t result
;
2875 if (get_user(frames
, _frames
))
2877 if (put_user(0, _frames
))
2879 result
= snd_pcm_playback_forward(substream
, frames
);
2880 __put_user(result
, _frames
);
2881 return result
< 0 ? result
: 0;
2884 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2887 static int snd_pcm_capture_ioctl1(struct file
*file
,
2888 struct snd_pcm_substream
*substream
,
2889 unsigned int cmd
, void __user
*arg
)
2891 if (snd_BUG_ON(!substream
))
2893 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
))
2896 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2898 struct snd_xferi xferi
;
2899 struct snd_xferi __user
*_xferi
= arg
;
2900 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2901 snd_pcm_sframes_t result
;
2902 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2904 if (put_user(0, &_xferi
->result
))
2906 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2908 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2909 __put_user(result
, &_xferi
->result
);
2910 return result
< 0 ? result
: 0;
2912 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2914 struct snd_xfern xfern
;
2915 struct snd_xfern __user
*_xfern
= arg
;
2916 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2918 snd_pcm_sframes_t result
;
2919 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2921 if (runtime
->channels
> 128)
2923 if (put_user(0, &_xfern
->result
))
2925 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2928 bufs
= memdup_user(xfern
.bufs
,
2929 sizeof(void *) * runtime
->channels
);
2931 return PTR_ERR(bufs
);
2932 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2934 __put_user(result
, &_xfern
->result
);
2935 return result
< 0 ? result
: 0;
2937 case SNDRV_PCM_IOCTL_REWIND
:
2939 snd_pcm_uframes_t frames
;
2940 snd_pcm_uframes_t __user
*_frames
= arg
;
2941 snd_pcm_sframes_t result
;
2942 if (get_user(frames
, _frames
))
2944 if (put_user(0, _frames
))
2946 result
= snd_pcm_capture_rewind(substream
, frames
);
2947 __put_user(result
, _frames
);
2948 return result
< 0 ? result
: 0;
2950 case SNDRV_PCM_IOCTL_FORWARD
:
2952 snd_pcm_uframes_t frames
;
2953 snd_pcm_uframes_t __user
*_frames
= arg
;
2954 snd_pcm_sframes_t result
;
2955 if (get_user(frames
, _frames
))
2957 if (put_user(0, _frames
))
2959 result
= snd_pcm_capture_forward(substream
, frames
);
2960 __put_user(result
, _frames
);
2961 return result
< 0 ? result
: 0;
2964 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2967 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2970 struct snd_pcm_file
*pcm_file
;
2972 pcm_file
= file
->private_data
;
2974 if (((cmd
>> 8) & 0xff) != 'A')
2977 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2978 (void __user
*)arg
);
2981 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2984 struct snd_pcm_file
*pcm_file
;
2986 pcm_file
= file
->private_data
;
2988 if (((cmd
>> 8) & 0xff) != 'A')
2991 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2992 (void __user
*)arg
);
2995 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2996 unsigned int cmd
, void *arg
)
3001 fs
= snd_enter_user();
3002 switch (substream
->stream
) {
3003 case SNDRV_PCM_STREAM_PLAYBACK
:
3004 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
3005 (void __user
*)arg
);
3007 case SNDRV_PCM_STREAM_CAPTURE
:
3008 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
3009 (void __user
*)arg
);
3019 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
3021 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
3024 struct snd_pcm_file
*pcm_file
;
3025 struct snd_pcm_substream
*substream
;
3026 struct snd_pcm_runtime
*runtime
;
3027 snd_pcm_sframes_t result
;
3029 pcm_file
= file
->private_data
;
3030 substream
= pcm_file
->substream
;
3031 if (PCM_RUNTIME_CHECK(substream
))
3033 runtime
= substream
->runtime
;
3034 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3036 if (!frame_aligned(runtime
, count
))
3038 count
= bytes_to_frames(runtime
, count
);
3039 result
= snd_pcm_lib_read(substream
, buf
, count
);
3041 result
= frames_to_bytes(runtime
, result
);
3045 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
3046 size_t count
, loff_t
* offset
)
3048 struct snd_pcm_file
*pcm_file
;
3049 struct snd_pcm_substream
*substream
;
3050 struct snd_pcm_runtime
*runtime
;
3051 snd_pcm_sframes_t result
;
3053 pcm_file
= file
->private_data
;
3054 substream
= pcm_file
->substream
;
3055 if (PCM_RUNTIME_CHECK(substream
))
3057 runtime
= substream
->runtime
;
3058 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3060 if (!frame_aligned(runtime
, count
))
3062 count
= bytes_to_frames(runtime
, count
);
3063 result
= snd_pcm_lib_write(substream
, buf
, count
);
3065 result
= frames_to_bytes(runtime
, result
);
3069 static ssize_t
snd_pcm_readv(struct kiocb
*iocb
, struct iov_iter
*to
)
3071 struct snd_pcm_file
*pcm_file
;
3072 struct snd_pcm_substream
*substream
;
3073 struct snd_pcm_runtime
*runtime
;
3074 snd_pcm_sframes_t result
;
3077 snd_pcm_uframes_t frames
;
3079 pcm_file
= iocb
->ki_filp
->private_data
;
3080 substream
= pcm_file
->substream
;
3081 if (PCM_RUNTIME_CHECK(substream
))
3083 runtime
= substream
->runtime
;
3084 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3086 if (!iter_is_iovec(to
))
3088 if (to
->nr_segs
> 1024 || to
->nr_segs
!= runtime
->channels
)
3090 if (!frame_aligned(runtime
, to
->iov
->iov_len
))
3092 frames
= bytes_to_samples(runtime
, to
->iov
->iov_len
);
3093 bufs
= kmalloc(sizeof(void *) * to
->nr_segs
, GFP_KERNEL
);
3096 for (i
= 0; i
< to
->nr_segs
; ++i
)
3097 bufs
[i
] = to
->iov
[i
].iov_base
;
3098 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
3100 result
= frames_to_bytes(runtime
, result
);
3105 static ssize_t
snd_pcm_writev(struct kiocb
*iocb
, struct iov_iter
*from
)
3107 struct snd_pcm_file
*pcm_file
;
3108 struct snd_pcm_substream
*substream
;
3109 struct snd_pcm_runtime
*runtime
;
3110 snd_pcm_sframes_t result
;
3113 snd_pcm_uframes_t frames
;
3115 pcm_file
= iocb
->ki_filp
->private_data
;
3116 substream
= pcm_file
->substream
;
3117 if (PCM_RUNTIME_CHECK(substream
))
3119 runtime
= substream
->runtime
;
3120 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3122 if (!iter_is_iovec(from
))
3124 if (from
->nr_segs
> 128 || from
->nr_segs
!= runtime
->channels
||
3125 !frame_aligned(runtime
, from
->iov
->iov_len
))
3127 frames
= bytes_to_samples(runtime
, from
->iov
->iov_len
);
3128 bufs
= kmalloc(sizeof(void *) * from
->nr_segs
, GFP_KERNEL
);
3131 for (i
= 0; i
< from
->nr_segs
; ++i
)
3132 bufs
[i
] = from
->iov
[i
].iov_base
;
3133 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
3135 result
= frames_to_bytes(runtime
, result
);
3140 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
3142 struct snd_pcm_file
*pcm_file
;
3143 struct snd_pcm_substream
*substream
;
3144 struct snd_pcm_runtime
*runtime
;
3146 snd_pcm_uframes_t avail
;
3148 pcm_file
= file
->private_data
;
3150 substream
= pcm_file
->substream
;
3151 if (PCM_RUNTIME_CHECK(substream
))
3153 runtime
= substream
->runtime
;
3155 poll_wait(file
, &runtime
->sleep
, wait
);
3157 snd_pcm_stream_lock_irq(substream
);
3158 avail
= snd_pcm_playback_avail(runtime
);
3159 switch (runtime
->status
->state
) {
3160 case SNDRV_PCM_STATE_RUNNING
:
3161 case SNDRV_PCM_STATE_PREPARED
:
3162 case SNDRV_PCM_STATE_PAUSED
:
3163 if (avail
>= runtime
->control
->avail_min
) {
3164 mask
= POLLOUT
| POLLWRNORM
;
3168 case SNDRV_PCM_STATE_DRAINING
:
3172 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
3175 snd_pcm_stream_unlock_irq(substream
);
3179 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
3181 struct snd_pcm_file
*pcm_file
;
3182 struct snd_pcm_substream
*substream
;
3183 struct snd_pcm_runtime
*runtime
;
3185 snd_pcm_uframes_t avail
;
3187 pcm_file
= file
->private_data
;
3189 substream
= pcm_file
->substream
;
3190 if (PCM_RUNTIME_CHECK(substream
))
3192 runtime
= substream
->runtime
;
3194 poll_wait(file
, &runtime
->sleep
, wait
);
3196 snd_pcm_stream_lock_irq(substream
);
3197 avail
= snd_pcm_capture_avail(runtime
);
3198 switch (runtime
->status
->state
) {
3199 case SNDRV_PCM_STATE_RUNNING
:
3200 case SNDRV_PCM_STATE_PREPARED
:
3201 case SNDRV_PCM_STATE_PAUSED
:
3202 if (avail
>= runtime
->control
->avail_min
) {
3203 mask
= POLLIN
| POLLRDNORM
;
3208 case SNDRV_PCM_STATE_DRAINING
:
3210 mask
= POLLIN
| POLLRDNORM
;
3215 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3218 snd_pcm_stream_unlock_irq(substream
);
3227 * Only on coherent architectures, we can mmap the status and the control records
3228 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3230 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3232 * mmap status record
3234 static int snd_pcm_mmap_status_fault(struct vm_area_struct
*area
,
3235 struct vm_fault
*vmf
)
3237 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3238 struct snd_pcm_runtime
*runtime
;
3240 if (substream
== NULL
)
3241 return VM_FAULT_SIGBUS
;
3242 runtime
= substream
->runtime
;
3243 vmf
->page
= virt_to_page(runtime
->status
);
3244 get_page(vmf
->page
);
3248 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
3250 .fault
= snd_pcm_mmap_status_fault
,
3253 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3254 struct vm_area_struct
*area
)
3257 if (!(area
->vm_flags
& VM_READ
))
3259 size
= area
->vm_end
- area
->vm_start
;
3260 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3262 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3263 area
->vm_private_data
= substream
;
3264 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3269 * mmap control record
3271 static int snd_pcm_mmap_control_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
->control
);
3281 get_page(vmf
->page
);
3285 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3287 .fault
= snd_pcm_mmap_control_fault
,
3290 static int snd_pcm_mmap_control(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_control
)))
3299 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3300 area
->vm_private_data
= substream
;
3301 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3304 #else /* ! coherent mmap */
3306 * don't support mmap for status and control records.
3308 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3309 struct vm_area_struct
*area
)
3313 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3314 struct vm_area_struct
*area
)
3318 #endif /* coherent mmap */
3320 static inline struct page
*
3321 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3323 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3324 return virt_to_page(vaddr
);
3328 * fault callback for mmapping a RAM page
3330 static int snd_pcm_mmap_data_fault(struct vm_area_struct
*area
,
3331 struct vm_fault
*vmf
)
3333 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3334 struct snd_pcm_runtime
*runtime
;
3335 unsigned long offset
;
3339 if (substream
== NULL
)
3340 return VM_FAULT_SIGBUS
;
3341 runtime
= substream
->runtime
;
3342 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3343 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3344 if (offset
> dma_bytes
- PAGE_SIZE
)
3345 return VM_FAULT_SIGBUS
;
3346 if (substream
->ops
->page
)
3347 page
= substream
->ops
->page(substream
, offset
);
3349 page
= snd_pcm_default_page_ops(substream
, offset
);
3351 return VM_FAULT_SIGBUS
;
3357 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3358 .open
= snd_pcm_mmap_data_open
,
3359 .close
= snd_pcm_mmap_data_close
,
3362 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3363 .open
= snd_pcm_mmap_data_open
,
3364 .close
= snd_pcm_mmap_data_close
,
3365 .fault
= snd_pcm_mmap_data_fault
,
3369 * mmap the DMA buffer on RAM
3373 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3374 * @substream: PCM substream
3377 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3378 * this function is invoked implicitly.
3380 int snd_pcm_lib_default_mmap(struct snd_pcm_substream
*substream
,
3381 struct vm_area_struct
*area
)
3383 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3384 #ifdef CONFIG_GENERIC_ALLOCATOR
3385 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_IRAM
) {
3386 area
->vm_page_prot
= pgprot_writecombine(area
->vm_page_prot
);
3387 return remap_pfn_range(area
, area
->vm_start
,
3388 substream
->dma_buffer
.addr
>> PAGE_SHIFT
,
3389 area
->vm_end
- area
->vm_start
, area
->vm_page_prot
);
3391 #endif /* CONFIG_GENERIC_ALLOCATOR */
3392 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3393 if (!substream
->ops
->page
&&
3394 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3395 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3397 substream
->runtime
->dma_area
,
3398 substream
->runtime
->dma_addr
,
3399 area
->vm_end
- area
->vm_start
);
3400 #endif /* CONFIG_X86 */
3401 /* mmap with fault handler */
3402 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3405 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap
);
3408 * mmap the DMA buffer on I/O memory area
3410 #if SNDRV_PCM_INFO_MMAP_IOMEM
3412 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3413 * @substream: PCM substream
3416 * When your hardware uses the iomapped pages as the hardware buffer and
3417 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3418 * is supposed to work only on limited architectures.
3420 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3421 struct vm_area_struct
*area
)
3423 struct snd_pcm_runtime
*runtime
= substream
->runtime
;;
3425 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3426 return vm_iomap_memory(area
, runtime
->dma_addr
, runtime
->dma_bytes
);
3429 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3430 #endif /* SNDRV_PCM_INFO_MMAP */
3435 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3436 struct vm_area_struct
*area
)
3438 struct snd_pcm_runtime
*runtime
;
3440 unsigned long offset
;
3444 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3445 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3448 if (!(area
->vm_flags
& VM_READ
))
3451 runtime
= substream
->runtime
;
3452 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3454 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3456 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3457 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3459 size
= area
->vm_end
- area
->vm_start
;
3460 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3461 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3462 if ((size_t)size
> dma_bytes
)
3464 if (offset
> dma_bytes
- size
)
3467 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3468 area
->vm_private_data
= substream
;
3469 if (substream
->ops
->mmap
)
3470 err
= substream
->ops
->mmap(substream
, area
);
3472 err
= snd_pcm_lib_default_mmap(substream
, area
);
3474 atomic_inc(&substream
->mmap_count
);
3478 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3480 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3482 struct snd_pcm_file
* pcm_file
;
3483 struct snd_pcm_substream
*substream
;
3484 unsigned long offset
;
3486 pcm_file
= file
->private_data
;
3487 substream
= pcm_file
->substream
;
3488 if (PCM_RUNTIME_CHECK(substream
))
3491 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3493 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3494 if (pcm_file
->no_compat_mmap
)
3496 return snd_pcm_mmap_status(substream
, file
, area
);
3497 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3498 if (pcm_file
->no_compat_mmap
)
3500 return snd_pcm_mmap_control(substream
, file
, area
);
3502 return snd_pcm_mmap_data(substream
, file
, area
);
3507 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3509 struct snd_pcm_file
* pcm_file
;
3510 struct snd_pcm_substream
*substream
;
3511 struct snd_pcm_runtime
*runtime
;
3513 pcm_file
= file
->private_data
;
3514 substream
= pcm_file
->substream
;
3515 if (PCM_RUNTIME_CHECK(substream
))
3517 runtime
= substream
->runtime
;
3518 return fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3524 #ifdef CONFIG_COMPAT
3525 #include "pcm_compat.c"
3527 #define snd_pcm_ioctl_compat NULL
3531 * To be removed helpers to keep binary compatibility
3534 #ifdef CONFIG_SND_SUPPORT_OLD_API
3535 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3536 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3538 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3539 struct snd_pcm_hw_params_old
*oparams
)
3543 memset(params
, 0, sizeof(*params
));
3544 params
->flags
= oparams
->flags
;
3545 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3546 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3547 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3548 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3549 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3550 params
->info
= oparams
->info
;
3551 params
->msbits
= oparams
->msbits
;
3552 params
->rate_num
= oparams
->rate_num
;
3553 params
->rate_den
= oparams
->rate_den
;
3554 params
->fifo_size
= oparams
->fifo_size
;
3557 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3558 struct snd_pcm_hw_params
*params
)
3562 memset(oparams
, 0, sizeof(*oparams
));
3563 oparams
->flags
= params
->flags
;
3564 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3565 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3566 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3567 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3568 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3569 oparams
->info
= params
->info
;
3570 oparams
->msbits
= params
->msbits
;
3571 oparams
->rate_num
= params
->rate_num
;
3572 oparams
->rate_den
= params
->rate_den
;
3573 oparams
->fifo_size
= params
->fifo_size
;
3576 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3577 struct snd_pcm_hw_params_old __user
* _oparams
)
3579 struct snd_pcm_hw_params
*params
;
3580 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3583 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3587 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3588 if (IS_ERR(oparams
)) {
3589 err
= PTR_ERR(oparams
);
3592 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3593 err
= snd_pcm_hw_refine(substream
, params
);
3594 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3595 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3606 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3607 struct snd_pcm_hw_params_old __user
* _oparams
)
3609 struct snd_pcm_hw_params
*params
;
3610 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3613 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3617 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3618 if (IS_ERR(oparams
)) {
3619 err
= PTR_ERR(oparams
);
3622 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3623 err
= snd_pcm_hw_params(substream
, params
);
3624 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3625 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3635 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3638 static unsigned long snd_pcm_get_unmapped_area(struct file
*file
,
3641 unsigned long pgoff
,
3642 unsigned long flags
)
3644 struct snd_pcm_file
*pcm_file
= file
->private_data
;
3645 struct snd_pcm_substream
*substream
= pcm_file
->substream
;
3646 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3647 unsigned long offset
= pgoff
<< PAGE_SHIFT
;
3650 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3651 return (unsigned long)runtime
->status
;
3652 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3653 return (unsigned long)runtime
->control
;
3655 return (unsigned long)runtime
->dma_area
+ offset
;
3659 # define snd_pcm_get_unmapped_area NULL
3666 const struct file_operations snd_pcm_f_ops
[2] = {
3668 .owner
= THIS_MODULE
,
3669 .write
= snd_pcm_write
,
3670 .write_iter
= snd_pcm_writev
,
3671 .open
= snd_pcm_playback_open
,
3672 .release
= snd_pcm_release
,
3673 .llseek
= no_llseek
,
3674 .poll
= snd_pcm_playback_poll
,
3675 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3676 .compat_ioctl
= snd_pcm_ioctl_compat
,
3677 .mmap
= snd_pcm_mmap
,
3678 .fasync
= snd_pcm_fasync
,
3679 .get_unmapped_area
= snd_pcm_get_unmapped_area
,
3682 .owner
= THIS_MODULE
,
3683 .read
= snd_pcm_read
,
3684 .read_iter
= snd_pcm_readv
,
3685 .open
= snd_pcm_capture_open
,
3686 .release
= snd_pcm_release
,
3687 .llseek
= no_llseek
,
3688 .poll
= snd_pcm_capture_poll
,
3689 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3690 .compat_ioctl
= snd_pcm_ioctl_compat
,
3691 .mmap
= snd_pcm_mmap
,
3692 .fasync
= snd_pcm_fasync
,
3693 .get_unmapped_area
= snd_pcm_get_unmapped_area
,