diff options
author | Takashi Iwai <tiwai@suse.de> | 2017-06-01 16:36:02 -0400 |
---|---|---|
committer | Takashi Iwai <tiwai@suse.de> | 2017-06-02 13:38:27 -0400 |
commit | f7a478178a8ea970abd34f7ab73e66c9119b1606 (patch) | |
tree | b55c301b4ba250aa5b782799f522b5eb06ec0aec /Documentation/sound | |
parent | fed5794fccf40297f17e0032cc8211cdad0ba6df (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.rst | 111 |
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 | ||
2081 | This callback is also atomic as default. | 2081 | This callback is also atomic as default. |
2082 | 2082 | ||
2083 | copy and silence callbacks | 2083 | copy_user, copy_kernel and fill_silence ops |
2084 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | 2084 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
2085 | 2085 | ||
2086 | These callbacks are not mandatory, and can be omitted in most cases. | 2086 | These callbacks are not mandatory, and can be omitted in most cases. |
2087 | These callbacks are used when the hardware buffer cannot be in the | 2087 | These 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 | ||
3533 | The first case works fine if the external hardware buffer is large | 3533 | The first case works fine if the external hardware buffer is large |
3534 | enough. This method doesn't need any extra buffers and thus is more | 3534 | enough. This method doesn't need any extra buffers and thus is more |
3535 | effective. You need to define the ``copy`` and ``silence`` callbacks | 3535 | effective. You need to define the ``copy_user`` and ``copy_kernel`` |
3536 | for the data transfer. However, there is a drawback: it cannot be | 3536 | callbacks for the data transfer, in addition to ``fill_silence`` |
3537 | callback for playback. However, there is a drawback: it cannot be | ||
3537 | mmapped. The examples are GUS's GF1 PCM or emu8000's wavetable PCM. | 3538 | mmapped. The examples are GUS's GF1 PCM or emu8000's wavetable PCM. |
3538 | 3539 | ||
3539 | The second case allows for mmap on the buffer, although you have to | 3540 | The 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 | |||
3545 | buffer instead of the host memory. In this case, mmap is available only | 3546 | buffer instead of the host memory. In this case, mmap is available only |
3546 | on certain architectures like the Intel one. In non-mmap mode, the data | 3547 | on certain architectures like the Intel one. In non-mmap mode, the data |
3547 | cannot be transferred as in the normal way. Thus you need to define the | 3548 | cannot 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, |
3549 | examples are found in ``rme32.c`` and ``rme96.c``. | 3550 | as in the cases above. The examples are found in ``rme32.c`` and |
3551 | ``rme96.c``. | ||
3550 | 3552 | ||
3551 | The implementation of the ``copy`` and ``silence`` callbacks depends | 3553 | The implementation of the ``copy_user``, ``copy_kernel`` and |
3552 | upon whether the hardware supports interleaved or non-interleaved | 3554 | ``silence`` callbacks depends upon whether the hardware supports |
3553 | samples. The ``copy`` callback is defined like below, a bit | 3555 | interleaved or non-interleaved samples. The ``copy_user`` callback is |
3554 | differently depending whether the direction is playback or capture: | 3556 | defined like below, a bit differently depending whether the direction |
3557 | is 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 | ||
3563 | In the case of interleaved samples, the second argument (``channel``) is | 3568 | In the case of interleaved samples, the second argument (``channel``) is |
3564 | not used. The third argument (``pos``) points the current position | 3569 | not used. The third argument (``pos``) points the current position |
3565 | offset in frames. | 3570 | offset in bytes. |
3566 | 3571 | ||
3567 | The meaning of the fourth argument is different between playback and | 3572 | The meaning of the fourth argument is different between playback and |
3568 | capture. For playback, it holds the source data pointer, and for | 3573 | capture. For playback, it holds the source data pointer, and for |
3569 | capture, it's the destination data pointer. | 3574 | capture, it's the destination data pointer. |
3570 | 3575 | ||
3571 | The last argument is the number of frames to be copied. | 3576 | The last argument is the number of bytes to be copied. |
3572 | 3577 | ||
3573 | What you have to do in this callback is again different between playback | 3578 | What you have to do in this callback is again different between playback |
3574 | and capture directions. In the playback case, you copy the given amount | 3579 | and 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 | ||
3584 | For the capture direction, you copy the given amount of data (``count``) | 3588 | For the capture direction, you copy the given amount of data (``count``) |
3585 | at the specified offset (``pos``) on the hardware buffer to the | 3589 | at 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 | |
3596 | Here the functions are named as ``from_user`` and ``to_user`` because | ||
3597 | it's the user-space buffer that is passed to these callbacks. That | ||
3598 | is, the callback is supposed to copy from/to the user-space data | ||
3599 | directly to/from the hardware buffer. | ||
3592 | 3600 | ||
3593 | Note that both the position and the amount of data are given in frames. | 3601 | Careful readers might notice that these callbacks receive the |
3602 | arguments in bytes, not in frames like other callbacks. It's because | ||
3603 | it would make coding easier like the examples above, and also it makes | ||
3604 | easier to unify both the interleaved and non-interleaved cases, as | ||
3605 | explained in the following. | ||
3594 | 3606 | ||
3595 | In the case of non-interleaved samples, the implementation will be a bit | 3607 | In the case of non-interleaved samples, the implementation will be a bit |
3596 | more complicated. | 3608 | more complicated. The callback is called for each channel, passed by |
3609 | the second argument, so totally it's called for N-channels times per | ||
3610 | transfer. | ||
3611 | |||
3612 | The meaning of other arguments are almost same as the interleaved | ||
3613 | case. The callback is supposed to copy the data from/to the given | ||
3614 | user-space buffer, but only for the given channel. For the detailed | ||
3615 | implementations, please check ``isa/gus/gus_pcm.c`` or | ||
3616 | "pci/rme9652/rme9652.c" as examples. | ||
3617 | |||
3618 | The above callbacks are the copy from/to the user-space buffer. There | ||
3619 | are some cases where we want copy from/to the kernel-space buffer | ||
3620 | instead. In such a case, ``copy_kernel`` callback is called. It'd | ||
3621 | look 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 | |||
3632 | As found easily, the only difference is that the buffer pointer is | ||
3633 | without ``__user`` prefix; that is, a kernel-buffer pointer is passed | ||
3634 | in the fourth argument. Correspondingly, the implementation would be | ||
3635 | a version without the user-copy, such as: | ||
3597 | 3636 | ||
3598 | You need to check the channel argument, and if it's -1, copy the whole | 3637 | :: |
3599 | channels. Otherwise, you have to copy only the specified channel. Please | 3638 | |
3600 | check ``isa/gus/gus_pcm.c`` as an example. | 3639 | my_memcpy(my_buffer + pos, src, count); |
3601 | 3640 | ||
3602 | The ``silence`` callback is also implemented in a similar way | 3641 | Usually for the playback, another callback ``fill_silence`` is |
3642 | defined. It's implemented in a similar way as the copy callbacks | ||
3643 | above: | ||
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 | ||
3609 | The meanings of arguments are the same as in the ``copy`` callback, | 3650 | The meanings of arguments are the same as in the ``copy_user`` and |
3610 | although there is no ``src/dst`` argument. In the case of interleaved | 3651 | ``copy_kernel`` callbacks, although there is no buffer pointer |
3611 | samples, the channel argument has no meaning, as well as on ``copy`` | 3652 | argument. In the case of interleaved samples, the channel argument has |
3612 | callback. | 3653 | no meaning, as well as on ``copy_*`` callbacks. |
3613 | 3654 | ||
3614 | The role of ``silence`` callback is to set the given amount | 3655 | The 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 |
3616 | hardware buffer. Suppose that the data format is signed (that is, the | 3657 | hardware buffer. Suppose that the data format is signed (that is, the |
3617 | silent-data is 0), and the implementation using a memset-like function | 3658 | silent-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 | ||
3625 | In the case of non-interleaved samples, again, the implementation | 3665 | In the case of non-interleaved samples, again, the implementation |
3626 | becomes a bit more complicated. See, for example, ``isa/gus/gus_pcm.c``. | 3666 | becomes a bit more complicated, as it's called N-times per transfer |
3667 | for each channel. See, for example, ``isa/gus/gus_pcm.c``. | ||
3627 | 3668 | ||
3628 | Non-Contiguous Buffers | 3669 | Non-Contiguous Buffers |
3629 | ---------------------- | 3670 | ---------------------- |