#!/bin/sh

#----------------------------------------------------------------------
# This shell script installs the the Lattice PCI-E Driver Module
# into the Linux kernel and creates the devices nodes based on the
# dynamically assigned Major number for lscpcie_m.o
# Minor nmbers identify the board type, instance and BAR.
# This version creates 4 SC entries and 4 EC entries, each with 6
# BARs. The base module name is "lscpcie".  The extension is ".ko"
# which represents a kernel object.
#----------------------------------------------------------------------

module="lscpcie"
SCdevice="SC"
ECdevice="EC"
mode="666"

if [ `whoami` != root ]; then
	echo "ERROR! Must be root to install driver."
	exit 1
fi

echo "Installing driver module "${module}" into kernel."

# Install the driver module (pass any command line args - none expected)
/sbin/insmod -f ./${module}.ko $* || exit 1

echo "Creating device directory: /dev/lscpcie"
# Remove old device entries in /dev/lscpcie/ because new major number could
# have been assigned with the above insmod 
if [ -d /dev/lscpcie ]; then
	rm -f /dev/lscpcie/*
else
	mkdir /dev/lscpcie
fi

echo "Discovering device Major number."
# Find the assigned major number by parsing /proc
major=`awk '\$2 == "lscpcie" {print \$1}' /proc/devices`


echo "Creating device nodes."
# Create all the device nodes for EC and SC devices
for brd in SC EC
do 
	for num in 1 2 3 4
	do
		# Add the global board device object
		if [ $brd == SC ]; then
			minor=$(( 0 + $num * 10 + 9 ))
		else
			minor=$(( 100 + $num * 10 + 9 ))
		fi
		/bin/mknod /dev/lscpcie/${brd}${num} c $major $minor 

		# Add a device node for each potential BAR on the board
		for bar in 0 1 2 3 4 5
		do
			node=${brd}${num}_${bar}
			if [ $brd == SC ]; then
				minor=$(( 0 + $num * 10 + $bar ))
			else
				minor=$(( 100 + $num * 10 + $bar ))
			fi
			/bin/mknod /dev/lscpcie/$node c $major $minor
		done
	done
done       


# Make the directory and its files read/write by anyone using the driver
chmod 777 /dev/lscpcie
chmod 666 /dev/lscpcie/*

echo "Done."

