From 33073a158934dbbcd8eafa026075765cdfb267d8 Mon Sep 17 00:00:00 2001 From: Javier Viguera Date: Tue, 14 Mar 2017 18:27:20 +0100 Subject: [PATCH] meta-digi-dey: enhance BT interface initialization script Implement: * support to start, stop and restart interface * states machine and retries system to recover from command failures * overall simplification and code clean-up https://jira.digi.com/browse/DEL-3858 Signed-off-by: Javier Viguera --- .../bluez/bluez5-5.41/bluetooth-init | 176 ------------------ .../bluez/bluez5-5.41/ccimx6/bluetooth-init | 138 ++++++++++++++ .../bluez/bluez5-5.41/ccimx6ul/bluetooth-init | 162 ++++++++++++++++ 3 files changed, 300 insertions(+), 176 deletions(-) delete mode 100644 meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/bluetooth-init create mode 100644 meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/ccimx6/bluetooth-init create mode 100644 meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/ccimx6ul/bluetooth-init diff --git a/meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/bluetooth-init b/meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/bluetooth-init deleted file mode 100644 index a2f2443bd..000000000 --- a/meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/bluetooth-init +++ /dev/null @@ -1,176 +0,0 @@ -#!/bin/sh -#=============================================================================== -# -# Copyright (C) 2012-2017 by Digi International Inc. -# All rights reserved. -# -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 2 as published by -# the Free Software Foundation. -# -# -# !Description: Initialize bluetooth hardware -# -#=============================================================================== - -set -e - -if [ "${1}" != "start" ]; then - exit 0 -fi - -SCRIPTNAME="$(basename "${0}")" -LOGFILE="/var/log/bluetoothd.log" -FIRMWARE_FILE="/lib/firmware/qca/nvm_tlv_3.2.bin" - -bt_init_qca6564() { - MOD_VERSION="$(($(cat /proc/device-tree/digi,hwid,hv 2>/dev/null || true)))" - - # Reset the BT_EN line - BT_EN_QCA_GPIO_NR="137" - BT_EN_L="/sys/class/gpio/gpio${BT_EN_QCA_GPIO_NR}" - [ -d "${BT_EN_L}" ] || printf "%s" ${BT_EN_QCA_GPIO_NR} > /sys/class/gpio/export - printf out > ${BT_EN_L}/direction && sleep .1 - printf 0 > ${BT_EN_L}/value && sleep .2 - printf 1 > ${BT_EN_L}/value && sleep .2 - [ -d "${BT_EN_L}" ] && printf "%s" ${BT_EN_QCA_GPIO_NR} > /sys/class/gpio/unexport - - # Module version older than revision 4 has swapped TX and RX lines - if [ "${MOD_VERSION}" -lt "4" ]; then - # Workaround to ignore the CTS flow control line - BT_CTS_QCA_GPIO_NR="18" - BT_CTS_L="/sys/class/gpio/gpio${BT_CTS_QCA_GPIO_NR}" - [ -d "${BT_CTS_L}" ] || printf "%s" ${BT_CTS_QCA_GPIO_NR} > /sys/class/gpio/export - printf out > ${BT_CTS_L}/direction && sleep .1 - printf 0 > ${BT_CTS_L}/value && sleep .1 - [ -d "${BT_CTS_L}" ] && printf "%s" ${BT_CTS_QCA_GPIO_NR} > /sys/class/gpio/unexport - # Reduce the rate to avoid the need for HW flow control - BT_RATE="115200" - BT_RATE_CODE="00" # 115200 bps - BT_FLOW="noflow" - # Check the current FW file rate - if [ "$(hexdump -s 56 -n 1 -ve '1/1 "%.2x"' ${FIRMWARE_FILE})" != "${BT_RATE_CODE}" ]; then - # Modify the BT FW file rate - printf "\x${BT_RATE_CODE}" | dd of="${FIRMWARE_FILE}" bs=1 seek=56 count=1 conv=notrunc,fsync 2>/dev/null - fi - fi - - if hciattach -t120 ttyBt qca ${BT_RATE:-3000000} ${BT_FLOW:-flow} 2>${LOGFILE}; then - : # No-op - else - echo "${SCRIPTNAME}: FAILED (hciattach)" - exit - fi - - # Convert the BT address to the hcitool command format. - # Example: "00:04:F3:11:22:33" coverted to "33 22 11 F3 04 00" - HCI_BTADDR="$(echo ${BTADDR} | tr ':' '\n' | tac | tr '\n' ' ' | sed -e 's/ $//g')" - - # Up the interface to be able to send hci commands - if ! hciconfig hci0 up; then - echo "${SCRIPTNAME}: FAILED (hci0 up)" - exit - fi - - # Set the MAC address - if ! hcitool -i hci0 cmd 3F 000B 01 02 06 ${HCI_BTADDR} > /dev/null; then - echo "${SCRIPTNAME}: FAILED (hci set MAC)" - exit - fi - - # HCI Reset - if ! hcitool -i hci0 cmd 03 0003 00 > /dev/null; then - echo "${SCRIPTNAME}: FAILED (hci reset)" - exit - fi - - # Down and up the interface to load the new MAC address - if ! hciconfig hci0 down; then - echo "${SCRIPTNAME}: FAILED (hci0 down)" - exit - fi - - hciconfig hci0 up && echo "${SCRIPTNAME}: OK" || echo "${SCRIPTNAME}: FAILED" -} - -bt_init_ar3k() { - if grep -qs '\' /proc/device-tree/compatible; then - BT_PWR_GPIO_NR="21" - elif grep -qs '\' /proc/device-tree/compatible; then - BT_PWR_GPIO_NR="244" - fi - - # Use a sub-shell here to change to firmware directory - ( - cd /lib/firmware/ar3k/1020200 - - # 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)" - 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) - RETRIES="5" - while [ "${RETRIES}" -gt "0" ]; do - hciattach ttyBt ath3k 4000000 1>${LOGFILE} && break - if [ -n "${BT_PWR_GPIO_NR}" ]; then - # - # If hciattach fails try to recover it by toggling the BT power GPIO - # - BT_PWR_L="/sys/class/gpio/gpio${BT_PWR_GPIO_NR}" - [ -d "${BT_PWR_L}" ] || printf "%s" ${BT_PWR_GPIO_NR} > /sys/class/gpio/export - printf out > ${BT_PWR_L}/direction && sleep .2 - printf 0 > ${BT_PWR_L}/value && sleep .2 - printf 1 > ${BT_PWR_L}/value && sleep .2 - [ -d "${BT_PWR_L}" ] && printf "%s" ${BT_PWR_GPIO_NR} > /sys/class/gpio/unexport - else - sleep .5 - fi - RETRIES="$((RETRIES - 1))" - done - if [ "${RETRIES}" -eq "0" ]; then - echo "${SCRIPTNAME}: FAILED (hciattach)" - exit - fi - if hciconfig hci0 up; then - : # No-op - else - echo "${SCRIPTNAME}: FAILED" - exit - fi -} - -# Check if this hardware does support Bluetooth -if [ -d "/proc/device-tree/bluetooth" ]; then - # Get MAC address from device tree. Use a default value if it has not been set. - if [ -f "/proc/device-tree/bluetooth/mac-address" ]; then - BTADDR="$(hexdump -ve '1/1 "%02X" ":"' /proc/device-tree/bluetooth/mac-address | sed 's/:$//g')" - fi - if [ -z "${BTADDR}" ] || [ "${BTADDR}" = "00:00:00:00:00:00" ]; then - BTADDR="00:04:F3:FF:FF:BB" - fi - - # Initialize for the specific bluetooth chip - if grep -qs '\' /proc/device-tree/compatible; then - bt_init_qca6564 - else - bt_init_ar3k - fi -fi diff --git a/meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/ccimx6/bluetooth-init b/meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/ccimx6/bluetooth-init new file mode 100644 index 000000000..6e43617c6 --- /dev/null +++ b/meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/ccimx6/bluetooth-init @@ -0,0 +1,138 @@ +#!/bin/sh +#=============================================================================== +# +# Copyright (C) 2012-2017 by Digi International Inc. +# All rights reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 as published by +# the Free Software Foundation. +# +# +# !Description: Initialize bluetooth hardware +# +#=============================================================================== + +# Bluetooth power GPIO +BT_PWR_GPIO_NR="244" + +# set_gpio_value +set_gpio_value() { + local SG_GPIONR="${1}" + local SG_GPIOVAL="${2}" + local SG_GPIOPATH="/sys/class/gpio/gpio${SG_GPIONR}" + + [ -d "${SG_GPIOPATH}" ] || printf "%s" "${SG_GPIONR}" > /sys/class/gpio/export + printf out > "${SG_GPIOPATH}/direction" && sleep .2 + printf "${SG_GPIOVAL}" > "${SG_GPIOPATH}/value" && sleep .2 + [ -d "${SG_GPIOPATH}" ] && printf "%s" "${SG_GPIONR}" > /sys/class/gpio/unexport +} + +# powercycle_gpio +powercycle_gpio() { + set_gpio_value "${1}" 0 + set_gpio_value "${1}" 1 +} + +error() { + echo ${1} + exit 1 +} + +bluetooth_init() { + # Get MAC address from the device tree. Use a default value if it has not been set. + BT_MACADDR="$(hexdump -ve '1/1 "%02X" ":"' /proc/device-tree/bluetooth/mac-address 2>/dev/null | sed 's/:$//g')" + if [ -z "${BT_MACADDR}" ] || [ "${BT_MACADDR}" = "00:00:00:00:00:00" ]; then + BT_MACADDR="00:04:F3:FF:FF:BB" + fi + + # Use a sub-shell here to change to firmware directory + ( + cd /lib/firmware/ar3k/1020200 + + # Update the MAC address file only if it has changed. + FW_MAC="ar3kbdaddr.pst" + [ -f "${FW_MAC}" ] && [ "$(cat ${FW_MAC})" = "${BT_MACADDR}" ] || echo ${BT_MACADDR} > ${FW_MAC} + + # Symlink the correct firmware file depending on region code + JPN_REGCODE="0x2" + REGCODE="$(cat /proc/device-tree/digi,hwid,cert 2>/dev/null)" + 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 + 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 bring up the interface + HCIATTACH_LOG="/var/log/hciattach.log" + BT_CMD="HCIATTACH" + RETRIES="5" + while [ "${RETRIES}" -gt "0" ]; do + case "${BT_CMD}" in + HCIATTACH) + # Reset BT + killproc hciattach + powercycle_gpio "${BT_PWR_GPIO_NR}" + if hciattach ttyBt ath3k 4000000 >${HCIATTACH_LOG} 2>&1; then + BT_CMD="HCICONFIG_UP" + else + BT_ERROR="FAILED (hciattach)" + BT_CMD="BT_INIT_FAIL" + fi + ;; + HCICONFIG_UP) + if hciconfig hci0 up; then + break + else + BT_ERROR="FAILED (hciconfig up)" + BT_CMD="BT_INIT_FAIL" + fi + ;; + BT_INIT_FAIL) + RETRIES="$((RETRIES - 1))" + BT_CMD="HCIATTACH" + ;; + esac + done + [ "${RETRIES}" = "0" ] && error "${BT_ERROR}" +} + +# Source function library +. /etc/init.d/functions + +case "$1" in + start) + if [ -d "/proc/device-tree/bluetooth" ]; then + echo -n "Starting bluetooth hardware: " + bluetooth_init + echo "done." + fi + ;; + stop) + if [ -d "/sys/class/bluetooth/hci0" ]; then + echo -n "Stopping bluetooth hardware: " + hciconfig hci0 down || BT_ERROR="FAILED (hciconfig down)" + killproc hciattach + # Power down bluetooth + set_gpio_value "${BT_PWR_GPIO_NR}" 0 + echo "${BT_ERROR:-done.}" + fi + ;; + restart) + $0 stop + sleep 1 + $0 start + ;; + *) + echo "Usage: $0 {start|stop|restart}" + exit 1 + ;; +esac diff --git a/meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/ccimx6ul/bluetooth-init b/meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/ccimx6ul/bluetooth-init new file mode 100644 index 000000000..b325cc9fa --- /dev/null +++ b/meta-digi-dey/recipes-connectivity/bluez/bluez5-5.41/ccimx6ul/bluetooth-init @@ -0,0 +1,162 @@ +#!/bin/sh +#=============================================================================== +# +# Copyright (C) 2012-2017 by Digi International Inc. +# All rights reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 as published by +# the Free Software Foundation. +# +# +# !Description: Initialize bluetooth hardware +# +#=============================================================================== + +# Bluetooth power GPIO +BT_EN_QCA_GPIO_NR="137" + +# set_gpio_value +set_gpio_value() { + local SG_GPIONR="${1}" + local SG_GPIOVAL="${2}" + local SG_GPIOPATH="/sys/class/gpio/gpio${SG_GPIONR}" + + [ -d "${SG_GPIOPATH}" ] || printf "%s" "${SG_GPIONR}" > /sys/class/gpio/export + printf out > "${SG_GPIOPATH}/direction" && sleep .2 + printf "${SG_GPIOVAL}" > "${SG_GPIOPATH}/value" && sleep .2 + [ -d "${SG_GPIOPATH}" ] && printf "%s" "${SG_GPIONR}" > /sys/class/gpio/unexport +} + +# powercycle_gpio +powercycle_gpio() { + set_gpio_value "${1}" 0 + set_gpio_value "${1}" 1 +} + +error() { + echo ${1} + exit 1 +} + +bluetooth_init() { + # Get MAC address from the device tree. Use a default value if it has not been set. + BT_MACADDR="$(hexdump -ve '1/1 "%02X" ":"' /proc/device-tree/bluetooth/mac-address 2>/dev/null | sed 's/:$//g')" + if [ -z "${BT_MACADDR}" ] || [ "${BT_MACADDR}" = "00:00:00:00:00:00" ]; then + BT_MACADDR="00:04:F3:FF:FF:BB" + fi + + # Module version older than revision 4 has swapped TX and RX lines + MOD_VERSION="$(($(cat /proc/device-tree/digi,hwid,hv 2>/dev/null || true)))" + if [ "${MOD_VERSION}" -lt "4" ]; then + # Ignore the CTS flow control + BT_CTS_QCA_GPIO_NR="18" + set_gpio_value "${BT_CTS_QCA_GPIO_NR}" 0 + + # Reduce the rate to avoid the need for HW flow control + BT_RATE="115200" + BT_RATE_CODE="00" # 115200 bps + BT_FLOW="noflow" + + # Modify the baudrate in the firmware file + BT_FW_FILE="/lib/firmware/qca/nvm_tlv_3.2.bin" + if [ "$(hexdump -s 56 -n 1 -ve '1/1 "%.2x"' ${BT_FW_FILE})" != "${BT_RATE_CODE}" ]; then + printf "\x${BT_RATE_CODE}" | dd of="${BT_FW_FILE}" bs=1 seek=56 count=1 conv=notrunc,fsync 2>/dev/null + fi + fi + + # Start the Bluetooth driver and bring up the interface + HCIATTACH_LOG="/var/log/hciattach.log" + BT_CMD="HCIATTACH" + RETRIES="5" + while [ "${RETRIES}" -gt "0" ]; do + case "${BT_CMD}" in + HCIATTACH) + # Reset BT + killproc hciattach + powercycle_gpio "${BT_EN_QCA_GPIO_NR}" + if hciattach ttyBt qca ${BT_RATE:-3000000} ${BT_FLOW:-flow} >${HCIATTACH_LOG} 2>&1; then + BT_CMD="HCICONFIG_UP" + else + BT_ERROR="FAILED (hciattach)" + BT_CMD="BT_INIT_FAIL" + fi + ;; + HCICONFIG_UP) + if hciconfig hci0 up; then + BT_CMD="HCITOOL_SETMAC" + else + BT_ERROR="FAILED (hciconfig up)" + BT_CMD="BT_INIT_FAIL" + fi + ;; + HCITOOL_SETMAC) + # + # Set the MAC address (the interface needs to be up to send HCI commands) + # + # Convert it to the format accepted by 'hcitool' ("00:04:F3:11:22:33" -> "33 22 11 F3 04 00") + # + HCI_MACADDR="$(echo ${BT_MACADDR} | tr ':' '\n' | tac | tr '\n' ' ' | sed -e 's/ $//g')" + if hcitool -i hci0 cmd 3F 000B 01 02 06 ${HCI_MACADDR} >/dev/null; then + BT_CMD="HCITOOL_RESET" + else + BT_ERROR="FAILED (hcitool set MAC)" + BT_CMD="BT_INIT_FAIL" + fi + ;; + HCITOOL_RESET) + if hcitool -i hci0 cmd 03 0003 00 >/dev/null; then + BT_CMD="HCICONFIG_RESET" + else + BT_ERROR="FAILED (hcitool reset)" + BT_CMD="BT_INIT_FAIL" + fi + ;; + HCICONFIG_RESET) + if hciconfig hci0 reset; then + break + else + BT_ERROR="FAILED (hciconfig reset)" + BT_CMD="BT_INIT_FAIL" + fi + ;; + BT_INIT_FAIL) + RETRIES="$((RETRIES - 1))" + BT_CMD="HCIATTACH" + ;; + esac + done + [ "${RETRIES}" = "0" ] && error "${BT_ERROR}" +} + +# Source function library +. /etc/init.d/functions + +case "$1" in + start) + if [ -d "/proc/device-tree/bluetooth" ]; then + echo -n "Starting bluetooth hardware: " + bluetooth_init + echo "done." + fi + ;; + stop) + if [ -d "/sys/class/bluetooth/hci0" ]; then + echo -n "Stopping bluetooth hardware: " + hciconfig hci0 down || BT_ERROR="FAILED (hciconfig down)" + killproc hciattach + # Power down bluetooth + set_gpio_value "${BT_EN_QCA_GPIO_NR}" 0 + echo "${BT_ERROR:-done.}" + fi + ;; + restart) + $0 stop + sleep 1 + $0 start + ;; + *) + echo "Usage: $0 {start|stop|restart}" + exit 1 + ;; +esac