forked from linrunner/TLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlp.in
347 lines (296 loc) · 9.19 KB
/
tlp.in
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/sh
# tlp - adjust power settings
#
# Copyright (c) 2016 Thomas Koch <linrunner at gmx.net>
# This software is licensed under the GPL v2 or later.
# --- Constants
readonly LIBDIR="@TLP_TLIB@"
readonly LIBS="tlp-functions tlp-rf-func"
# --- Source libraries
for lib in $LIBS; do
if [ ! -f $LIBDIR/$lib ]; then
echo "Error: missing function library \'$LIBDIR/$lib\'." 1>&2
exit 1
fi
. $LIBDIR/$lib
done
# --- Subroutines
apply_common_settings () { # apply settings common to all modes
# $1: 0=ac mode, 1=battery mode
set_laptopmode $1
set_dirty_parms $1
set_scaling_governor $1
set_scaling_min_max_freq $1
set_cpu_perf_pct $1
set_cpu_boost_all $1
set_sched_powersave $1
set_nmi_watchdog
set_phc_controls $1
set_energy_perf_policy $1
set_disk_apm_level $1
set_disk_spindown_timeout $1
set_disk_io_sched
set_sata_link_power $1
set_ahci_runtime_pm $1
set_pcie_aspm $1
set_radeon_profile $1
set_wifi_power_mode $1
disable_wake_on_lan
set_sound_power_mode $1
set_runtime_pm $1
return 0
}
# --- MAIN
read_defaults
check_tlp_enabled || exit 1
add_sbin2path
check_laptop_mode_tools
# get cmd line args
mode=$(echo $1 | tr "[:upper:]" "[:lower:]")
mode2=$(echo $2 | tr "[:upper:]" "[:lower:]")
# inhibit trace output for tlp stat
[ "$mode" = "stat" ] && nodebug=1
# get current power state
get_power_state
pwrmode=$?
modedebug=$mode
[ -n "$mode2" ] && modedebug="$modedebug $mode2"
echo_debug "run" "+++ mode=$modedebug ($TLPVER) ++++++++++++++++++++++++++++++++++++++++"
if [ -n "$addpath" ]; then
echo_debug "path" "PATH=$oldpath[$addpath]"
else
echo_debug "path" "PATH=$oldpath"
fi
if [ $pwrmode -eq 1 ]; then
echo_debug "run" "power_source=bat"
else
echo_debug "run" "power_source=ac"
fi
exitcode=0
case "$mode" in
init) # system initialization/shutdown: sysv, upstart, systemd, ...
check_root
# try to obtain lock (with timeout)
locked=0
if lock_tlp; then
locked=1
else
echo "Failed to get lock, continuing anyway." 1>&2
fi
# do init business ...
case $mode2 in
start)
# apply radio states
set_radio_device_states start
# apply power save settings -- but only if not previously run
# (by the udev rule) for the same power state
if compare_and_save_power_state $pwrmode; then
echo -n "Applying power save settings..."
apply_common_settings $pwrmode
[ "$pwrmode" = "1" ] && poweroff_drivebay 0
[ "$X_TLP_USB_MODE" = "1" ] && set_usb_suspend 0 auto
echo "done."
fi
# apply battery settings
echo -n "Setting battery charge thresholds..."
set_charge_thresholds
echo "done."
;;
restart|force-reload)
# apply power save settings
compare_and_save_power_state $pwrmode
echo -n "Applying power save settings..."
apply_common_settings $pwrmode
[ "$pwrmode" = "1" ] && poweroff_drivebay 0
[ "$X_TLP_USB_MODE" = "1" ] && set_usb_suspend 0 auto
echo "done."
# apply battery settings
echo -n "Setting battery charge thresholds..."
set_charge_thresholds
echo "done."
;;
stop)
set_radio_device_states stop
if [ "$USB_AUTOSUSPEND_DISABLE_ON_SHUTDOWN" = "1" ]; then
echo -n "Disabling usb autosuspend..."
set_usb_suspend 0 on
echo "done."
fi
# remove usb startup flag
[ -f $USB_DONE ] && rm $USB_DONE
# clear saved power state
clear_saved_power_state
# apply ac settings for faster shutdown
echo -n "Applying power save settings..."
apply_common_settings 0
echo "done."
;;
*)
echo "Usage: tlp init {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
# unlock if necessary
[ $locked -eq 0 ] || unlock_tlp
;;
auto) # set mode depending on state (called by udev rule)
# -- but only if not previously run for the same power state
# rationale: filter out duplicate power_supply udev events
check_root
if lock_tlp_nb; then
if compare_and_save_power_state $pwrmode; then
apply_common_settings $pwrmode
[ "$pwrmode" = "1" ] && poweroff_drivebay 0
set_radio_device_states $pwrmode
if [ "$RESTORE_THRESHOLDS_ON_BAT" = "1" ] && [ "$pwrmode" = "1" ]; then
set_charge_thresholds
fi
fi
unlock_tlp
else
exitcode=4
fi
;;
start) # set mode depending on state (interactive mode)
check_root
if lock_tlp; then
compare_and_save_power_state $pwrmode
apply_common_settings $pwrmode
[ "$pwrmode" = "1" ] && poweroff_drivebay 0
set_usb_suspend 0 auto
set_charge_thresholds
set_radio_device_states $pwrmode
unlock_tlp
echo_started_mode $pwrmode
else
echo_tlp_locked
exitcode=4
fi
;;
true|bat*) # set battery power mode
check_root
if lock_tlp; then
compare_and_save_power_state 1
apply_common_settings 1
poweroff_drivebay 0
[ "$X_TLP_USB_MODE" = "1" ] && set_usb_suspend 0 auto
set_radio_device_states 1
unlock_tlp
echo_started_mode 1
else
echo_tlp_locked
exitcode=4
fi
;;
false|ac) # set ac power mode
check_root
if lock_tlp; then
compare_and_save_power_state 0
apply_common_settings 0
[ "$X_TLP_USB_MODE" = "1" ] && set_usb_suspend 0 auto
set_radio_device_states 0
unlock_tlp
echo_started_mode 0
else
echo_tlp_locked
exitcode=4
fi
;;
suspend) # handle suspend/hibernate
check_root
if lock_tlp; then
save_device_states "bluetooth wwan"
apply_common_settings 0
suspend_drivebay $pwrmode
unlock_tlp
else
exitcode=4
fi
;;
resume) # handle resume
check_root
if lock_tlp; then
restore_device_states
compare_and_save_power_state $pwrmode
apply_common_settings $pwrmode
resume_drivebay
unlock_tlp
else
exitcode=4
fi
;;
usb) # Enable usb autosuspend
check_root
set_usb_suspend 1 auto
;;
bayoff) # power off drive bay
check_root
poweroff_drivebay 1
;;
setcharge) # set charge thresholds (temporarily)
check_root
setcharge_battery $2 $3 $4
exitcode=$?
;;
fullcharge) # charge battery up to 100% (temporarily)
if check_ac_power fullcharge; then
check_root
setcharge_battery 96 100 $2
exitcode=$?
else
exitcode=2
fi
;;
chargeonce) # charge battery to upper threshold once
if check_ac_power chargeonce; then
check_root
chargeonce_battery $2
exitcode=$?
else
exitcode=2
fi
;;
discharge) # discharge battery completely (to recalibrate)
if check_ac_power discharge; then
check_root
discharge_battery $2
exitcode=$?
else
exitcode=2
fi
;;
recalibrate) # recalibrate battery, i.e. discharge and charge to 100%
if check_ac_power recalibrate; then
check_root
setcharge_battery 96 100 $2
sleep 1
discharge_battery $2
exitcode=$?
if [ $exitcode -eq 0 ]; then
echo " The battery starts charging now. For a complete recalibration"
echo " keep AC connected until the battery is fully charged."
fi
else
exitcode=2
fi
;;
stat) # show status
shift
tlp-stat $*
exitcode=$?
;;
diskid) # show disk-by-id
{ for dev in $(ls /dev/disk/by-id/ | egrep '^ata' | egrep -v '\-part[1-9]+'); do
if [ -n "$dev" ]; then
get_disk_dev $dev
echo "$disk_dev: $disk_id"
fi
done } | sort
;;
*)
echo "Error: unknown command \"$mode\"." 1>&2
echo "Usage: tlp start|true|bat|false|ac|usb|bayoff|discharge|setcharge|fullcharge|recalibrate|stat|diskid" 1>&2
exitcode=3
;;
esac
exit $exitcode