#!/bin/sh
#
# MAKEDEV
#
# Create all necessary '/dev/*' stuff.
# This includes all required device files.
# This will create everything from scratch, including owner.group and
# permissions.
# Adtec Digital, Inc. (c) 2003-2007
# Andre G Ancelin
# Mike Goins

##############################################################################
# Make sure running as root.
#
  if [ "$UID" -ne 0 ] ; then
   echo "Must run this script as root."
	exit $E_NOT_ROOT
  fi    

##############################################################################
# Do not execute in /.
#
  if [ "$PWD" == "/" ] ; then
    echo "In root directory, execution not allowed!"
    echo "This script is TOO dangerous to run here, since it runs as root."
	echo "Abandoning execution."
	exit $E_ROOT_DIRECTORY    
  fi
	

##############################################################################
# Make a "dev" directory and move to it.
#
${RFSDIR}/bin/mkdir -m775 dev
${RFSDIR}/bin/chown -f 0:0 dev
${RFSDIR}/bin/chmod 775 dev   # just in case
cd dev
${RFSDIR}/bin/rm -rf *

##############################################################################
# Miscellaneous odds and ends devices.
#
${RFSDIR}/bin/mknod -m666 null    c 1  3
${RFSDIR}/bin/mknod -m666 zero    c 1  5
${RFSDIR}/bin/mknod -m600 initctl p
${RFSDIR}/bin/mknod -m600 rtc     c 10 135

##############################################################################
# Terminals and consoles.
#
owner_group="0:0"
permission="666"

# Basic terminals and serial ports.
${RFSDIR}/bin/mknod -m$permission tty   c 5 0
for ((device=0; device <= 1; device++)) ; do
  ${RFSDIR}/bin/mknod -m$permission tty$device  c 4 $device
  ${RFSDIR}/bin/mknod -m$permission ttyS$device c 4 $(($device+64))
done

# These are the master/slave virtual terminals.
# Not sure if we need these, check to see if Rick needs any.
${RFSDIR}/bin/mknod -m$permission ptmx  c 5 2
terminals="0 1 2 3 4 5 6 7 8 9 a b c d e f"
let i=0
for terminal in $terminals ; do
  ${RFSDIR}/bin/mknod -m$permission ptyp$terminal c 2 $i
  ${RFSDIR}/bin/mknod -m$permission ttyp$terminal c 3 $i
  let i+=1
done

# Console is on the first serial port.
${RFSDIR}/bin/ln -s ttyS0 console

# Setup the owner and group
${RFSDIR}/bin/chown -f $owner_group tty tty? ttyS? ptmx ptyp? ttyp?

##############################################################################
# Disc devices, all block devices.
#
owner_group="root.disk"
permission="660"

# RAM disc.
${RFSDIR}/bin/mknod -m$permission ram b 1 0

permission="644"
# Parallel drives.
a="a  3  0"
b="b  3 64"
c="c 22  0"
d="d 22 64"
e="e 33  0"
f="f 33 64"
g="g 34  0"
h="h 34 64"
i="i 56  0"
j="j 563 64"
partitions="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
for device_major_minor in "$a" "$b" "$c" "$d" "$e" "$f" "$g" "$h" "$i" "$j"; do
  set -- $device_major_minor
  ${RFSDIR}/bin/mknod -m$permission hd$1 b $2 $3
  for partition in $partitions ; do
	${RFSDIR}/bin/mknod -m$permission hd$1$partition b $2 $(($partition+$3))
  done
done

# Serial drives.
a="a 8  0"
b="b 8 16"
c="c 8 32"
d="d 8 48"
partitions="1 2 3 4 5 6 7"
for device_major_minor in "$a" "$b" "$c" "$d" ; do
  set -- $device_major_minor
  ${RFSDIR}/bin/mknod -m$permission sd$1 b $2 $3
  for partition in $partitions ; do
	${RFSDIR}/bin/mknod -m$permission sd$1$partition b $2 $(($partition+$3))
  done
done

#raid
${RFSDIR}/bin/mknod -m$permission md0 b 9 0 
${RFSDIR}/bin/mknod -m$permission md1 b 9 1 
${RFSDIR}/bin/mknod -m$permission md2 b 9 2
${RFSDIR}/bin/mknod -m$permission md3 b 9 3



# Setup the owner and group.
#chown -f $owner_group ram hd[a-f]*


##############################################################################
# Character devices

${RFSDIR}/bin/mknod -m666 fuse c 10 229
${RFSDIR}/bin/mknod -m660 random c 1 8
${RFSDIR}/bin/mknod -m644 urandom c 1 9
${RFSDIR}/bin/mknod -m644 mem c 1 1

##############################################################################
# Processor specific drivers for the PPC405.
#
owner_group="0:0"
permission="755"

# Create 'dpi' subdirectory.
subdir="dpi"
${RFSDIR}/bin/mkdir $subdir
${RFSDIR}/bin/chmod $permission $subdir
${RFSDIR}/bin/chown $owner_group $subdir

# Devices: major & minor.
permission="666"
gpio21="gpio21 100 149"
gpio23="gpio23 100 151"
for device in "$gpio21" "$gpio23"; do
  set -- $device
  ${RFSDIR}/bin/mknod -m$permission $subdir/$1 c $2 $3
done

permission="644"
streams="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"
for stream in $streams; do
  let "minor = 10#$stream"
  ${RFSDIR}/bin/mknod -m${permission} ${subdir}/stream$(printf %.2d ${stream}) c 100 $minor
done
${RFSDIR}/bin/chown -f $owner_group $subdir/*


##############################################################################
# parport
#
owner_group="0:0"
permission="755"

# Create 'parport' subdirectory.
subdir="parport"
${RFSDIR}/bin/mkdir $subdir
${RFSDIR}/bin/chmod $permission $subdir
${RFSDIR}/bin/chown $owner_group $subdir

# Devices: major & minor.
permission="666"
parallelport="parallelport 233 0"
parchar="parchar 233 10"
for device in "$parallelport" "$parchar"; do
  set -- $device
  ${RFSDIR}/bin/mknod -m$permission $subdir/$1 c $2 $3
done

pars="1 2 3 4 5 6 7 8 9"
for par in $pars; do
  ${RFSDIR}/bin/mknod -m${permission} ${subdir}/parpin${par} c 233 ${par}
done
${RFSDIR}/bin/chown -f $owner_group $subdir/*

##################################################
# LEDS
owner_group="0:0"
permission="666"

leds="0 1 2 3 4 5 6 7"
for led in $leds; do
  ${RFSDIR}/bin/mknod -m${permission} led${led} c 60 ${led}
done
${RFSDIR}/bin/chown -f $owner_group led*

##################################################
# GEM Iolite Platform
#
#${RFSDIR}/bin/mknod -m644 iolite0 c 254 0  #MPG, not used?
${RFSDIR}/bin/mknod -m666 epb0 c 254 0
${RFSDIR}/bin/mknod -m644 frpanel0 c 253 0
${RFSDIR}/bin/mknod -m644 frpanel1 c 253 1
${RFSDIR}/bin/mknod -m644 frpanel2 c 253 2
${RFSDIR}/bin/mknod -m644 dma0 c 100 0
${RFSDIR}/bin/mknod -m644 ecid c 231 0
${RFSDIR}/bin/mknod -m644 eth0frm c 232 0
${RFSDIR}/bin/mknod -m644 eth1frm c 232 1
# i2c 
for (( CNT=0; CNT<31; CNT++ )); do
  ${RFSDIR}/bin/mknod -m${permission} i2c-${CNT} c 89 ${CNT}
done

##################################################
# PC70xx PCI Multistandard Decoder
#
${RFSDIR}/bin/mknod -m644 mum0 c 126 0
${RFSDIR}/bin/mknod -m644 mum1 c 126 1
${RFSDIR}/bin/mknod -m644 em8xxx0 c 127 0
${RFSDIR}/bin/mknod -m644 mdec_rst0 c 235 0
${RFSDIR}/bin/mknod -m644 mdec_rst1 c 235 1
${RFSDIR}/bin/mknod -m644 mdec_sdicfg c 235 2
${RFSDIR}/bin/mknod -m644 mdec_asicfg c 235 3
${RFSDIR}/bin/mknod -m644 mdec_stat c 235 4
${RFSDIR}/bin/mknod -m644 mdec_update c 235 5

##################################################
# MediaHUB SD and HD
#
${RFSDIR}/bin/mknod -m644 rt_ingress_aux c 234 251
${RFSDIR}/bin/mknod -m644 rt_egress c 234 252
${RFSDIR}/bin/mknod -m644 rt_ingress c 234 253
${RFSDIR}/bin/mknod -m644 rt_cmd c 234 254
${RFSDIR}/bin/mknod -m644 rt_stat c 234 255

##################################################
# Input device support
${RFSDIR}/bin/mkdir input
${RFSDIR}/bin/mknod input/event0 -m664 c 13 64
${RFSDIR}/bin/mknod input/event1 -m664 c 13 65
${RFSDIR}/bin/mknod input/event2 -m664 c 13 66
${RFSDIR}/bin/mknod input/event3 -m664 c 13 67

# dta-iptv
${RFSDIR}/bin/mknod -m644 dta_egress c 121 252
${RFSDIR}/bin/mknod -m644 dta_ingress c 121 253
${RFSDIR}/bin/mknod -m644 dta_command c 121 254
${RFSDIR}/bin/mknod -m644 dta_status c 121 255

# dta-tuner
for (( CNT=0; CNT<4; CNT++ )); do
  ${RFSDIR}/bin/mknod -m${permission} tuner${CNT} c 123 ${CNT}
  ${RFSDIR}/bin/mknod -m${permission} tuner_rst${CNT} c 123 $((${CNT}+6))
  ${RFSDIR}/bin/mknod -m${permission} tuner_ledrd${CNT} c 123 $((${CNT}+10))
  ${RFSDIR}/bin/mknod -m${permission} tuner_ledgr${CNT} c 123 $((${CNT}+14))
done

${RFSDIR}/bin/mknod -m${permission} lnbpwr0 c 123 4
${RFSDIR}/bin/mknod -m${permission} lnbpwr1 c 123 5
