#!/bin/sh #=============================================================================== # # Copyright (C) 2025,2026, 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 interface # #=============================================================================== SCRIPTNAME="$(basename ${0})" RETRIES=3 HCI_IFACE="hci0" MODULE_NAME="hci_uart" if grep -qs '\' /proc/device-tree/compatible; then # CCMP13 - BT_REG_EN - GPIO PD13 GPIO_CHIP="GPIOD" GPIO_NUMBER="13" elif grep -qs '\' /proc/device-tree/compatible; then # CCMP15 - BT_REG_EN - GPIO PZ6 GPIO_CHIP="GPIOZ" GPIO_NUMBER="6" elif grep -qs '\' /proc/device-tree/compatible; then # CCIMX95 GPIO_CHIP="1" GPIO_NUMBER="18" fi log() { if type "systemd-cat" >/dev/null 2>/dev/null; then systemd-cat -p "${1}" -t "${SCRIPTNAME}" printf "%s" "${2}" else logger -p "${1}" -t "${SCRIPTNAME}" "${2}" fi } bt_power() { gpioset "${GPIO_CHIP}" "${GPIO_NUMBER}"="${1}" } set_btaddr() { hciconfig hci0 up sleep 0.2 bt_addr=$(hexdump -v -e '1/1 "%02X "' /proc/device-tree/bluetooth/mac-address \ | awk '{ for(i=NF; i>0; i--) printf "0x%s ", $i }' \ | sed 's/ $//') hcitool -i hci0 cmd 0x3f 0x0001 ${bt_addr} hciconfig hci0 down sleep 0.2 hciconfig hci0 up } is_kernel_module_loaded() { lsmod | grep -qs -w "^${MODULE_NAME}" } bt_init() { # If module is already loaded, skip is_kernel_module_loaded && log info "kernel module already present, skipping" && return 1 for i in $(seq ${RETRIES}); do # Perform a power cycle bt_power 0 sleep 0.5 bt_power 1 # Load manually the kernel module modprobe "${MODULE_NAME}" sleep 1 # Reconfigure the HCI interface with the expected MAC address is_kernel_module_loaded && set_btaddr && log info "Bluetooth activated" && return 0 log err "Cannot initialize Bluetooth, retrying..." done log err "Failed to initialize Bluetooth interface" return 1 } bt_stop() { hciconfig ${HCI_IFACE} down # Unload HCI driver instance rmmod hci_uart rmmod btbcm # Disable Bluetooth chip bt_power 0 && log info "Bluetooth disabled" && return 0 } case "$1" in start) bt_init ;; stop) bt_stop ;; restart) $0 stop $0 start ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 ;; esac