#!/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