live_dahdi 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #!/bin/sh
  2. LINUX_DIR=${LINUX_DIR:=.}
  3. TOOLS_DIR=${TOOLS_DIR:=../dahdi-tools}
  4. DYNAMIC_LOC="${DYNAMIC_LOC:-no}"
  5. DESTDIR=$PWD/live
  6. KVERS=${KVERS:-`uname -r`}
  7. XPP_SYNC=${XPP_SYNC:-auto}
  8. AST_SCRIPT=${AST_SCRIPT:-/etc/init.d/asterisk}
  9. # Use this file to pass options to modules:
  10. PERLLIBDIR=`perl -V:sitelib | cut -d "'" -f 2`
  11. EXTRA_MODS="crc_ccitt"
  12. # Manual list of modules. They will be loaded by insmod.
  13. # If reside in a subdir, add it explicitly.
  14. MODULES_LOAD="${MODULES_LOAD:-dahdi dahdi_echocan_mg2}"
  15. # this one *is* resolved recusively.
  16. # the only reason to set a different value is if you use hpec / oslec,
  17. # as Zaptel depends on them.
  18. REMOVE_MODULES="${REMOVE_MODULES:-dahdi}" # "dahdi oslec"?
  19. if [ -r $DESTDIR/live.conf ]; then . $DESTDIR/live.conf; fi
  20. MODULES_DIR="$DESTDIR/lib/modules/$KVERS/dahdi"
  21. if [ ! -d "$LINUX_DIR" ]; then
  22. echo >&2 "$0:dahdi-linux dir '$LINUX_DIR' does not exits. Aborting".
  23. exit 1
  24. fi
  25. set -e
  26. LINUX_DIR_FULL=`(cd $LINUX_DIR; pwd)`
  27. set_tools_dir() {
  28. if [ ! -d "$TOOLS_DIR" ]; then
  29. echo >&2 "$0:dahdi-tools dir '$TOOLS_DIR' does not exits. Aborting".
  30. exit 1
  31. fi
  32. TOOLS_DIR_FULL=`(cd $TOOLS_DIR; pwd)`
  33. }
  34. # Give priority to our installed binaries:
  35. PATH=$DESTDIR/sbin:$DESTDIR/usr/sbin:$PATH
  36. export PATH
  37. # TODO: If you already use PERL5DIR, please fix this part:
  38. PERL5LIB="$DESTDIR/$PERLLIBDIR"
  39. export PERL5LIB
  40. # used in xpp_fxloader:
  41. FIRMWARE_DIR="$DESTDIR/usr/share/dahdi"
  42. export FIRMWARE_DIR
  43. ASTRIBANK_TOOL="$DESTDIR/usr/sbin/astribank_tool"
  44. export ASTRIBANK_TOOL
  45. ASTRIBANK_HEXLOAD="$DESTDIR/usr/sbin/astribank_hexload"
  46. export ASTRIBANK_HEXLOAD
  47. # make sure Astribank initialization scripts are from our tree.
  48. xpp_ARGS="$xpp_ARGS initdir=$FIRMWARE_DIR"
  49. #dahdi_ARGS="$dahdi_ARGS initdir=$FIRMWARE_DIR"
  50. if [ "$DYNAMIC_LOC" = 'yes' ]; then
  51. MODULES_LOAD="$MODULES_LOAD dahdi_dynamic dahdi_dynamic_loc"
  52. fi
  53. # the same as xpp/utils/dahdi_drivers .
  54. # With the remote mode, I can't rely on files in the source directory.
  55. dahdi_drivers() {
  56. perl -MDahdi::Hardware -e '
  57. my @drivers = Dahdi::Hardware->drivers;
  58. print join(" ", @drivers);
  59. '
  60. }
  61. # Add modules for existing hardware on the system for the list of
  62. # modules to load.
  63. #
  64. # As module loading is manual with insmod, some manual fixes are needed.
  65. set_modules_to_load() {
  66. for mod in `dahdi_drivers`; do
  67. case "$mod" in
  68. xpp_usb)
  69. MODULES_LOAD="$MODULES_LOAD xpp/xpp xpp/xpd_fxs"
  70. MODULES_LOAD="$MODULES_LOAD xpp/xpd_fxo xpp/xpd_pri"
  71. if [ -r "$MODULES_DIR/xpp/xpd_bri.ko" ]; then
  72. MODULES_LOAD="$MODULES_LOAD xpp/xpd_bri"
  73. fi
  74. MODULES_LOAD="$MODULES_LOAD xpp/xpp_usb"
  75. ;;
  76. wctdm24xxp | wcte12xp)
  77. # FIXME: better automation of the voicebus
  78. # dependency:
  79. MODULES_LOAD="$MODULES_LOAD voicebus/dahdi_voicebus $mod/$mod"
  80. EXTRA_MODS="$EXTRA_MODS firmware_class"
  81. ;;
  82. wct4xxp | wcte12xp | wctc4xp | wcb4xxp)
  83. MODULES_LOAD="$MODULES_LOAD $mod/$mod"
  84. ;;
  85. wanpipe)
  86. : # requires different handling
  87. ;;
  88. *)
  89. MODULES_LOAD="$MODULES_LOAD $mod"
  90. ;;
  91. esac
  92. done
  93. }
  94. # Initialize the Xorcom Astribank (xpp/) using perl utiliites:
  95. # intended to replace all the the three functions below if user has
  96. # installed the dahdi-perl utilities.
  97. xpp_startup() {
  98. # do nothing if there are no astribank devices:
  99. if ! grep -q connected /proc/xpp/xbuses 2>/dev/null; then return 0; fi
  100. echo "Waiting for Astribank devices to initialize:"
  101. "$FIRMWARE_DIR/waitfor_xpds" # Asusmes a recent dahdi-tools
  102. # overriding locales for the above two, as perl can be noisy
  103. # when locales are missing.
  104. # No register all the devices if they didn't auto-register:
  105. LC_ALL=C dahdi_registration on
  106. # this one could actually be run after dahdi_cfg:
  107. LC_ALL=C xpp_sync "$XPP_SYNC"
  108. }
  109. # recursively unload a module and its dependencies, if possible.
  110. # where's modprobe -r when you need it?
  111. # inputs: module to unload.
  112. # returns: the result from
  113. unload_module() {
  114. module="$1"
  115. line=`lsmod 2>/dev/null | grep "^$1 " || :`
  116. if [ "$line" = '' ]; then return; fi # module was not loaded
  117. set -- $line
  118. # $1: the original module, $2: size, $3: refcount, $4: deps list
  119. mods=`echo $4 | tr , ' '`
  120. ec_modules=""
  121. # xpp_usb keeps the xpds below busy if an xpp hardware is
  122. # connected. Hence must be removed before them:
  123. case "$module" in xpd_*) mods="xpp_usb $mods";; esac
  124. for mod in $mods; do
  125. case "$mod" in
  126. dahdi_echocan_*)
  127. ec_modules="$mod $ec_modules"
  128. ;;
  129. *)
  130. # run in a subshell, so it won't step over our vars:
  131. (unload_module $mod)
  132. ;;
  133. esac
  134. done
  135. # Now that all the other dependencies are unloaded, we can unload the
  136. # dahdi_echocan modules. The drivers that register spans may keep
  137. # references on the echocan modules before they are unloaded.
  138. for mod in $ec_modules; do
  139. (unload_module $mod)
  140. done
  141. rmmod $module
  142. }
  143. load_dynamic() {
  144. if [ "$DYNAMIC_LOC" != yes ]; then return; fi
  145. local conf_file="$DESTDIR/etc/dahdi/dynamic.conf"
  146. if [ ! -r "$conf_file" ]; then
  147. cat <<EOF >"$conf_file"
  148. dynamic=loc,1:0,5,0
  149. dynamic=loc,1:1,5,0
  150. EOF
  151. fi
  152. dahdi_cfg -c "$conf_file"
  153. }
  154. unload_dynamic() {
  155. if [ "$DYNAMIC_LOC" != yes ]; then return; fi
  156. local conf_file="$DESTDIR/etc/dahdi/dynamic.conf"
  157. if [ ! -r "$conf_file" ]; then return; fi
  158. dahdi_cfg -c "$conf_file" -s || :
  159. }
  160. genconf() {
  161. GENCONF_PARAMETERS=$DESTDIR/etc/dahdi/genconf_parameters \
  162. DAHDI_CONF_FILE=$DESTDIR/etc/dahdi/system.conf \
  163. DAHDI_MODS_FILE=$DESTDIR/etc/dahdi/modules \
  164. CHAN_DAHDI_CHANNELS_FILE=$DESTDIR/etc/asterisk/dahdi-channels.conf \
  165. dahdi_genconf
  166. if [ "$DYNAMIC_LOC" = yes ]; then
  167. cat "$DESTDIR/etc/dahdi/dynamic.conf" >>"$DESTDIR/etc/dahdi/system.conf"
  168. fi
  169. dahdi_cfg -c $DESTDIR/etc/dahdi/system.conf
  170. # TODO: fxotune, hpec
  171. }
  172. run_asterisk() {
  173. $AST_SCRIPT start
  174. }
  175. usage() {
  176. me=`basename $0`
  177. echo "$me: Run DAHDI in a test environment"
  178. echo 'Version: $Id$'
  179. echo ''
  180. echo "Usage: equivalent of:"
  181. echo "$me configure ./configure"
  182. echo "$me install make install"
  183. echo "$me config make config"
  184. echo "$me unload /etc/init.d/dahdi stop"
  185. echo "$me load /etc/init.d/dahdi start"
  186. echo "$me reload /etc/init.d/dahdi restart"
  187. echo "$me xpp-firm (Reset and load xpp firmware)"
  188. echo "$me genconf dahdi_genconf; dahdi_cfg"
  189. echo "$me asterisk /etc/init.d/asterisk start"
  190. echo "$me rsync TARGET (copy filea to /tmp/live in host TARGET)"
  191. echo "$me exec COMMAND (Run COMMAND in 'live' environment)"
  192. echo ""
  193. echo "dahdi-linux: $LINUX_DIR"
  194. echo "dahdi-tools: $TOOLS_DIR"
  195. }
  196. case "$1" in
  197. configure)
  198. shift
  199. set_tools_dir
  200. cd "$TOOLS_DIR"; ./configure --with-dahdi="$LINUX_DIR_FULL" "$@"
  201. ;;
  202. install)
  203. shift
  204. set_tools_dir
  205. cd "$LINUX_DIR"; make install DESTDIR=$DESTDIR "$@"
  206. cd "$TOOLS_DIR_FULL"; make install DESTDIR=$DESTDIR DYNFS=yes "$@"
  207. ;;
  208. config)
  209. shift
  210. set_tools_dir
  211. cd "$TOOLS_DIR"; make config DESTDIR=$DESTDIR "$@"
  212. mkdir -p $DESTDIR/etc/asterisk
  213. cat >"$FIRMWARE_DIR/live-init.conf" <<EOF
  214. PATH="$PATH" # FIXME: add to existing \$PATH?
  215. export PATH
  216. dahdiconfdir="${DESTDIR}/etc/dahdi"
  217. export dahdiconfdir
  218. EOF
  219. ln -s ../../../etc/dahdi/xpp.conf "$DESTDIR/usr/share/dahdi/"
  220. ;;
  221. rsync)
  222. if [ $# -ne 2 ]; then
  223. echo >&2 "$0: Error: rsync requires a target parameter".
  224. exit 1
  225. fi
  226. # copy the script itself and the installed directory to the
  227. # target host:
  228. rsync -ai "$0" $DESTDIR "$2:/tmp/"
  229. ;;
  230. unload)
  231. # OK for Asterisk not to be running. TODO: a better test?
  232. $AST_SCRIPT stop || :
  233. unload_dynamic
  234. for mod in $REMOVE_MODULES; do
  235. unload_module $mod
  236. done
  237. ;;
  238. load)
  239. # TODO: Find a way to use modprobe.
  240. # Or implement a way to pass arguments to modules here (yuck)
  241. set_modules_to_load
  242. for mod in $EXTRA_MODS; do
  243. modprobe $mod || : # FIXME: Make this optional?
  244. done
  245. for module in $MODULES_LOAD; do
  246. eval module_args="\$`basename ${module}`_ARGS"
  247. insmod $MODULES_DIR/$module.ko $module_args
  248. done
  249. xpp_startup
  250. load_dynamic
  251. genconf
  252. # or find a way to reuse init.d start sequence.
  253. # TODO: A local copy of Asterisk, configured with dahdi_gnconf.
  254. # doable, but trickier.
  255. run_asterisk
  256. ;;
  257. genconf)
  258. genconf
  259. ;;
  260. asterisk)
  261. run_asterisk
  262. ;;
  263. reload)
  264. $0 unload
  265. $0 load
  266. ;;
  267. exec)
  268. if [ $# -lt 2 ]; then
  269. # No command given: start a subshell in the environemnt
  270. # of the "live" system:
  271. echo >&2 "$0: Error: exec requires a command to run"
  272. exit 1
  273. fi
  274. # Command given: run it:
  275. shift
  276. "$@"
  277. ;;
  278. xpp-firm)
  279. # Still broken. Needs to be run several times.
  280. # set XPP_HOTPLUG_DISABLED=yes in /etc/dahdi/init.conf
  281. XPP_FIRMWARE_DIR=$FIRMWARE_DIR \
  282. "$FIRMWARE_DIR/xpp_fxloader" reset
  283. sleep 5
  284. XPP_FIRMWARE_DIR=$FIRMWARE_DIR \
  285. "$FIRMWARE_DIR/xpp_fxloader" load
  286. ;;
  287. help|'')
  288. usage
  289. ;;
  290. *)
  291. echo >&2 "$0: Error: incorrect command \"$1\". Aborting"
  292. usage
  293. exit 1
  294. esac