#!/bin/sh
#
#	Set TX continuous mode on 88W878x WLAN device
#
# define interface to be used for the test: 
#	STA .... WLAN client device mlanx, or 
#	MMH .... WLAN uAP device uapx 
# STA mode requires support of WLAN ad-hoc mode commands!
#
TSTMODE=STA

#
# define groups of valid channels
#
CHAN_GRP1="0 1 2 3 4 5 6 7 8 9 10 11"
CHAN_GRP2="12 13"
CHAN_GRP3="14"
CHAN_GRP4="36 40 44 48"
CHAN_GRP5="52 56 60 64"
CHAN_GRP6="100 104 108 112 116 120 124 128 132 136 140"
CHAN_GRP7="149 153 157 161 165"

DEBUGFS=`mount | grep debugfs | awk '{ print $3; }`
if [ -z $DEBUGFS ]; then
	DEBUGFS=/debugfs
	mkdir $DEBUGFS
	mount -t debugfs debugfs $DEBUGFS
fi

IFSTA=wlan0
IFUAP=uap0


RW_MAC_REG() {
	if [ $TSTMODE == "STA" ]; then
		echo "1 $@" > $DEBUGFS/mwifiex/$IFSTA/regrdwr
		cat $DEBUGFS/mwifiex/$IFSTA/regrdwr
	else
		./uaputl.exe regrdwr 1 $@
	fi
}
RW_MAC_REG=RW_MAC_REG

RW_BBP_REG() {
	if [ $TSTMODE == "STA" ]; then
		echo "2 $@" > $DEBUGFS/mwifiex/$IFSTA/regrdwr
		cat $DEBUGFS/mwifiex/$IFSTA/regrdwr
	else
		./uaputl.exe regrdwr 2 $@
	fi
}
RW_BBP_REG=RW_BBP_REG

TXOFF() {
	if [ $TSTMODE == "STA" ]; then
		echo 0 > $DEBUGFS/ieee80211/phy0/ibss_enable_all_channels
		iwconfig $IFSTA mode managed
	else
		./uaputl.exe bss_stop
	fi
}
TXOFF=TXOFF

#
# check number of parameters
echo ""
if [ $# -lt 2 ]; then
	echo "Usage: $0 channel rate_index [txpower] [pattern]"
	echo ""
	echo " where channel is 1 - 14 (limited by region settings)"
	echo " where rate_index is"
	echo "        0   1     2    3    4   5   6    7    8    9   10   11   12   13"
	echo "    for 1M, 2M, 5.5M, 11M, 22M, 6M, 9M, 12M, 18M, 24M, 36M, 48M, 54M, 72M"
	echo " where twpower is optional, 2 - 18 (default 18)"
	echo " where pattern is optional, specifying TX data pattern (default zero)"
	echo ""
	echo "Example: $0 1 12    # switch on TX cont @ channel 1, 54 Mbps"
	echo "         $0 0 12    # switch off TX cont (rate_index is ignored)"
	echo ""
	exit 0;
fi
#
# channel validity checking (allow chan==0 as it means off)
#
FREQ=0
for chan in $CHAN_GRP1; do
	if [ $1 -eq $chan ]; then
		FREQ=$((2407+($1*5)))
		COUN="USA, EU, Japan"
	fi
done
#
for chan in $CHAN_GRP2; do
	if [ $1 -eq $chan ]; then
		FREQ=$((2407+($1*5)))
		COUN="EU, Japan"
	fi
done
#
for chan in $CHAN_GRP3; do
	if [ $1 -eq $chan ]; then
		FREQ=$((2484))
		COUN="Japan"
	fi
done
#
for chan in $CHAN_GRP4; do
	if [ $1 -eq $chan ]; then
		FREQ=$((5180+(($1-36)*5)))
		COUN="USA, EU, Japan"
	fi
done
#
for chan in $CHAN_GRP5; do
	if [ $1 -eq $chan ]; then
		FREQ=$((5180+(($1-36)*5)))
		COUN="USA, EU"
	fi
done
#
for chan in $CHAN_GRP6; do
	if [ $1 -eq $chan ]; then
		FREQ=$((5500+(($1-100)*5)))
		COUN="EU"
	fi
done
#
for chan in $CHAN_GRP7; do
	if [ $1 -eq $chan ]; then
		FREQ=$((5745+(($1-149)*5)))
		COUN="USA"
	fi
done
#
if [ $FREQ -eq 0 ]; then
	echo "Invalid channel selected ($1)!"
	exit 1
fi
#
# rate index validity  checking
#
if [ $2 -gt 13 ] || [ $2 -lt 0 ]; then
	echo "Invalid rate index selected ($2)!"
	exit 1;
fi
#
# setup data rate mapping
#
case $2 in
	0) rate=0x0a
	;;
	1) rate=0x14
	;;
	2) rate=0x37
	;;
	3) rate=0x6e
	;;
	4) rate=0xdc
	;;
	5) rate=0x0b
	;;
	6) rate=0x0f
	;;
	7) rate=0x0a
	;;
	8) rate=0x0e
	;;
	9) rate=0x09
	;;
	10) rate=0x0d
	;;
	11) rate=0x08
	;;
	12) rate=0x0c
	;;
	13) rate=0x07
	;;
esac

#
# define TX power level [dBm] used for the test 
# (must match calibration data)
#
# range for 8787: 2-18, default on boot is 18
#
if [ $# -gt 2 ]; then
	if [ $3 -gt 18 ] || [ $3 -lt 2 ]; then
		echo "Invalid txpower selected ($3)!"
		exit 1;
	fi
	TXPOWER=$3
	echo "Using txpower = $TXPOWER"
else
	TXPOWER=18
fi

#
# set data pattern, or use zero as default
#
if [ $# -gt 3 ]; then
	DATA=$4
else
	DATA=0x00
fi

DEBUGFS=`mount | grep debugfs | awk '{ print $3; }`
if [ -z $DEBUGFS ]; then
	DEBUGFS=/debugfs
	mkdir $DEBUGFS
	mount -t debugfs debugfs $DEBUGFS
fi

echo "--- run script in $TSTMODE mode over $IFSTA ---"

if [ $TSTMODE == "STA" ]; then
	# interface needs to be up first
	ifconfig $IFSTA 0.0.0.0 up

	# allow setting any channel in IBSS mode; otherwise only 1-11 are available
	echo 1 > $DEBUGFS/ieee80211/phy0/ibss_enable_all_channels
fi


# disable continuous transmit mode first
$RW_MAC_REG 0xa88c 0x00000000 | tail -1
$RW_MAC_REG 0xa824 0x27000009 | tail -1
$RW_MAC_REG 0xa820 0x00010C00 | tail -1

# clear previous settings
$RW_BBP_REG 0x002a 0x00 | tail -1
#$RW_BBP_REG 0x0140 0x00 | tail -1
#$RW_BBP_REG 0x0294 0x00 | tail -1
$RW_BBP_REG 0x00dd 0x00 | tail -1
$RW_BBP_REG 0x002f 0x6d | tail -1
$RW_BBP_REG 0x0141 0x00 | tail -1
$RW_BBP_REG 0x002c 0x41 | tail -1

# EXIT HERE if only wanted to turn OFF (chan==0)!
if [ $1 -eq 0 ]; then
	$TXOFF
	echo "TX continuous mode deactivated!"
	echo ""
	exit 0;
fi

# set up TX channel
echo "set channel: $1 ($FREQ MHz), valid in $COUN"
if [ $TSTMODE == "STA" ]; then
	iwconfig $IFSTA txpower $TXPOWER
	iwconfig $IFSTA mode ad-hoc
	iwconfig $IFSTA channel $1
	iwconfig $IFSTA essid mrvl878x
else
	./uaputl.exe sys_cfg_tx_power $TXPOWER
	./uaputl.exe sys_cfg_channel $1
	./uaputl.exe bss_start
fi

# disable watch dog
$RW_MAC_REG 0xa820 0x00000000 | tail -1

# bypass 11e timeout, back off, and use reg 0x8000a828 for tx mode
$RW_MAC_REG 0xa824 0x000820a8 | tail -1

# MAC: 0xa828 bit[7] = 1 for CCK, = 0 for OFDM mode
# BBP: 0x002c bit[1:0] = 00 - 802.11a/g, 01 - 802.11b
if [ $2 -lt 4 ]; then
	echo "set to 802.11b mode"
	$RW_MAC_REG 0xa828 0x00000080 | tail -1
	$RW_BBP_REG 0x2c 0x61 | tail -1
else
	echo "set to 802.11g mode"
	$RW_MAC_REG 0xa828 0x00000000 | tail -1
	$RW_BBP_REG 0x2c 0x60 | tail -1
fi

# set tx rate
echo "set tx rate"
$RW_MAC_REG 0xa888 $rate | tail -1

# set tx/rx active
$RW_MAC_REG 0xa650 0x0000 | tail -1

# set test pattern
$RW_MAC_REG 0xa880 $DATA | tail -1

# set length (1 Kbyte)
$RW_MAC_REG 0xa884 0x0400 | tail -1

# 0x08 bit[5:4] = 00 - CCK or OFDM mixed mode
$RW_BBP_REG 0x0008 0x00 | tail -1

$RW_BBP_REG 0x0141 0x08 | tail -1
$RW_BBP_REG 0x002f 0x2d | tail -1

# disable DSP TX from going to stby when TxPE low 
$RW_BBP_REG 0x00dd 0x01 | tail -1

# turn ON cont_tx
$RW_BBP_REG 0x002a 0x04 | tail -1

# toggle the tx mode enable
$RW_MAC_REG 0xa88c 0x0000 | tail -1
$RW_MAC_REG 0xa88c 0x0003 | tail -1

echo "TX continous mode activated!"
echo ""

