fp@563: #!/bin/sh
fp@563: 
fp@563: #------------------------------------------------------------------------------
fp@563: #
fp@563: #  Init script for EtherCAT
fp@563: #
fp@563: #  $Id$
fp@563: #
fp@563: #  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
fp@563: #
fp@563: #  This file is part of the IgH EtherCAT Master.
fp@563: #
fp@563: #  The IgH EtherCAT Master is free software; you can redistribute it
fp@563: #  and/or modify it under the terms of the GNU General Public License
fp@563: #  as published by the Free Software Foundation; either version 2 of the
fp@563: #  License, or (at your option) any later version.
fp@563: #
fp@563: #  The IgH EtherCAT Master is distributed in the hope that it will be
fp@563: #  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
fp@563: #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
fp@563: #  GNU General Public License for more details.
fp@563: #
fp@563: #  You should have received a copy of the GNU General Public License
fp@563: #  along with the IgH EtherCAT Master; if not, write to the Free Software
fp@563: #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
fp@563: #
fp@563: #  The right to use EtherCAT Technology is granted and comes free of
fp@563: #  charge under condition of compatibility of product made by
fp@563: #  Licensee. People intending to distribute/sell products based on the
fp@563: #  code, have to sign an agreement to guarantee that products using
fp@563: #  software based on IgH EtherCAT master stay compatible with the actual
fp@563: #  EtherCAT specification (which are released themselves as an open
fp@563: #  standard) as the (only) precondition to have the right to use EtherCAT
fp@563: #  Technology, IP and trade marks.
fp@563: #
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@563: ### BEGIN INIT INFO
fp@563: # Provides:          ethercat
fp@563: # Required-Start:    $local_fs $syslog $network
fp@563: # Should-Start:      $time ntp
fp@563: # Required-Stop:     $local_fs $syslog $network
fp@563: # Should-Stop:       $time ntp
fp@563: # Default-Start:     3 5
fp@563: # Default-Stop:      0 1 2 6
fp@563: # Short-Description: IgH EtherCAT master modules
fp@563: # Description:
fp@563: ### END INIT INFO
fp@563: 
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@573: XMLDEVICE='ecxml'
fp@563: 
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@563: ETHERCAT_CONFIG=/etc/sysconfig/ethercat
fp@563: 
fp@573: if [ ! -r ${ETHERCAT_CONFIG} ]; then
fp@573:     echo "${ETHERCAT_CONFIG} not existing";
fp@573:     if [ "${1}" = "stop" ]; then
fp@563: 	exit 0
fp@563:     else
fp@563: 	exit 6
fp@563:     fi
fp@563: fi
fp@563: 
fp@573: . ${ETHERCAT_CONFIG}
fp@573: 
fp@573: #------------------------------------------------------------------------------
fp@573: 
fp@573: function make_device_id()
fp@563: {
fp@573:     if [ -z "${1}" ]; then
fp@573:         DEVICE_ID=";"
fp@573:     elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then
fp@573:         DEVICE_ID="M${1};"
fp@573:     elif echo ${1} | grep -qE '^[^:]+:[0-9]+$'; then
fp@573:         DEVICE_ID="D${1};"
fp@573:     else
fp@573:         echo Invalid device ID syntax in ${ETHERCAT_CONFIG}
fp@573:         /bin/false
fp@573:         rc_status -v
fp@573:         rc_exit
fp@573:     fi 
fp@563: }
fp@563: 
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@563: . /etc/rc.status
fp@563: rc_reset
fp@563: 
fp@573: case "${1}" in
fp@573: 
fp@573: start)
fp@573:     echo -n "Starting EtherCAT master "
fp@573: 
fp@573:     # construct DEVICES and BACKUPS from configuration variables
fp@573:     DEVICES=""
fp@573:     BACKUPS=""
fp@573:     MASTER_INDEX=0
fp@573:     while true; do
fp@573:         DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}")
fp@573:         BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}")
fp@573:         if [ -z "${DEVICE}" ]; then break; fi
fp@573: 
fp@573:         make_device_id ${DEVICE}
fp@573:         DEVICES=${DEVICES}${DEVICE_ID}
fp@573:         make_device_id ${BACKUP}
fp@573:         BACKUPS=${BACKUPS}${DEVICE_ID}
fp@573: 
fp@573:         MASTER_INDEX=$(expr ${MASTER_INDEX} + 1)
fp@573:     done
fp@573: 
fp@573:     # unload conflicting modules at first
fp@573:     for MODULE in 8139too; do
fp@573:         if lsmod | grep "^${MODULE} " > /dev/null; then
fp@573:             if ! rmmod ${MODULE}; then
fp@573:                 /bin/false
fp@573:                 rc_status -v
fp@573:                 rc_exit
fp@573:             fi
fp@573:         fi
fp@573:     done
fp@573: 
fp@573:     # load master module
fp@573:     if ! modprobe ec_master main=${DEVICES} backup=${BACKUPS}; then
fp@573:         modprobe 8139too
fp@573:         /bin/false
fp@573:         rc_status -v
fp@573:         rc_exit
fp@573:     fi
fp@573: 
fp@573:     # remove stale device node
fp@573:     rm -f /dev/${XMLDEVICE}0
fp@573: 
fp@573:     # get dynamic major number
fp@573:     MAJOR=$(awk "\$2==\"EtherCAT\" {print \$1}" /proc/devices)
fp@573: 
fp@573:     # create character device
fp@573:     mknod /dev/${XMLDEVICE}0 c ${MAJOR} 0
fp@573: 
fp@573:     # load device module
fp@573:     if ! modprobe ec_8139too; then
fp@573:         rmmod ec_master
fp@573:         modprobe 8139too
fp@573:         /bin/false
fp@573:         rc_status -v
fp@573:         rc_exit
fp@573:     fi
fp@573: 
fp@573:     rc_status -v
fp@573:     ;;
fp@573: 
fp@573: stop)
fp@573:     echo -n "Shutting down EtherCAT master "
fp@573: 
fp@573:     # unload modules
fp@573:     for mod in ec_8139too ec_master; do
fp@573:         if lsmod | grep "^$mod " > /dev/null; then
fp@573:             if ! rmmod $mod; then
fp@573:                 /bin/false
fp@573:                 rc_status -v
fp@573:                 rc_exit
fp@573:             fi;
fp@573:         fi;
fp@573:     done
fp@573: 
fp@573:     # remove device node
fp@573:     rm -f /dev/${XMLDEVICE}0
fp@573: 
fp@573:     sleep 1
fp@573: 
fp@573:     # reload previous modules
fp@573:     if ! modprobe 8139too; then
fp@573:         echo "Warning: Failed to restore 8139too module."
fp@573:     fi
fp@573: 
fp@573:     rc_status -v
fp@573:     ;;
fp@573: 
fp@573: restart)
fp@573:     $0 stop || exit 1
fp@573:     sleep 1
fp@573:     $0 start
fp@573:     rc_status
fp@573:     ;;
fp@573: 
fp@573: status)
fp@573:     echo -n "Checking for EtherCAT "
fp@573: 
fp@573:     lsmod | grep "^ec_master " > /dev/null
fp@573:     master_running=$?
fp@573:     lsmod | grep "^ec_8139too " > /dev/null
fp@573:     device_running=$?
fp@573: 
fp@573:     # master module and device module loaded?
fp@573:     test $master_running -eq 0 -a $device_running -eq 0
fp@573: 
fp@573:     rc_status -v
fp@573:     ;;
fp@573: 
fp@573: *)
fp@573:     echo "USAGE: $0 {start|stop|restart|status}"
fp@573:     ;;
fp@563: 
fp@563: esac
fp@563: 
fp@563: rc_exit
fp@563: 
fp@563: #------------------------------------------------------------------------------