#!/sbin/sh

#############################################################################
# Copyright (c) 2000 Digi International Inc., All Rights Reserved
# 
# This software contains proprietary and confidential information of Digi
# International Inc.  By accepting transfer of this copy, Recipient agrees
# to retain this software in confidence, to prevent disclosure to others,
# and to make no use of this software other than that for which it was
# delivered.  This is an unpublished copyrighted work of Digi International
# Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
# prohibited.
# 
# Restricted Rights Legend
#
# Use, duplication, or disclosure by the Government is subject to
# restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
# Technical Data and Computer Software clause at DFARS 252.227-7031 or
# subparagraphs (c)(1) and (2) of the Commercial Computer Software -
# Restricted Rights at 48 CFR 52.227-19, as applicable.
#
# Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
#
#############################################################################

#############################################################################
#
#  Filename:
#
#     $Id: dgdxb_reg_devs,v 1.4 2000/08/01 16:02:57 jamesp Exp $
#
#  Description:
#
#     Using the device driver internal lists as references, creates
#     the necessary control/support devices for each adapter.
#
#  Author:
#
#     James A. Puzzo, Digi International, Inc.
#
#############################################################################

#
# Global variables
#
DRVMAJOR=""

DRVLISTLOC="/dev/dgdxb/adapt_list"

BDINDEX=0   # the driver assigns adapter numbers in the
            # order in which adapters are detected.

#
# The following "STR" values are taken with intimate knowledge of the
# device numbering described in dgdxb_ops.h
#
DLSTR="03"
MGMTSTR="04"
INFOSTR="06"

load_the_drvlist() {
	if [ ! -e ${DRVLISTLOC} ]
	then
		echo "List file ${DRVLISTLOC} does not exist." >&2
		exit 1
	fi
	
	while read lin
	do
		set - $lin
		if [ $# -lt 2 ] ;then continue ;fi
		dummy=`expr "$1" : '\(.\).*'`
		if [ "$dummy" = "#" ] ;then continue ;fi

		sn="$1"
		echo "Detected adapter with SN: ${sn}"

		BDINDEX=`expr ${BDINDEX} + 1`
	done < ${DRVLISTLOC}
}

create_device() {
	mknod /dev/dgdxb/$1 c $DRVMAJOR $2
	chown 0:0 /dev/dgdxb/$1
	chmod 600 /dev/dgdxb/$1
}

create_info_device() {
	rm -fr /dev/dgdxb
	mkdir /dev/dgdxb

	create_device "adapt_list" "0x00${INFOSTR}00"
}

create_misc_devices() {
	rm -fr /dev/dgdxb
	mkdir /dev/dgdxb

	i=0
	while [ $i -lt $BDINDEX ] ;do
		padi=`expr "00${i}" : '.*\(..\)'`
		j=0
		while [ $j -lt 4 ] ;do
			padj=`expr "00${j}" : '.*\(..\)'`
			create_device "mgmt${i}_${j}" \
			              "0x${padi}${MGMTSTR}${padj}"
			j=`expr $j + 1`
		done
		i=`expr $i + 1`
	done

	create_device "dl" "0x00${DLSTR}00"
	create_device "adapt_list" "0x00${INFOSTR}00"
}

#
#  Main
#

DRVMAJOR=`lsdev 2>&1 | grep dgdxb 2>&1 | awk '{print $1}' 2>&1`
if [ "${DRVMAJOR}" = "" ] ;then
	echo "ERROR: couldn't get driver major number." >&2
	echo "ERROR: aborting dgdxb device registration." >&2
	exit 1
fi

create_info_device
load_the_drvlist
create_misc_devices
