summaryrefslogtreecommitdiffstats
path: root/Documentation/sound
diff options
context:
space:
mode:
authorTakashi Iwai <tiwai@suse.de>2017-06-01 16:36:02 -0400
committerTakashi Iwai <tiwai@suse.de>2017-06-02 13:38:27 -0400
commitf7a478178a8ea970abd34f7ab73e66c9119b1606 (patch)
treeb55c301b4ba250aa5b782799f522b5eb06ec0aec /Documentation/sound
parentfed5794fccf40297f17e0032cc8211cdad0ba6df (diff)
ALSA: doc: Update copy_user, copy_kernel and fill_silence PCM ops
Reviewed-by: Takashi Sakamoto <o-takashi@sakamocchi.jp> Signed-off-by: Takashi Iwai <tiwai@suse.de>
Diffstat (limited to 'Documentation/sound')
-rw-r--r--Documentation/sound/kernel-api/writing-an-alsa-driver.rst111
1 files changed, 76 insertions, 35 deletions
diff --git a/Documentation/sound/kernel-api/writing-an-alsa-driver.rst b/Documentation/sound/kernel-api/writing-an-alsa-driver.rst
index 95c5443eff38..58ffa3f5bda7 100644
--- a/Documentation/sound/kernel-api/writing-an-alsa-driver.rst
+++ b/Documentation/sound/kernel-api/writing-an-alsa-driver.rst
@@ -2080,8 +2080,8 @@ sleeping poll threads, etc.
2080 2080
2081This callback is also atomic as default. 2081This callback is also atomic as default.
2082 2082
2083copy and silence callbacks 2083copy_user, copy_kernel and fill_silence ops
2084~~~~~~~~~~~~~~~~~~~~~~~~~~ 2084~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2085 2085
2086These callbacks are not mandatory, and can be omitted in most cases. 2086These callbacks are not mandatory, and can be omitted in most cases.
2087These callbacks are used when the hardware buffer cannot be in the 2087These callbacks are used when the hardware buffer cannot be in the
@@ -3532,8 +3532,9 @@ external hardware buffer in interrupts (or in tasklets, preferably).
3532 3532
3533The first case works fine if the external hardware buffer is large 3533The first case works fine if the external hardware buffer is large
3534enough. This method doesn't need any extra buffers and thus is more 3534enough. This method doesn't need any extra buffers and thus is more
3535effective. You need to define the ``copy`` and ``silence`` callbacks 3535effective. You need to define the ``copy_user`` and ``copy_kernel``
3536for the data transfer. However, there is a drawback: it cannot be 3536callbacks for the data transfer, in addition to ``fill_silence``
3537callback for playback. However, there is a drawback: it cannot be
3537mmapped. The examples are GUS's GF1 PCM or emu8000's wavetable PCM. 3538mmapped. The examples are GUS's GF1 PCM or emu8000's wavetable PCM.
3538 3539
3539The second case allows for mmap on the buffer, although you have to 3540The second case allows for mmap on the buffer, although you have to
@@ -3545,30 +3546,34 @@ Another case is when the chip uses a PCI memory-map region for the
3545buffer instead of the host memory. In this case, mmap is available only 3546buffer instead of the host memory. In this case, mmap is available only
3546on certain architectures like the Intel one. In non-mmap mode, the data 3547on certain architectures like the Intel one. In non-mmap mode, the data
3547cannot be transferred as in the normal way. Thus you need to define the 3548cannot be transferred as in the normal way. Thus you need to define the
3548``copy`` and ``silence`` callbacks as well, as in the cases above. The 3549``copy_user``, ``copy_kernel`` and ``fill_silence`` callbacks as well,
3549examples are found in ``rme32.c`` and ``rme96.c``. 3550as in the cases above. The examples are found in ``rme32.c`` and
3551``rme96.c``.
3550 3552
3551The implementation of the ``copy`` and ``silence`` callbacks depends 3553The implementation of the ``copy_user``, ``copy_kernel`` and
3552upon whether the hardware supports interleaved or non-interleaved 3554``silence`` callbacks depends upon whether the hardware supports
3553samples. The ``copy`` callback is defined like below, a bit 3555interleaved or non-interleaved samples. The ``copy_user`` callback is
3554differently depending whether the direction is playback or capture: 3556defined like below, a bit differently depending whether the direction
3557is playback or capture:
3555 3558
3556:: 3559::
3557 3560
3558 static int playback_copy(struct snd_pcm_substream *substream, int channel, 3561 static int playback_copy_user(struct snd_pcm_substream *substream,
3559 snd_pcm_uframes_t pos, void *src, snd_pcm_uframes_t count); 3562 int channel, unsigned long pos,
3560 static int capture_copy(struct snd_pcm_substream *substream, int channel, 3563 void __user *src, unsigned long count);
3561 snd_pcm_uframes_t pos, void *dst, snd_pcm_uframes_t count); 3564 static int capture_copy_user(struct snd_pcm_substream *substream,
3565 int channel, unsigned long pos,
3566 void __user *dst, unsigned long count);
3562 3567
3563In the case of interleaved samples, the second argument (``channel``) is 3568In the case of interleaved samples, the second argument (``channel``) is
3564not used. The third argument (``pos``) points the current position 3569not used. The third argument (``pos``) points the current position
3565offset in frames. 3570offset in bytes.
3566 3571
3567The meaning of the fourth argument is different between playback and 3572The meaning of the fourth argument is different between playback and
3568capture. For playback, it holds the source data pointer, and for 3573capture. For playback, it holds the source data pointer, and for
3569capture, it's the destination data pointer. 3574capture, it's the destination data pointer.
3570 3575
3571The last argument is the number of frames to be copied. 3576The last argument is the number of bytes to be copied.
3572 3577
3573What you have to do in this callback is again different between playback 3578What you have to do in this callback is again different between playback
3574and capture directions. In the playback case, you copy the given amount 3579and capture directions. In the playback case, you copy the given amount
@@ -3578,8 +3583,7 @@ way, the copy would be like:
3578 3583
3579:: 3584::
3580 3585
3581 my_memcpy(my_buffer + frames_to_bytes(runtime, pos), src, 3586 my_memcpy_from_user(my_buffer + pos, src, count);
3582 frames_to_bytes(runtime, count));
3583 3587
3584For the capture direction, you copy the given amount of data (``count``) 3588For the capture direction, you copy the given amount of data (``count``)
3585at the specified offset (``pos``) on the hardware buffer to the 3589at the specified offset (``pos``) on the hardware buffer to the
@@ -3587,31 +3591,68 @@ specified pointer (``dst``).
3587 3591
3588:: 3592::
3589 3593
3590 my_memcpy(dst, my_buffer + frames_to_bytes(runtime, pos), 3594 my_memcpy_to_user(dst, my_buffer + pos, count);
3591 frames_to_bytes(runtime, count)); 3595
3596Here the functions are named as ``from_user`` and ``to_user`` because
3597it's the user-space buffer that is passed to these callbacks. That
3598is, the callback is supposed to copy from/to the user-space data
3599directly to/from the hardware buffer.
3592 3600
3593Note that both the position and the amount of data are given in frames. 3601Careful readers might notice that these callbacks receive the
3602arguments in bytes, not in frames like other callbacks. It's because
3603it would make coding easier like the examples above, and also it makes
3604easier to unify both the interleaved and non-interleaved cases, as
3605explained in the following.
3594 3606
3595In the case of non-interleaved samples, the implementation will be a bit 3607In the case of non-interleaved samples, the implementation will be a bit
3596more complicated. 3608more complicated. The callback is called for each channel, passed by
3609the second argument, so totally it's called for N-channels times per
3610transfer.
3611
3612The meaning of other arguments are almost same as the interleaved
3613case. The callback is supposed to copy the data from/to the given
3614user-space buffer, but only for the given channel. For the detailed
3615implementations, please check ``isa/gus/gus_pcm.c`` or
3616"pci/rme9652/rme9652.c" as examples.
3617
3618The above callbacks are the copy from/to the user-space buffer. There
3619are some cases where we want copy from/to the kernel-space buffer
3620instead. In such a case, ``copy_kernel`` callback is called. It'd
3621look like:
3622
3623::
3624
3625 static int playback_copy_kernel(struct snd_pcm_substream *substream,
3626 int channel, unsigned long pos,
3627 void *src, unsigned long count);
3628 static int capture_copy_kernel(struct snd_pcm_substream *substream,
3629 int channel, unsigned long pos,
3630 void *dst, unsigned long count);
3631
3632As found easily, the only difference is that the buffer pointer is
3633without ``__user`` prefix; that is, a kernel-buffer pointer is passed
3634in the fourth argument. Correspondingly, the implementation would be
3635a version without the user-copy, such as:
3597 3636
3598You need to check the channel argument, and if it's -1, copy the whole 3637::
3599channels. Otherwise, you have to copy only the specified channel. Please 3638
3600check ``isa/gus/gus_pcm.c`` as an example. 3639 my_memcpy(my_buffer + pos, src, count);
3601 3640
3602The ``silence`` callback is also implemented in a similar way 3641Usually for the playback, another callback ``fill_silence`` is
3642defined. It's implemented in a similar way as the copy callbacks
3643above:
3603 3644
3604:: 3645::
3605 3646
3606 static int silence(struct snd_pcm_substream *substream, int channel, 3647 static int silence(struct snd_pcm_substream *substream, int channel,
3607 snd_pcm_uframes_t pos, snd_pcm_uframes_t count); 3648 unsigned long pos, unsigned long count);
3608 3649
3609The meanings of arguments are the same as in the ``copy`` callback, 3650The meanings of arguments are the same as in the ``copy_user`` and
3610although there is no ``src/dst`` argument. In the case of interleaved 3651``copy_kernel`` callbacks, although there is no buffer pointer
3611samples, the channel argument has no meaning, as well as on ``copy`` 3652argument. In the case of interleaved samples, the channel argument has
3612callback. 3653no meaning, as well as on ``copy_*`` callbacks.
3613 3654
3614The role of ``silence`` callback is to set the given amount 3655The role of ``fill_silence`` callback is to set the given amount
3615(``count``) of silence data at the specified offset (``pos``) on the 3656(``count``) of silence data at the specified offset (``pos``) on the
3616hardware buffer. Suppose that the data format is signed (that is, the 3657hardware buffer. Suppose that the data format is signed (that is, the
3617silent-data is 0), and the implementation using a memset-like function 3658silent-data is 0), and the implementation using a memset-like function
@@ -3619,11 +3660,11 @@ would be like:
3619 3660
3620:: 3661::
3621 3662
3622 my_memcpy(my_buffer + frames_to_bytes(runtime, pos), 0, 3663 my_memset(my_buffer + pos, 0, count);
3623 frames_to_bytes(runtime, count));
3624 3664
3625In the case of non-interleaved samples, again, the implementation 3665In the case of non-interleaved samples, again, the implementation
3626becomes a bit more complicated. See, for example, ``isa/gus/gus_pcm.c``. 3666becomes a bit more complicated, as it's called N-times per transfer
3667for each channel. See, for example, ``isa/gus/gus_pcm.c``.
3627 3668
3628Non-Contiguous Buffers 3669Non-Contiguous Buffers
3629---------------------- 3670----------------------