Merge "Revert "msm8916: Add kernel headers""
diff --git a/soc/msm8916/init.msm8916.rc b/soc/msm8916/init.msm8916.rc
index 85460d6..28399d1 100644
--- a/soc/msm8916/init.msm8916.rc
+++ b/soc/msm8916/init.msm8916.rc
@@ -14,9 +14,8 @@
 # limitations under the License.
 #
 
-# TODO(ralphnathan): Determine why 0771 (b/24072709)
 on early-init
-    mkdir /firmware 0771 system system
+    mkdir /firmware 0700 system system
 
 on init
     symlink /dev/block/platform/soc.0/7824900.sdhci/by-name /dev/block/by-name
@@ -29,7 +28,7 @@
     mount functionfs adb /dev/usb-ffs/adb uid=2000,gid=2000
     write /sys/class/android_usb/android0/f_ffs/aliases adb
     wait /dev/block/platform/soc.0/7824900.sdhci/by-name/modem
-    mount vfat /dev/block/platform/soc.0/7824900.sdhci/by-name/modem /firmware ro context=u:object_r:firmware_file:s0,shortname=lower,uid=1000,gid=1000,dmask=227,fmask=337
+    mount vfat /dev/block/platform/soc.0/7824900.sdhci/by-name/modem /firmware ro context=u:object_r:firmware_file:s0,shortname=lower,uid=1000,gid=1000,dmask=277,fmask=337
     chown bluetooth net_bt_stack /dev/smd2
     chown bluetooth net_bt_stack /dev/smd3
     chown system system /dev/ion
diff --git a/soc/msm8916/prebuilts/bluetooth.rc b/soc/msm8916/prebuilts/bluetooth.rc
new file mode 100644
index 0000000..89b5257
--- /dev/null
+++ b/soc/msm8916/prebuilts/bluetooth.rc
@@ -0,0 +1,49 @@
+# Copyright (c) 2016, The Linux Foundation. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#    * Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials provided
+#      with the distribution.
+#    * Neither the name of The Linux Foundation nor the names of its
+#      contributors may be used to endorse or promote products derived
+#      from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+on boot
+    start config_bt_addr
+
+service config_bt_addr /system/bin/btnvtool -O
+    class core
+    user bluetooth
+    group system bluetooth
+    oneshot
+
+service hciattach /system/bin/sh /system/etc/init.msm.bt.sh
+    class late_start
+    user bluetooth
+    group bluetooth net_bt_admin
+    seclabel u:r:bluetooth_loader:s0
+    disabled
+    oneshot
+
+on property:bluetooth.hciattach=true
+    start hciattach
+
+on property:bluetooth.hciattach=false
+    setprop bluetooth.status off
diff --git a/soc/msm8916/prebuilts/init.msm.bt.sh b/soc/msm8916/prebuilts/init.msm.bt.sh
new file mode 100644
index 0000000..325a089
--- /dev/null
+++ b/soc/msm8916/prebuilts/init.msm.bt.sh
@@ -0,0 +1,100 @@
+#!/system/bin/sh
+# Copyright (c) 2009-2016, The Linux Foundation. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#    * Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials provided
+#      with the distribution.
+#    * Neither the name of The Linux Foundation nor the names of its
+#      contributors may be used to endorse or promote products derived
+#      from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+LOG_TAG="msm-bluetooth"
+LOG_NAME="${0}:"
+
+loge ()
+{
+  /system/bin/log -t $LOG_TAG -p e "$LOG_NAME $@"
+}
+
+logi ()
+{
+  /system/bin/log -t $LOG_TAG -p i "$LOG_NAME $@"
+}
+
+failed ()
+{
+  loge "$1: exit code $2"
+  exit $2
+}
+
+POWER_CLASS=`getprop qcom.bt.dev_power_class`
+LE_POWER_CLASS=`getprop qcom.bt.le_dev_pwr_class`
+
+setprop ro.qualcomm.bt.hci_transport smd
+TRANSPORT=`getprop ro.qualcomm.bt.hci_transport`
+logi "Transport : $TRANSPORT"
+setprop bluetooth.status off
+
+case $POWER_CLASS in
+  1) PWR_CLASS="-p 0" ;
+     logi "Power Class: 1";;
+  2) PWR_CLASS="-p 1" ;
+     logi "Power Class: 2";;
+  3) PWR_CLASS="-p 2" ;
+     logi "Power Class: CUSTOM";;
+  *) PWR_CLASS="";
+     logi "Power Class: Ignored. Default(1) used (1-CLASS1/2-CLASS2/3-CUSTOM)";
+     logi "Power Class: To override, Before turning BT ON; setprop qcom.bt.dev_power_class <1 or 2 or 3>";;
+esac
+
+case $LE_POWER_CLASS in
+  1) LE_PWR_CLASS="-P 0" ;
+     logi "LE Power Class: 1";;
+  2) LE_PWR_CLASS="-P 1" ;
+     logi "LE Power Class: 2";;
+  3) LE_PWR_CLASS="-P 2" ;
+     logi "LE Power Class: CUSTOM";;
+  *) LE_PWR_CLASS="-P 1";
+     logi "LE Power Class: Ignored. Default(2) used (1-CLASS1/2-CLASS2/3-CUSTOM)";
+     logi "LE Power Class: To override, Before turning BT ON; setprop qcom.bt.le_dev_pwr_class <1 or 2 or 3>";;
+esac
+
+eval $(/system/bin/hci_qcomm_init -e $PWR_CLASS $LE_PWR_CLASS && echo "exit_code_hci_qcomm_init=0" || echo "exit_code_hci_qcomm_init=1")
+
+case $exit_code_hci_qcomm_init in
+  0) logi "Bluetooth QSoC firmware download succeeded, $BTS_DEVICE $BTS_TYPE $BTS_BAUD $BTS_ADDRESS";;
+  *) failed "Bluetooth QSoC firmware download failed" $exit_code_hci_qcomm_init;
+
+     setprop bluetooth.status off
+
+     exit $exit_code_hci_qcomm_init;;
+esac
+
+case $TRANSPORT in
+    "smd")
+        setprop bluetooth.status on
+     ;;
+     *)
+        logi "Transport not supported"
+     ;;
+esac
+
+exit 0
diff --git a/soc/msm8916/prebuilts/sepolicy/bluetooth.te b/soc/msm8916/prebuilts/sepolicy/bluetooth.te
new file mode 100644
index 0000000..a50c0c8
--- /dev/null
+++ b/soc/msm8916/prebuilts/sepolicy/bluetooth.te
@@ -0,0 +1,51 @@
+# Copyright (c) 2016, The Linux Foundation. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#    * Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials provided
+#      with the distribution.
+#    * Neither the name of The Linux Foundation nor the names of its
+#      contributors may be used to endorse or promote products derived
+#      from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# Bluetooth executables and script (init.msm.bt.sh, btnvtool)
+type bluetooth_loader, domain;
+type bluetooth_loader_exec, exec_type, file_type;
+
+init_daemon_domain(bluetooth_loader)
+
+# Access to /data/misc/bluedroid
+allow bluetooth_loader bluetooth_data_file:dir rw_dir_perms;
+allow bluetooth_loader bluetooth_data_file:file create_file_perms;
+allow bluetooth_loader system_data_file:dir r_dir_perms;
+
+# Run init.msm.bt.sh
+allow bluetooth_loader shell_exec:file { entrypoint getattr read };
+allow bluetooth_loader bluetooth_loader_exec:file { execute_no_trans getattr read };
+allow bluetooth_loader sysfs:file rw_file_perms;
+allow bluetooth_loader toolbox_exec:file rx_file_perms;
+
+# Set properties for init
+set_prop(bluetooth_loader, bluetooth_prop)
+
+# Run hci_qcomm_init from init.msm.bt.sh
+domain_auto_trans(bluetooth_loader, hciattach_exec, hciattach)
+allow hciattach bluetooth_loader:fd use;
+allow hciattach bluetooth_loader:fifo_file { write getattr };
diff --git a/soc/msm8916/prebuilts/sepolicy/file_contexts b/soc/msm8916/prebuilts/sepolicy/file_contexts
index 79c9f0a..241febc 100644
--- a/soc/msm8916/prebuilts/sepolicy/file_contexts
+++ b/soc/msm8916/prebuilts/sepolicy/file_contexts
@@ -1,8 +1,8 @@
-# Daemons for audio
+# Daemons for audio.
 /system/bin/qmuxd                                      u:object_r:qmux_exec:s0
 /system/bin/rmt_storage                                u:object_r:rmt_exec:s0
 
-# Daemon for TEE
+# Daemon for TEE.
 /system/bin/qseecomd                                   u:object_r:tee_exec:s0
 
 # Files and symlinks used by qmuxd and rmt_storage.
@@ -34,3 +34,8 @@
 /dev/diag                                              u:object_r:diag_device:s0
 /dev/smem_log                                          u:object_r:smem_log_device:s0
 /dev/qseecom                                           u:object_r:tee_device:s0
+
+# Bluetooth daemons and script.
+/system/bin/btnvtool                                   u:object_r:bluetooth_loader_exec:s0
+/system/bin/hci_qcomm_init                             u:object_r:hciattach_exec:s0
+/system/etc/init\.msm\.bt\.sh                          u:object_r:bluetooth_loader_exec:s0
diff --git a/soc/msm8916/prebuilts/sepolicy/hciattach.te b/soc/msm8916/prebuilts/sepolicy/hciattach.te
new file mode 100644
index 0000000..b120376
--- /dev/null
+++ b/soc/msm8916/prebuilts/sepolicy/hciattach.te
@@ -0,0 +1,48 @@
+# Copyright (c) 2016, The Linux Foundation. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#    * Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials provided
+#      with the distribution.
+#    * Neither the name of The Linux Foundation nor the names of its
+#      contributors may be used to endorse or promote products derived
+#      from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+type hciattach, domain;
+type hciattach_exec, exec_type, file_type;
+
+brillo_domain(hciattach)
+
+allow hciattach shell_exec:file { entrypoint read };
+allow hciattach hciattach_exec:file rx_file_perms;
+
+allow hciattach kernel:system module_request;
+allow hciattach bluetoothtbd_device:chr_file rw_file_perms;
+allow hciattach bluetooth_efs_file:dir r_dir_perms;
+allow hciattach bluetooth_efs_file:file r_file_perms;
+
+allow hciattach self:capability { setuid setgid setpcap };
+
+# Set properties for init.
+set_prop(hciattach, bluetooth_prop)
+
+allow hciattach sysfs:file rw_file_perms;
+allow hciattach tmpfs:file { read write };
+allow hciattach toolbox_exec:file { read getattr };
diff --git a/soc/msm8916/prebuilts/sepolicy/property_contexts b/soc/msm8916/prebuilts/sepolicy/property_contexts
new file mode 100644
index 0000000..330922d
--- /dev/null
+++ b/soc/msm8916/prebuilts/sepolicy/property_contexts
@@ -0,0 +1,31 @@
+# Copyright (c) 2016, The Linux Foundation. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#    * Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials provided
+#      with the distribution.
+#    * Neither the name of The Linux Foundation nor the names of its
+#      contributors may be used to endorse or promote products derived
+#      from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+qualcomm.bluetooth.           u:object_r:bluetooth_prop:s0
+ro.bluetooth.                 u:object_r:bluetooth_prop:s0
+ro.qualcomm.bluetooth.        u:object_r:bluetooth_prop:s0
+ro.qualcomm.bt.hci_transport  u:object_r:bluetooth_prop:s0
diff --git a/soc/msm8916/soc.mk b/soc/msm8916/soc.mk
index 4e4915e..ba73744 100644
--- a/soc/msm8916/soc.mk
+++ b/soc/msm8916/soc.mk
@@ -96,6 +96,11 @@
 PRODUCT_COPY_FILES += \
 	$(LOCAL_PATH)/prebuilts/qseecom.rc:system/etc/init/qseecom.rc \
 
+# Include Bluetooth prebuilts.
+PRODUCT_COPY_FILES += \
+	$(LOCAL_PATH)/prebuilts/bluetooth.rc:system/etc/init/bluetooth.rc \
+	$(LOCAL_PATH)/prebuilts/init.msm.bt.sh:system/etc/init.msm.bt.sh \
+
 PRODUCT_LIBRARY_PATH := $(TOP)/vendor/bsp/qcom/device/dragonboard/linux_410c_board_support_package_LA.BR.1.2.4_rb1.10
 
 # Audio daemons.
@@ -119,10 +124,16 @@
 	$(PRODUCT_LIBRARY_PATH)/lib/libsmemlog.so:/system/lib/libsmemlog.so \
 	$(PRODUCT_LIBRARY_PATH)/lib/libxml.so:/system/lib/libxml.so \
 
+# Bluetooth binaries.
+PRODUCT_COPY_FILES += \
+	$(PRODUCT_LIBRARY_PATH)/bin/btnvtool:/system/bin/btnvtool \
+	$(PRODUCT_LIBRARY_PATH)/bin/hci_qcomm_init:/system/bin/hci_qcomm_init \
+
 # Bluetooth libs.
 PRODUCT_COPY_FILES += \
 	$(PRODUCT_LIBRARY_PATH)/lib/libbtnv.so:/system/lib/libbtnv.so \
 	$(PRODUCT_LIBRARY_PATH)/lib/libbt-vendor.so:/system/lib/libbt-vendor.so \
+	$(PRODUCT_LIBRARY_PATH)/lib/libqcci_legacy.so:/system/lib/libqcci_legacy.so \
 
 # QSEE libs.
 PRODUCT_COPY_FILES += \