bluez init: rework script

Mostly coding style and simplifications. No functionality change.

Signed-off-by: Javier Viguera <javier.viguera@digi.com>
This commit is contained in:
Javier Viguera 2014-12-23 17:23:34 +01:00
parent d6c2b36c6e
commit 217aadc7ae
1 changed files with 47 additions and 73 deletions

View File

@ -21,112 +21,86 @@ if [ "${1}" != "start" ]; then
exit 0 exit 0
fi fi
SCRIPTNAME="$(basename ${0})" SCRIPTNAME="$(basename "${0}")"
bt_init() { bt_init() {
#
# Exit if this hardware does not support Bluetooth # Exit if this hardware does not support Bluetooth
#
if [ "${MACHINENAME}" = "ccardimx28" ]; then if [ "${MACHINENAME}" = "ccardimx28" ]; then
BLUE_TOOTH_VARIANTS="0x02 0x03 0x10 0x11" BLUETOOTH_VARIANTS="0x02 0x03 0x10 0x11"
MOD_VARIANT="$(cat /proc/device-tree/digi,hwid,variant 2>/dev/null || \ MOD_VARIANT="$(cat /proc/device-tree/digi,hwid,variant 2>/dev/null || \
cat /sys/kernel/${MACHINENAME}/mod_variant)" cat "/sys/kernel/${MACHINENAME}/mod_variant")"
if ! echo ${BLUE_TOOTH_VARIANTS} | grep -qs ${MOD_VARIANT}; then if ! echo "${BLUETOOTH_VARIANTS}" | grep -qs "${MOD_VARIANT}"; then
echo "${SCRIPTNAME}: variant ${MOD_VARIANT} does not support bluetooth" echo "${SCRIPTNAME}: variant ${MOD_VARIANT} does not support bluetooth"
exit exit
fi fi
BT_PWR_GPIO_NR="21" BT_PWR_GPIO_NR="21"
elif [ "${MACHINENAME}" = "ccimx6sbc" ]; then elif [ "${MACHINENAME}" = "ccimx6sbc" ]; then
BLUE_TOOTH_VARIANTS="0x02 0x03 0x04 0x06" BLUETOOTH_VARIANTS="0x02 0x03 0x04 0x06"
MOD_VARIANT="$(cat /proc/device-tree/digi,hwid,variant)" MOD_VARIANT="$(cat /proc/device-tree/digi,hwid,variant)"
if ! echo ${BLUE_TOOTH_VARIANTS} | grep -qs ${MOD_VARIANT}; then if ! echo "${BLUETOOTH_VARIANTS}" | grep -qs "${MOD_VARIANT}"; then
echo "${SCRIPTNAME}: variant ${MOD_VARIANT} does not support bluetooth" echo "${SCRIPTNAME}: variant ${MOD_VARIANT} does not support bluetooth"
exit exit
fi fi
BT_PWR_GPIO_NR="244" BT_PWR_GPIO_NR="244"
fi fi
# # Use a sub-shell here to change to firmware directory
# Get the Bluetooth MAC address from NVRAM. Use a default value if the (
# address has not been set. cd /lib/firmware/ar3k/1020200
#
if [ -f "/proc/device-tree/bluetooth/mac-address" ]; then
BTADDR="$(hexdump -ve '1/1 "%02X" ":"' /proc/device-tree/bluetooth/mac-address | sed 's/:$//g')"
else
BTADDR="$(sed -ne 's,^.*btaddr1=\([^[:blank:]]\+\)[:blank:]*.*,\1,g;T;p' /proc/cmdline)"
fi
if [ -z "${BTADDR}" -o "${BTADDR}" = "00:00:00:00:00:00" ]; then
BTADDR="00:04:F3:FF:FF:BB"
fi
# # Get MAC address from device tree or NVRAM. Use a default value it it has not been set.
# We need to write the Bluetooth MAC address to ar3kbdaddr.pst in if [ -f "/proc/device-tree/bluetooth/mac-address" ]; then
# the AR3k firmware directory. However, we don't want to rewrite the BTADDR="$(hexdump -ve '1/1 "%02X" ":"' /proc/device-tree/bluetooth/mac-address | sed 's/:$//g')"
# file if it already exists and the address is the same because we else
# don't want to wear out NAND flash. So compare the two and only BTADDR="$(sed -ne 's,^.*btaddr1=\([^[:blank:]]\+\)[:blank:]*.*,\1,g;T;p' /proc/cmdline)"
# update the copy on NAND if the address has changed. fi
# if [ -z "${BTADDR}" ] || [ "${BTADDR}" = "00:00:00:00:00:00" ]; then
FW_MAC="/lib/firmware/ar3k/1020200/ar3kbdaddr.pst" BTADDR="00:04:F3:FF:FF:BB"
[ -f "${FW_MAC}" ] && [ "$(cat ${FW_MAC})" = "${BTADDR}" ] || echo ${BTADDR} > ${FW_MAC}
BT_CONFIG_FILE="/lib/firmware/ar3k/1020200/PS_ASIC.pst"
BT_CLASS_1_FILE="/lib/firmware/ar3k/1020200/PS_ASIC_class_1.pst"
BT_CLASS_2_FILE="/lib/firmware/ar3k/1020200/PS_ASIC_class_2.pst"
BT_READ_ME="/lib/firmware/ar3k/1020200/readme.txt"
#
# It is illegal to operate a class 1 Bluetooth device in Japan. So see
# if this is a Japanese unit, and, if so, make sure it is configured
# for class 2 Bluetooth.
#
JAPANESE_REGION_CODE="0x2"
REGION_CODE="$(cat /proc/device-tree/digi,hwid,cert 2>/dev/null || \
cat /sys/kernel/${MACHINENAME}/mod_cert)"
if [ -n "${REGION_CODE}" -a "${JAPANESE_REGION_CODE}" = "${REGION_CODE}" ]; then
#
# We don't want to wear out flash rewriting the configuration file,
# so only replace the configuration file if it is not the class 2
# version.
#
if ! cmp -s ${BT_CLASS_2_FILE} ${BT_CONFIG_FILE}; then
ln -sf $(basename ${BT_CLASS_2_FILE}) ${BT_CONFIG_FILE}
fi fi
#
# We don't want the Bluetooth police to drag away our Japanese
# users, so delete the class 1 configuration file and the readme
# file that refers to it.
#
rm -f ${BT_CLASS_1_FILE} ${BT_READ_ME}
elif [ ! -e ${BT_CONFIG_FILE} ]; then
#
# Default to class 1 Bluetooth for non-japanese users.
#
ln -sf $(basename ${BT_CLASS_1_FILE}) ${BT_CONFIG_FILE}
fi
# # Update the MAC address file only if it has changed.
FW_MAC="ar3kbdaddr.pst"
[ -f "${FW_MAC}" ] && [ "$(cat ${FW_MAC})" = "${BTADDR}" ] || echo ${BTADDR} > ${FW_MAC}
JPN_REGCODE="0x2"
REGCODE="$(cat /proc/device-tree/digi,hwid,cert 2>/dev/null || \
cat "/sys/kernel/${MACHINENAME}/mod_cert")"
BT_CLASS_LINK="PS_ASIC.pst"
BT_CLASS_FILE="PS_ASIC_class_1.pst"
if [ -n "${REGCODE}" ] && [ "${JPN_REGCODE}" = "${REGCODE}" ]; then
BT_CLASS_FILE="PS_ASIC_class_2.pst"
fi
# Replace the configuration file if different
if ! cmp -s ${BT_CLASS_FILE} ${BT_CLASS_LINK}; then
ln -sf ${BT_CLASS_FILE} ${BT_CLASS_LINK}
fi
# Remove not used configuration and readme files
# -- Do not quote the subcommand to avoid leading/trailing whitespace
# -- being part of the file name.
rm -f $(echo PS_ASIC_class_?.pst | sed -e "s,${BT_CLASS_FILE},,g") readme.txt
)
# Start the Bluetooth driver and daemon (D-BUS must already be running) # Start the Bluetooth driver and daemon (D-BUS must already be running)
#
HCIATTACH_ARGS="ttyBt ath3k 4000000"
RETRIES="5" RETRIES="5"
while [ "${RETRIES}" -gt "0" ]; do while [ "${RETRIES}" -gt "0" ]; do
hciattach ${HCIATTACH_ARGS} 1>/dev/null && break hciattach ttyBt ath3k 4000000 1>/dev/null && break
# #
# If hciattach fails try to recover it by toggling the GPIO # If hciattach fails try to recover it by toggling the GPIO
# #
BT_PWR_L="/sys/class/gpio/gpio${BT_PWR_GPIO_NR}" BT_PWR_L="/sys/class/gpio/gpio${BT_PWR_GPIO_NR}"
[ -d "${BT_PWR_L}" ] || echo -n ${BT_PWR_GPIO_NR} > /sys/class/gpio/export [ -d "${BT_PWR_L}" ] || printf "%s" ${BT_PWR_GPIO_NR} > /sys/class/gpio/export
echo -n out > ${BT_PWR_L}/direction && sleep .2 printf out > ${BT_PWR_L}/direction && sleep .2
echo -n 0 > ${BT_PWR_L}/value && sleep .2 printf 0 > ${BT_PWR_L}/value && sleep .2
echo -n 1 > ${BT_PWR_L}/value && sleep .2 printf 1 > ${BT_PWR_L}/value && sleep .2
[ -d "${BT_PWR_L}" ] && echo -n ${BT_PWR_GPIO_NR} > /sys/class/gpio/unexport [ -d "${BT_PWR_L}" ] && printf "%s" ${BT_PWR_GPIO_NR} > /sys/class/gpio/unexport
RETRIES="$((RETRIES - 1))" RETRIES="$((RETRIES - 1))"
done done
if [ "${RETRIES}" -eq "0" ]; then if [ "${RETRIES}" -eq "0" ]; then
echo "${SCRIPTNAME}: FAILED (hciattach)" echo "${SCRIPTNAME}: FAILED (hciattach)"
exit exit
fi fi
BT_FILTER_ARGS="-b -x -s -w wlan0" if ! abtfilt -b -x -s -w wlan0 1>/dev/null; then
if ! abtfilt ${BT_FILTER_ARGS} 1>/dev/null; then
echo "${SCRIPTNAME}: FAILED (abtfilt)" echo "${SCRIPTNAME}: FAILED (abtfilt)"
exit exit
fi fi