#!/bin/sh

detect_conexant_modem()
{
${LSPCI} -x |
${AWK} 'BEGIN { 
					
					modem_count=0;
					AC_link_present = 0;
					AC_link_supported = 0;
					hc_modem_found = 0;
					download_site="http://www.linuxant.com/";

					HCF_DEV["1032"] = "HCF P85 Data/Fax";
					HCF_DEV["1033"] = "HCF P85 Data/Fax";
					HCF_DEV["1034"] = "HCF P85 Data/Fax/Remote TAM";
					HCF_DEV["1035"] = "HCF P85 Data/Fax/Voice/Speakerphone";
					HCF_DEV["1036"] = "HCF P85 Data/Fax/RemoteTam/Speakerphone";
					HCF_DEV["10B3"] = "HCF Data/Fax";
					HCF_DEV["10B4"] = "HCF Data/Fax/Remote TAM";
					HCF_DEV["10B5"] = "HCF Data/Fax/Voice/Speakerphone";
					HCF_DEV["10B6"] = "HCF Data/Fax/RemoteTam/Speakerphone";
					HCF_DEV["1002"] = "HCF Data/Fax";
					HCF_DEV["1003"] = "HCF Data/Fax";
					HCF_DEV["1004"] = "HCF Data/Fax/Remote TAM";
					HCF_DEV["1005"] = "HCF Data/Fax/Voice/Speakerphone";
					HCF_DEV["1006"] = "HCF Data/Fax/Voice/Speakerphone";
					HCF_DEV["1022"] = "HCF Data/Fax";
					HCF_DEV["1023"] = "HCF Data/Fax";
					HCF_DEV["1024"] = "HCF Data/Fax/Remote TAM";
					HCF_DEV["1025"] = "HCF or HSF";
					HCF_DEV["1026"] = "HCF Data/Fax";
					HCF_DEV["1F10"] = "HCF US Data/Fax";
					HCF_DEV["1F11"] = "HCF WW Data/Fax";
					HCF_DEV["1F14"] = "HCF US Data/Fax/Voice";
					HCF_DEV["1F15"] = "HCF WW Data/Fax/Voice";
					HCF_DEV["1052"] = "SmartHCF Data/Fax";
					HCF_DEV["1053"] = "SmartHCF P95 Data/Fax";
					HCF_DEV["1054"] = "SmartHCF P95 Data/Fax/RemoteTam";
					HCF_DEV["1055"] = "SmartHCF P95 Data/Fax/Voice/Speakerphone";
					HCF_DEV["1056"] = "SmartHCF P95 Data/Fax/RemoteTam/Speakerphone";
					HCF_DEV["1057"] = "SmartHCF Data/Fax/RemoteTam/Speakerphone";
					HCF_DEV["1058"] = "SmartHCF P95 Data/Fax/Voice/Speakerphone";
					HCF_DEV["1059"] = "SmartHCF P95 Data/Fax/RemoteTam";
					HCF_DEV["1063"] = "SmartHCF P95 Data/Fax";
					HCF_DEV["1064"] = "SmartHCF P95 Data/Fax/RemoteTam";
					HCF_DEV["1065"] = "SmartHCF P95 Data/Fax/Voice/Speakerphone";
					HCF_DEV["1066"] = "SmartHCF P95 Data/Fax/RemoteTam/Speakerphone";
					HCF_DEV["1085"] = "HCF or HSF";
					HCF_DEV["1453"] = "SmartHCF P95 mini-PCI Data/Fax";
					HCF_DEV["1454"] = "SmartHCF P95 mini-PCI Data/Fax";
					HCF_DEV["1455"] = "SmartHCF P95 mini-PCI Data/Fax";
					HCF_DEV["1456"] = "SmartHCF P95 mini-PCI Data/Fax";
					HCF_DEV["1457"] = "SmartHCF P95 mini-PCI Data/Fax";
					HCF_DEV["1459"] = "SmartHCF P95 mini-PCI Data/Fax";
					HCF_DEV["4321"] = "SmartHCF P95 mini-PCI Data/Fax (RIPTIDE)";
					HCF_DEV["1802"] = "HCF LAN/Modem Combo";
					HCF_DEV["1804"] = "HCF LAN/Modem Combo";
					HCF_DEV["1805"] = "HCF LAN/Modem Combo";
					HCF_DEV["1803"] = "HCF LAN/Modem Combo";
					HCF_DEV["1813"] = "HCF LAN/Modem Combo";
					HCF_DEV["1815"] = "HCF LAN/Modem Combo";


					HSF_DEV["2013"] = "HSF Data/Fax";
					HSF_DEV["2014"] = "HSF Data/Fax/TAM";
					HSF_DEV["2015"] = "HSF Data/Fax/Voice/Speakerphone";
					HSF_DEV["2016"] = "HSF Data/Fax/TAM/Speakerphone";
					HSF_DEV["4311"] = "HSF Data/Fax/TAM/Speakerphone (RIPTIDE)";
					HSF_DEV["2003"] = "HSF Data/Fax";
					HSF_DEV["2004"] = "HSF Data/Fax/TAM";
					HSF_DEV["2005"] = "HSF Data/Fax/Voice/Speakerphone or HCF";
					HSF_DEV["2006"] = "HSF Data/Fax/TAM/Speakerphone";
					HSF_DEV["2F00"] = "HSFi Data/Fax";
					HSF_DEV["2F01"] = "HSFi Data/Fax";
					HSF_DEV["2F02"] = "HSFi Data/Fax";
					HSF_DEV["2F03"] = "HSFi Data/Fax";
					HSF_DEV["2F04"] = "HSFi Data/Fax";
					HSF_DEV["2F10"] = "HSF Data/Fax";
					HSF_DEV["2F11"] = "HSF Data/Fax";
					HSF_DEV["2F12"] = "HSF Data/Fax";
					HSF_DEV["2F13"] = "HSF Data/Fax";
					HSF_DEV["2F14"] = "HSF Data/Fax";
					HSF_DEV["2702"] = "HSF Data/Fax";
					HSF_DEV["2F20"] = "HSF Data/Fax";
					HSF_DEV["2043"] = "SmartHSF Data/Fax";
					HSF_DEV["2044"] = "SmartHSF Data/Fax/TAM";
					HSF_DEV["2045"] = "SmartHSF Data/Fax/Voice/Speakerphone";
					HSF_DEV["2046"] = "SmartHSF Data/Fax/TAM/Speakerphone";
					HSF_DEV["2053"] = "SmartHSF Data/Fax";
					HSF_DEV["2054"] = "SmartHSF Data/Fax/TAM";
					HSF_DEV["2055"] = "SmartHSF Data/Fax/Voice/Speakerphone";
					HSF_DEV["2056"] = "SmartHSF Data/Fax/TAM/Speakerphone";
					HSF_DEV["2063"] = "SmartHSF Data/Fax";
					HSF_DEV["2064"] = "SmartHSF Data/Fax/TAM";
					HSF_DEV["2065"] = "SmartHSF Data/Fax/Voice/Speakerphone";
					HSF_DEV["2066"] = "SmartHSF Data/Fax/TAM/Speakerphone";
					HSF_DEV["2343"] = "SmartHSF Data/Fax";
					HSF_DEV["2344"] = "SmartHSF Data/Fax/TAM";
					HSF_DEV["2345"] = "SmartHSF Data/Fax/Voice/Speakerphone";
					HSF_DEV["2346"] = "SmartHSF Data/Fax/TAM/Speakerphone";
					HSF_DEV["2363"] = "SmartHSF Data/Fax";
					HSF_DEV["2364"] = "SmartHSF Data/Fax/TAM";
					HSF_DEV["2365"] = "SmartHSF Data/Fax/Voice/Speakerphone";
					HSF_DEV["2366"] = "SmartHSF Data/Fax/TAM/Speakerphone";
					HSF_DEV["2443"] = "SmartHSF Data/Fax";
					HSF_DEV["2444"] = "SmartHSF Data/Fax/TAM";
					HSF_DEV["2445"] = "SmartHSF Data/Fax/Voice/Speakerphone";
					HSF_DEV["2446"] = "SmartHSF Data/Fax/TAM/Speakerphone";
					HSF_DEV["2463"] = "SmartHSF Data/Fax";
					HSF_DEV["2464"] = "SmartHSF Data/Fax/TAM";
					HSF_DEV["2465"] = "SmartHSF Data/Fax/Voice/Speakerphone";
					HSF_DEV["2466"] = "SmartHSF Data/Fax/TAM/Speakerphone";
					HSF_DEV["1631"] = "SmartHSF Data/Fax";
					HSF_DEV["2093"] = "SmartHSF Data/Fax";
					HSF_DEV["201A"] = "SmartHSF Data/Fax";
					HSF_DEV["201B"] = "SmartHSF Data/Fax";
					HSF_DEV["204A"] = "SmartHSF Data/Fax";
					HSF_DEV["204B"] = "SmartHSF Data/Fax";

} 
{
	if ( $1 ~ /^00:0/ )
	{
	    if (/\(rev [a-zA-Z0-9]+\)$/)	
			revision = toupper (gsub(")", "", $NF))
		else
			revision = "NONE"
		next
	}

	if ( $1 ~ /^00:$/ )
	{
		vendor = toupper ($3 $2)
	    device_id = toupper ($5 $4)
		class_code = toupper($13)
		next
	}

	if ( $1 ~ /^20:$/ )
	{
		sub_vendor = toupper ($15 $14)
	    sub_device_id = toupper ($17 $16)

		if (class_code !~ "07") # not a communication device
			next

		# Conexant communication device
		if (vendor ~ "14F1" ||  vendor ~ "127A" ) 
		{
			rev_arr[modem_count]        = revision
			vendor_arr[modem_count]     = vendor
			device_arr[modem_count]     = device_id
			sub_vendor_arr[modem_count] = sub_vendor
			sub_device_arr[modem_count] = sub_device_id
			if (device_id in HSF_DEV)
			{
			    modem_type[modem_count] = "HSF"
			}
			else if (device_id in HCF_DEV )
			{
				modem_type[modem_count] = "HCF"
				hcf_modem_found = 1
			}
			else
			{
				modem_type[modem_count] = "Unknown communication device"
			}
			vendor_name[modem_count] = "CONEXANT"
			modem_count++
			next
		}
		# check for AC-link support
		else if ( (vendor ~ "8086" && (
			device_id ~ "7186" ||
			device_id ~ "7196" ||
			device_id ~ "2416" ||
			device_id ~ "2426" ||
			device_id ~ "2446" ||
			device_id ~ "2486" ||
			device_id ~ "24C6" ||
			device_id ~ "24D6" ||
			device_id ~ "266D") ) || # Intel
			      ((vendor ~ "1025" || vendor ~ "10B9") && ( device_id ~ "5453" || device_id ~ "5457" ) )|| # Ali, Ali+
			      (vendor ~ "1106" && device_id ~ "3068")  ||  # Via 
			      (vendor ~ "10DE" && device_id ~ "00D9")  ||  # Nvidia
			      (vendor ~ "10DE" && device_id ~ "01C1")  ||  # Nvidia
			      (vendor ~ "1002" && device_id ~ "434D")  ||  # ATI
			      (vendor ~ "1039" && device_id ~ "7013"))     # Sis
			
		{
			AC_link_supported = 1
			AC_link_present = 1
			next
		}
	}
}
END {
	print "============================================================="
	print "=                  RESULT OF MODEM QUERY                    ="
	print "============================================================="
	print ""	

	# Conexant PCI modem was found
	if (modem_count)
	{
		for (k = 0; k < modem_count ; k++)
		{
      		print "MODEM #" k+1 ":"
	    	print "  PCI CONFIGURATION INFORMATION READ:"
	    	print "     Vendor Id         : " vendor_arr[k]
	    	print "     Device Id         : " device_arr[k]
		    print "     Revision Id       : " rev_arr[k]
		    print "     Sub Vendor Id     : " sub_vendor_arr[k]
		    print "     Sub Device Id     : " sub_device_arr[k]
			print ""
			print "  DEDUCED INFORMATION:"
			print "     Vendor Name       : " vendor_name[k]
			if ( modem_type[k] ~ "HSF" )
					print "     Device Name       : " HSF_DEV[ device_arr[k] ] 
			else if ( modem_type[k] ~ "HCF" )
					print "     Device Name       : " HCF_DEV[ device_arr[k] ] 
			print "     Modem Type        : " modem_type[k]
			if ( ( modem_type[k] ~ "HSF" ) || ( modem_type[k] ~ "HCF" ) )
				print "     Download Site     : " download_site
			else
			{
				print ""
				print "     ATTENTION: An unknown communication device (modem)"
				print "     was detected. You can download and install the Conexant"
				print "     driver to find out if it works with your modem device."
				print ""
				print "     Download Site     : " download_site
			}	
			print ""
			print ""
			print ""
		}
	}

	# AC-link modem was detected
	if (AC_link_present)
	{
		if (AC_link_supported)
		{
			print "An AC-link communication device (Modem) was detected."
			print "This utility cannot determine if it is a Conexant"
			print "manufactured modem."
			print "You can download the Conexant driver from: " download_site
			print "and install it on your system."
			print "The driver will work only if you have a Conexant modem."
		}
		else
		{
			print "The utility detected an AC-link modem on your system."
			print "Currently Conexant Linux modem driver does not support"
			print "this vendor platform."
		}
		print ""
		print ""

	} else {
		if (modem_count == 0 && AC_link_present) {
			print "     No Conexant Modem found on this system."
			print ""
			print ""
		}
	}
	
}'
}

show_intro()
{
	echo "Conexant Modem Detection Utility, version ${util_version}"
}

show_usage_and_exit()
{
	show_intro

	cat 1>&2 << EOT

Usage: `basename $0` [options]

  -d, --detect                 Detects Conexant modem devices on the system,
                               Currentlly there isn't a full support for
                               AC-link devices
                               This flag is set by default unless other option
                               was defined

  -h, --help                   This small usage guide

  -v, --version                Show Detection utility version

EOT
	exit 1
}

print_download_site()
{
	echo "Conexant download site is $download_site"
}

PATH=/usr/sbin:/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:
export PATH

LSPCI="lspci"
AWK="awk"
util_version="1.2"
explicitopt=false
do_detection=false

TEMP=`getopt -a -o dhv --long version,help,detect -n "\`basename $0\`" -- "$@"`
if [ $? != 0 ]; then
	show_usage_and_exit
fi

eval set -- "$TEMP"


while true ; do
	case "$1" in
		-d|--detect) explicitopt=true; do_detection=true; shift ;;
		-h|--help) show_usage_and_exit; shift ;;
		-v|--version) echo "${util_version}"; exit 0;;
		--) shift ; break ;;
		*) echo "Internal error!" ; exit 1 ;;
	esac
done


# If no option was choosed
if ! ${explicitopt}; then
	do_detection=true
fi

# Do the modem detection
if ${do_detection}; then
	detect_conexant_modem || exit $?
fi


