summaryrefslogtreecommitdiff
path: root/pm/power.d/wireless
blob: 0487dc13412895b4ba23fc224d9c727faf4ebc78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh

# See if we have the usual wireless tools.
# Do not just fail because not all cards require these.
which iwpriv >/dev/null 2>&1 && have_iwpriv="true"
which iwconfig >/dev/null 2>&1 && have_iwconfig="true"

# If only all the drivers did The Right Thing with iwconfig power.
# Too bad they do not.

get_wireless_params() {
    # $1 = interface 
    # $2 = on or off
    unset iwpriv iwconfig iwlevel
    
    # Don't do anything if we cannot find a driver for this iface.
    [ -L "/sys/class/net/$1/device/driver" ] || return 1
    # Skip if not a wireless card.
    [ -d "/sys/class/net/$1/wireless" ] || return 1
    # Also don't do anything if the device is disabled
    [ "$(cat /sys/class/net/$1/device/enabled)" = "1" ] || return 1
    driver="$(readlink "/sys/class/net/$1/device/driver")"
    driver=${driver##*/}
    case $driver in
        ipw2100) iwpriv_ac="set_power 0"
            iwpriv_batt="set_power 5"
            iwconfig_ac="power on"
            iwconfig_batt="power on";;
        ipw3945)
            iwpriv_ac="set_power 6"
            iwpriv_batt="set_power 7";;
        iwl*) if [ -f "/sys/class/net/$1/device/power_level" ]; then
                 iwlevel_ac=0
                 iwlevel_batt=3
              else
                 iwconfig_ac="power off"
                 iwconfig_batt="power on"
              fi;;
        *) iwconfig_ac="power off"
           iwconfig_batt="power on";;
    esac
    case $2 in
        off) [ "$iwpriv_ac" ] && iwpriv="$iwpriv_ac"
            [ "$iwconfig_ac" ] && iwconfig="$iwconfig_ac"
            [ "$iwlevel_ac" ] && iwlevel="$iwlevel_ac";;
        on) [ "$iwpriv_batt" ] && iwpriv="$iwpriv_batt"
            [ "$iwconfig_batt" ] && iwconfig="$iwconfig_batt"
            [ "$iwlevel_batt" ] && iwlevel="$iwlevel_batt";;
    esac
    return 0
}

wireless_powersave() {
    for dev in /sys/class/net/*; do
        get_wireless_params "${dev##*/}" "$1"
	[ "$have_iwconfig" = true -a "$iwconfig" ] && iwconfig $iwconfig
        [ "$have_iwpriv" = true -a "$iwpriv" ] && iwpriv $iwpriv
        [ "$iwlevel" ] && echo "$iwlevel" > "$dev/device/power_level"
    done
}

case $1 in
    true) wireless_powersave on ;;
    false) wireless_powersave off ;;
    *) exit $NA ;;
esac

exit 0