Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 88 additions & 25 deletions bat.d/15-lenovo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ batdrv_init () {
#
# 1. check for vendor specific kernel api (Linux 4.14 or higher required)
# --> retval $_natacpi:
# 0=thresholds/
# 0=charge_types support/
# 1=old thresholds
# 32=disabled/
# 128=no kernel support/
# 254=laptop not supported
Expand All @@ -45,6 +46,9 @@ batdrv_init () {
# config suffix (BAT0) --> retval $_bt_cfg_bat,
# sysfile --> retval $_bf_stop,
# default --> retval $_bt_def_stop;
#
# 5. define sysfile basenames for natacpi (6.17 onwards)
# charge type --> retval $_bn_chtype;

_batdrv_plugin="lenovo"
_batdrv_kmod="ideapad_laptop" # kernel module for natacpi
Expand Down Expand Up @@ -80,40 +84,56 @@ batdrv_init () {

# iterate batteries
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 readable_sysf "$bd/charge_types"; then
# use charge_types
_natacpi=0
_bm_thresh="natacpi"
_bn_chtype="charge_types"
elif [ -f "$BATDRV_LENOVO_CMODE" ] && readable_sysf "$BATDRV_LENOVO_CMODE"; then
# sysfile exists and is actually readable
_natacpi=1
_bm_thresh="natacpi"
_bf_stop="$BATDRV_LENOVO_CMODE"
elif [ "$X_BAT_PLUGIN_SIMULATE" = "$_batdrv_plugin" ]; then
# simulate api
_natacpi=0
_bm_thresh="natacpi"
_bf_stop="$BATDRV_LENOVO_CMODE"
else
# nothing detected
_natacpi=254
fi
fi
done

# check for vendor specific kernel api
if [ "$NATACPI_ENABLE" = "0" ]; then
# natacpi disabled in configuration --> skip actual detection
_natacpi=32
elif [ -f "$BATDRV_LENOVO_CMODE" ] && readable_sysf "$BATDRV_LENOVO_CMODE"; then
# sysfile exists and is actually readable
_natacpi=0
_bm_thresh="natacpi"
_bf_stop="$BATDRV_LENOVO_CMODE"
elif [ "$X_BAT_PLUGIN_SIMULATE" = "$_batdrv_plugin" ]; then
# simulate api
_natacpi=0
_bm_thresh="natacpi"
_bf_stop="$BATDRV_LENOVO_CMODE"
else
# nothing detected
_natacpi=254
fi



# shellcheck disable=SC2034
_batdrv_selected=$_batdrv_plugin
echo_debug "bat" "batdrv_init.${_batdrv_plugin}: batteries=$_batteries; natacpi=$_natacpi; thresh=$_bm_thresh; bf_stop=$_bf_stop"
echo_debug "bat" "batdrv_init.${_batdrv_plugin}: batteries=$_batteries; natacpi=$_natacpi; thresh=$_bm_thresh; bf_stop=$_bf_stop; bn_chtype=$_bn_chtype"
return 0
}

Expand Down Expand Up @@ -154,8 +174,11 @@ batdrv_select_battery () {

# determine natacpi sysfiles
_bd_read="$ACPIBATDIR/$_bat_str"
if [ "$_natacpi" = "0" ]; then
_bf_stop="$_bd_read/$_bn_chtype"
fi

echo_debug "bat" "batdrv.${_batdrv_plugin}.select_battery($1): bat_str=$_bat_str; cfg=$_bt_cfg_bat; bd_read=$_bd_read;"
echo_debug "bat" "batdrv.${_batdrv_plugin}.select_battery($1): bat_str=$_bat_str; cfg=$_bt_cfg_bat; bd_read=$_bd_read; bf_stop=$_bf_stop"
return 0
}

Expand All @@ -178,12 +201,24 @@ batdrv_read_threshold () {
fi

if [ "$_bm_thresh" = "natacpi" ]; then
out=$(read_sysf "$_bf_stop") || rc=4
if [ "$_natacpi" = "0" ]; then
if ! raw=$(read_sysf "$_bf_stop"); then
rc=4
else
chtype=$(echo "$raw" | sed -r 's/.*\[([A-Za-z_]++)\].*/\1/')
if [ "$chtype" = "Long_Life" ]; then
out=1
else
out=0
fi
fi
else
out=$(read_sysf "$_bf_stop") || rc=4
fi
else
# no threshold api
rc=255
fi

# "return" threshold
if [ "$X_THRESH_SIMULATE_READERR" != "1" ]; then
printf "%s" "$out"
Expand Down Expand Up @@ -212,6 +247,8 @@ batdrv_write_thresholds () {
local new_stop=${2:-}
local verb=${3:-0}
local old_stop
local new_stop_str


# insert defaults
[ "$new_stop" = "DEF" ] && new_stop=$_bt_def_stop
Expand Down Expand Up @@ -266,7 +303,17 @@ batdrv_write_thresholds () {
local rc=0
if [ "$old_stop" != "$new_stop" ]; then
# new threshold differs from effective one --> write it
write_sysf "$new_stop" "$_bf_stop" || rc=5
if [ "$_natacpi" = "0" ]; then
if [ "$new_stop" = "0" ]; then
new_stop_str="Standard"
else
new_stop_str="Long_Life"
fi
else
new_stop_str="$new_stop"
fi

write_sysf "$new_stop_str" "$_bf_stop" || rc=5
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).write: bat=$_bat_str; cfg=$_bt_cfg_bat; old=$old_stop; new=$new_stop; rc=$rc"
case $verb in
2)
Expand Down Expand Up @@ -401,7 +448,7 @@ batdrv_show_battery_data () {
*) cprintf "err" "* vendor (%s) = unknown status\n" "$_batdrv_kmod" ;;
esac

if [ "$_bm_thresh" = "natacpi" ]; then
if [ "$_bm_thresh" = "natacpi" ] && [ "$_natacpi" = "1" ]; then
local th sfx=
printf "Parameter value range:\n"
printf "* STOP_CHARGE_THRESH_BAT0: 0(off), 1(on) -- battery conservation mode\n\n"
Expand Down Expand Up @@ -431,6 +478,22 @@ batdrv_show_battery_data () {
print_bat_cycle_count "$_bd_read/cycle_count"
print_bat_energy
print_bat_state "$_bd_read/status"
if [ "$_bm_thresh" = "natacpi" ] && [ "$_natacpi" = "0" ]; then
local th sfx=
printf "\nParameter value range:\n"
printf "* STOP_CHARGE_THRESH_%s: 0(off), 1(on) -- battery long life mode\n\n" "$bat"
if th=$(batdrv_read_threshold stop 1); then
case $th in
0) sfx=" (off)" ;;
1) sfx=" (on)" ;;
*) sfx=" (invalid)" ;;
esac
printf "%-59s = %d%s\n" "$_bf_stop" "$th" "$sfx"
else
printf "%-59s = %s\n" "$_bf_stop" "(not available)"
fi
fi

printf "\n"
if [ "$verbose" -eq 1 ]; then
print_bat_voltages
Expand Down