script/init.d/ethercat
author Florian Pose <fp@igh-essen.com>
Fri, 02 Mar 2007 14:47:09 +0000
changeset 606 a82c53d6a5d4
parent 588 ba69f7f34fbf
child 639 aa23c48dca2d
permissions -rwxr-xr-x
Minor changes in EEPROM write code.
#!/bin/sh

#------------------------------------------------------------------------------
#
#  Init script for EtherCAT
#
#  $Id$
#
#  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
#
#  This file is part of the IgH EtherCAT Master.
#
#  The IgH EtherCAT Master 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.
#
#  The IgH EtherCAT Master is distributed in the hope that it will be
#  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with the IgH EtherCAT Master; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#  The right to use EtherCAT Technology is granted and comes free of
#  charge under condition of compatibility of product made by
#  Licensee. People intending to distribute/sell products based on the
#  code, have to sign an agreement to guarantee that products using
#  software based on IgH EtherCAT master stay compatible with the actual
#  EtherCAT specification (which are released themselves as an open
#  standard) as the (only) precondition to have the right to use EtherCAT
#  Technology, IP and trade marks.
#
#------------------------------------------------------------------------------

### BEGIN INIT INFO
# Provides:          ethercat
# Required-Start:    $local_fs $syslog $network
# Should-Start:      $time ntp
# Required-Stop:     $local_fs $syslog $network
# Should-Stop:       $time ntp
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: IgH EtherCAT master modules
# Description:
### END INIT INFO

#------------------------------------------------------------------------------

XMLDEVICE='ecxml'

#------------------------------------------------------------------------------

ETHERCAT_CONFIG=/etc/sysconfig/ethercat

if [ ! -r ${ETHERCAT_CONFIG} ]; then
    echo ${ETHERCAT_CONFIG} not existing;
    if [ "${1}" = "stop" ]; then
	exit 0
    else
	exit 6
    fi
fi

. ${ETHERCAT_CONFIG}

#------------------------------------------------------------------------------

function make_device_id()
{
    if [ -z "${1}" ]; then
        DEVICE_ID=";"
    elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then
        DEVICE_ID="M${1};"
    elif echo ${1} | grep -qE '^[^:]+:[0-9]+$'; then
        DEVICE_ID="D${1};"
    else
        echo Invalid device ID syntax in ${ETHERCAT_CONFIG}
        /bin/false
        rc_status -v
        rc_exit
    fi 
}

#------------------------------------------------------------------------------

. /etc/rc.status
rc_reset

case "${1}" in

start)
    echo -n "Starting EtherCAT master "

    # construct DEVICES and BACKUPS from configuration variables
    DEVICES=""
    BACKUPS=""
    MASTER_INDEX=0
    while true; do
        DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}")
        BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}")
        if [ -z "${DEVICE}" ]; then break; fi

        make_device_id ${DEVICE}
        DEVICES=${DEVICES}${DEVICE_ID}
        make_device_id ${BACKUP}
        BACKUPS=${BACKUPS}${DEVICE_ID}

        MASTER_INDEX=$(expr ${MASTER_INDEX} + 1)
    done

    # load master module
    if ! modprobe ec_master main=${DEVICES} backup=${BACKUPS}; then
        modprobe 8139too
        /bin/false
        rc_status -v
        rc_exit
    fi

    # remove stale device node
    rm -f /dev/${XMLDEVICE}0

    # get dynamic major number
    MAJOR=$(awk "\$2==\"EtherCAT\" {print \$1}" /proc/devices)

    # create character device
    mknod /dev/${XMLDEVICE}0 c ${MAJOR} 0

    # check for modules to replace
    for MODULE in ${DEVICE_MODULES}; do
        ECMODULE=ec_${MODULE}
        if ! modinfo ${ECMODULE} > /dev/null; then
            continue
        fi
        if lsmod | grep "^${MODULE} " > /dev/null; then
            if ! rmmod ${MODULE}; then
                /bin/false
                rc_status -v
                rc_exit
            fi
        fi
        if ! modprobe ${ECMODULE}; then
            modprobe ${MODULE} # try to restore module
            /bin/false
            rc_status -v
            rc_exit
        fi
    done

    rc_status -v
    ;;

stop)
    echo -n "Shutting down EtherCAT master "

    # unload EtherCAT device modules
    for MODULE in ${DEVICE_MODULES} master; do
        ECMODULE=ec_${MODULE}
        if ! lsmod | grep -q "^${ECMODULE} "; then
            continue
        fi
        if ! rmmod ${ECMODULE}; then
            /bin/false
            rc_status -v
            rc_exit
        fi;
    done

    # remove device node
    rm -f /dev/${XMLDEVICE}0

    sleep 1

    # reload previous modules
    for MODULE in ${DEVICE_MODULES}; do
        if ! modprobe ${MODULE}; then
            echo Warning: Failed to restore ${MODULE}.
        fi
    done

    rc_status -v
    ;;

restart)
    $0 stop || exit 1
    sleep 1
    $0 start
    rc_status
    ;;

status)
    echo -n "Checking for EtherCAT "

    lsmod | grep -q "^ec_master "
    MASTERS_RUNNING=$?

    ! grep -q "(WAITING)" /sys/ethercat/master*/info
    MASTERS_IDLE=$?

    # master module loaded and masters not waiting for devices?
    test ${MASTERS_RUNNING} -eq 0 -a ${MASTERS_IDLE} -eq 0

    rc_status -v
    ;;

*)
    echo "USAGE: $0 {start|stop|restart|status}"
    ;;

esac

rc_exit

#------------------------------------------------------------------------------