Removed that old spam-blacklist...
[ipfire-2.x.git] / src / initscripts / init.d / mISDN
CommitLineData
c6c9630e
MT
1#!/bin/bash
2
3#----------------------------------------------
4#
5# CONFIGURATION:
6#
7MISDN_CONF="/etc/mISDN.conf"
8MISDN_CONF_XSL="/usr/lib/mISDN/mISDN.conf.xsl"
9#
10#----------------------------------------------
11
12SELF="${0}"
13USAGE="Usage: ${SELF} start|stop|restart|config|scan|help"
14
15function die {
16 echo "[!!] ${1}"
17 exit 1
18}
19
20function check_cmd
21{
22 if ! type -p "${1}" > /dev/null; then
23 if [ "${2}" = "opt" ]; then
24 return
25 fi
26 if [ "$(id -u)" != "0" ]; then
27 die "$1 not in path, please install and/or be root."
28 else
29 die "$1 not in path, please install."
30 fi
31 exit 1
32 else
33 local var=$(echo ${1} | tr a-z A-Z)
34 eval "$var=`type -p ${1}`"
35 fi
36}
37
38function check_misdn_conf
39{
40 if [ ! -f ${MISDN_CONF} ]; then
41 die "${MISDN_CONF} not found. Please run: ${SELF} config"
42 fi
43}
44
45check_cmd sed
46check_cmd cut
47check_cmd cp
48check_cmd wc
49check_cmd grep
50check_cmd xsltproc
51check_cmd modprobe
52check_cmd sleep
53check_cmd lspci
54check_cmd lsusb opt
55check_cmd mknod
56check_cmd chown
57check_cmd chmod
58
59declare -a START_COMMANDS
60declare -a STOP_COMMANDS
61
62declare -a HFCMULTI_card
63declare -a HFCMULTI_type
64declare -a HFCMULTI_protocol
65declare -a HFCMULTI_layermask
66HFCMULTI_options=''
67MISDNDSP_options=''
68L1OIP_options=''
69
70AVMFRITZ_protocol=''
71AVMFRITZ_layermask=''
72
73HFCPCI_protocol=''
74HFCPCI_layermask=''
75
76L1OIP_type=''
77L1OIP_protocol=''
78L1OIP_layermask=''
79L1OIP_codec=''
80L1OIP_ip=''
81L1OIP_port=''
82L1OIP_localport=''
83L1OIP_ondemand=''
84L1OIP_id=''
85
86DEVNODE_user='root'
87DEVNODE_group='root'
88DEVNODE_mode='0644'
89
90declare -a SCAN_card
91declare -a SCAN_opts
92declare -a SCAN_num_ports
93declare -a SCAN_port_opts
94
95function parse_config
96{
97 local CONFIG=$(${XSLTPROC} ${MISDN_CONF_XSL} ${MISDN_CONF})
98 local t p l line i tmpcmd curr tmpstr val
99 local IFS=$'\n'
100
101 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install capi"
102 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install mISDN_core debug=0"
103 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install mISDN_l1 debug=0"
104 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install mISDN_l2 debug=0"
105 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install l3udss1 debug=0"
106 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install mISDN_capi"
107
108 for line in ${CONFIG}; do
109 case "${line}" in
110 DEVNODE:mISDN*)
111 tmpstr=$(echo ${line} | ${SED} -n 's/.*user:\([^ ]*\).*/\1/p')
112 if [ ! -z "${tmpstr}" ]; then
113 DEVNODE_user="${tmpstr}"
114 fi
115 tmpstr=$(echo ${line} | ${SED} -n 's/.*group:\([^ ]*\).*/\1/p')
116 if [ ! -z "${tmpstr}" ]; then
117 DEVNODE_group="${tmpstr}"
118 fi
119 tmpstr=$(echo ${line} | ${SED} -n 's/.*mode:\([^ ]*\).*/\1/p')
120 if [ ! -z "${tmpstr}" ]; then
121 DEVNODE_mode="${tmpstr}"
122 fi
123 ;;
124 MODULE:hfcmulti*)
125 HFCMULTI_options=${line:16}
126 ;;
127 MODULE:mISDN_dsp*)
128 MISDNDSP_options=${line:17}
129 ;;
130 MODULE:l1oip*)
131 L1OIP_options=${line:13}
132 ;;
133 CARD:BN*)
134 curr='hfcmulti'
135 i=${#HFCMULTI_type[@]}
136 let "t = $(echo ${line} | ${SED} -n 's/.*type:\([^,]*\).*/\1/p')"
137 HFCMULTI_type[${i}]=$(printf "0x%x" ${t})
138
139# this is for the BN2E1 card that needs two type numbers
140 t=$(echo ${line} | ${SED} -n 's/.*type:[^,]*,\([^ ]*\).*/\1/p')
141 if [ ! -z "${t}" ]; then
142 let "t = ${t}"
143 HFCMULTI_type[${i}]="${HFCMULTI_type[${i}]},$(printf "0x%x" ${t})"
144 fi
145
146 HFCMULTI_card[${i}]=$(echo ${line:5} | ${CUT} -d" " -f1)
147 ;;
148 CARD:hfcpci*)
149 curr='hfcpci'
150 ;;
151 CARD:avmfritz*)
152 curr='avmfritz'
153 ;;
154 CARD:l1oip*)
155 curr='l1oip'
156 ;;
157 PORT*)
158 case "${curr}" in
159 hfcmulti)
160 let "p = $(echo ${line} | ${SED} -n 's/.*protocol:\([^ ]*\).*/\1/p')"
161 HFCMULTI_protocol[${i}]="${HFCMULTI_protocol[${i}]:+"${HFCMULTI_protocol[${i}]},"}$(printf "0x%x" ${p})"
162 let "l = $(echo ${line} | ${SED} -n 's/.*layermask:\([^ ]*\).*/\1/p')"
163 HFCMULTI_layermask[${i}]="${HFCMULTI_layermask[${i}]:+"${HFCMULTI_layermask[${i}]},"}$(printf "0x%x" ${l})"
164 ;;
165 hfcpci)
166 let "p = $(echo ${line} | ${SED} -n 's/.*protocol:\([^ ]*\).*/\1/p')"
167 HFCPCI_protocol="${HFCPCI_protocol:+"${HFCPCI_protocol},"}$(printf "0x%x" ${p})"
168 let "l = $(echo ${line} | ${SED} -n 's/.*layermask:\([^ ]*\).*/\1/p')"
169 HFCPCI_layermask="${HFCPCI_layermask:+"${HFCPCI_layermask},"}$(printf "0x%x" ${l})"
170 ;;
171 avmfritz)
172 let "p = $(echo ${line} | ${SED} -n 's/.*protocol:\([^ ]*\).*/\1/p')"
173 AVMFRITZ_protocol="${AVMFRITZ_protocol:+"${AVMFRITZ_protocol},"}$(printf "0x%x" ${p})"
174 let "l = $(echo ${line} | ${SED} -n 's/.*layermask:\([^ ]*\).*/\1/p')"
175 AVMFRITZ_layermask="${AVMFRITZ_layermask:+"${AVMFRITZ_layermask},"}$(printf "0x%x" ${l})"
176 ;;
177 l1oip)
178 let "val = $(echo ${line} | ${SED} -n 's/.*type:\([^ ]*\).*/\1/p')"
179 L1OIP_type="${L1OIP_type:+"${L1OIP_type},"}$(printf "0x%x" ${val})"
180 let "val = $(echo ${line} | ${SED} -n 's/.*protocol:\([^ ]*\).*/\1/p')"
181 L1OIP_protocol="${L1OIP_protocol:+"${L1OIP_protocol},"}$(printf "0x%x" ${val})"
182 let "val = $(echo ${line} | ${SED} -n 's/.*layermask:\([^ ]*\).*/\1/p')"
183 L1OIP_layermask="${L1OIP_layermask:+"${L1OIP_layermask},"}$(printf "0x%x" ${val})"
184 val="$(echo ${line} | ${SED} -n 's/.*codec:\([^ ]*\).*/\1/p')"
185 L1OIP_codec="${L1OIP_codec:+"${L1OIP_codec},"}${val}"
186 val="$(echo ${line} | ${SED} -n 's/.*ip:\([^ ]*\).*/\1/p')"
187 L1OIP_ip="${L1OIP_ip:+"${L1OIP_ip},"}${val}"
188 val="$(echo ${line} | ${SED} -n 's/.*port:\([^ ]*\).*/\1/p')"
189 L1OIP_port="${L1OIP_port:+"${L1OIP_port},"}${val}"
190 val="$(echo ${line} | ${SED} -n 's/.*localport:\([^ ]*\).*/\1/p')"
191 L1OIP_localport="${L1OIP_localport:+"${L1OIP_localport},"}${val}"
192 val="$(echo ${line} | ${SED} -n 's/.*ondemand:\([^ ]*\).*/\1/p')"
193 L1OIP_ondemand="${L1OIP_ondemand:+"${L1OIP_ondemand},"}${val}"
194 val="$(echo ${line} | ${SED} -n 's/.*id:\([^ ]*\).*/\1/p')"
195 L1OIP_id="${L1OIP_id:+"${L1OIP_id},"}${val}"
196 ;;
197 esac
198 ;;
199 esac
200 done
201
202 if [ ! -z "${HFCMULTI_protocol[0]}" ]; then
203 tmpcmd="${MODPROBE} --ignore-install hfcmulti type=${HFCMULTI_type[0]}"
204 i=1
205 while [ ! -z "${HFCMULTI_type[${i}]}" ]; do
206 tmpcmd="${tmpcmd},${HFCMULTI_type[${i}]}"
207 let "i = ${i} + 1"
208 done
209 tmpcmd="${tmpcmd} protocol=${HFCMULTI_protocol[0]}"
210 i=1
211 while [ ! -z "${HFCMULTI_protocol[${i}]}" ]; do
212 tmpcmd="${tmpcmd},${HFCMULTI_protocol[${i}]}"
213 let "i = ${i} + 1"
214 done
215 tmpcmd="${tmpcmd} layermask=${HFCMULTI_layermask[0]}"
216 i=1
217 while [ ! -z "${HFCMULTI_layermask[${i}]}" ]; do
218 tmpcmd="${tmpcmd},${HFCMULTI_layermask[${i}]}"
219 let "i = ${i} + 1"
220 done
221 START_COMMANDS[${#START_COMMANDS[@]}]="${tmpcmd} ${HFCMULTI_options}"
222 fi
223
224 if [ ! -z "${HFCPCI_protocol}" ]; then
225 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install hfcpci protocol=${HFCPCI_protocol} layermask=${HFCPCI_layermask}"
226 fi
227
228 if [ ! -z "${AVMFRITZ_protocol}" ]; then
229 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install avmfritz protocol=${AVMFRITZ_protocol} layermask=${AVMFRITZ_layermask}"
230 fi
231
232 if [ ! -z "${L1OIP_type}" ]; then
233 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install l1oip type=${L1OIP_type} protocol=${L1OIP_protocol} layermask=${L1OIP_layermask} codec=${L1OIP_codec} ip=${L1OIP_ip} port=${L1OIP_port} localport=${L1OIP_localport} ondemand=${L1OIP_ondemand} id=${L1OIP_id} ${L1OIP_options}"
234 fi
235
236 START_COMMANDS[${#START_COMMANDS[@]}]="${MODPROBE} --ignore-install mISDN_dsp ${MISDNDSP_options}"
237}
238
239function run_start_commands
240{
241 local i=0
242
243 echo "-- Loading mISDN modules --"
244 while [ ! -z "${START_COMMANDS[${i}]}" ]; do
245 echo ">> ${START_COMMANDS[${i}]}"
246 eval "${START_COMMANDS[${i}]}"
247 let "i = ${i} + 1"
248 done
249}
250
251function run_stop_commands
252{
253 local mod i=0
254
255 for mod in $(lsmod | ${SED} -ne '/Module/!{s/\([^ ]*\).*/\1/;p}'); do
256 case "${mod}" in
257 mISDN_capi | mISDN_dsp | l3udss1 | mISDN_l2 | mISDN_l1 | mISDN_isac | hfcmulti | avmfritz | l1oip)
258 STOP_COMMANDS[0]="${STOP_COMMANDS[0]:-"${MODPROBE} -r --ignore-remove"} ${mod}"
259 ;;
260 mISDN_core)
261 STOP_COMMANDS[1]="${MODPROBE} -r --ignore-remove mISDN_core"
262 ;;
263 esac
264 done
265
266 echo "-- Unloading mISDN modules --"
267 while [ ! -z "${STOP_COMMANDS[${i}]}" ]; do
268 echo ">> ${STOP_COMMANDS[${i}]}"
269 eval "${STOP_COMMANDS[${i}]}"
270 let "i = ${i} + 1"
271 done
272}
273
274function scan_devices
275{
276 local skipnext=0 IFS=$'\n'
277 local NL="
278"
279
280 function addcard {
281 SCAN_card[${#SCAN_card[@]}]="${1}"
282 SCAN_opts[${#SCAN_opts[@]}]="${2}"
283 SCAN_num_ports[${#SCAN_num_ports[@]}]="${3}"
284 SCAN_port_opts[${#SCAN_port_opts[@]}]="${4}"
285 }
286
287 for line in $(${LSPCI} -n -d 0xd161:b410); do
288 addcard "BN4S0" "" 4 'mode="te" link="ptmp"'
289 done
290
291 for line in $(${LSPCI} -n | ${SED} -n 's/^\(0000:\|\)\([0-9a-f]\{2\}:[0-9a-f]\{2\}.[0-9a-f]\{1\}\)\( Class \| \)[0-9a-f]\{4\}: 1397:\([0-9a-f]\{4\}\).*$/\4 \2/p'); do
292 if [ ${skipnext} -eq 1 ]; then
293 skipnext=0
294 continue
295 fi
296 case "${line}" in
297 30b1*)
298 case "${line:5}" in
299 00*)
300 addcard "BN1E1" "" 1 'mode="nt" link="ptp"'
301 ;;
302 *)
303 if [ $(${LSPCI} -n -s "${line:5:3}" | ${WC} -l) -eq 2 ]; then
304 addcard "BN2E1" "" 2 'mode="nt" link="ptp"'
305 skipnext=1
306 else
307 addcard "BN1E1" "" 1 'mode="nt" link="ptp"'
308 fi
309 ;;
310 esac
311 ;;
312 16b8*)
313 addcard "BN8S0" "" 8 'mode="te" link="ptmp"'
314 ;;
315 08b4*)
316 if ${LSPCI} -n -v -s "${line:5}" | ${GREP} "Subsystem" | ${GREP} "1397:b567" > /dev/null ; then
317 addcard "BN1S0" "" 1 'mode="te" link="ptmp"'
318 elif ${LSPCI} -n -v -s "${line:5}" | ${GREP} "Subsystem" | ${GREP} "1397:b566\|1397:b569" > /dev/null ; then
319 addcard "BN2S0" "" 2 'mode="te" link="ptmp"'
320 else
321 addcard "BN4S0" "" 4 'mode="te" link="ptmp"'
322 fi
323 ;;
324 esac
325 done
326 for line in $(${LSPCI} -n | ${GREP} "1397:\(2bd\(0\|6\|7\|8\|9\|a\|b\|c\)\|b100\)\|1043:0675\|0871:ffa\(1\|2\)\|1051:0100\|15b0:2bd0\|114f:007\(0\|1\|2\|3\)\|13d1:2bd1\|182d:3069"); do
327 addcard "hfcpci" "" 1 'mode="te" link="ptmp"'
328 done
329 for line in $(${LSPCI} -n | ${GREP} "1244:\(0a00\|0e00\)"); do
330 addcard "avmfritz" "" 1 'mode="te" link="ptmp"'
331 done
332 for line in $(${LSPCI} -n -d 1050:6692); do
333 addcard "w6692pci" "" 1 'mode="te" link="ptmp"'
334 done
335 if [ -e ${LSUSB} ]; then
336 for line in $(${LSUSB} | ${GREP} "0959:2bd0\|0675:1688\|07b0:0007\|0742:200\(7\|8\|9\|A\)\|08e3:0301\|07fa:084\(7\|8\)\|07ba:0006"); do
337 addcard "hfcsusb" "" 1 'mode="te" link="ptmp"'
338 done
339 fi
340}
341
342function write_mISDN_conf
343{
344 local NL="
345"
346 local TAB=" "
347 local HEADER="<?xml version=\"1.0\"?>
348<!--
349- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
350Card Type: BN2S0, BN4S0, BN8S0
351Card Attributes: ulaw=(yes|no), dtmf=(yes|no), pcm_slave=(yes|no),
352 ignore_pcm_frameclock=(yes|no), rxclock=(yes|no),
353 crystalclock=(yes|no), watchdog=(yes|no)
354Port Attributes: mode=(te|nt), link=(ptp|ptmp), master-clock=(yes|no),
355 capi=(yes|no)
356- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
357Card Type: BN2E1
358Card Attributes: ulaw=(yes|no), dtmf=(yes|no), pcm_slave=(yes|no),
359 ignore_pcm_frameclock=(yes|no), rxclock=(yes|no),
360 crystalclock=(yes|no), watchdog=(yes|no)
361Port Attributes: mode=(te|nt), link=(ptp|ptmp), optical=(yes|no), los=(yes|no),
362 ais=(yes|no), slip=(yes|no), nocrc4=(yes|no), capi=(yes|no)
363- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
364Card Type: hfcmulti, avmfritz, w6692pci
365Port Attributes: mode=(te|nt), link=(ptp|ptmp), capi=(yes|no)
366- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
367Module: hfcmulti
368Options: poll=<number>, pcm=<number>, debug=<number>, timer=(yes|no)
369- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
370Module: mISDN_dsp
371Options: debug=<number>, options=<number>, poll=<number>,
372 dtmfthreshold=<number>
373- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
374-->
375<mISDNconf>
376${TAB}<module poll=\"128\" debug=\"0\" timer=\"no\">hfcmulti</module>
377${TAB}<module debug=\"0\" options=\"0\">mISDN_dsp</module>
378${TAB}<devnode user=\"root\" group=\"root\" mode=\"644\">mISDN</devnode>"
379 local FOOTER="</mISDNconf>"
380 local i=0 j=0 MAIN=""
381
382 echo "Writing ${MISDN_CONF} for ${#SCAN_card[@]} mISDN compatible device(s):"
383 while [ ! -z "${SCAN_card[${i}]}" ]; do
384 echo ">> ${SCAN_card[${i}]}"
385 MAIN="${MAIN}${NL}${TAB}<card type=\"${SCAN_card[${i}]}\"${SCAN_opts[${i}]:+" ${SCAN_opts[${i}]}"}>"
386 j=1
387 while [ ${j} -le ${SCAN_num_ports[${i}]} ]; do
388 MAIN="${MAIN}${NL}${TAB}${TAB}<port${SCAN_port_opts[${i}]:+" ${SCAN_port_opts[${i}]}"}>${j}</port>"
389 let "j = ${j} + 1"
390 done
391 MAIN="${MAIN}${NL}${TAB}</card>"
392 let "i = ${i} + 1"
393 done
394
395 if [ -f ${MISDN_CONF} ]; then
396 echo "${MISDN_CONF} already present, saving a backup: ${MISDN_CONF}.bak"
397 ${CP} "${MISDN_CONF}" "${MISDN_CONF}.bak" || die "Could not backup your existing ${MISDN_CONF}!"
398 fi
399 echo "${HEADER}${MAIN}${NL}${FOOTER}" > ${MISDN_CONF}
400}
401
402function print_scan_results
403{
404 local i=0
405
406 echo "${#SCAN_card[@]} mISDN compatible device(s) found:"
407 while [ ! -z "${SCAN_card[${i}]}" ]; do
408 echo ">> ${SCAN_card[${i}]}"
409 let "i = ${i} + 1"
410 done
411}
412
413function mk_misdn_dev
414{
415 if [ ! -e /dev/mISDN ]; then
416 echo "creating device node: /dev/mISDN"
417 ${MKNOD} /dev/mISDN c 46 0
418 fi
419 ${CHOWN} ${DEVNODE_user}:${DEVNODE_group} /dev/mISDN
420 ${CHMOD} ${DEVNODE_mode} /dev/mISDN
421}
422
423#
424# MAIN
425#
426
427case "${1}" in
428
429 start|--start)
430
431 check_misdn_conf
432 parse_config
433 run_start_commands
434 mk_misdn_dev
435 ;;
436
437 stop|--stop)
438
439 run_stop_commands
440 ;;
441
442 restart|--restart)
443
444 check_misdn_conf
445 parse_config
446 run_stop_commands
447 ${SLEEP} 2
448 run_start_commands
449 mk_misdn_dev
450 ;;
451
452 config|--config)
453
454 scan_devices
455 write_mISDN_conf
456 ;;
457
458 scan|--scan)
459
460 scan_devices
461 print_scan_results
462 ;;
463
464 help|--help)
465 echo "${USAGE}"
466 exit 0
467 ;;
468
469 *)
470 echo "${USAGE}"
471 exit 2
472 ;;
473
474esac
475