Skip to contentSkip to conversation

ALSA: Extend PCM buffer-copy helpers

PATCH New Takashi Iwai 1 participant 5 messages May 23, 2017 Sound

lore
Share & tools
lore
#0 lore raw

cover ALSA: Extend PCM buffer-copy helpers

Hop, step... before jump.

This is a patch series based on the previous one ("[PATCH 00/16] ALSA:
Convert to new copy_silence PCM ops"), as the second step for killing set_fs(). It extends the PCM buffer-copy helper functions to allow in-kernel buffer copy via the new PCM ops, as well as some code refactoring.

Once when these are implemented, the set_fs() can be eliminated by simply replacing with these new helpers.

The latest patches are found in topic/kill-set_fs branch in sound git tree.

Takashi

===

Takashi Iwai (4):
ALSA: pcm: Shuffle code
ALSA: pcm: Call directly the common read/write helpers
ALSA: pcm: Provide read/write helpers for in-kernel buffer transfer
ALSA: pcm: Split PCM transfer codes

 include/sound/pcm.h  |  81 ++++++++--
 sound/core/pcm_lib.c | 410 ++++++++++++++++++++++++++-------------------------
 2 files changed, 282 insertions(+), 209 deletions(-)
Signature · 1 line
2.13.0
#1 lore raw

PATCH 1/4 ALSA: pcm: Shuffle code

Just shuffling the code, no changes otherwise at all. It's a preparation for the next change to cleanup snd_pcm_lib_read() and snd_pcm_lib_write().

---
 sound/core/pcm_lib.c | 190 +++++++++++++++++++++++++--------------------------
 1 file changed, 95 insertions(+), 95 deletions(-)
sound/core/pcm_lib.c +95 -95
index 1c9d43fefacc..2593412091dc 100644
@@ -1989,6 +1989,24 @@ static int wait_for_avail(struct snd_pcm_substream *substream,
 	return err;
 }
 	
+typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
+			  unsigned long data, unsigned int off,
+			  snd_pcm_uframes_t size);
+
+/* sanity-check for read/write methods */
+static int pcm_sanity_check(struct snd_pcm_substream *substream)
+{
+	struct snd_pcm_runtime *runtime;
+	if (PCM_RUNTIME_CHECK(substream))
+		return -ENXIO;
+	runtime = substream->runtime;
+	if (snd_BUG_ON(!substream->ops->copy_silence && !runtime->dma_area))
+		return -EINVAL;
+	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
+		return -EBADFD;
+	return 0;
+}
+
 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
 				      unsigned int hwoff,
 				      unsigned long data, unsigned int off,
@@ -2010,9 +2028,45 @@ static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
 	return 0;
 }
  
-typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
-			  unsigned long data, unsigned int off,
-			  snd_pcm_uframes_t size);
+static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
+				       unsigned int hwoff,
+				       unsigned long data, unsigned int off,
+				       snd_pcm_uframes_t frames)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	int err;
+	void __user **bufs = (void __user **)data;
+	int channels = runtime->channels;
+	char __user *buf;
+	int c;
+
+	if (substream->ops->copy_silence) {
+		for (c = 0; c < channels; ++c, ++bufs) {
+			if (!*bufs)
+				buf = NULL;
+			else
+				buf = *bufs + samples_to_bytes(runtime, off);
+			err = substream->ops->copy_silence(substream, c, hwoff,
+							   buf, frames, false);
+			if (err < 0)
+				return err;
+		}
+	} else {
+		/* default transfer behaviour */
+		size_t dma_csize = runtime->dma_bytes / channels;
+		for (c = 0; c < channels; ++c, ++bufs) {
+			char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
+			if (*bufs == NULL) {
+				snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
+			} else {
+				char __user *buf = *bufs + samples_to_bytes(runtime, off);
+				if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
+					return -EFAULT;
+			}
+		}
+	}
+	return 0;
+}
 
 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, 
 					    unsigned long data,
@@ -2116,20 +2170,6 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
 }
 
-/* sanity-check for read/write methods */
-static int pcm_sanity_check(struct snd_pcm_substream *substream)
-{
-	struct snd_pcm_runtime *runtime;
-	if (PCM_RUNTIME_CHECK(substream))
-		return -ENXIO;
-	runtime = substream->runtime;
-	if (snd_BUG_ON(!substream->ops->copy_silence && !runtime->dma_area))
-		return -EINVAL;
-	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
-		return -EBADFD;
-	return 0;
-}
-
 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
 {
 	struct snd_pcm_runtime *runtime;
@@ -2151,46 +2191,6 @@ snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const v
 
 EXPORT_SYMBOL(snd_pcm_lib_write);
 
-static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
-				       unsigned int hwoff,
-				       unsigned long data, unsigned int off,
-				       snd_pcm_uframes_t frames)
-{
-	struct snd_pcm_runtime *runtime = substream->runtime;
-	int err;
-	void __user **bufs = (void __user **)data;
-	int channels = runtime->channels;
-	char __user *buf;
-	int c;
-
-	if (substream->ops->copy_silence) {
-		for (c = 0; c < channels; ++c, ++bufs) {
-			if (!*bufs)
-				buf = NULL;
-			else
-				buf = *bufs + samples_to_bytes(runtime, off);
-			err = substream->ops->copy_silence(substream, c, hwoff,
-							   buf, frames, false);
-			if (err < 0)
-				return err;
-		}
-	} else {
-		/* default transfer behaviour */
-		size_t dma_csize = runtime->dma_bytes / channels;
-		for (c = 0; c < channels; ++c, ++bufs) {
-			char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
-			if (*bufs == NULL) {
-				snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
-			} else {
-				char __user *buf = *bufs + samples_to_bytes(runtime, off);
-				if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
-					return -EFAULT;
-			}
-		}
-	}
-	return 0;
-}
- 
 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
 				     void __user **bufs,
 				     snd_pcm_uframes_t frames)
@@ -2234,6 +2234,44 @@ static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
 	return 0;
 }
 
+static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
+				      unsigned int hwoff,
+				      unsigned long data, unsigned int off,
+				      snd_pcm_uframes_t frames)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	int err;
+	void __user **bufs = (void __user **)data;
+	int channels = runtime->channels;
+	char __user *buf;
+	char *hwbuf;
+	int c;
+
+	if (substream->ops->copy_silence) {
+		for (c = 0; c < channels; ++c, ++bufs) {
+			if (!*bufs)
+				continue;
+			buf = *bufs + samples_to_bytes(runtime, off);
+			err = substream->ops->copy_silence(substream, c, hwoff,
+							   buf, frames, false);
+			if (err < 0)
+				return err;
+		}
+	} else {
+		snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
+		for (c = 0; c < channels; ++c, ++bufs) {
+			if (*bufs == NULL)
+				continue;
+
+			hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
+			buf = *bufs + samples_to_bytes(runtime, off);
+			if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
+				return -EFAULT;
+		}
+	}
+	return 0;
+}
+
 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
 					   unsigned long data,
 					   snd_pcm_uframes_t size,
@@ -2362,44 +2400,6 @@ snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __u
 
 EXPORT_SYMBOL(snd_pcm_lib_read);
 
-static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
-				      unsigned int hwoff,
-				      unsigned long data, unsigned int off,
-				      snd_pcm_uframes_t frames)
-{
-	struct snd_pcm_runtime *runtime = substream->runtime;
-	int err;
-	void __user **bufs = (void __user **)data;
-	int channels = runtime->channels;
-	char __user *buf;
-	char *hwbuf;
-	int c;
-
-	if (substream->ops->copy_silence) {
-		for (c = 0; c < channels; ++c, ++bufs) {
-			if (!*bufs)
-				continue;
-			buf = *bufs + samples_to_bytes(runtime, off);
-			err = substream->ops->copy_silence(substream, c, hwoff,
-							   buf, frames, false);
-			if (err < 0)
-				return err;
-		}
-	} else {
-		snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
-		for (c = 0; c < channels; ++c, ++bufs) {
-			if (*bufs == NULL)
-				continue;
-
-			hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
-			buf = *bufs + samples_to_bytes(runtime, off);
-			if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
-				return -EFAULT;
-		}
-	}
-	return 0;
-}
- 
 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
 				    void __user **bufs,
 				    snd_pcm_uframes_t frames)
Signature · 1 line
2.13.0
#2 lore raw

PATCH 2/4 ALSA: pcm: Call directly the common read/write helpers

In reply to Takashi Iwai

Make snd_pcm_lib_read() and *_write() static inline functions that call the common helper functions directly. This reduces a slight amount of codes, and at the same time, it's a preparation for the further cleanups / fixes.

---
 include/sound/pcm.h  |  43 ++++++++++++----
 sound/core/pcm_lib.c | 143 +++++++++++++++++----------------------------------
 2 files changed, 82 insertions(+), 104 deletions(-)
include/sound/pcm.h +34 -9
index 4243c02c3f11..339ae4f77766 100644
@@ -1084,15 +1084,40 @@ int snd_pcm_update_state(struct snd_pcm_substream *substream,
 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream);
 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr);
 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream);
-snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream,
-				    const void __user *buf,
-				    snd_pcm_uframes_t frames);
-snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream,
-				   void __user *buf, snd_pcm_uframes_t frames);
-snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
-				     void __user **bufs, snd_pcm_uframes_t frames);
-snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
-				    void __user **bufs, snd_pcm_uframes_t frames);
+snd_pcm_sframes_t __snd_pcm_lib_write(struct snd_pcm_substream *substream,
+				      unsigned long data, bool interleaved,
+				      snd_pcm_uframes_t size);
+snd_pcm_sframes_t __snd_pcm_lib_read(struct snd_pcm_substream *substream,
+				     unsigned long data, bool interleaved,
+				     snd_pcm_uframes_t size);
+
+static inline snd_pcm_sframes_t
+snd_pcm_lib_write(struct snd_pcm_substream *substream,
+		  const void __user *buf, snd_pcm_uframes_t size)
+{
+	return __snd_pcm_lib_write(substream, (unsigned long)buf, true, size);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_lib_writev(struct snd_pcm_substream *substream,
+		   void __user **bufs, snd_pcm_uframes_t size)
+{
+	return __snd_pcm_lib_write(substream, (unsigned long)bufs, false, size);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_lib_read(struct snd_pcm_substream *substream,
+		 void __user *buf, snd_pcm_uframes_t size)
+{
+	return __snd_pcm_lib_read(substream, (unsigned long)buf, true, size);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_lib_readv(struct snd_pcm_substream *substream,
+		  void __user **bufs, snd_pcm_uframes_t size)
+{
+	return __snd_pcm_lib_read(substream, (unsigned long)bufs, false, size);
+}
 
 extern const struct snd_pcm_hw_constraint_list snd_pcm_known_rates;
 
sound/core/pcm_lib.c +48 -95

186 lines not on this page · raw message

Signature · 1 line
2.13.0
#3 lore raw

PATCH 3/4 ALSA: pcm: Provide read/write helpers for in-kernel buffer transfer

In reply to Takashi Iwai

Thanks to the conversion to copy_silence PCM ops, now we can copy from/to the kernel-space buffer directly via in_kernel parameter without set_fs() hack in all places. This patch extends the existing helper functions for buffer copy to handle in-kernel buffer transfers, too.

Also, the similar API functions for in-kernel buffer transfer are provided as well.

---
 include/sound/pcm.h  | 50 +++++++++++++++++++++++++++----
 sound/core/pcm_lib.c | 83 ++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 102 insertions(+), 31 deletions(-)
include/sound/pcm.h +44 -6
index 339ae4f77766..1c9107af7385 100644
@@ -1086,37 +1086,75 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram
 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream);
 snd_pcm_sframes_t __snd_pcm_lib_write(struct snd_pcm_substream *substream,
 				      unsigned long data, bool interleaved,
-				      snd_pcm_uframes_t size);
+				      snd_pcm_uframes_t size, bool in_kernel);
 snd_pcm_sframes_t __snd_pcm_lib_read(struct snd_pcm_substream *substream,
 				     unsigned long data, bool interleaved,
-				     snd_pcm_uframes_t size);
+				     snd_pcm_uframes_t size, bool in_kernel);
 
+/* copy from/to user-space buffer */
 static inline snd_pcm_sframes_t
 snd_pcm_lib_write(struct snd_pcm_substream *substream,
 		  const void __user *buf, snd_pcm_uframes_t size)
 {
-	return __snd_pcm_lib_write(substream, (unsigned long)buf, true, size);
+	return __snd_pcm_lib_write(substream, (unsigned long)buf, true, size,
+				   false);
 }
 
 static inline snd_pcm_sframes_t
 snd_pcm_lib_writev(struct snd_pcm_substream *substream,
 		   void __user **bufs, snd_pcm_uframes_t size)
 {
-	return __snd_pcm_lib_write(substream, (unsigned long)bufs, false, size);
+	return __snd_pcm_lib_write(substream, (unsigned long)bufs, false, size,
+				   false);
 }
 
 static inline snd_pcm_sframes_t
 snd_pcm_lib_read(struct snd_pcm_substream *substream,
 		 void __user *buf, snd_pcm_uframes_t size)
 {
-	return __snd_pcm_lib_read(substream, (unsigned long)buf, true, size);
+	return __snd_pcm_lib_read(substream, (unsigned long)buf, true, size,
+				  false);
 }
 
 static inline snd_pcm_sframes_t
 snd_pcm_lib_readv(struct snd_pcm_substream *substream,
 		  void __user **bufs, snd_pcm_uframes_t size)
 {
-	return __snd_pcm_lib_read(substream, (unsigned long)bufs, false, size);
+	return __snd_pcm_lib_read(substream, (unsigned long)bufs, false, size,
+				  false);
+}
+
+/* copy from/to kernel-space buffer */
+static inline snd_pcm_sframes_t
+snd_pcm_kernel_write(struct snd_pcm_substream *substream,
+		     const void *buf, snd_pcm_uframes_t size)
+{
+	return __snd_pcm_lib_write(substream, (unsigned long)buf, true, size,
+				   true);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_kernel_writev(struct snd_pcm_substream *substream,
+		      void **bufs, snd_pcm_uframes_t size)
+{
+	return __snd_pcm_lib_write(substream, (unsigned long)bufs, false, size,
+				   true);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_kernel_read(struct snd_pcm_substream *substream,
+		    void *buf, snd_pcm_uframes_t size)
+{
+	return __snd_pcm_lib_read(substream, (unsigned long)buf, true, size,
+				  true);
+}
+
+static inline snd_pcm_sframes_t
+snd_pcm_kernel_readv(struct snd_pcm_substream *substream,
+		     void **bufs, snd_pcm_uframes_t size)
+{
+	return __snd_pcm_lib_read(substream, (unsigned long)bufs, false, size,
+				  true);
 }
 
 extern const struct snd_pcm_hw_constraint_list snd_pcm_known_rates;
sound/core/pcm_lib.c +58 -25

183 lines not on this page · raw message

Signature · 1 line
2.13.0
#4 lore raw

PATCH 4/4 ALSA: pcm: Split PCM transfer codes

In reply to Takashi Iwai

The read and write transfer codes have the branch for the cases with and without copy_silence ops. Split the transfer functions to each of them and pass the appropriate one before the loop.

---
 sound/core/pcm_lib.c | 222 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 123 insertions(+), 99 deletions(-)
sound/core/pcm_lib.c +123 -99
index dfa6d13ba8bd..a08059f74794 100644
@@ -2007,80 +2007,85 @@ static int pcm_sanity_check(struct snd_pcm_substream *substream)
 	return 0;
 }
 
+static int snd_pcm_lib_write_via_ops(struct snd_pcm_substream *substream,
+				     unsigned int hwoff,
+				     unsigned long data, unsigned int off,
+				     snd_pcm_uframes_t frames, bool in_kernel)
+{
+	char __user *buf = (char __user *) data +
+		frames_to_bytes(substream->runtime, off);
+
+	return substream->ops->copy_silence(substream, -1, hwoff, buf,
+					    frames, in_kernel);
+}
+
 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
 				      unsigned int hwoff,
 				      unsigned long data, unsigned int off,
 				      snd_pcm_uframes_t frames, bool in_kernel)
 {
 	struct snd_pcm_runtime *runtime = substream->runtime;
-	int err;
 	char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
+	char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
+
+	if (in_kernel)
+		memcpy(hwbuf, (void *)buf, frames_to_bytes(runtime, frames));
+	else if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
+		return -EFAULT;
+	return 0;
+}
 
-	if (substream->ops->copy_silence) {
-		err = substream->ops->copy_silence(substream, -1, hwoff, buf,
-						   frames, in_kernel);
+static int snd_pcm_lib_writev_via_ops(struct snd_pcm_substream *substream,
+				      unsigned int hwoff,
+				      unsigned long data, unsigned int off,
+				      snd_pcm_uframes_t frames, bool in_kernel)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	int channels = runtime->channels;
+	void __user **bufs = (void __user **)data;
+	char __user *buf;
+	int c, err;
+
+	for (c = 0; c < channels; ++c, ++bufs) {
+		if (!*bufs)
+			buf = NULL;
+		else
+			buf = *bufs + samples_to_bytes(runtime, off);
+		err = substream->ops->copy_silence(substream, c, hwoff,
+						   buf, frames, in_kernel);
 		if (err < 0)
 			return err;
-	} else {
-		char *hwbuf = runtime->dma_area +
-			frames_to_bytes(runtime, hwoff);
-
-		if (in_kernel)
-			memcpy(hwbuf, (void *)buf,
-			       frames_to_bytes(runtime, frames));
-		else if (copy_from_user(hwbuf, buf,
-					frames_to_bytes(runtime, frames)))
-			return -EFAULT;
 	}
 	return 0;
 }
- 
+
 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
 				       unsigned int hwoff,
 				       unsigned long data, unsigned int off,
 				       snd_pcm_uframes_t frames, bool in_kernel)
 {
 	struct snd_pcm_runtime *runtime = substream->runtime;
-	int err;
-	void __user **bufs = (void __user **)data;
 	int channels = runtime->channels;
+	size_t dma_csize = runtime->dma_bytes / channels;
+	void __user **bufs = (void __user **)data;
 	char __user *buf;
+	char *hwbuf;
 	int c;
 
-	if (substream->ops->copy_silence) {
-		for (c = 0; c < channels; ++c, ++bufs) {
-			if (!*bufs)
-				buf = NULL;
-			else
-				buf = *bufs + samples_to_bytes(runtime, off);
-			err = substream->ops->copy_silence(substream, c, hwoff,
-							   buf, frames,
-							   in_kernel);
-			if (err < 0)
-				return err;
-		}
-	} else {
-		/* default transfer behaviour */
-		size_t dma_csize = runtime->dma_bytes / channels;
-		for (c = 0; c < channels; ++c, ++bufs) {
-			char *hwbuf = runtime->dma_area + (c * dma_csize) +
-				samples_to_bytes(runtime, hwoff);
-
-			if (!*bufs) {
-				snd_pcm_format_set_silence(runtime->format,
-							   hwbuf, frames);
-			} else {
-				char __user *buf = *bufs +
-					samples_to_bytes(runtime, off);
-
-				if (in_kernel)
-					memcpy(hwbuf, (void *)buf,
-					       samples_to_bytes(runtime, frames));
-				else if (copy_from_user(hwbuf, buf,
-							samples_to_bytes(runtime, frames)))
-					return -EFAULT;
-			}
-		}
+	for (c = 0; c < channels; ++c, ++bufs) {
+		hwbuf = runtime->dma_area + (c * dma_csize) +
+			samples_to_bytes(runtime, hwoff);
+		buf = *bufs + samples_to_bytes(runtime, off);
+
+		if (!*bufs)
+			snd_pcm_format_set_silence(runtime->format,
+						   hwbuf, frames);
+		else if (in_kernel)
+			memcpy(hwbuf, (void *)buf,
+			       samples_to_bytes(runtime, frames));
+		else if (copy_from_user(hwbuf, buf,
+					samples_to_bytes(runtime, frames)))
+			return -EFAULT;
 	}
 	return 0;
 }
@@ -2105,11 +2110,17 @@ snd_pcm_sframes_t __snd_pcm_lib_write(struct snd_pcm_substream *substream,
 		if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
 		    runtime->channels > 1)
 			return -EINVAL;
-		transfer = snd_pcm_lib_write_transfer;
+		if (substream->ops->copy_silence)
+			transfer = snd_pcm_lib_write_via_ops;
+		else
+			transfer = snd_pcm_lib_write_transfer;
 	} else {
 		if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
 			return -EINVAL;
-		transfer = snd_pcm_lib_writev_transfer;
+		if (substream->ops->copy_silence)
+			transfer = snd_pcm_lib_writev_via_ops;
+		else
+			transfer = snd_pcm_lib_writev_transfer;
 	}
 
 	if (size == 0)
@@ -2207,30 +2218,53 @@ snd_pcm_sframes_t __snd_pcm_lib_write(struct snd_pcm_substream *substream,
 EXPORT_SYMBOL(__snd_pcm_lib_write);
 
 
+static int snd_pcm_lib_read_via_ops(struct snd_pcm_substream *substream,
+				    unsigned int hwoff,
+				    unsigned long data, unsigned int off,
+				    snd_pcm_uframes_t frames, bool in_kernel)
+{
+	char __user *buf = (char __user *) data +
+		frames_to_bytes(substream->runtime, off);
+
+	return substream->ops->copy_silence(substream, -1, hwoff, buf,
+					    frames, in_kernel);
+}
+
 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream, 
 				     unsigned int hwoff,
 				     unsigned long data, unsigned int off,
 				     snd_pcm_uframes_t frames, bool in_kernel)
 {
 	struct snd_pcm_runtime *runtime = substream->runtime;
-	int err;
 	char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
+	char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
 
-	if (substream->ops->copy_silence) {
-		err = substream->ops->copy_silence(substream, -1, hwoff, buf,
-						   frames, in_kernel);
+	if (in_kernel)
+		memcpy((void *)buf, hwbuf, frames_to_bytes(runtime, frames));
+	else if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
+		return -EFAULT;
+	return 0;
+}
+
+static int snd_pcm_lib_readv_via_ops(struct snd_pcm_substream *substream,
+				     unsigned int hwoff,
+				     unsigned long data, unsigned int off,
+				     snd_pcm_uframes_t frames, bool in_kernel)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	int channels = runtime->channels;
+	void __user **bufs = (void __user **)data;
+	char __user *buf;
+	int c, err;
+
+	for (c = 0; c < channels; ++c, ++bufs) {
+		if (!*bufs)
+			continue;
+		buf = *bufs + samples_to_bytes(runtime, off);
+		err = substream->ops->copy_silence(substream, c, hwoff,
+						   buf, frames, in_kernel);
 		if (err < 0)
 			return err;
-	} else {
-		char *hwbuf = runtime->dma_area +
-			frames_to_bytes(runtime, hwoff);
-
-		if (in_kernel)
-			memcpy((void *)buf, hwbuf,
-			       frames_to_bytes(runtime, frames));
-		else if (copy_to_user(buf, hwbuf,
-				      frames_to_bytes(runtime, frames)))
-			return -EFAULT;
 	}
 	return 0;
 }
@@ -2241,41 +2275,25 @@ static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
 				      snd_pcm_uframes_t frames, bool in_kernel)
 {
 	struct snd_pcm_runtime *runtime = substream->runtime;
-	int err;
-	void __user **bufs = (void __user **)data;
 	int channels = runtime->channels;
+	snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
+	void __user **bufs = (void __user **)data;
 	char __user *buf;
 	char *hwbuf;
 	int c;
 
-	if (substream->ops->copy_silence) {
-		for (c = 0; c < channels; ++c, ++bufs) {
-			if (!*bufs)
-				continue;
-			buf = *bufs + samples_to_bytes(runtime, off);
-			err = substream->ops->copy_silence(substream, c, hwoff,
-							   buf, frames,
-							   in_kernel);
-			if (err < 0)
-				return err;
-		}
-	} else {
-		snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
-
-		for (c = 0; c < channels; ++c, ++bufs) {
-			if (!*bufs)
-				continue;
-
-			hwbuf = runtime->dma_area + (c * dma_csize) +
-				samples_to_bytes(runtime, hwoff);
-			buf = *bufs + samples_to_bytes(runtime, off);
-			if (in_kernel)
-				memcpy((void *)buf, hwbuf,
-				       samples_to_bytes(runtime, frames));
-			else if (copy_to_user(buf, hwbuf,
-					      samples_to_bytes(runtime, frames)))
-				return -EFAULT;
-		}
+	for (c = 0; c < channels; ++c, ++bufs) {
+		if (!*bufs)
+			continue;
+		hwbuf = runtime->dma_area + (c * dma_csize) +
+			samples_to_bytes(runtime, hwoff);
+		buf = *bufs + samples_to_bytes(runtime, off);
+		if (in_kernel)
+			memcpy((void *)buf, hwbuf,
+			       samples_to_bytes(runtime, frames));
+		else if (copy_to_user(buf, hwbuf,
+				      samples_to_bytes(runtime, frames)))
+			return -EFAULT;
 	}
 	return 0;
 }
@@ -2300,11 +2318,17 @@ snd_pcm_sframes_t __snd_pcm_lib_read(struct snd_pcm_substream *substream,
 		if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
 		    runtime->channels > 1)
 			return -EINVAL;
-		transfer = snd_pcm_lib_read_transfer;
+		if (substream->ops->copy_silence)
+			transfer = snd_pcm_lib_read_via_ops;
+		else
+			transfer = snd_pcm_lib_read_transfer;
 	} else {
 		if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
 			return -EINVAL;
-		transfer = snd_pcm_lib_readv_transfer;
+		if (substream->ops->copy_silence)
+			transfer = snd_pcm_lib_readv_via_ops;
+		else
+			transfer = snd_pcm_lib_readv_transfer;
 	}
 
 	if (size == 0)
Signature · 1 line
2.13.0