db845c: Add rb5 support in db845c-userdebug build target

* Enabled multiple appended DTBs support to allow db845c
boot.img/vendor_boot.img to boot on rb5 as well.

* Introduced a vendor.hw property which will be used to run
device specific services.

* Folded Caleb's HDMI audio routing fix.

Signed-off-by: Amit Pundir <amit.pundir@linaro.org>
Change-Id: I779d01946ae359d5c405d959419e29460083e5df
diff --git a/audio/audio_hw.c b/audio/audio_hw.c
index c0cd55d..75d3ff0 100644
--- a/audio/audio_hw.c
+++ b/audio/audio_hw.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <sys/time.h>
 #include <unistd.h>
+#include <string.h>
 
 #include <log/log.h>
 #include <cutils/str_parms.h>
@@ -41,6 +42,7 @@
 #include <audio_utils/clock.h>
 #include <audio_utils/echo_reference.h>
 #include <audio_utils/resampler.h>
+#include <cutils/properties.h>
 #include <hardware/audio_alsaops.h>
 #include <hardware/audio_effect.h>
 #include <sound/asound.h>
@@ -1121,6 +1123,9 @@
 static int adev_open(const hw_module_t* module, const char* name,
         hw_device_t** device)
 {
+    char vendor_hw[PROPERTY_VALUE_MAX] = {0};
+    // Prefix for the hdmi path, the board name is the suffix
+    char path_name[256] = "hdmi_";
     ALOGV("adev_open: %s", name);
 
     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) {
@@ -1170,6 +1175,15 @@
         goto error_2;
     }
 
+    /*
+     * To support both the db845c and rb5 we need to used the right mixer path
+     * we do this by checking the hardware name. Which is set at boot time.
+     */
+    property_get("vendor.hw", vendor_hw, "db845c");
+    strlcat(path_name, vendor_hw, 256);
+    ALOGV("%s: Using mixer path: %s", __func__, path_name);
+    audio_route_apply_and_update_path(adev->audio_route, path_name);
+
     pthread_mutex_lock(&adev->lock);
     if (init_aec(CAPTURE_CODEC_SAMPLING_RATE, NUM_AEC_REFERENCE_CHANNELS,
                     CHANNEL_STEREO, &adev->aec)) {
diff --git a/build/tasks/kernel.mk b/build/tasks/kernel.mk
new file mode 100644
index 0000000..959347b
--- /dev/null
+++ b/build/tasks/kernel.mk
@@ -0,0 +1,8 @@
+ifneq ($(filter db845c, $(TARGET_DEVICE)),)
+
+$(PRODUCT_OUT)/dtb.img: $(DB845C_KERNEL_DIR)/sdm845-db845c.dtb
+	-cat $(DB845C_KERNEL_DIR)/sdm845-db845c.dtb $(DB845C_KERNEL_DIR)/qrb5165-rb5.dtb > $@
+
+droidcore: $(PRODUCT_OUT)/dtb.img
+
+endif
diff --git a/db845c/BoardConfig.mk b/db845c/BoardConfig.mk
index 99b94f6..7ef8549 100644
--- a/db845c/BoardConfig.mk
+++ b/db845c/BoardConfig.mk
@@ -19,6 +19,7 @@
 BOARD_KERNEL_CMDLINE := earlycon firmware_class.path=/vendor/firmware/ androidboot.hardware=db845c
 BOARD_KERNEL_CMDLINE += init=/init androidboot.boot_devices=soc@0/1d84000.ufshc printk.devkmsg=on
 BOARD_KERNEL_CMDLINE += deferred_probe_timeout=30
+BOARD_KERNEL_CMDLINE += pcie_pme=nomsi #For WiFi to work on rb5
 
 # Image Configuration
 BOARD_BOOTIMAGE_PARTITION_SIZE := 103079215104 #96M
diff --git a/db845c/device.mk b/db845c/device.mk
index c08bbc4..b8332a0 100644
--- a/db845c/device.mk
+++ b/db845c/device.mk
@@ -34,7 +34,6 @@
 
 PRODUCT_COPY_FILES := \
     $(DB845C_KERNEL_DIR)/Image.gz:kernel \
-    $(DB845C_KERNEL_DIR)/sdm845-db845c.dtb:dtb.img \
     device/linaro/dragonboard/fstab.common:$(TARGET_COPY_OUT_RAMDISK)/fstab.db845c \
     device/linaro/dragonboard/fstab.common:$(TARGET_COPY_OUT_VENDOR)/etc/fstab.db845c \
     device/linaro/dragonboard/init.common.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/init.db845c.rc \
@@ -50,8 +49,9 @@
     android.hardware.boot@1.2-impl.recovery \
     android.hardware.boot@1.2-service
 
-# Install script to set *.usb.controller properties
+# Install scripts to set vendor.* properties
 PRODUCT_COPY_FILES += \
+    device/linaro/dragonboard/qcom/set_hw.sh:$(TARGET_COPY_OUT_VENDOR)/bin/set_hw.sh \
     device/linaro/dragonboard/qcom/set_udc.sh:$(TARGET_COPY_OUT_VENDOR)/bin/set_udc.sh
 
 # Install scripts to set Ethernet MAC address
@@ -66,3 +66,4 @@
 
 # Copy firmware files
 $(call inherit-product-if-exists, vendor/linaro/db845c/$(EXPECTED_LINARO_VENDOR_VERSION)/device.mk)
+$(call inherit-product-if-exists, vendor/linaro/rb5/$(EXPECTED_LINARO_VENDOR_VERSION)/device.mk)
diff --git a/db845c/eth_mac_addr.rc b/db845c/eth_mac_addr.rc
index e710c19..a30c0a5 100644
--- a/db845c/eth_mac_addr.rc
+++ b/db845c/eth_mac_addr.rc
@@ -5,5 +5,5 @@
     disabled
     oneshot
 
-on post-fs-data
+on post-fs-data && property:vendor.hw=db45c
     start eth_mac_addr
diff --git a/etc/mixer_paths.xml b/etc/mixer_paths.xml
index dc28741..966e1ef 100644
--- a/etc/mixer_paths.xml
+++ b/etc/mixer_paths.xml
@@ -1,5 +1,11 @@
-<?xml version="1.0" encoding="ISO-8859-1"?>
 <mixer>
-	<!-- Enable HDMI out -->
-	<ctl name="QUAT_MI2S_RX Audio Mixer MultiMedia1" value="1" />
+    <!-- Enable HDMI out for db845c -->
+    <path name="hdmi_db845c">
+        <ctl name="QUAT_MI2S_RX Audio Mixer MultiMedia1" value="1" />
+    </path>
+
+    <!-- Enable HDMI out for rb5 -->
+    <path name="hdmi_rb5">
+        <ctl name="TERT_MI2S_RX Audio Mixer MultiMedia1" value="1" />
+    </path>
 </mixer>
diff --git a/qcom/init.qcom.rc b/qcom/init.qcom.rc
index f97b502..38d8d00 100644
--- a/qcom/init.qcom.rc
+++ b/qcom/init.qcom.rc
@@ -1,3 +1,10 @@
+service set_hw /vendor/bin/set_hw.sh
+    class core
+    user root
+    group system
+    disabled
+    oneshot
+
 service qrtr-ns /vendor/bin/qrtr-ns -f 1
     class core
     user root
@@ -27,6 +34,7 @@
     oneshot
 
 on early-init
+    start set_hw
     exec - root -- /vendor/bin/qrtr-cfg 1
     start qrtr-ns
 
diff --git a/qcom/set_hw.sh b/qcom/set_hw.sh
new file mode 100644
index 0000000..98f76c3
--- /dev/null
+++ b/qcom/set_hw.sh
@@ -0,0 +1,12 @@
+#! /vendor/bin/sh
+# Set vendor.hw property to run device specific services
+#
+# grep the device name from /proc/device-tree/compatible
+
+HW=`/vendor/bin/cat /proc/device-tree/compatible | /vendor/bin/grep rb5`
+
+if [ -z "${HW}" ]; then
+    setprop vendor.hw db845c
+else
+    setprop vendor.hw rb5
+fi
diff --git a/sepolicy/file.te b/sepolicy/file.te
index 54de779..e5a0bd1 100644
--- a/sepolicy/file.te
+++ b/sepolicy/file.te
@@ -3,6 +3,7 @@
 type sysfs_rmtfs, fs_type, sysfs_type;
 type sysfs_remoteproc, fs_type, sysfs_type;
 type sysfs_udc, fs_type, sysfs_type;
+type sysfs_dt_compatible, fs_type, sysfs_type;
 type dri_device, dev_type;
 type rmtfs_device, dev_type;
 type modem_block_device, dev_type;
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index e8738ac..d7419dc 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -42,6 +42,8 @@
 
 /sys/class/udc										u:object_r:sysfs_udc:s0
 
+/sys/firmware/devicetree/base/compatible						u:object_r:sysfs_dt_compatible:s0
+
 /data/vendor/tmp(/.*)?									u:object_r:tqftpserv_vendor_data_file:s0
 /data/vendor/readwrite(/.*)?								u:object_r:tqftpserv_vendor_data_file:s0
 /data/vendor/readonly(/.*)?								u:object_r:tqftpserv_vendor_data_file:s0
@@ -58,6 +60,7 @@
 /vendor/bin/rmtfs									u:object_r:rmtfs_exec:s0
 /vendor/bin/tqftpserv									u:object_r:tqftpserv_exec:s0
 /vendor/bin/suspend_blocker								u:object_r:suspend_blocker_exec:s0
+/vendor/bin/set_hw\.sh									u:object_r:set_hw_exec:s0
 /vendor/bin/set_udc\.sh									u:object_r:set_udc_exec:s0
 
 /vendor/lib(64)?/dri/.*									u:object_r:same_process_hal_file:s0
diff --git a/sepolicy/genfs_contexts b/sepolicy/genfs_contexts
index 95ce0cc..e8ddb12 100644
--- a/sepolicy/genfs_contexts
+++ b/sepolicy/genfs_contexts
@@ -6,6 +6,7 @@
 genfscon sysfs   /devices/platform/soc@0/17300000.remoteproc					u:object_r:sysfs_remoteproc:s0
 genfscon sysfs   /devices/platform/soc@0/ae00000.mdss						u:object_r:sysfs_gpu:s0
 genfscon sysfs   /class/udc									u:object_r:sysfs_udc:s0
+genfscon sysfs   /firmware/devicetree/base/compatible						u:object_r:sysfs_dt_compatible:s0
 
 #wakeups on db845c
 genfscon sysfs   /devices/platform/soc@0/1c00000.pci/pci0000:00/0000:00:00.0/0000:01:00.0/wakeup/wakeup1					u:object_r:sysfs_wakeup:s0
diff --git a/sepolicy/hal_audio_default.te b/sepolicy/hal_audio_default.te
new file mode 100644
index 0000000..cede442
--- /dev/null
+++ b/sepolicy/hal_audio_default.te
@@ -0,0 +1,2 @@
+# audit2allow
+allow hal_audio_default vendor_hw_prop:file { getattr map open read };
diff --git a/sepolicy/property_contexts b/sepolicy/property_contexts
index 0d68bd3..9c495ab 100644
--- a/sepolicy/property_contexts
+++ b/sepolicy/property_contexts
@@ -1 +1,2 @@
 vendor.usb.controller  u:object_r:vendor_usb_prop:s0
+vendor.hw  u:object_r:vendor_hw_prop:s0
diff --git a/sepolicy/set_hw.te b/sepolicy/set_hw.te
new file mode 100644
index 0000000..996e574
--- /dev/null
+++ b/sepolicy/set_hw.te
@@ -0,0 +1,11 @@
+type set_hw, domain;
+type set_hw_exec, exec_type, vendor_file_type, file_type;
+init_daemon_domain(set_hw);
+
+vendor_public_prop(vendor_hw_prop)
+set_prop(set_hw, vendor_hw_prop)
+get_prop(vendor_init, vendor_hw_prop)
+
+allow set_hw vendor_shell_exec:file rx_file_perms;
+allow set_hw vendor_toolbox_exec:file rx_file_perms;
+allow set_hw sysfs_dt_compatible:file { open read };