summaryrefslogtreecommitdiffstats
path: root/tp_smapi.init
blob: d0fce107f0ed5abf0329c41b62efac97427873ef (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
#
#       /etc/rc.d/init.d/tp_smapi
#
# Load tp_smapi kernel driver and apply settings
#
# chkconfig: 345 10 90
# description: Load tp_smapi kernel driver and apply settings
# processname:

### BEGIN INIT INFO
# Provides: tp_smapi
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: load and set up tp_smapi
# Description: Load tp_smapi kernel driver and apply settings
### END INIT INFO

# TODO: Make sure this script's startup runs after akmod's startup

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0
CONFIG=/etc/sysconfig/tp_smapi

# Source function library.
. /etc/rc.d/init.d/functions

load_modules() {
    echo -n "Loading tp_smapi driver module"
    if modprobe tp_smapi; then
	success "Loading tp_smapi driver module"
    else
	failure "Loading tp_smapi driver module"
        RETVAL=2
    fi
    echo
}

unload_modules() {
    mdoules="tp_smapi thinkpad_ec"
    echo -n "Unloading driver modules ${modules}"
    if modprobe -r tp_smapi && modprobe -r thinkpad_ec; then
	success "Unloading driver modules ${modules}"
    else
	failure "Unloading driver modules ${modules}"
        RETVAL=2
    fi
    echo
}

smapi_config() {
    if [ -s "$CONFIG" ]; then
	. "$CONFIG"
	dirty=false
	cd /sys/devices/platform/smapi
	echo -n "Configuring tp_smapi battery settings"
	for bat in BAT*; do
	    if test -d "$bat"; then
		if [ "x$START_CHARGE_THRESH" != x ]; then
		    echo "$START_CHARGE_THRESH" > "$bat/start_charge_thresh"
		fi
		if [ "x$STOP_CHARGE_THRESH" != x ]; then
		    echo "$STOP_CHARGE_THRESH" > "$bat/stop_charge_thresh"
		fi
	    else
		dirty=true
		break
	    fi
	done
	if "$dirty"; then
	    passed "No tp_smapi battery settings"
	else
	    success "Configured tp_smapi battery settings"
	fi
        echo
    fi
}

is_loaded() {
    if /sbin/lsmod | grep '^tp_smapi ' > /dev/null; then
	echo -n "tp_smapi settings:"
	cd /sys/devices/platform/smapi
	for bat in BAT*; do
	    if [ -d "$bat" ] && [ "$(<$bat/installed)" -eq 1 ]; then
		echo -n " $bat($(<$bat/remaining_percent)%, $(<$bat/start_charge_thresh)%..$(<$bat/stop_charge_thresh)%)"
	    fi
	done
	echo
    else
	echo "tp_smapi driver is not loaded"
	return 2
    fi
}

case "$1" in
force-reload)
	echo "$0: Unimplemented feature."
	RETVAL=3
	;;
start)
	load_modules
	smapi_config
	;;
reload|restart|force-reload)
	unload_modules
	load_modules
	smapi_config
	;;
condrestart)
	if is_loaded; then
	    unload_modules
	    load_modules
	    smapi_config
	fi
	;;
stop)
	unload_modules
	;;
status)
	is_loaded
	RETVAL=$?
	;;
*)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|force-reload}"
        RETVAL=2
esac

exit $RETVAL