UCM: Support reading MinBufferLevel as low as zero

Value zero was originally treated as a special case (an error). The
value is now passed as a pointer to resolve it.

BUG=chromium:754198
TEST=Unittest, FEATURES="test" emerge-${BOARD} -v media-sound/adhd

Change-Id: Ia45547977a76f7980cadb168fc977c178c828f7d
Signed-off-by: Chun-Ta Lin <itspeter@google.com>
Reviewed-on: https://chromium-review.googlesource.com/619048
Commit-Ready: Chun-ta Lin <itspeter@chromium.org>
Tested-by: Chun-ta Lin <itspeter@chromium.org>
Reviewed-by: Cheng-Yi Chiang <cychiang@chromium.org>
diff --git a/cras/src/server/cras_alsa_io.c b/cras/src/server/cras_alsa_io.c
index 6f5d68c..11e1ff3 100644
--- a/cras/src/server/cras_alsa_io.c
+++ b/cras/src/server/cras_alsa_io.c
@@ -1915,6 +1915,7 @@
 	aio->ucm = ucm;
 	if (ucm) {
 		unsigned int level;
+		int rc;
 
 		aio->dsp_name_default = ucm_get_dsp_name_default(ucm,
 								 direction);
@@ -1924,8 +1925,8 @@
 			aio->base.set_swap_mode_for_node =
 				set_alsa_node_swapped;
 
-		level = ucm_get_min_buffer_level(ucm);
-		if (level && direction == CRAS_STREAM_OUTPUT)
+		rc = ucm_get_min_buffer_level(ucm, &level);
+		if (!rc && direction == CRAS_STREAM_OUTPUT)
 			iodev->min_buffer_level = level;
 
 		aio->enable_htimestamp =
diff --git a/cras/src/server/cras_alsa_ucm.c b/cras/src/server/cras_alsa_ucm.c
index 1057fb9..7926cb1 100644
--- a/cras/src/server/cras_alsa_ucm.c
+++ b/cras/src/server/cras_alsa_ucm.c
@@ -620,16 +620,18 @@
 	return ucm_get_dsp_name(mgr, "", direction);
 }
 
-unsigned int ucm_get_min_buffer_level(struct cras_use_case_mgr *mgr)
+int ucm_get_min_buffer_level(struct cras_use_case_mgr *mgr,
+			     unsigned int *level)
 {
 	int value;
 	int rc;
 
 	rc = get_int(mgr, min_buffer_level_var, "", uc_verb(mgr), &value);
 	if (rc)
-		return 0;
+		return -ENOENT;
+	*level = value;
 
-	return value;
+	return 0;
 }
 
 unsigned int ucm_get_disable_software_volume(struct cras_use_case_mgr *mgr)
diff --git a/cras/src/server/cras_alsa_ucm.h b/cras/src/server/cras_alsa_ucm.h
index 12c653e..2008c54 100644
--- a/cras/src/server/cras_alsa_ucm.h
+++ b/cras/src/server/cras_alsa_ucm.h
@@ -184,12 +184,17 @@
  * unreliable dma residue.
  * Args:
  *    mgr - The cras_use_case_mgr pointer returned from alsa_ucm_create.
+ *    level - The pointer to the returned value.
+ *
  */
-unsigned int ucm_get_min_buffer_level(struct cras_use_case_mgr *mgr);
+int ucm_get_min_buffer_level(struct cras_use_case_mgr *mgr,
+			     unsigned int *level);
 
 /* Gets the flag for disabling software volume.
  * Args:
  *    mgr - The cras_use_case_mgr pointer returned from alsa_ucm_create.
+ * Returns:
+ *    0 on success, -ENOENT on failure.
  */
 unsigned int ucm_get_disable_software_volume(struct cras_use_case_mgr *mgr);
 
diff --git a/cras/src/tests/alsa_io_unittest.cc b/cras/src/tests/alsa_io_unittest.cc
index ca2c9b2..9a3ec2c 100644
--- a/cras/src/tests/alsa_io_unittest.cc
+++ b/cras/src/tests/alsa_io_unittest.cc
@@ -2574,8 +2574,10 @@
   return ucm_enable_swap_mode_ret_value;
 }
 
-unsigned int ucm_get_min_buffer_level(struct cras_use_case_mgr *mgr)
+int ucm_get_min_buffer_level(struct cras_use_case_mgr *mgr,
+			     unsigned int *level)
 {
+  *level = 0;
   return 0;
 }