#!/bin/sh
#############################################################
# This script programs agcio ftdi GPIOs
#############################################################
path="${0%/*}"
. "$path/inc/gwhw.inc"

needRoot

conf="$path/bin/agcio_ftdi.conf"
devid="s:0x0403:0x6015:AGCIO"

testcmd2 ftdi_eeprom
#############################################################
# Turn ON 
$GPIO -es PERIF_USB_PWR

# check if it is already programmed
if ftdi_eeprom --read-eeprom --device "${devid}" "${conf}"; then
	echo "FTDI is configured"
else
	# most likely not programmed
	ftdi_eeprom --flash-eeprom "${conf}"
fi

# Reset io board, for changes to take effect and to get serial port back
if grep -Fq -- FT230X "/sys/bus/usb/devices/1-1.3/product"; then
	# AGC-BASE-SFP rev4 has new USB port
	echo "1-1.3" > /sys/bus/usb/drivers/usb/unbind
	echo "1-1.3" > /sys/bus/usb/drivers/usb/bind
else
	$GPIO -t PERIF_USB_PWR OFFON
fi
