#! /bin/sh
#----------------------------------------------------------------------
# create-devices
#
# Creation:     2007-10-09 hbfl
# Last Update:  $Id$
#
# Copyright (c) 2010 the eisfair team, team(at)eisfair(dot)org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#---------------------------------------------------------------------
#exec 2>create-devices-trace$$.log
#set -x
FOLDER=/dev/

# create hd devices
rm -f /dev/hd[efgh]{17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}

HD_DEVICES="a:3:0:16
            b:3:64:16
            c:22:0:16
            d:22:64:16
            e:33:0:16
            f:33:64:16
            g:34:0:16
            h:34:64:16
            i:56:0:16
            j:56:64:16
            k:57:0:16
            l:57:64:16
            m:88:0:16
            n:88:64:16
            o:89:0:16
            p:89:64:16
            q:90:0:16
            r:90:64:16
            s:91:0:16
            t:91:64:16"

for hd in ${HD_DEVICES}
do
        _OLD_IFS=${IFS}
        IFS=:
        set -- ${hd}
        if [ ! -b "${FOLDER}hd${1}" ]
        then
                mknod ${FOLDER}hd${1} b ${2} ${3}
        fi
        idx=1
        idy=`/usr/bin/expr ${3} + 1`
        while [ ${idx} -le ${4} ]
        do
                if [ ! -b "${FOLDER}hd${1}${idx}" ]
                then
                        mknod ${FOLDER}hd${1}${idx} b ${2} ${idy}
                fi
                idx=`/usr/bin/expr ${idx} + 1`
                idy=`/usr/bin/expr ${idy} + 1`
        done
        IFS=${_OLD_IFS}
done
chown root.root ${FOLDER}hd{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t}*
chmod 0660 ${FOLDER}hd{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t}*

# create parport devices
idx=0
while [ ${idx} -le 3 ]
do
        if [ ! -c "${FOLDER}parport${idx}" ]
        then
                mknod ${FOLDER}parport${idx} c 99 ${idx}
                chown root.root ${FOLDER}parport${idx}
                chmod 0660 ${FOLDER}parport${idx}
        fi
        idx=`/usr/bin/expr ${idx} + 1`
done

# create pty devices
PTY_DEVICES="a:176
             b:192
             c:208
             d:224
             e:240
             p:0
             q:16
             r:32
             s:48
             t:64
             u:80
             v:96
             w:112
             x:128
             y:144
             z:160"

PTY_SUB_DEVICES="0 1 2 3 4 5 6 7 8 9 a b c d e f"

for pty in ${PTY_DEVICES}
do
        _OLD_IFS=${IFS}
        IFS=:
        set -- ${pty}
        IFS=${_OLD_IFS}
        idx=${2}
        for pty_sub in ${PTY_SUB_DEVICES}
        do
                if [ ! -c "${FOLDER}pty${1}${pty_sub}" ]
                then
                        mknod ${FOLDER}pty${1}${pty_sub} c 2 ${idx}
                fi
                idx=`/usr/bin/expr ${idx} + 1`
        done
done
chown root.tty ${FOLDER}pty*
chmod 0666 ${FOLDER}pty*

# create devices for DAC960/AcceleRAID/eXtremeRAID
dac960_devices_dir='/dev/rd'
mkdir -p ${dac960_devices_dir}
chown root.root ${dac960_devices_dir}
chmod 755 ${dac960_devices_dir}
major=48
for controller in 0 1 2 3 4 5 6 7
do
  minor=0
  for logical_disk in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  do
    for partition in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    do
      if [ "$partition" = "0" ]
      then
        if [ ! -b "${dac960_devices_dir}/c${controller}d${logical_disk}" ]
        then
          mknod ${dac960_devices_dir}/c${controller}d${logical_disk} b ${major} ${minor}
        fi
      else
        if [ ! -b "${dac960_devices_dir}/c${controller}d${logical_disk}p${partition}" ]
        then
          mknod ${dac960_devices_dir}/c${controller}d${logical_disk}p${partition} b ${major} ${minor}
        fi
      fi
      minor=`expr $minor + 1`
    done
  done
  major=`expr $major + 1`
done
chown root.root ${dac960_devices_dir}/c?*d?*
chmod 660 ${dac960_devices_dir}/c?*d?*

#Promise SX8, xaximum number of SX8 controllers per system is 2
sx8_devices_dir='/dev/sx8'
majors='160 161'
# Each controller has 8 channels:
minors='0 32 64 96 128 160 192 224'
# Specify how many partition block devices should be made for each disk (channel) (you may want to change this):
partitions=15
# Make channel counter start at 0
count=0
mkdir -p ${sx8_devices_dir}
chmod 755 ${sx8_devices_dir}
for controller in $majors
do
  for disk in $minors
  do
    #echo "major: $controller, minor: $disk will become /dev/sx8/$count"
    # Create disk block device:
    #echo "Creating disk device: Executing mknod ${sx8_devices_dir}/${count} b ${controller} ${disk}"
    if [ ! -b "${sx8_devices_dir}/${count}" ]
    then
      mknod ${sx8_devices_dir}/${count} b ${controller} ${disk}
    fi
    # Create partition block device(s):
    for part in `seq 1 $partitions`
    do
      minor=`expr $disk + $part`
      #echo "Creating partition device: Executing mknod $sx8_devices_dir/"$count"p"$part" b $controller $minor"
      if [ ! -b "${sx8_devices_dir}/${count}p${part}" ]
      then
        mknod ${sx8_devices_dir}/${count}p${part} b ${controller} ${minor}
      fi
    done
    count=`expr $count + 1`
  done
done
chown root.root ${sx8_devices_dir}/?*
chmod 660 ${sx8_devices_dir}/?*

# coda device
major=67
for minor in 0 1 2 3 4
do
  if [ ! -c "${FOLDER}cfs${minor}" ]
  then
    mknod ${FOLDER}cfs${minor} c 67 ${minor}
  fi
done
chown root.root ${FOLDER}cfs?*
chmod 660 ${FOLDER}cfs?*

# tmpfs
mkdir -p /dev/shm

# create devices for IDE BIOS powered software RAID interfaces
ataraid_devices_dir='/dev/ataraid'
mkdir -p ${ataraid_devices_dir}
chown root.root ${ataraid_devices_dir}
chmod 755 ${ataraid_devices_dir}
major=114
minor=0
for logical_disk in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
  for partition in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  do
    if [ "$partition" = "0" ]
    then
      if [ ! -b "${ataraid_devices_dir}/d${logical_disk}" ]
      then
        mknod ${ataraid_devices_dir}/d${logical_disk} b ${major} ${minor}
      fi
    else
      if [ ! -b "${ataraid_devices_dir}/d${logical_disk}p${partition}" ]
      then
        mknod ${ataraid_devices_dir}/d${logical_disk}p${partition} b ${major} ${minor}
      fi
    fi
    minor=`expr ${minor} + 1`
  done
done
chown root.root ${ataraid_devices_dir}/d?*
chmod 660 ${ataraid_devices_dir}/d?*

# create devices for COMPAQ IDA Controlers
NR_CTLR=1
NR_VOL=8
NR_PART=8
ida_devices_dir='/dev/ida'
if [ ! -d ${ida_devices_dir} ]
then
    mkdir -p ${ida_devices_dir}
fi
C=0
while [ $C -lt $NR_CTLR ]
do
    MAJ=`expr $C + 72`
    D=0
    while [ $D -lt $NR_VOL ]
    do
        P=0
        while [ $P -lt $NR_PART ]
        do
    	    MIN=`expr $D \* 16 + $P`
	    if [ $P -eq 0 ]
	    then
		if [ ! -b "${ida_devices_dir}/c${C}d${D}" ]
    		then
		    mknod ${ida_devices_dir}/c${C}d${D} b $MAJ $MIN
		fi
	    else
		if [ ! -b "${ida_devices_dir}/c${C}d${D}p${P}" ]
    		then
		    mknod ${ida_devices_dir}/c${C}d${D}p${P} b $MAJ $MIN
		fi
    	    fi
    	    P=`expr $P + 1`
	done
        D=`expr $D + 1`
    done
    C=`expr $C + 1`
done
chown root.root ${ida_devices_dir}/c?*
chmod 660 ${ida_devices_dir}/c?*

# create devices for COMPAQ CCISS Controlers
NR_CTLR=1
NR_VOL=8
NR_PART=8
cciss_devices_dir='/dev/cciss'
if [ ! -d ${cciss_devices_dir} ]
then
    mkdir -p ${cciss_devices_dir}
fi
C=0
while [ $C -lt $NR_CTLR ]
do
    MAJ=`expr $C + 104`
    D=0
    while [ $D -lt $NR_VOL ]
    do
        P=0
        while [ $P -lt $NR_PART ]
        do
    	    MIN=`expr $D \* 16 + $P`
	    if [ $P -eq 0 ]
	    then
		if [ ! -b "${cciss_devices_dir}/c${C}d${D}" ]
    		then
		    mknod ${cciss_devices_dir}/c${C}d${D} b $MAJ $MIN
		fi
	    else
		if [ ! -b "${cciss_devices_dir}/c${C}d${D}p${P}" ]
    		then
		    mknod ${cciss_devices_dir}/c${C}d${D}p${P} b $MAJ $MIN
		fi
    	    fi
    	    P=`expr $P + 1`
	done
        D=`expr $D + 1`
    done
    C=`expr $C + 1`
done
chown root.root ${cciss_devices_dir}/c?*
chmod 660 ${cciss_devices_dir}/c?*

# create md devices
for i in 0 1 2 3 4 5 6 7 8 9 10
do
  if [ ! -b "${FOLDER}md${i}" ]
  then
    mknod ${FOLDER}md${i} b 9 ${i}
  fi
done
chown root.root ${FOLDER}md?*
chmod 660 ${FOLDER}md?*

# create framebuffer device
if [ ! -c ${FOLDER}fb0 ]
then
  mknod ${FOLDER}fb0 c 29 0
fi
chown root.root ${FOLDER}fb0
chmod 660 ${FOLDER}fb0

rm -f /dev/ptmx
mknod /dev/ptmx c 5 2
chown root.root /dev/ptmx
chmod 666 /dev/ptmx
mknod /dev/ppp c 108 0 2>/dev/null

# create fuse device
rm -f /dev/fuse
mknod /dev/fuse -m 0666 c 10 229

# create hid devices
hid_devices_dir='/dev/usb'
mkdir -p ${hid_devices_dir}
chown root.root ${hid_devices_dir}
chmod 755 ${hid_devices_dir}
major=180
minor=96
for hid_dev in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
    if [ ! -c ${hid_devices_dir}/hiddev${hid_dev} ]
    then
	mknod ${hid_devices_dir}/hiddev${hid_dev} c ${major} ${minor}
    fi
    minor=`expr ${minor} + 1`
done
chown root.root ${hid_devices_dir}/hiddev?*
chmod 660 ${hid_devices_dir}/hiddev?*

#create event device
event_devices_dir='/dev/input'
mkdir -p ${event_devices_dir}
if [ ! -c ${event_devices_dir}/event0 ]
then
    mknod ${event_devices_dir}/event0 c 13 64
fi
chown root.root ${event_devices_dir}/event0
chmod 660 ${event_devices_dir}/event0