OSDN Git Service

ALSA: pcm: Direct in-kernel read/write support
authorTakashi Iwai <tiwai@suse.de>
Wed, 24 May 2017 16:23:20 +0000 (18:23 +0200)
committerTakashi Iwai <tiwai@suse.de>
Fri, 2 Jun 2017 17:38:24 +0000 (19:38 +0200)
Now all materials are ready, let's allow the direct in-kernel
read/write, i.e. a kernel-space buffer is passed for read or write,
instead of the normal user-space buffer.  This feature is used by OSS
layer and UAC1 driver, for example.

The __snd_pcm_lib_xfer() takes in_kernel argument that indicates the
in-kernel buffer copy.  When this flag is set, another transfer code
is used.  It's either via copy_kernel PCM ops or the normal memcpy(),
depending on the driver setup.

As external API, snd_pcm_kernel_read(), *_write() and other variants
are provided.

That's all.  This support is really simple because of the code
refactoring until now.

Reviewed-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
include/sound/pcm.h
sound/core/pcm_lib.c

index db64908..c24f85f 100644 (file)
@@ -1074,34 +1074,62 @@ int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream);
 snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
                                     void *buf, bool interleaved,
-                                    snd_pcm_uframes_t frames);
+                                    snd_pcm_uframes_t frames, bool in_kernel);
 
 static inline snd_pcm_sframes_t
 snd_pcm_lib_write(struct snd_pcm_substream *substream,
                  const void __user *buf, snd_pcm_uframes_t frames)
 {
-       return __snd_pcm_lib_xfer(substream, (void *)buf, true, frames);
+       return __snd_pcm_lib_xfer(substream, (void *)buf, true, frames, false);
 }
 
 static inline snd_pcm_sframes_t
 snd_pcm_lib_read(struct snd_pcm_substream *substream,
                 void __user *buf, snd_pcm_uframes_t frames)
 {
-       return __snd_pcm_lib_xfer(substream, (void *)buf, true, frames);
+       return __snd_pcm_lib_xfer(substream, (void *)buf, true, frames, false);
 }
 
 static inline snd_pcm_sframes_t
 snd_pcm_lib_writev(struct snd_pcm_substream *substream,
                   void __user **bufs, snd_pcm_uframes_t frames)
 {
-       return __snd_pcm_lib_xfer(substream, (void *)bufs, false, frames);
+       return __snd_pcm_lib_xfer(substream, (void *)bufs, false, frames, false);
 }
 
 static inline snd_pcm_sframes_t
 snd_pcm_lib_readv(struct snd_pcm_substream *substream,
                  void __user **bufs, snd_pcm_uframes_t frames)
 {
-       return __snd_pcm_lib_xfer(substream, (void *)bufs, false, frames);
+       return __snd_pcm_lib_xfer(substream, (void *)bufs, false, frames, false);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_kernel_write(struct snd_pcm_substream *substream,
+                    const void *buf, snd_pcm_uframes_t frames)
+{
+       return __snd_pcm_lib_xfer(substream, (void *)buf, true, frames, true);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_kernel_read(struct snd_pcm_substream *substream,
+                   void *buf, snd_pcm_uframes_t frames)
+{
+       return __snd_pcm_lib_xfer(substream, buf, true, frames, true);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_kernel_writev(struct snd_pcm_substream *substream,
+                     void **bufs, snd_pcm_uframes_t frames)
+{
+       return __snd_pcm_lib_xfer(substream, bufs, false, frames, true);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_kernel_readv(struct snd_pcm_substream *substream,
+                    void **bufs, snd_pcm_uframes_t frames)
+{
+       return __snd_pcm_lib_xfer(substream, bufs, false, frames, true);
 }
 
 int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime);
index f31949b..95b8ef1 100644 (file)
@@ -1994,6 +1994,15 @@ static int default_write_copy(struct snd_pcm_substream *substream,
        return 0;
 }
 
+/* default copy_kernel ops for write */
+static int default_write_copy_kernel(struct snd_pcm_substream *substream,
+                                    int channel, unsigned long hwoff,
+                                    void *buf, unsigned long bytes)
+{
+       memcpy(get_dma_ptr(substream->runtime, channel, hwoff), buf, bytes);
+       return 0;
+}
+
 /* fill silence instead of copy data; called as a transfer helper
  * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when
  * a NULL buffer is passed
@@ -2027,6 +2036,15 @@ static int default_read_copy(struct snd_pcm_substream *substream,
        return 0;
 }
 
+/* default copy_kernel ops for read */
+static int default_read_copy_kernel(struct snd_pcm_substream *substream,
+                                   int channel, unsigned long hwoff,
+                                   void *buf, unsigned long bytes)
+{
+       memcpy(buf, get_dma_ptr(substream->runtime, channel, hwoff), bytes);
+       return 0;
+}
+
 /* call transfer function with the converted pointers and sizes;
  * for interleaved mode, it's one shot for all samples
  */
@@ -2126,7 +2144,7 @@ static int pcm_accessible_state(struct snd_pcm_runtime *runtime)
 /* the common loop for read/write data */
 snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
                                     void *data, bool interleaved,
-                                    snd_pcm_uframes_t size)
+                                    snd_pcm_uframes_t size, bool in_kernel)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
        snd_pcm_uframes_t xfer = 0;
@@ -2159,6 +2177,12 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
                        transfer = fill_silence;
                else
                        return -EINVAL;
+       } else if (in_kernel) {
+               if (substream->ops->copy_kernel)
+                       transfer = substream->ops->copy_kernel;
+               else
+                       transfer = is_playback ?
+                               default_write_copy_kernel : default_read_copy_kernel;
        } else {
                if (substream->ops->copy_user)
                        transfer = (pcm_transfer_f)substream->ops->copy_user;