#! /bin/sh
# Copyright (C) 2005-2012 by Cambridge Silicon Radio Ltd.
# /usr/sbin/unififw
# This is run from the driver via kerneld when a UniFi card
# is inserted.
#
# Usage:  unififw [instance]
#    [instance]     instance number of card. "0" for first card,
#                   "1" for second card.
#    [init mode]    2 for SME/NME
#

# Run body of script in a sub-shell so we can pipe output into logger
(

# Backwards-compatibility, drop any <device> arg
case "$1" in
    unifi_sdio | unifi_pci) shift ;;
esac

instance="0"
if [ -n "$1" ]; then
    instance="$1"
fi
case "$instance" in
    "0"|"1") ;;
    *)  echo "Bad device instance"
        exit 1
        ;;
esac

init_mode="$2"

# Set up local vars
dev=/dev/unifi${instance}
fwpath=/lib/firmware/unifi-sdio-${instance}


# Find files for:
#   localmib.dat  - any local additions
#   ufmib.dat     - standard MIB for the h/w
#   mac.txt       - MAC address (in text format)
#   initprog      - optional program which can do things before first MLME reset

localmib=$fwpath/localmib.dat
if [ ! -f ${localmib} ]; then
localmib=""
fi

mibfile=$fwpath/ufmib.dat
[ -f ${mibfile} ] || mibfile=

## If we have an initprog and it's executable, supply the -x parameter
## to unifi_manager. The main purpose of this is to allow some MIB
## parameters which need setting prior to the first MLME reset to be
## set by the test system without having to rewrite ufmib.dat on the
## fly.
initprog=""
[ -x "${fwpath}/initprog" ] && initprog="-x ${fwpath}/initprog"

# Fallback to the broadcast MAC addr, which UniFi takes to mean "use the
# MAC address from MIB", typically read from EEPROM
macaddr="FF:FF:FF:FF:FF:FF"
macfile=$fwpath/mac.txt
if [ -f $macfile ]; then
    macaddr=`cat $macfile`
fi

# Optional parameters to be passed to the SME.
sme_args=""
sme_args_file=$fwpath/sme_args.txt
if [ -f $sme_args_file ]; then
    sme_args=`cat $sme_args_file`
fi

# wait until the character device node is ready.
dev_node_retries=0
while [ "$dev_node_retries" -lt "3" ]; do
    if [ -e $dev ]; then
        dev_node_retries=3
    else
        echo "unififw: device=$dev is not ready, will wait..."
        sleep 0.5
        dev_node_retries=`expr $dev_node_retries + 1`
    fi
done

# init_mode==2 means an SME userspace build
if [ "${init_mode}" = "2" ]; then
    mibfileparam="--wifi-mib $fwpath/ufmib.dat"
    [ -f $fwpath/ufmib.dat ] || mibfileparam=

    localmibfileparam="--wifi-mib $fwpath/localmib.dat"
    [ -f $fwpath/localmib.dat ] || localmibfileparam=

    /usr/sbin/unifi_helper --wifi-char-device $dev --wifi-on --wifi-restart-on-error --wifi-exit-on-error --wifi-exit-on-unplug --wifi-address $macaddr $mibfileparam $localmibfileparam $sme_args
    exit 0
fi

# Set the MAC address, MIB(s) and start the driver.
/usr/sbin/unifi_manager -d $dev -s $macaddr -b $init_mode $initprog $mibfile $localmib || {
    result=$?
    echo "unifi_manager -d $dev -s $macaddr -b $init_mode $initprog $mibfile $localmib failed: $result"
    exit 2
}

) 2>&1 | logger -t unififw -p daemon.notice

exit 0
