-
Notifications
You must be signed in to change notification settings - Fork 126
/
05-thinkpad
1355 lines (1184 loc) · 49.4 KB
/
05-thinkpad
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# 05-thinkpad - Battery Plugin for ThinkPads using natacpi and/or
# tpacpi-bat/acpi-call for thresholds and forced discharge, i.e. X220/T420 and newer.
#
# Copyright (c) 2021 Thomas Koch <linrunner at gmx.net> and others.
# This software is licensed under the GPL v2 or later.
# Needs: tlp-func-base, 35-tlp-func-batt, tlp-func-stat
# shellcheck disable=SC2086
# --- Hardware Detection
readonly SMAPIBATDIR=/sys/devices/platform/smapi
readonly RE_TPSMAPI_ONLY='^(Edge( 13.*)?|G41|R[56][012][eip]?|R[45]00|SL[45]10|T23|T[346][0123][p]?|T[45][01]0[s]?|W[57]0[01]|X[346][012][s]?( Tablet)?|X1[02]0e|X[23]0[01][s]?( Tablet)?|Z6[01][mpt])$'
readonly RE_TPSMAPI_AND_TPACPI='^(X1|X220[s]?( Tablet)?|T[45]20[s]?|W520)$'
readonly RE_TP_NONE='^(L[45]20|L512|SL[345]00|X121e)$'
readonly MODINFO=modinfo
readonly MOD_TPSMAPI="tp_smapi"
readonly MOD_TPACPI="acpi_call"
supports_tpsmapi_only () {
# rc: 0=ThinkPad supports tpsmapi only/1=false
# prerequisite: check_thinkpad()
printf '%s' "$_tpmodel" | grep -E -q "${RE_TPSMAPI_ONLY}"
}
supports_tpsmapi_and_tpacpi () {
# rc: 0=ThinkPad supports tpsmapi, tpacpi-bat, natacpi/1=false
# prerequisite: check_thinkpad()
printf '%s' "$_tpmodel" | grep -E -q "${RE_TPSMAPI_AND_TPACPI}"
}
supports_no_tp_bat_funcs () {
# rc: 0=ThinkPad doesn't support battery features/1=false
# prerequisite: check_thinkpad()
printf '%s' "$_tpmodel" | grep -E -q "${RE_TP_NONE}"
}
supports_tpacpi () {
# rc: 0=ThinkPad does support tpacpi-bat, natacpi/1=false
# prerequisite: check_thinkpad()
# assumption: all newer models support tpapaci-bat/natacapi except
# explicitly unsupported or older tpsmapi only models
! supports_no_tp_bat_funcs && ! supports_tpsmapi_only
}
check_thinkpad () {
# check for ThinkPad hardware and save model string,
# load ThinkPad specific kernel modules
# rc: 0=ThinkPad, 1=other hardware
# retval: $_tpmodel
local pv
_tpmodel=""
if [ -d $TPACPID ]; then
# kernel module thinkpad_acpi is loaded
if [ -z "$X_SIMULATE_MODEL" ]; then
# get DMI product string and sanitize it
pv="$(read_dmi product_version | tr -C -d 'a-zA-Z0-9 ')"
else
# simulate arbitrary model
pv="$X_SIMULATE_MODEL"
fi
# check DMI product string for occurrence of "ThinkPad"
if printf '%s' "$pv" | grep -E -q 'Think[Pp]ad'; then
# it's a real ThinkPad --> save model substring
_tpmodel=$(printf '%s\n' "$pv" | sed -r 's/^Think[Pp]ad //')
fi
else
# not a ThinkPad: get DMI product string
pv="$(read_dmi product_version)"
fi
if [ -n "$_tpmodel" ]; then
# load tp-smapi for supported models only; prevents kernel messages
if supports_tpsmapi_only || supports_tpsmapi_and_tpacpi; then
load_modules $MOD_TPSMAPI
fi
# load acpi-call for supported models only; prevents kernel messages
if supports_tpacpi; then
load_modules $MOD_TPACPI
fi
echo_debug "bat" "check_thinkpad: tpmodel=$_tpmodel"
return 0
fi
# not a ThinkPad
echo_debug "bat" "check_thinkpad.not_a_thinkpad: model=$pv"
return 1
}
# --- Plugin API functions
batdrv_init () {
# detect hardware and initialize driver
# rc: 0=matching hardware detected/1=not detected/2=no batteries detected
# retval: $_batdrv_plugin, $_batdrv_kmod
#
# 1. check for native kernel acpi (Linux 4.19 or higher required)
# --> retval $_natacpi:
# 0=thresholds and discharge/
# 1=thresholds only/
# 32=disabled/
# 128=no kernel support/
# 254=ThinkPad not supported
#
# 2. check for acpi-call external kernel module and test with integrated
# tpacpi-bat [ThinkPads only]
# --> retval $_tpacpi:
# 0=thresholds and discharge/
# 1=thresholds only/
# 32=disabled/
# 64=acpi_call module not loaded/
# 127=tpacpi-bat not installed/
# 128=acpi_call module not installed/
# 137=kernel error (oops)/
# 253=tpacpi-bat error/
# 254=ThinkPad not supported/
# 255=superseded by natacpi/
# 256=superseded and kernel module not loaded
#
# 3. check for tp-smapi external kernel module
# --> retval $_tpsmapi:
# 1=readonly/
# 32=disabled/
# 64=tp_smapi module not loaded/
# 128=tp_smapi module not installed
#
# 4. determine best method for
# reading battery data --> retval $_bm_read,
# reading/writing charging thresholds --> retval $_bm_thresh,
# reading/writing force discharge --> retval $_bm_dischg:
# none/natacpi/tpacpi/tpsmapi
#
# 5. determine sysfile basenames for natacpi
# start threshold --> retval $_bn_start,
# stop threshold --> retval $_bn_stop,
# force discharge --> retval $_bn_dischg;
#
# 6. determine present batteries
# list of batteries (space separated) --> retval $_batteries;
#
# 7. define charge threshold defaults
# start threshold --> retval $_bt_def_start,
# stop threshold --> retval $_bt_def_stop;
_batdrv_plugin="thinkpad"
_batdrv_kmod="thinkpad_acpi" # kernel module for natacpi
if [ -n "$X_BAT_PLUGIN_SIMULATE" ]; then
if [ "$X_BAT_PLUGIN_SIMULATE" = "$_batdrv_plugin" ]; then
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.simulate"
else
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.simulate_skip"
return 1
fi
elif wordinlist "$_batdrv_plugin" "$X_BAT_PLUGIN_DENYLIST"; then
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.denylist"
return 1
else
# check if hardware matches and load necessary kernel modules
if ! check_thinkpad; then
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.not_a_thinkpad"
return 1
elif ! supports_tpacpi; then
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.unsupported_model"
return 1
fi
fi
# presume no features at all
_natacpi=128
_tpacpi=255
_tpsmapi=254
_bm_read="natacpi"
_bm_thresh="none"
_bm_dischg="none"
_bn_start=""
_bn_stop=""
_bn_dischg=""
_batteries=""
# shellcheck disable=SC2034
_bt_def_start=96
# shellcheck disable=SC2034
_bt_def_stop=100
# --- 1. iterate batteries and check for native kernel ACPI
local bd bs
local done=0
for bd in "$ACPIBATDIR"/BAT[01]; do
if [ "$(read_sysf $bd/present)" = "1" ]; then
# record detected batteries and directories
bs=${bd##/*/}
if [ -n "$_batteries" ]; then
_batteries="$_batteries $bs"
else
_batteries="$bs"
fi
# skip natacpi detection for 2nd and subsequent batteries
[ $done -eq 1 ] && continue
done=1
if [ "$NATACPI_ENABLE" = "0" ]; then
# natacpi disabled in configuration --> skip actual detection
_natacpi=32
continue
fi
if [ -f $bd/charge_control_start_threshold ] \
&& [ -f $bd/charge_control_end_threshold ]; then
# sysfiles for thresholds exist (kernel 5.9 and newer)
_bn_start="charge_control_start_threshold"
_bn_stop="charge_control_end_threshold"
_natacpi=254
elif [ -f $bd/charge_start_threshold ] \
&& [ -f $bd/charge_stop_threshold ]; then
# sysfiles for thresholds exist (kernel 4.17 and newer)
_bn_start="charge_start_threshold"
_bn_stop="charge_stop_threshold"
_natacpi=254
else
# nothing detected
_natacpi=254
continue
fi
if readable_sysf $bd/$_bn_start \
&& readable_sysf $bd/$_bn_stop; then
# start/stop thresholds are actually readable
_natacpi=1
_bm_thresh="natacpi"
if readable_sysf $bd/charge_behaviour; then
# sysfile for force-discharge exists and is actually readable
_natacpi=0
_bm_dischg="natacpi"
_bn_dischg="charge_behaviour"
fi
fi
fi
done
# quit if no battery detected, there is no point in activating the plugin
if [ -z "$_batteries" ]; then
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.no_batteries"
return 2
fi
# --- 2. check for acpi-call external kernel module and test with integrated tpacpi-bat
if [ $_natacpi -gt 0 ]; then
# less than full natacpi --> tpacpi required
if [ ! -e /proc/acpi/call ]; then
# call API not present
if $MODINFO $MOD_TPACPI > /dev/null 2>&1; then
# module installed but not loaded
_tpacpi=64
else
# module neither installed nor builtin
_tpacpi=128
fi
else
# call API present --> try tpacpi-bat
if $TPACPIBAT -g ST 1 > /dev/null 2>&1; then
# thresholds capable
_tpacpi=1
if $TPACPIBAT -g FD 1 > /dev/null 2>&1; then
# force_discharge capable
_tpacpi=0
fi
else
# tpacpi-bat failed
case $? in
255) # acpi call non-existent (AE_NOT_FOUND)
_tpacpi=254
;;
137) # kernel error (oops)
_tpacpi=137
;;
* ) # tpacpi-bat error
_tpacpi=253
;;
esac
fi
if [ $_tpacpi -le 1 ]; then
# tpacpi capable
if [ "$TPACPI_ENABLE" = "0" ]; then
# disabled by configuration
_tpacpi=32
else
# not disabled
case $_natacpi in
1) # use for discharge only
[ $_tpacpi -eq 0 ] && _bm_dischg="tpacpi"
;;
*) # use for thresholds and discharge
_bm_thresh="tpacpi"
[ $_tpacpi -eq 0 ] && _bm_dischg="tpacpi"
;;
esac
fi
fi
fi
elif [ ! -e /proc/acpi/call ]; then
# superseded and kernel module not loaded
_tpacpi=256
fi
# --- 3. check for tp-smapi external kernel module (relevant models only)
if supports_tpsmapi_and_tpacpi; then
if [ -d $SMAPIBATDIR ]; then
# module loaded --> tp-smapi available
if [ "$TPSMAPI_ENABLE" = "0" ]; then
# tpsmapi disabled by configuration
_tpsmapi=32
elif supports_tpsmapi_and_tpacpi; then
# reading battery data via tpsmapi is preferred over natacpi
# because it provides cycle count and more
_tpsmapi=1
_bm_read="tpsmapi"
fi
elif $MODINFO $MOD_TPSMAPI > /dev/null 2>&1; then
# module installed but not loaded
_tpsmapi=64
else
# module neither installed nor builtin
_tpsmapi=128
fi
fi
# shellcheck disable=SC2034
_batdrv_selected=$_batdrv_plugin
echo_debug "bat" "batdrv_init.${_batdrv_plugin}: batteries=$_batteries; natacpi=$_natacpi; tpacpi=$_tpacpi; tpsmapi=$_tpsmapi"
echo_debug "bat" "batdrv_init.${_batdrv_plugin}: read=$_bm_read; thresh=$_bm_thresh; bn_start=$_bn_start; bn_stop=$_bn_stop; dischg=$_bm_dischg; bn_dischg=$_bn_dischg"
return 0
}
batdrv_select_battery () {
# determine battery sysfiles and tpacpi-bat index
# $1: BAT0/BAT1/DEF
# global param: $_bm_read
# rc: 0=bat exists/1=bat non-existent
# retval: $_bat_str: BAT0/BAT1;
# $_bat_idx: 1/2;
# $_bd_read: directory with battery data sysfiles;
# $_bf_start: sysfile for start threshold;
# $_bf_stop: sysfile for stop threshold;
# $_bf_dischg: sysfile for force discharge
# prerequisite: batdrv_init()
# defaults
_bat_idx=0 # no index
_bat_str="" # no bat
_bd_read="" # no directories
_bf_start=""
_bf_stop=""
_bf_dischg=""
# validate battery param
local bs
case $1 in
DEF) # 1st battery is default
bs="${_batteries%% *}"
;;
*)
if wordinlist "$1" "$_batteries"; then
bs=$1
else
# battery not present --> quit
echo_debug "bat" "batdrv.${_batdrv_plugin}.select_battery($1).not_present"
return 1
fi
;;
esac
# determine bat index for tpacpi and main/aux distinction
case $bs in
BAT0)
_bat_str="$bs"
# BAT0 is always assumed main battery
_bat_idx=1
;;
BAT1)
_bat_str="$bs"
if [ $_tpacpi -le 1 ]; then
# tpacpi: try to read start threshold for index 2
if $TPACPIBAT -g ST 2 2> /dev/null 1>&2 ; then
_bat_idx=2 # BAT1 is aux
else
_bat_idx=1 # BAT1 is main
fi
else
# without tpacpi: BAT1 is aux
_bat_idx=2
fi
;;
esac
# determine natacpi sysfiles
if [ "$_bm_thresh" = "natacpi" ]; then
_bf_start="$ACPIBATDIR/$bs/$_bn_start"
_bf_stop="$ACPIBATDIR/$bs/$_bn_stop"
fi
if [ "$_bm_dischg" = "natacpi" ]; then
_bf_dischg="$ACPIBATDIR/$bs/$_bn_dischg"
fi
case "$_bm_read" in
natacpi) _bd_read="$ACPIBATDIR/$bs" ;;
tpsmapi) _bd_read="$SMAPIBATDIR/$bs" ;;
esac
echo_debug "bat" "batdrv.${_batdrv_plugin}.select_battery($1): bat_str=$_bat_str; bat_idx=$_bat_idx; bd_read=$_bd_read; bf_start=$_bf_start; bf_stop=$_bf_stop; bf_dischg=$_bf_dischg"
return 0
}
batdrv_read_threshold () {
# read and print charge threshold
# $1: start/stop
# $2: error string
# global param: $_bm_thresh, $_bf_start, $_bf_stop, $_bat_idx
# out: threshold 0..100/"$2" on error
# rc: 0=ok/4=read error/255=no api
# prerequisite: batdrv_init(), batdrv_select_battery()
local bf out="" rc=0
case $1 in
start) out="$X_THRESH_SIMULATE_START" ;;
stop) out="$X_THRESH_SIMULATE_STOP" ;;
esac
if [ -n "$out" ]; then
printf "%s" "$out"
echo_debug "bat" "batdrv.${_batdrv_plugin}.read_threshold($1).simulate: bm_thresh=$_bm_thresh; bf=$bf; bat_idx=$_bat_idx; out=$out; rc=$rc"
return 0
fi
case $_bm_thresh in
natacpi)
# read threshold from sysfile, "$2" if not readable/non-existent
case $1 in
start) bf=$_bf_start ;;
stop) bf=$_bf_stop ;;
esac
out=$(read_sysf $bf "$2") || rc=4
# workaround: read threshold sysfile a second time to mitigate
# the annoying firmware issue on ThinkPad A/E/L/S/X series
# (refer to issue #369 and FAQ)
out=$(read_sysf $bf "$2") || rc=4
;;
tpacpi)
case $1 in
start)
out=$($TPACPIBAT -g ST $_bat_idx 2> /dev/null | cut -f1 -d' ')
# workaround: read threshold a second time (see above)
out=$($TPACPIBAT -g ST $_bat_idx 2> /dev/null | cut -f1 -d' ')
;;
stop)
out=$($TPACPIBAT -g SP $_bat_idx 2> /dev/null | cut -f1 -d' ')
# workaround: read threshold a second time (see above)
out=$($TPACPIBAT -g SP $_bat_idx 2> /dev/null | cut -f1 -d' ')
;;
esac
# shellcheck disable=SC2181
if [ $? -eq 0 ] && is_uint "$out"; then
if [ $out -ge 128 ]; then
# remove offset of 128 for Edge S430 et al.
out=$((out - 128))
fi
if [ "$1" = "stop" ] && [ $out -eq 0 ]; then
# stop: 0 (hardware default) means 100
out=100
fi
else
out="$2"
rc=4
fi
;;
*) # no threshold api
rc=255
;;
esac
# "return" threshold
if [ "$X_THRESH_SIMULATE_READERR" != "1" ]; then
printf "%s" "$out"
else
rc=4
fi
echo_debug "bat" "batdrv.${_batdrv_plugin}.read_threshold($1): bm_thresh=$_bm_thresh; bf=$bf; bat_idx=$_bat_idx; out=$out; rc=$rc"
return $rc
}
batdrv_write_thresholds () {
# write both charge thresholds for a battery
# use pre-determined method and sysfiles from global parms
# $1: new start threshold 0(disabled)..99/DEF(default)
# $2: new stop threshold 1..100/DEF(default)
# $3: 0=quiet/1=output parameter errors/2=output progress and errors
# $4: battery - non-empty string indicates thresholds stem from configuration
# global param: $_bm_thresh, $_bat_str, $_bat_idx, $_bf_start, $_bf_stop
# rc: 0=ok/
# 1=not configured/
# 2=threshold(s) out of range or non-numeric/
# 3=minimum start stop diff violated/
# 4=threshold read error/
# 5=threshold write error
# prerequisite: batdrv_init(), batdrv_select_battery()
local new_start=${1:-}
local new_stop=${2:-}
local verb=${3:-0}
local cfg_bat="$4"
local old_start old_stop
# insert defaults
[ "$new_start" = "DEF" ] && new_start=$_bt_def_start
[ "$new_stop" = "DEF" ] && new_stop=$_bt_def_stop
# --- validate thresholds
local rc
if [ -n "$cfg_bat" ] && [ -z "$new_start" ] && [ -z "$new_stop" ]; then
# do nothing if unconfigured
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).not_configured: bat=$_bat_str"
return 1
fi
# start: check for 3 digits max, ensure min 0 / max 99
if ! is_uint "$new_start" 3 || \
! is_within_bounds $new_start 0 99; then
# threshold out of range
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).invalid_start: bat=$_bat_str"
case $verb in
1)
if [ -n "$cfg_bat" ]; then
echo_message "Error in configuration at START_CHARGE_THRESH_${cfg_bat}=\"${new_start}\": not specified, invalid or out of range (0..99). Battery skipped."
fi
;;
2)
if [ -n "$cfg_bat" ]; then
printf "Error in configuration at START_CHARGE_THRESH_%s=\"%s\": not specified, invalid or out of range (0..99). Aborted.\n" "$cfg_bat" "$new_start" 1>&2
else
printf "Error: charge start threshold (%s) for %s is not specified, invalid or out of range (0..99). Aborted.\n" "$new_start" "$_bat_str" 1>&2
fi
;;
esac
return 2
fi
# stop: check for 3 digits max, ensure min 1 / max 100
if ! is_uint "$new_stop" 3 || \
! is_within_bounds $new_stop 1 100; then
# threshold out of range
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).invalid_stop: bat=$_bat_str"
case $verb in
1)
if [ -n "$cfg_bat" ]; then
echo_message "Error in configuration at STOP_CHARGE_THRESH_${cfg_bat}=\"${new_stop}\": not specified, invalid or out of range (1..100). Battery skipped."
fi
;;
2)
if [ -n "$cfg_bat" ]; then
printf "Error in configuration at STOP_CHARGE_THRESH_%s=\"%s\": not specified, invalid or out of range (1..100). Aborted.\n" "$cfg_bat" "$new_stop" 1>&2
else
printf "Error: charge stop threshold (%s) for %s is not specified, invalid or out of range (1..100). Aborted.\n" "$new_stop" "$_bat_str" 1>&2
fi
;;
esac
return 2
fi
# check if start < stop
if [ $new_start -ge $new_stop ]; then
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).invalid_diff: bat=$_bat_str"
case $verb in
1)
if [ -n "$cfg_bat" ]; then
echo_message "Error in configuration: START_CHARGE_THRESH_${cfg_bat} >= STOP_CHARGE_THRESH_${cfg_bat}. Battery skipped."
fi
;;
2)
if [ -n "$cfg_bat" ]; then
printf "Error in configuration: START_CHARGE_THRESH_%s >= STOP_CHARGE_THRESH_%s. Aborted.\n" "$cfg_bat" "$cfg_bat" 1>&2
else
printf "Error: start threshold >= stop threshold for %s. Aborted.\n" "$_bat_str" 1>&2
fi
;;
esac
return 3
fi
# read active threshold values
if ! old_start=$(batdrv_read_threshold start) || \
! old_stop=$(batdrv_read_threshold stop); then
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).read_error: bat=$_bat_str"
case $verb in
1) echo_message "Warning: could not read current charge threshold(s) for $_bat_str. Battery skipped." ;;
2) printf "Error: could not read current charge threshold(s) for %s. Aborted.\n" "$_bat_str" 1>&2 ;;
esac
return 4
fi
if [ $old_start -ge $old_stop ]; then
# invalid threshold reading, happens on ThinkPad E/L series
old_start="none"
old_stop="none"
fi
# determine write sequence because driver's intrinsic boundary conditions
# must be met in all write stages:
# - natacpi: start < stop (write fails if not met)
# - tpacpi: nothing (maybe BIOS/ECP enforces something)
local rc=0 steprc tseq
if [ "$old_stop" != "none" ] && [ $new_start -gt $old_stop ]; then
tseq="stop start"
else
tseq="start stop"
fi
# write new thresholds in determined sequence
if [ "$verb" = "2" ]; then
printf "Setting temporary charge thresholds for %s:\n" "$_bat_str"
fi
for step in $tseq; do
local old_thresh new_thresh steprc
case $step in
start)
old_thresh=$old_start
new_thresh=$new_start
;;
stop)
old_thresh=$old_stop
new_thresh=$new_stop
;;
esac
if [ "$old_thresh" != "$new_thresh" ]; then
# new threshold differs from effective one --> write it
case $_bm_thresh in
natacpi)
case $step in
start) write_sysf "$new_thresh" $_bf_start ;;
stop) write_sysf "$new_thresh" $_bf_stop ;;
esac
steprc=$?; [ $steprc -ne 0 ] && [ $rc -eq 0 ] && rc=5
;;
tpacpi)
case $step in
start) $TPACPIBAT -s ST $_bat_idx $new_thresh > /dev/null 2>&1 ;;
stop)
if [ $new_thresh -eq 100 ]; then
# tpacpi-bat won't accept 100
$TPACPIBAT -s SP $_bat_idx 0 > /dev/null 2>&1
else
$TPACPIBAT -s SP $_bat_idx $new_thresh > /dev/null 2>&1
fi
;;
esac
steprc=$?; [ $steprc -ne 0 ] && rc=5
;;
*) # no threshold api
steprc=255
rc=5
;;
esac
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).$step.write: bat=$_bat_str; old=$old_thresh; new=$new_thresh; steprc=$steprc"
case $verb in
2)
if [ $steprc -eq 0 ]; then
if [ "$step" = "start" ] && [ $new_thresh -eq 0 ]; then
printf " %-5s = %3d (disabled)\n" $step $new_thresh
else
printf " %-5s = %3d\n" $step $new_thresh
fi
else
printf " %-5s = %3d (Error: write failed)\n" $step $new_thresh 1>&2
fi
;;
1)
if [ $steprc -gt 0 ]; then
echo_message "Error: writing charge $step threshold for $_bat_str failed."
fi
;;
esac
else
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).$step.no_change: bat=$_bat_str; old=$old_thresh; new=$new_thresh"
if [ "$verb" = "2" ]; then
printf " %-5s = %3d (no change)\n" $step $new_thresh
fi
fi
done # for step
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).complete: bat=$_bat_str; rc=$rc"
return $rc
}
batdrv_chargeonce () {
# charge battery to stop threshold once
# use pre-determined method and sysfiles from global parms
# global param: $_bm_thresh, $_bat_str, $_bat_idx, $_bf_start, $_bf_stop
# rc: 0=ok/
# 2=charge level read error
# 3=charge level too high/
# 4=threshold read error/
# 5=threshold write error/
# prerequisite: batdrv_init(), batdrv_select_battery()
local ccharge cur_stop efull enow temp_start
local rc=0
if ! cur_stop=$(batdrv_read_threshold stop); then
printf "Error: reading charge stop threshold for %s failed. Aborted.\n" $_bat_str 1>&2
echo_debug "bat" "batdrv.${_batdrv_plugin}.chargeonce($_bat_str).thresh_unknown: stop=$cur_stop; rc=4"
return 4
fi
# get current charge level (in %)
case $_bm_read in
natacpi) # use ACPI sysfiles
if [ -f $_bd_read/energy_full ]; then
efull=$(read_sysval $_bd_read/energy_full)
enow=$(read_sysval $_bd_read/energy_now)
fi
if is_uint "$enow" && is_uint "$efull" && [ $efull -ne 0 ]; then
# calculate charge level rounded to integer
ccharge=$(perl -e 'printf("%.0f\n", 100.0 * '$enow' / '$efull')')
else
ccharge=-1
fi
;;
tpsmapi) # use tp-smapi sysfile
ccharge=$(read_sysval $_bd_read/remaining_percent) || ccharge=-1
;;
esac
if [ $ccharge -eq -1 ]; then
printf "Error: cannot determine charge level for %s.\n" "$_bat_str" 1>&2
echo_debug "bat" "batdrv.${_batdrv_plugin}.chargeonce($_bat_str).charge_level_unknown: enow=$enow; efull=$efull; rc=2"
return 2
fi
temp_start=$(( cur_stop - 1 ))
if [ $ccharge -gt $temp_start ]; then
printf "Error: the %s charge level is %s%%. " "$_bat_str" "$ccharge" 1>&2
printf "For this command to work, it must not exceed %s%% (stop threshold - 1).\n" "$temp_start" 1>&2
echo_debug "bat" "batdrv.${_batdrv_plugin}.chargeonce($_bat_str).charge_level_too_high: soc=$ccharge; stop=$cur_stop; rc=3"
return 3
fi
printf "Setting temporary charge threshold for %s:\n" "$_bat_str"
case $_bm_thresh in
natacpi)
write_sysf "$temp_start" $_bf_start || rc=5
;;
tpacpi)
$TPACPIBAT -s ST $_bat_idx $temp_start > /dev/null 2>&1 || rc=5
;;
esac
if [ $rc -eq 0 ]; then
printf " start = %3d\n" $temp_start
else
printf " start = %3d (Error: write failed)\n" $temp_start 1>&2
fi
echo_debug "bat" "batdrv.${_batdrv_plugin}.chargeonce($_bat_str): soc=$ccharge; start=$temp_start; stop=$cur_stop; rc=$rc"
return $rc
}
batdrv_apply_configured_thresholds () {
# apply configured thresholds from configuration to all batteries
# output parameter errors only
if batdrv_select_battery BAT0; then
batdrv_write_thresholds "$START_CHARGE_THRESH_BAT0" "$STOP_CHARGE_THRESH_BAT0" 1 "BAT0"; rc=$?
fi
if batdrv_select_battery BAT1; then
# write configured thresholds, output parameter errors
batdrv_write_thresholds "$START_CHARGE_THRESH_BAT1" "$STOP_CHARGE_THRESH_BAT1" 1 "BAT1"; rc=$?
fi
return 0
}
batdrv_read_force_discharge () {
# read and print force-discharge state
# $1: error string
# global param: $_bm_dischg, $_bf_dischg, $_bat_idx
# out: 0=off/1=on/"$1" on error
# rc: 0=ok/4=read error/255=no api
# retval: $_bt_charge_behaviour
# prerequisite: batdrv_init(), batdrv_select_battery()
local rc=0 out
case $_bm_dischg in
natacpi)
# read state from sysfile
if _bt_charge_behaviour=$(read_sysf $_bf_dischg); then
if echo "$_bt_charge_behaviour" | grep -q "\[force-discharge\]"; then
out=1
else
out=0
fi
else
# not readable/non-existent
out="$1"
rc=4
fi
;;
tpacpi) # read via tpacpi-bat
case $($TPACPIBAT -g FD $_bat_idx 2> /dev/null) in
yes) out=1 ;;
no) out=0 ;;
*)
out="$1"
rc=4
;;
esac
;;
*) # no discharge api
out="$1"
rc=255
;;
esac
printf "%s" "$out"
echo_debug "bat" "batdrv.${_batdrv_plugin}.read_force_discharge($_bat_str): bm_dischg=$_bm_dischg; bf_dischg=$_bf_dischg; bat_idx=$_bat_idx; out=$out; cb=$_bt_charge_behaviour; rc=$rc"
return $rc
}
batdrv_write_force_discharge () {
# write force discharge state
# $1: 0=off/1=on
# global param: $_bat_str, $_bat_idx, $_bm_dischg, $_bf_dischg
# rc: 0=done/5=write error/255=no api
# prerequisite: batdrv_init(), batdrv_select_battery()
local rc=0
case $_bm_dischg in
natacpi)
# write force_discharge
case "$1" in
0) write_sysf "auto" $_bf_dischg || rc=5 ;;
1) write_sysf "force-discharge" $_bf_dischg || rc=5 ;;
esac
;; # natacpi
tpacpi) # use tpacpi-bat
$TPACPIBAT -s FD $_bat_idx $1 > /dev/null 2>&1 || rc=5
;; # tpcpaci
*) # no discharge api
rc=255
;;
esac
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_force_discharge($_bat_str, $1): bm_dischg=$_bm_dischg; bf_dischg=$_bf_dischg; bat_idx=$_bat_idx; rc=$rc"
return $rc
}
batdrv_cancel_force_discharge () {
# trap: called from batdrv_discharge
# global param: $_bat_str
# prerequisite: batdrv_discharge()
batdrv_write_force_discharge 0
unlock_tlp tlp_discharge
echo_debug "bat" "batdrv.${_batdrv_plugin}.discharge.cancelled($_bat_str)"
printf " Cancelled.\n"
do_exit 0
}
batdrv_force_discharge_active () { # check if battery is in 'force_discharge' state
# global param: $_bat_str, $_bm_read, $_bd_read
# rc: 0=discharging/1=not discharging/255=no api
# prerequisite: batdrv_init(), batdrv_select_battery()
local bsys rc=0
if [ "$(batdrv_read_force_discharge "")" = "0" ]; then
# force_discharge is off --> quit
echo_debug "bat" "batdrv.${_batdrv_plugin}.force_discharge_active($_bat_str): rc=1"
return 1
fi
if ! get_sys_power_supply; then
# AC unplugged --> cancel discharge
rc=1
else
# force_discharge is still on, but quirky firmware (e.g. ThinkPad E-series)
# may keep force_discharge on --> check battery status sysfile too
case $_bm_read in
natacpi)
bsys=$_bd_read/status # use ACPI sysfile
;;
tpsmapi)
bsys=$_bd_read/state # use tpsmapi sysfile
;;
*) # no read api
bsys=""
rc=255
;;
esac
# read battery state
if [ -f "$bsys" ]; then
case "$(read_sysf $bsys)" in
[Dd]ischarging) rc=0 ;;
*) rc=1 ;;
esac
fi
fi
echo_debug "bat" "batdrv.${_batdrv_plugin}.force_discharge_active($_bat_str): bm_read=$_bm_read; bf=$bsys rc=$rc"
return $rc
}
batdrv_discharge () {
# discharge battery
# global param: $_bm_dischg, $_bat_idx, $_bf_dischg
# rc: 0=done/1=malfunction/2=not emptied/3=ac removed/255=no api
# prerequisite: batdrv_init(), batdrv_select_battery()
local en ef pn rc rp wt
# start discharge
if ! batdrv_write_force_discharge 1; then
echo_debug "bat" "batdrv.${_batdrv_plugin}.discharge.force_discharge_malfunction($_bat_str)"
echo "Error: discharge $_bat_str malfunction -- check your hardware (battery, charger)." 1>&2
return 1
fi
trap batdrv_cancel_force_discharge INT # enable ^C hook
rc=0; rp=0
# wait for start == while status not "discharging" -- 15.0 sec timeout
printf "Initiating discharge of battery %s " $_bat_str
wt=15
while ! batdrv_force_discharge_active && [ $wt -gt 0 ] ; do
sleep 1
printf "."
wt=$((wt - 1))
done
printf "\n"
if batdrv_force_discharge_active; then
# discharge initiated sucessfully --> wait for completion == while status "discharging"
echo_debug "bat" "batdrv.${_batdrv_plugin}.discharge.running($_bat_str)"
while batdrv_force_discharge_active; do
clear
printf "Currently discharging battery %s:\n" "$_bat_str"
# show current battery state
case $_bm_read in
natacpi|tpacpi) # use ACPI sysfiles
perl -e 'printf ("voltage = %6d [mV]\n", '"$(read_sysval $_bd_read/voltage_now)"' / 1000.0);'
en=$(read_sysval $_bd_read/energy_now)
perl -e 'printf ("remaining capacity = %6d [mWh]\n", '$en' / 1000.0);'