meta-zaius: vcs-control: Add retries for power sequencer commands

For reasons yet fully debugged, PMBus communication with the UCD90160
power sequencer is flaky, with the symptom that some i2c-{get,set}
commands fail or the ucd9000 driver fails to probe.

Add retries in the script that performs these commands as a workaround
to their random failures.

Though this script exits at first error and returns its code, the
systemd services that use it have [Unit] Type=oneshot. So, systemd can
not be configured to restart the script during failures.

Signed-off-by: Xo Wang <xow@google.com>
Change-Id: Ia9eee013001c47ffc9706984b110f312ccdbc175
diff --git a/meta-zaius/recipes-phosphor/chassis/vcs-control/zaius_vcs.sh b/meta-zaius/recipes-phosphor/chassis/vcs-control/zaius_vcs.sh
index fc81f27..fc5dc08 100755
--- a/meta-zaius/recipes-phosphor/chassis/vcs-control/zaius_vcs.sh
+++ b/meta-zaius/recipes-phosphor/chassis/vcs-control/zaius_vcs.sh
@@ -7,24 +7,36 @@
 ucd_addr="0x64"
 ucd_path="/sys/bus/i2c/drivers/ucd9000"
 ucd_driver="${ucd_bus}-00${ucd_addr#0x}"
+ucd_retries="5"
+
+retry()
+{
+    local i=0
+    until [ $i -ge $ucd_retries ]; do
+        i=$((i+1))
+        retry_output=`$@` && break
+    done
+    local ret=$?
+    if [ $i -eq $ucd_retries ]; then exit $ret; fi
+}
 
 # Usage: ucd_get address
 # Result stored in $ucd_reg
 ucd_get()
 {
-    ucd_reg=`i2cget -y $ucd_bus $ucd_addr $1 b`
+    retry i2cget -y $ucd_bus $ucd_addr $1 b
+    ucd_reg=$retry_output
 }
 
 # Usage: ucd_get address value
 ucd_set()
 {
-    i2cset -y $ucd_bus $ucd_addr $1 $2 b
+    retry i2cset -y $ucd_bus $ucd_addr $1 $2 b
 }
 
 unbind_ucd()
 {
-    if [ -e $ucd_path/$ucd_driver ]
-    then
+    if [ -e $ucd_path/$ucd_driver ]; then
         echo -e "\tUnbinding UCD driver $ucd_driver"
         echo $ucd_driver > $ucd_path/unbind
     else
@@ -34,10 +46,13 @@
 
 rebind_ucd()
 {
-    if [ -e $ucd_path ]
-    then
+    if [ -e $ucd_path ]; then
         echo -e "\tBinding UCD driver $ucd_driver"
-        echo $ucd_driver > $ucd_path/bind
+        local i=0
+        until [ -d $ucd_path/$ucd_driver ] || [ $i -ge $ucd_retries ]; do
+            i=$((i+1))
+            echo $ucd_driver > $ucd_path/bind
+        done
     fi
 }
 
@@ -71,13 +86,10 @@
 }
 
 
-if [ "$1" == "on" ]
-then
+if [ "$1" == "on" ]; then
     echo Turning on VCS
     vcs_set_gpios 0x7
-
-elif [ "$1" == "off" ]
-then
+elif [ "$1" == "off" ]; then
     echo Turning off VCS
     vcs_set_gpios 0x3
 else