#!/bin/sh
#
#######################################
# CHANGELOG
#
# 2013/04/24    Ziming Xu <Ziming.b.Xu@alcatel-lucent.com>
#               PR431002: Add FocalTech touchpanel firmwre upgrade function
#
#######################################

#DO NOT EDIT THIS FILE

FT_TPFW_DIR=/firmware/touch_panel
# touchpanel firmware upgrade driver
FT_FWUP_DRV=${FT_TPFW_DIR}/fwup_drv.ko
FT_TPFW_DESC=${FT_TPFW_DIR}/ft_tpfw_desc
# touchpanel firmware upgrade tool
FT_TPFW_UP=/usr/bin/ft_tpfw_up
# touchpanel driver 
FT_TP_DRV=ts_ft5x06
FT_TPDRV_DEV=/dev/tcmdrv_sintek
FT_TPDRV_MAJOR=239
FT_TPDRV_LOADED=false
FT_TPFW=${FT_TPFW_DIR}/ft_app.i
FT_FWUP_DEV=/dev/fwup_dev
# firmware corrupt code
readonly FW_CRPT_CODE=166

#if upgrade in progress then exit 
upgrade_check(){
	STATUS=/usr/sbin/upgd_status
	if [ -x $STATUS ]; then
		$STATUS >/dev/null 2>&1
		if [ $? -eq 1 ]; then 
			echo "upgrade in progress : do not run touchpanel update"
			exit 0
		fi	
	fi
}

install_driver(){
	local major=

	# uninstall touchpanel driver if it's loaded
	if lsmod | awk '{ print $1 }' | grep -q $FT_TP_DRV; then
		FT_TPDRV_LOADED=true
		rmmod $FT_TP_DRV >/dev/null 2>&1
	fi

	# install firmware upgrade driver if it's not installed
	if ! lsmod | awk '{ print $1 }' | grep -q $(basename ${FT_FWUP_DRV%.*}); then
		insmod $FT_FWUP_DRV >/dev/null 2>&1
		if [[ $? -ne 0 ]]; then
			logger -s -t touchpanel -p err "Error: install firmware upgrade module failed!"
			return 1
		fi
	fi

	# lookup major number
	major=$(awk -v drv=$(basename ${FT_FWUP_DRV%.*}) '$2==drv { print $1 }' /proc/devices 2>/dev/null)
	if [[ -z "$major" ]]; then 
		logger -s -t touchpanel -p err "Error: can't get major number for driver!"
		return 1
	fi

	# create device node
	if [[ ! -e $FT_FWUP_DEV ]]; then
		mknod $FT_FWUP_DEV c $major 0
		if [[ $? -ne 0 ]]; then
			logger -s -t touchpanel -p err "Error: create device node failed!"
			return 1
		fi
	fi

	return 0
}

uninstall_driver(){
	# unload driver
	rmmod $(basename ${FT_FWUP_DRV%.*}) 2>/dev/null
	
	# remove device node
	rm -f $FT_FWUP_DEV

	# load touchpanel driver if it's loaded before
	if [[ "$FT_TPDRV_LOADED" == true ]]; then
		modprobe $FT_TP_DRV
		if [[ "$?" -ne 0 ]]; then
			logger -s -t touchpanel -p emerg "Error: recover $FT_TP_DRV failed, reboot is needed"
			return 1
		fi
	fi

	return 0
}

# touchpanel firmware force upgrade
tpfw_force_upgrade() {
	local tpfw_path=$1 ret=0

	# check parameter
	if [[ ! -s "$tpfw_path" ]]; then
		echo "Touchpanel firmware is empty, firmware force upgrade quit."
		return 1
	fi

	# check firmware upgrade driver and tool
	if [[ -s "$FT_FWUP_DRV" ]] && [[ -x "$FT_TPFW_UP" ]]; then
		install_driver
		if [[ $? -ne 0 ]]; then
			logger -s -t touchpanel -p err "Error: upgrade failed to install driver, force upgrade quit."
			uninstall_driver
			return 1
		fi
	else
		logger -s -t touchpanel -p emerg "Error: driver is empty or tool cannot be executed, force upgrade quit."
		return 1
	fi

	echo "Touchpanel firmware upgrade with \"$tpfw_path\" started..."

	# force upgrade with specified firmware
	$FT_TPFW_UP -f $tpfw_path
	ret=$?
	if [[ "$ret" -ne 0 ]]; then
		logger -s -t touchpanel -p emerg "Error: firmware force upgrade failed, firmware may be corrupt!"
	else
		echo "Touchpanel firmware force upgrade succeed!"
	fi

	uninstall_driver

	return $ret
}

# touchpanel firmware automatical upgrade
tpfw_auto_upgrade() {
	local ret=0 max_try=3 counter=0

	# check firmware upgrade driver and tool
	if [[ -s "$FT_FWUP_DRV" ]] && [[ -x "$FT_TPFW_UP" ]]; then
		install_driver
		if [[ $? -ne 0 ]];  then
			logger -s -t touchpanel -p err "Error: install driver failed, firmware automatical upgrade quit."
			uninstall_driver
			return 1
		fi
	else
		logger -s -t touchpanel -p err "Error: driver is empty or tool can't be executed, automatical upgrade quit."
		return 1
	fi

	echo "Firmware upgrade started..."

	# start automatical upgrade, retry "$max_try" times at most if failed
	while [[ "$counter" -lt "$max_try" ]]; do
		counter=$(($counter + 1))
		# start touchpanel firmware automatical upgrade
		$FT_TPFW_UP -a
		ret=$?
		if [[ "$ret" -ne 0 ]]; then
			logger -s -t touchpanel -p emerg "Error: automatical firmware upgrade failed: $counter !"
		else
			echo "Firmware automatical upgrade succeed!"
			break
		fi
	done

	uninstall_driver
	
	return $ret
}

# read touchpanel firmware version, if failed zero will be returned, otherwise a value 
# greater than zero will be returned indicating the version read.
read_fw_version() {
	local version=0

	# read firmware version from driver at first
	if lsmod | grep -q $FT_TP_DRV; then
		if [[ ! -e $FT_TPDRV_DEV ]]; then
			mknod $FT_TPDRV_DEV c $FT_TPDRV_MAJOR 0
		fi

		$FT_TPFW_UP -d
		version=$?
	fi

	# if cannot read version from driver, then read it through i2c
	if [[ $version -le 0 ]]; then
		$FT_TPFW_UP -v
		version=$?
		if [[ $version -le 0 ]]; then
			logger -s -t touchpanel -p emerg "Error: read chip version failed!"
			return 0
		fi
	fi

	echo "Touchpanel firmware version: $version"

	return $version
}

start() {
	local chip_version=0 host_version=0 hw_version= firmware= checksum= calc_cksum=

	# read chip version
	$FT_TPFW_UP -v
	chip_version=$?
	if [[ "$chip_version" -le 0 ]]; then
		logger -s -t touchpanel -p emerg "Error: read chip version failed!"
		return 1
	fi

	# read host version
	if [[ -s "$FT_TPFW_DESC" ]]; then
		read hw_version host_version firmware checksum < $FT_TPFW_DESC
		host_version=$(($host_version))
	else
		logger -s -t touchpanel -p err "Error: description file doesn't exist, try read version from firmware..."
		# read host version from firmware
		host_version=$(awk 'BEGIN { FS=", ?"} END {print $(NF - 2)}' $FT_TPFW 2>/dev/null)
		if [[ "$?" -ne 0 ]]; then
			logger -s -t touchpanel -p emerg "Error: read version from firmware failed."
			return 1
		fi
	fi

	# check firmware in chip is corrupt or not
	if [[ "$chip_version" -eq $FW_CRPT_CODE ]]; then
		logger -s -t touchpanel -p emerg "firmware is corrupt, upgrade is needed!"
	# if chip version is same as host version, skip firmware upgrade
	elif [[ "$chip_version" -gt 0 ]] && [[ "$host_version" -gt 0 ]] && [[ "$chip_version" -ge "$host_version" ]]; then
		echo "Firmware is already latest, firmware upgrade quit."
		return 0
	fi

	# host version is higher than chip version, start firmware upgrade
	tpfw_auto_upgrade
	
	return $?
}

# options and arguments are same as old touchpanel firmware upgrade script to keep interface compatibility
case "$1" in
	start|"")
		# check upgrade first
		upgrade_check
		# if not in upgrade progress, start firmware upgrade
		start
		RET=$?
	;;
	stop)
		if [ -x /etc/init.d/rc.modules.file ]; then
			if [  -f /etc/modules.touchpanel ]; then
				/etc/init.d/rc.modules.file /etc/modules.touchpanel unload /etc/modules.no-unload
			else
				echo "Cannot find the file /etc/modules.touchpanel containing modules"
			fi
		else
			echo "Cannot execute modules.file script"
		fi
	;;
	dwlforce)
		# force chip firmware upgrade to specified firmware
		TPFW_PATH=$2
		if [[ -z "$TPFW_PATH" ]]; then
			echo "Error: missing parameter \"firmware_pathname\""
			RET=1
		else
			# force touchpanel firmware upgrade to specified firmware
			tpfw_force_upgrade $TPFW_PATH
			RET=$?
		fi
	;;
	fwversion)
		# read chip version. 
		read_fw_version
		[[ $? -gt 0 ]] && RET=0 || RET=1
	;;
	*)
		echo "Usage: $0 [start|stop|fwversion]"
		RET=1
	;;
esac

exit $RET
