[Sound-open-firmware] [PATCH 5/5] host: integrate consume and produce to host_update_buffer and fix for capture

Keyon Jie yang.jie at linux.intel.com
Wed Feb 15 11:07:59 CET 2017


For capture, the read/write pointers are opposite, here we
integrate produce/consume to host_update_buffer(hd, produce)
and introduce local variables w_ptr and r_ptr for more
understandable, we also change 'inline' to real function
to save code size at the same time.

Signed-off-by: Keyon Jie <yang.jie at linux.intel.com>
---
 src/audio/host.c | 46 ++++++++++++++++++++++------------------------
 1 file changed, 22 insertions(+), 24 deletions(-)

diff --git a/src/audio/host.c b/src/audio/host.c
index f814603..62678d2 100644
--- a/src/audio/host.c
+++ b/src/audio/host.c
@@ -89,27 +89,25 @@ struct host_data {
 	struct comp_position cp;
 };
 
-static inline void host_update_buffer_produce(struct host_data *hd)
+static void host_update_buffer(struct host_data *hd, uint32_t produce)
 {
-	if (hd->host_pos_local < hd->host_app_pos)
-		hd->host_avail = hd->host_app_pos - hd->host_pos_local;
-	else if (hd->host_pos_local == hd->host_app_pos)
-		hd->host_avail = hd->host_size; /* full */
-	else
-		hd->host_avail = hd->host_size -hd->host_pos_local +
-			hd->host_app_pos;
-	hd->host_free = hd->host_size - hd->host_avail;
-}
+	uint32_t w_ptr, r_ptr;
+	if (hd->params.direction == STREAM_DIRECTION_PLAYBACK) {
+		/* for playback, app_pos is write pointer, pos_local is read pointer */
+		w_ptr = hd->host_app_pos;
+		r_ptr = hd->host_pos_local;
+	} else {
+		/* for capture, pos_local is write pointer, app_pos is read pointer */
+		w_ptr = hd->host_pos_local;
+		r_ptr = hd->host_app_pos;
+	}
 
-static inline void host_update_buffer_consume(struct host_data *hd)
-{
-	if (hd->host_pos_local < hd->host_app_pos)
-		hd->host_avail = hd->host_app_pos - hd->host_pos_local;
-	else if (hd->host_pos_local == hd->host_app_pos)
-		hd->host_avail = 0; /* empty */
+	if (r_ptr < w_ptr)
+		hd->host_avail = w_ptr - r_ptr;
+	else if (r_ptr == w_ptr)
+		hd->host_avail = produce ? hd->host_size : 0;
 	else
-		hd->host_avail = hd->host_size -hd->host_pos_local +
-			hd->host_app_pos;
+		hd->host_avail = hd->host_size -r_ptr + w_ptr;
 	hd->host_free = hd->host_size - hd->host_avail;
 }
 
@@ -168,7 +166,7 @@ static void host_dma_cb_playback(struct comp_dev *dev,
 	/* buffer overlap ? */
 	if (hd->host_pos_local >= hd->host_size)
 		hd->host_pos_local = 0;
-	host_update_buffer_consume(hd);
+	host_update_buffer(hd, 0); /* consume */
 
 	/* send IPC message to driver if needed */
 	hd->host_period_pos += local_elem->size;
@@ -638,7 +636,7 @@ static int host_prepare(struct comp_dev *dev)
 		hd->params.period_frames * hd->params.frame_size;
 	hd->split_remaining = 0;
 
-	host_update_buffer_consume(hd);
+	host_update_buffer(hd, 0); /* consume */
 
 	dev->preload = PLAT_HOST_PERIODS;
 
@@ -677,7 +675,7 @@ static int host_pointer_reset(struct comp_dev *dev)
 	hd->host_pos_local = 0;
 	hd->host_period_pos = 0;
 
-	host_update_buffer_consume(hd);
+	host_update_buffer(hd, 0); /* consume */
 
 	return 0;
 }
@@ -736,9 +734,9 @@ static int host_cmd(struct comp_dev *dev, int cmd, void *data)
 		hd->host_app_pos = app_pos->position;
 
 		if (hd->params.direction == STREAM_DIRECTION_PLAYBACK)
-			host_update_buffer_produce(hd);
+			host_update_buffer(hd, 1); /* produce */
 		else
-			host_update_buffer_consume(hd);
+			host_update_buffer(hd, 0); /* consume */
 		break;
 	case COMP_CMD_VOLUME:
 		vol_dev = host_volume_component(dev);
@@ -766,7 +764,7 @@ static int host_buffer(struct comp_dev *dev, struct dma_sg_elem *elem,
 	*e = *elem;
 	hd->host_size = host_size;
 
-	host_update_buffer_consume(hd);
+	host_update_buffer(hd, 0); /* consume */
 
 	list_item_append(&e->list, &hd->host.elem_list);
 	return 0;
-- 
2.7.4



More information about the Sound-open-firmware mailing list