1 #!/bin/sh |
|
2 |
|
3 #------------------------------------------------------------------------------ |
|
4 # |
|
5 # Init script for EtherCAT |
|
6 # |
|
7 # $Id$ |
|
8 # |
|
9 # Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH |
|
10 # |
|
11 # This file is part of the IgH EtherCAT Master. |
|
12 # |
|
13 # The IgH EtherCAT Master is free software; you can redistribute it |
|
14 # and/or modify it under the terms of the GNU General Public License |
|
15 # as published by the Free Software Foundation; either version 2 of the |
|
16 # License, or (at your option) any later version. |
|
17 # |
|
18 # The IgH EtherCAT Master is distributed in the hope that it will be |
|
19 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 # GNU General Public License for more details. |
|
22 # |
|
23 # You should have received a copy of the GNU General Public License |
|
24 # along with the IgH EtherCAT Master; if not, write to the Free Software |
|
25 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
26 # |
|
27 # The right to use EtherCAT Technology is granted and comes free of |
|
28 # charge under condition of compatibility of product made by |
|
29 # Licensee. People intending to distribute/sell products based on the |
|
30 # code, have to sign an agreement to guarantee that products using |
|
31 # software based on IgH EtherCAT master stay compatible with the actual |
|
32 # EtherCAT specification (which are released themselves as an open |
|
33 # standard) as the (only) precondition to have the right to use EtherCAT |
|
34 # Technology, IP and trade marks. |
|
35 # |
|
36 #------------------------------------------------------------------------------ |
|
37 |
|
38 ### BEGIN INIT INFO |
|
39 # Provides: ethercat |
|
40 # Required-Start: $local_fs $syslog $network |
|
41 # Should-Start: $time ntp |
|
42 # Required-Stop: $local_fs $syslog $network |
|
43 # Should-Stop: $time ntp |
|
44 # Default-Start: 3 5 |
|
45 # Default-Stop: 0 1 2 6 |
|
46 # Short-Description: IgH EtherCAT master modules |
|
47 # Description: |
|
48 ### END INIT INFO |
|
49 |
|
50 #------------------------------------------------------------------------------ |
|
51 |
|
52 IFCONFIG=ifconfig |
|
53 BRCTL=brctl |
|
54 ROUTE=route |
|
55 |
|
56 #------------------------------------------------------------------------------ |
|
57 |
|
58 ETHERCAT_CONFIG=/etc/sysconfig/ethercat |
|
59 |
|
60 if [ ! -r $ETHERCAT_CONFIG ]; then |
|
61 echo "$ETHERCAT_CONFIG not existing"; |
|
62 if [ "$1" = "stop" ]; then |
|
63 exit 0 |
|
64 else |
|
65 exit 6 |
|
66 fi |
|
67 fi |
|
68 |
|
69 . $ETHERCAT_CONFIG |
|
70 |
|
71 #------------------------------------------------------------------------------ |
|
72 |
|
73 # |
|
74 # Function for setting up the EoE bridge |
|
75 # |
|
76 build_eoe_bridge() |
|
77 { |
|
78 if [ -z "$EOE_BRIDGE" ]; then return; fi |
|
79 |
|
80 EOEIF=`$IFCONFIG -a | grep -o -E "^eoe[0-9]+ "` |
|
81 |
|
82 # add bridge, if it does not already exist |
|
83 if ! $BRCTL show | grep -E -q "^$EOE_BRIDGE"; then |
|
84 if ! $BRCTL addbr $EOE_BRIDGE; then |
|
85 /bin/false |
|
86 rc_status -v |
|
87 rc_exit |
|
88 fi |
|
89 fi |
|
90 |
|
91 # check if specified interfaces are bridged |
|
92 for interf in $EOEIF $EOE_EXTRA_INTERFACES; do |
|
93 # interface is already part of the bridge (FIXME->show $EOE_BRIDGE) |
|
94 if $BRCTL show | grep -E -q $interf |
|
95 then continue |
|
96 fi |
|
97 # clear IP address and open interface |
|
98 if ! $IFCONFIG $interf 0.0.0.0 up; then |
|
99 /bin/false |
|
100 rc_status -v |
|
101 rc_exit |
|
102 fi |
|
103 # add interface to the bridge |
|
104 if ! $BRCTL addif $EOE_BRIDGE $interf; then |
|
105 /bin/false |
|
106 rc_status -v |
|
107 rc_exit |
|
108 fi |
|
109 done |
|
110 |
|
111 # configure IP on bridge |
|
112 if [ -n "$EOE_IP_ADDRESS" -a -n "$EOE_IP_NETMASK" ]; then |
|
113 if ! $IFCONFIG $EOE_BRIDGE $EOE_IP_ADDRESS \ |
|
114 netmask $EOE_IP_NETMASK; then |
|
115 /bin/false |
|
116 rc_status -v |
|
117 rc_exit |
|
118 fi |
|
119 fi |
|
120 |
|
121 # open bridge |
|
122 if ! $IFCONFIG $EOE_BRIDGE up; then |
|
123 /bin/false |
|
124 rc_status -v |
|
125 rc_exit |
|
126 fi |
|
127 |
|
128 # install new default gateway |
|
129 if [ -n "$EOE_GATEWAY" ]; then |
|
130 while $ROUTE -n | grep -E -q "^0.0.0.0"; do |
|
131 if ! $ROUTE del default; then |
|
132 echo "Failed to remove route!" 1>&2 |
|
133 /bin/false |
|
134 rc_status -v |
|
135 rc_exit |
|
136 fi |
|
137 done |
|
138 if ! $ROUTE add default gw $EOE_GATEWAY; then |
|
139 /bin/false |
|
140 rc_status -v |
|
141 rc_exit |
|
142 fi |
|
143 fi |
|
144 } |
|
145 |
|
146 #------------------------------------------------------------------------------ |
|
147 |
|
148 . /etc/rc.status |
|
149 rc_reset |
|
150 |
|
151 case "$1" in |
|
152 |
|
153 start) |
|
154 echo -n "Starting EtherCAT master " |
|
155 |
|
156 if [ -z "$DEVICE_INDEX" ]; then |
|
157 echo "ERROR: DEVICE_INDEX not set!" |
|
158 /bin/false |
|
159 rc_status -v |
|
160 rc_exit |
|
161 fi |
|
162 |
|
163 if [ -z "$EOE_INTERFACES" ]; then |
|
164 if [ -n "$EOE_DEVICES" ]; then # support legacy sysconfig files |
|
165 EOE_INTERFACES=$EOE_DEVICES |
|
166 else |
|
167 EOE_INTERFACES=0 |
|
168 fi |
|
169 fi |
|
170 |
|
171 # unload conflicting modules at first |
|
172 for mod in 8139too; do |
|
173 if lsmod | grep "^$mod " > /dev/null; then |
|
174 if ! rmmod $mod; then |
|
175 /bin/false |
|
176 rc_status -v |
|
177 rc_exit |
|
178 fi |
|
179 fi |
|
180 done |
|
181 |
|
182 # load master module |
|
183 if ! modprobe ec_master ec_eoeif_count=$EOE_INTERFACES; then |
|
184 modprobe 8139too |
|
185 /bin/false |
|
186 rc_status -v |
|
187 rc_exit |
|
188 fi |
|
189 |
|
190 # load device module |
|
191 if ! modprobe ec_8139too ec_device_index=$DEVICE_INDEX; then |
|
192 rmmod ec_master |
|
193 modprobe 8139too |
|
194 /bin/false |
|
195 rc_status -v |
|
196 rc_exit |
|
197 fi |
|
198 |
|
199 # build EoE bridge |
|
200 build_eoe_bridge |
|
201 |
|
202 rc_status -v |
|
203 ;; |
|
204 |
|
205 stop) |
|
206 echo -n "Shutting down EtherCAT master " |
|
207 |
|
208 # unload modules |
|
209 for mod in ec_8139too ec_master; do |
|
210 if lsmod | grep "^$mod " > /dev/null; then |
|
211 if ! rmmod $mod; then |
|
212 /bin/false |
|
213 rc_status -v |
|
214 rc_exit |
|
215 fi; |
|
216 fi; |
|
217 done |
|
218 |
|
219 sleep 1 |
|
220 |
|
221 # reload previous modules |
|
222 if ! modprobe 8139too; then |
|
223 echo "Warning: Failed to restore 8139too module." |
|
224 fi |
|
225 |
|
226 rc_status -v |
|
227 ;; |
|
228 |
|
229 restart) |
|
230 $0 stop || exit 1 |
|
231 sleep 1 |
|
232 $0 start |
|
233 rc_status |
|
234 ;; |
|
235 |
|
236 status) |
|
237 echo -n "Checking for EtherCAT " |
|
238 |
|
239 lsmod | grep "^ec_master " > /dev/null |
|
240 master_running=$? |
|
241 lsmod | grep "^ec_8139too " > /dev/null |
|
242 device_running=$? |
|
243 |
|
244 # master module and device module loaded? |
|
245 test $master_running -eq 0 -a $device_running -eq 0 |
|
246 |
|
247 rc_status -v |
|
248 ;; |
|
249 |
|
250 bridge) |
|
251 echo -n "Building EoE bridge " |
|
252 build_eoe_bridge |
|
253 rc_status -v |
|
254 ;; |
|
255 |
|
256 *) |
|
257 echo "USAGE: $0 {start|stop|restart|status|bridge}" |
|
258 ;; |
|
259 |
|
260 esac |
|
261 |
|
262 rc_exit |
|
263 |
|
264 #------------------------------------------------------------------------------ |
|