| 1 | #!/bin/sh |
| 2 | |
| 3 | MODULES="dahdi" |
| 4 | DAHDI_MODULES_FILE="/etc/dahdi/modules" |
| 5 | |
| 6 | usage() { |
| 7 | cat <<EOF |
| 8 | $0: loads / unloads DAHDI kernel modules |
| 9 | |
| 10 | Usage: $0 <load|unload> |
| 11 | |
| 12 | * load: Loads all modules listed in /etc/dahdi/modules (one per line) |
| 13 | * unload: Unloads the DAHDI modules (all the modules that are dependencies |
| 14 | of $MODULES). |
| 15 | EOF |
| 16 | } |
| 17 | |
| 18 | # recursively unload a module and its dependencies, if possible. |
| 19 | # where's modprobe -r when you need it? |
| 20 | # inputs: module to unload. |
| 21 | # returns: the result from |
| 22 | unload_module() { |
| 23 | module="$1" |
| 24 | line=`lsmod 2>/dev/null | grep "^$1 "` |
| 25 | if [ "$line" = '' ]; then return; fi # module was not loaded |
| 26 | |
| 27 | set -- $line |
| 28 | # $1: the original module, $2: size, $3: refcount, $4: deps list |
| 29 | mods=`echo $4 | tr , ' '` |
| 30 | ec_modules="" |
| 31 | # xpp_usb keeps the xpds below busy if an xpp hardware is |
| 32 | # connected. Hence must be removed before them: |
| 33 | case "$module" in xpd_*) mods="xpp_usb $mods";; esac |
| 34 | |
| 35 | for mod in $mods; do |
| 36 | case "$mod" in |
| 37 | dahdi_echocan_*) |
| 38 | ec_modules="$mod $ec_modules" |
| 39 | ;; |
| 40 | *) |
| 41 | # run in a subshell, so it won't step over our vars: |
| 42 | (unload_module $mod) |
| 43 | ;; |
| 44 | esac |
| 45 | done |
| 46 | # Now that all the other dependencies are unloaded, we can unload the |
| 47 | # dahdi_echocan modules. The drivers that register spans may keep |
| 48 | # references on the echocan modules before they are unloaded. |
| 49 | for mod in $ec_modules; do |
| 50 | (unload_module $mod) |
| 51 | done |
| 52 | rmmod $module |
| 53 | } |
| 54 | |
| 55 | unload_modules() { |
| 56 | for module in "$@"; do |
| 57 | unload_module $module |
| 58 | done |
| 59 | } |
| 60 | |
| 61 | load_modules() { |
| 62 | modules=`sed -e 's/#.*$//' $DAHDI_MODULES_FILE 2>/dev/null` |
| 63 | for line in $modules; do |
| 64 | modprobe $line |
| 65 | done |
| 66 | } |
| 67 | |
| 68 | case "$1" in |
| 69 | load) load_modules "$@";; |
| 70 | unload) unload_modules $MODULES;; |
| 71 | *) usage;; |
| 72 | esac |