#! /bin/bash
# Get the Device information from command lspci
# Created data 2014/6/27 zjb

#Global variables
maxlen_device=14			#The maxium length of device string
maxlen_port=24				#The maxium length of port string
port_bar=0				#The number of ports
str_device=""				#The string for device
str_port=""				#The string for ttyCom*
num=0					#Device number


# Format the string of devices 
function strFormat_device(){
	local str=$1
	local tmpstr="              "
	local len=${#str}
	Sublen=$(($maxlen_device-$len))
	echo $str"${tmpstr:0:$Sublen}"
}

# Format the string of ports 
function strFormat_port(){
	local tmpstr=""
	local len=0
	tmp=${1:0:3}
	if [ $tmp == "COM" ]
	then
		local num_port=`echo $1 | cut -c 5`
	else
		
		local num_port=`echo $1 | cut -c 9`
	fi
	tmp_num=$port_bar
	port_bar=$(($port_bar+$num_port-1))
	str=""ttyCom"$tmp_num"\\t-\\t""ttyCom"$port_bar"
	len=${#str}
	local Sublen=$(($maxlen_port-$len))
	port_bar=$(($port_bar+1))	
	str_port=$str"${tmpstr:0:$Sublen}"
}

# Format the output string
function str_output(){
	if [ ${#deviceInfo[0x$1]} -eq 0 ]
	then
		return 0
	fi
	str_device=`strFormat_device ${deviceInfo[0x$1]}`
	strFormat_port ${deviceInfo[0x$1]}
	echo -e "|""  $str_device""\t|  $2""\t\t|  ""$str_port"" \t|"
}

# Output the device information
function GetDeviceInfo(){
	str_output $1 $2
}

# Check Load Driver Module (For each comdrv** driver)
function ChkDriverModule(){
	local devModule
	local array=("comdrv" "comdrv_cs" "comdrv_ex")
	for listmod in ${array[@]}
	do
		if [ ${#devModule} -eq 0 ]
		then
			devModule=$(lsmod | grep -w ${listmod})
		fi
	done

	if [ ${#devModule} -eq 0 ]
	then
		echo "Driver load  : None."
	else
		echo "Driver load  : Load."
	fi
}

#===================Device Information=============================
## PCI Devices
deviceInfo[0x8111]="COM-4(PCI)H"
deviceInfo[0x8131]="COM-2(PCI)H"
deviceInfo[0x8141]="COM-2PD(PCI)H"
deviceInfo[0x8151]="COM-8(PCI)H"
deviceInfo[0x8161]="COM-4PD(PCI)H"
deviceInfo[0x8171]="COM-1(LPCI)H"
deviceInfo[0x8181]="COM-2(LPCI)H"
deviceInfo[0x8191]="COM-4(LPCI)H"
deviceInfo[0x81A1]="COM-2P(PCI)H"
deviceInfo[0x81B1]="COM-4P(PCI)H"
deviceInfo[0x81D1]="COM-1PD(LPCI)H"
deviceInfo[0x81E1]="COM-2PD(LPCI)H"
deviceInfo[0x81F1]="COM-2CL-PCI"
deviceInfo[0x9101]="COM-4CL-PCI"
deviceInfo[0x9111]="COM-2DL-PCI"
deviceInfo[0x9121]="COM-4DL-PCI"
deviceInfo[0x9131]="COM-8C-LPCI"
## Cardbus Devices
deviceInfo[0x8501]="COM-4(CB)H"
deviceInfo[0x8511]="COM-2(CB)H"
deviceInfo[0x8521]="COM-1(CB)H"
deviceInfo[0x8551]="COM-1PD(CB)H"
## PCI Express Devices
deviceInfo[0x8601]="COM-4C-LPE"
deviceInfo[0x8611]="COM-2C-LPE"
deviceInfo[0x8621]="COM-1C-LPE"
deviceInfo[0x8631]="COM-2C-PE"
deviceInfo[0x8641]="COM-4C-PE"
deviceInfo[0x8651]="COM-8C-PE"
deviceInfo[0x8661]="COM-2PC-PE"
deviceInfo[0x8671]="COM-4PC-PE"
deviceInfo[0x8681]="COM-2PD-PE"
deviceInfo[0x8691]="COM-4PD-PE"
deviceInfo[0X86A1]="COM-8C-LPE"
deviceInfo[0X86B1]="COM-1PDH-LPE"
deviceInfo[0X86C1]="COM-2PD-LPE"
## CPS Devices
deviceInfo[0xE1040003]="CPS-COM-2PC"
deviceInfo[0xE1040004]="CPS-COM-1PC"
deviceInfo[0xE1040005]="CPS-COM-2PD"
deviceInfo[0xE1040006]="CPS-COM-1PD"

#==================================================================

clear					# clear
ChkDriverModule
deviceMM=$(lspci -nd 1221: | awk '{print $3}' | awk -F: '{print $2}')
boardMM=$( lspci -nd 1221: | awk '{if($5~/[0-9a-f][0-9a-f]/){ print $5 }else{ print "00"}}' | awk -F\) '{print $1}')  
SubsystemMM=$(lspci -v | grep Subsystem:\ Contec\ Co | awk '{print $6}')
_deviceMM=($deviceMM)
_boardMM=($boardMM)
SubsystemMM_t=${SubsystemMM:5}
_SubsystemMM=($SubsystemMM_t)
j=0

echo "|===============================================================================|"
echo -e "|  Device\t\t|  Board Id\t|  Port\t\t\t\t\t|"
for i in ${_deviceMM[*]};
do 
	if [ $i == "e104" ]
	then
		for k in ${_SubsystemMM[*]}
		do
			dev_bd="E104"$k
			GetDeviceInfo $dev_bd ${_boardMM[$j]}
		done
	else
		GetDeviceInfo $i ${_boardMM[$j]}
	fi
	let j=j+1
done
echo "|===============================================================================|"

