|
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 device="ecxml" |
|
53 |
|
54 IFCONFIG=ifconfig |
|
55 BRCTL=brctl |
|
56 ROUTE=route |
|
57 |
|
58 #------------------------------------------------------------------------------ |
|
59 |
|
60 ETHERCAT_CONFIG=/etc/sysconfig/ethercat |
|
61 |
|
62 if [ ! -r $ETHERCAT_CONFIG ]; then |
|
63 echo "$ETHERCAT_CONFIG not existing"; |
|
64 if [ "$1" = "stop" ]; then |
|
65 exit 0 |
|
66 else |
|
67 exit 6 |
|
68 fi |
|
69 fi |
|
70 |
|
71 . $ETHERCAT_CONFIG |
|
72 |
|
73 #------------------------------------------------------------------------------ |
|
74 |
|
75 # |
|
76 # Function for setting up the EoE bridge |
|
77 # |
|
78 build_eoe_bridge() |
|
79 { |
|
80 if [ -z "$EOE_BRIDGE" ]; then return; fi |
|
81 |
|
82 EOEIF=`$IFCONFIG -a | grep -o -E "^eoe[0-9]+ "` |
|
83 |
|
84 # add bridge, if it does not already exist |
|
85 if ! $BRCTL show | grep -E -q "^$EOE_BRIDGE"; then |
|
86 if ! $BRCTL addbr $EOE_BRIDGE; then |
|
87 /bin/false |
|
88 rc_status -v |
|
89 rc_exit |
|
90 fi |
|
91 fi |
|
92 |
|
93 # check if specified interfaces are bridged |
|
94 for interf in $EOEIF $EOE_EXTRA_INTERFACES; do |
|
95 # interface is already part of the bridge (FIXME->show $EOE_BRIDGE) |
|
96 if $BRCTL show | grep -E -q $interf |
|
97 then continue |
|
98 fi |
|
99 # clear IP address and open interface |
|
100 if ! $IFCONFIG $interf 0.0.0.0 up; then |
|
101 /bin/false |
|
102 rc_status -v |
|
103 rc_exit |
|
104 fi |
|
105 # add interface to the bridge |
|
106 if ! $BRCTL addif $EOE_BRIDGE $interf; then |
|
107 /bin/false |
|
108 rc_status -v |
|
109 rc_exit |
|
110 fi |
|
111 done |
|
112 |
|
113 # configure IP on bridge |
|
114 if [ -n "$EOE_IP_ADDRESS" -a -n "$EOE_IP_NETMASK" ]; then |
|
115 if ! $IFCONFIG $EOE_BRIDGE $EOE_IP_ADDRESS \ |
|
116 netmask $EOE_IP_NETMASK; then |
|
117 /bin/false |
|
118 rc_status -v |
|
119 rc_exit |
|
120 fi |
|
121 fi |
|
122 |
|
123 # open bridge |
|
124 if ! $IFCONFIG $EOE_BRIDGE up; then |
|
125 /bin/false |
|
126 rc_status -v |
|
127 rc_exit |
|
128 fi |
|
129 |
|
130 # install new default gateway |
|
131 if [ -n "$EOE_GATEWAY" ]; then |
|
132 while $ROUTE -n | grep -E -q "^0.0.0.0"; do |
|
133 if ! $ROUTE del default; then |
|
134 echo "Failed to remove route!" 1>&2 |
|
135 /bin/false |
|
136 rc_status -v |
|
137 rc_exit |
|
138 fi |
|
139 done |
|
140 if ! $ROUTE add default gw $EOE_GATEWAY; then |
|
141 /bin/false |
|
142 rc_status -v |
|
143 rc_exit |
|
144 fi |
|
145 fi |
|
146 } |
|
147 |
|
148 #------------------------------------------------------------------------------ |
|
149 |
|
150 . /etc/rc.status |
|
151 rc_reset |
|
152 |
|
153 case "$1" in |
|
154 |
|
155 start) |
|
156 echo -n "Starting EtherCAT master " |
|
157 |
|
158 if [ -z "$DEVICE_INDEX" ]; then |
|
159 echo "ERROR: DEVICE_INDEX not set!" |
|
160 /bin/false |
|
161 rc_status -v |
|
162 rc_exit |
|
163 fi |
|
164 |
|
165 if [ -z "$EOE_INTERFACES" ]; then |
|
166 # support legacy sysconfig files |
|
167 if [ -n "$EOE_DEVICES" ]; then |
|
168 EOE_INTERFACES=$EOE_DEVICES |
|
169 else |
|
170 EOE_INTERFACES=0 |
|
171 fi |
|
172 fi |
|
173 |
|
174 # unload conflicting modules at first |
|
175 for mod in 8139too; do |
|
176 if lsmod | grep "^$mod " > /dev/null; then |
|
177 if ! rmmod $mod; then |
|
178 /bin/false |
|
179 rc_status -v |
|
180 rc_exit |
|
181 fi |
|
182 fi |
|
183 done |
|
184 |
|
185 # load master module |
|
186 if ! modprobe ec_master ec_eoeif_count=$EOE_INTERFACES; then |
|
187 modprobe 8139too |
|
188 /bin/false |
|
189 rc_status -v |
|
190 rc_exit |
|
191 fi |
|
192 |
|
193 # remove stale device node |
|
194 rm -f /dev/${device}0 |
|
195 |
|
196 # get dynamic major number |
|
197 major=$(awk "\$2==\"EtherCAT\" {print \$1}" /proc/devices) |
|
198 |
|
199 # create character device |
|
200 mknod /dev/${device}0 c $major 0 |
|
201 |
|
202 # load device module |
|
203 if ! modprobe ec_8139too ec_device_index=$DEVICE_INDEX; then |
|
204 rmmod ec_master |
|
205 modprobe 8139too |
|
206 /bin/false |
|
207 rc_status -v |
|
208 rc_exit |
|
209 fi |
|
210 |
|
211 # build EoE bridge |
|
212 build_eoe_bridge |
|
213 |
|
214 rc_status -v |
|
215 ;; |
|
216 |
|
217 stop) |
|
218 echo -n "Shutting down EtherCAT master " |
|
219 |
|
220 # unload modules |
|
221 for mod in ec_8139too ec_master; do |
|
222 if lsmod | grep "^$mod " > /dev/null; then |
|
223 if ! rmmod $mod; then |
|
224 /bin/false |
|
225 rc_status -v |
|
226 rc_exit |
|
227 fi; |
|
228 fi; |
|
229 done |
|
230 |
|
231 # remove device node |
|
232 rm -f /dev/${device}0 |
|
233 |
|
234 sleep 1 |
|
235 |
|
236 # reload previous modules |
|
237 if ! modprobe 8139too; then |
|
238 echo "Warning: Failed to restore 8139too module." |
|
239 fi |
|
240 |
|
241 rc_status -v |
|
242 ;; |
|
243 |
|
244 restart) |
|
245 $0 stop || exit 1 |
|
246 sleep 1 |
|
247 $0 start |
|
248 rc_status |
|
249 ;; |
|
250 |
|
251 status) |
|
252 echo -n "Checking for EtherCAT " |
|
253 |
|
254 lsmod | grep "^ec_master " > /dev/null |
|
255 master_running=$? |
|
256 lsmod | grep "^ec_8139too " > /dev/null |
|
257 device_running=$? |
|
258 |
|
259 # master module and device module loaded? |
|
260 test $master_running -eq 0 -a $device_running -eq 0 |
|
261 |
|
262 rc_status -v |
|
263 ;; |
|
264 |
|
265 bridge) |
|
266 echo -n "Building EoE bridge " |
|
267 build_eoe_bridge |
|
268 rc_status -v |
|
269 ;; |
|
270 |
|
271 *) |
|
272 echo "USAGE: $0 {start|stop|restart|status|bridge}" |
|
273 ;; |
|
274 |
|
275 esac |
|
276 |
|
277 rc_exit |
|
278 |
|
279 #------------------------------------------------------------------------------ |