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@698: MODPROBE=/sbin/modprobe fp@698: RMMOD=/sbin/rmmod fp@698: MODINFO=/sbin/modinfo fp@563: fp@563: #------------------------------------------------------------------------------ fp@563: fp@563: ETHERCAT_CONFIG=/etc/sysconfig/ethercat fp@563: fp@573: if [ ! -r ${ETHERCAT_CONFIG} ]; then fp@588: 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@698: function exit_success() fp@698: { fp@698: if [ -r /etc/rc.status ]; then fp@698: rc_reset fp@698: rc_status -v fp@698: rc_exit fp@698: else fp@698: echo " done" fp@698: exit 0 fp@698: fi fp@698: } fp@698: fp@698: #------------------------------------------------------------------------------ fp@698: fp@698: function exit_running() fp@698: { fp@698: if [ -r /etc/rc.status ]; then fp@698: rc_reset fp@698: rc_status -v fp@698: rc_exit fp@698: else fp@698: echo " running" fp@698: exit 0 fp@698: fi fp@698: } fp@698: fp@698: #------------------------------------------------------------------------------ fp@698: fp@698: function exit_fail() fp@698: { fp@698: if [ -r /etc/rc.status ]; then fp@698: rc_failed fp@698: rc_exit fp@698: else fp@698: echo " failed" fp@698: exit 1 fp@698: fi fp@698: } fp@698: fp@698: #------------------------------------------------------------------------------ fp@698: fp@639: function parse_mac_address() fp@563: { fp@573: if [ -z "${1}" ]; then fp@639: MAC="" fp@573: elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then fp@639: MAC=${1} fp@573: else fp@639: echo Invalid MAC address \"${1}\" in ${ETHERCAT_CONFIG} fp@698: exit_fail fp@573: fi fp@563: } fp@563: fp@563: #------------------------------------------------------------------------------ fp@563: fp@698: if [ -r /etc/rc.status ]; then fp@698: . /etc/rc.status fp@698: rc_reset fp@698: fi 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@639: if [ ${MASTER_INDEX} -gt 0 ]; then fp@639: DEVICES=${DEVICES}, fp@639: BACKUPS=${BACKUPS}, fp@639: fi fp@639: fp@639: parse_mac_address ${DEVICE} fp@639: DEVICES=${DEVICES}${MAC} fp@639: fp@639: parse_mac_address ${BACKUP} fp@639: BACKUPS=${BACKUPS}${MAC} fp@573: fp@573: MASTER_INDEX=$(expr ${MASTER_INDEX} + 1) fp@573: done fp@573: fp@573: # load master module fp@698: if ! ${MODPROBE} ec_master main=${DEVICES} backup=${BACKUPS}; then fp@698: exit_fail 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@588: # check for modules to replace fp@588: for MODULE in ${DEVICE_MODULES}; do fp@588: ECMODULE=ec_${MODULE} fp@698: if ! ${MODINFO} ${ECMODULE} > /dev/null; then fp@698: continue # ec_* module not found fp@588: fi fp@588: if lsmod | grep "^${MODULE} " > /dev/null; then fp@698: if ! ${RMMOD} ${MODULE}; then fp@698: exit_fail fp@588: fi fp@588: fi fp@698: if ! ${MODPROBE} ${ECMODULE}; then fp@698: ${MODPROBE} ${MODULE} # try to restore module fp@698: exit_fail fp@698: fi fp@698: done fp@698: fp@698: exit_success fp@588: ;; fp@588: fp@588: stop) fp@588: echo -n "Shutting down EtherCAT master " fp@588: fp@588: # unload EtherCAT device modules fp@588: for MODULE in ${DEVICE_MODULES} master; do fp@588: ECMODULE=ec_${MODULE} fp@588: if ! lsmod | grep -q "^${ECMODULE} "; then fp@698: continue # ec_* module not loaded fp@698: fi fp@698: if ! ${RMMOD} ${ECMODULE}; then fp@698: exit_fail 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@588: for MODULE in ${DEVICE_MODULES}; do fp@698: if ! ${MODPROBE} ${MODULE}; then fp@588: echo Warning: Failed to restore ${MODULE}. fp@588: fi fp@588: done fp@573: fp@698: exit_success fp@573: ;; fp@573: fp@573: restart) fp@573: $0 stop || exit 1 fp@573: sleep 1 fp@573: $0 start fp@573: ;; fp@573: fp@573: status) fp@573: echo -n "Checking for EtherCAT " fp@573: fp@588: lsmod | grep -q "^ec_master " fp@588: MASTERS_RUNNING=$? fp@588: fp@588: ! grep -q "(WAITING)" /sys/ethercat/master*/info fp@588: MASTERS_IDLE=$? fp@588: fp@588: # master module loaded and masters not waiting for devices? fp@698: if [ ${MASTERS_RUNNING} -eq 0 -a ${MASTERS_IDLE} -eq 0 ]; then fp@698: exit_running fp@698: else fp@698: exit_fail fp@698: fi 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@698: if [ -r /etc/rc.status ]; then fp@698: rc_exit fp@698: else fp@698: exit 1 fp@698: fi fp@698: fp@698: #------------------------------------------------------------------------------