Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 #!/bin/bash
  2 # Sample init script for the a driver module <rubini@linux.it>
  3 
  4 DEVICE="scull"
  5 SECTION="misc"
  6 
  7 # The list of filenames and minor numbers: $PREFIX is prefixed to all names
  8 PREFIX="scull"
  9 FILES="     0 0         1 1         2 2        3 3    priv 16 
 10         pipe0 32    pipe1 33    pipe2 34   pipe3 35
 11        single 48      uid 64     wuid 80"
 12 
 13 INSMOD=/sbin/insmod; # use /sbin/modprobe if you prefer
 14 
 15 function device_specific_post_load () {
 16     true; # fill at will
 17 }
 18 function device_specific_pre_unload () {
 19     true; # fill at will
 20 }
 21 
 22 # Everything below this line should work unchanged for any char device.
 23 # Obviously, however, no options on the command line: either in
 24 # /etc/${DEVICE}.conf or /etc/modules.conf (if modprobe is used)
 25 
 26 # Optional configuration file: format is
 27 #    owner  <ownername>
 28 #    group  <groupname>
 29 #    mode   <modename>
 30 #    options <insmod options>
 31 CFG=/etc/${DEVICE}.conf
 32 
 33 # kernel version, used to look for modules
 34 KERNEL=`uname -r`
 35 
 36 #FIXME: it looks like there is no misc section. Where should it be?
 37 MODDIR="/lib/modules/${KERNEL}/kernel/drivers/${SECTION}"
 38 if [ ! -d $MODDIR ]; then MODDIR="/lib/modules/${KERNEL}/${SECTION}"; fi
 39 
 40 # Root or die
 41 if [ "$(id -u)" != "0" ]
 42 then
 43   echo "You must be root to load or unload kernel modules"
 44   exit 1
 45 fi
 46 
 47 # Read configuration file
 48 if [ -r $CFG ]; then
 49     OWNER=`awk "\\$1==\"owner\" {print \\$2}" $CFG`
 50     GROUP=`awk "\\$1==\"group\" {print \\$2}" $CFG`
 51     MODE=`awk "\\$1==\"mode\" {print \\$2}" $CFG`
 52     # The options string may include extra blanks or only blanks
 53     OPTIONS=`sed -n '/^options / s/options //p' $CFG`
 54 fi
 55 
 56 
 57 # Create device files
 58 function create_files () {
 59     cd /dev
 60     local devlist=""
 61     local file
 62     while true; do
 63         if [ $# -lt 2 ]; then break; fi
 64         file="${DEVICE}$1"
 65         mknod $file c $MAJOR $2
 66         devlist="$devlist $file"
 67         shift 2
 68     done
 69     if [ -n "$OWNER" ]; then chown $OWNER $devlist; fi
 70     if [ -n "$GROUP" ]; then chgrp $GROUP $devlist; fi
 71     if [ -n "$MODE"  ]; then chmod $MODE  $devlist; fi
 72 }
 73 
 74 # Remove device files
 75 function remove_files () {
 76     cd /dev
 77     local devlist=""
 78     local file
 79     while true; do
 80         if [ $# -lt 2 ]; then break; fi
 81         file="${DEVICE}$1"
 82         devlist="$devlist $file"
 83         shift 2
 84     done
 85     rm -f $devlist
 86 }
 87 
 88 # Load and create files
 89 function load_device () {
 90     
 91     if [ -f $MODDIR/$DEVICE.o ]; then
 92         devpath=$MODDIR/$DEVICE.o
 93     else if [ -f ./$DEVICE.o ]; then
 94         devpath=./$DEVICE.o
 95     else
 96         devpath=$DEVICE; # let insmod/modprobe guess
 97     fi; fi
 98     if [ "$devpath" != "$DEVICE" ]; then
 99         echo -n " (loading file $devpath)"
100     fi
101 
102     if $INSMOD $devpath $OPTIONS; then
103         MAJOR=`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`
104         remove_files $FILES
105         create_files $FILES
106         device_specific_post_load
107     else
108         echo " FAILED!"
109      fi
110 }
111 
112 # Unload and remove files
113 function unload_device () {
114     device_specific_pre_unload 
115     /sbin/rmmod $DEVICE
116     remove_files $FILES
117 }
118 
119 
120 case "$1" in
121   start)
122      echo -n "Loading $DEVICE"
123      load_device
124      echo "."
125      ;;
126   stop)
127      echo -n "Unloading $DEVICE"
128      unload_device
129      echo "."
130      ;;
131   force-reload|restart)
132      echo -n "Reloading $DEVICE"
133      unload_device
134      load_device
135      echo "."
136      ;;
137   *)
138      echo "Usage: $0 {start|stop|restart|force-reload}"
139      exit 1
140 esac
141 
142 exit 0
  This page was automatically generated by the LXR engine.