#!/bin/sh

if [ $# -lt 1 ]; then
        echo "Interface argument missing ... /dev/ttyX"
        exit 1
fi

echo 205 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio205/direction

# Reset BT
echo "Resetting BT chip"
echo 0 > /sys/class/gpio/gpio205/value
echo 1 > /sys/class/gpio/gpio205/value

CHIP_REV=`dmesg | grep brcmfm | grep "chip 0x00a9a6" | awk '{print $11}'`
BT_FIRMWARE="bcm43438a0.hcd" # AP6212 chip

if [ "$CHIP_REV" = "0x000001" ]; then
    BT_FIRMWARE="bcm43438a1.hcd" # AP6212A chip
fi

/usr/bin/brcm_patchram_plus -d --patchram /lib/firmware/brcm/$BT_FIRMWARE --no2bytes --tosleep 5000 $1 > /tmp/brcm_log 2>&1
hciattach -n $1 bcm43xx 115200 flow
