115 lines · bash
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.03#4# Copyright (c) 2022, 2024 Collabora Ltd5#6# This test validates the power supply uAPI: namely, the files in sysfs and7# lines in uevent that expose the power supply properties.8#9# By default all power supplies available are tested. Optionally the name of a10# power supply can be passed as a parameter to test only that one instead.11DIR="$(dirname "$(readlink -f "$0")")"12 13. "${DIR}"/../kselftest/ktap_helpers.sh14 15. "${DIR}"/helpers.sh16 17count_tests() {18 SUPPLIES=$119 20 # This needs to be updated every time a new test is added.21 NUM_TESTS=3322 23 total_tests=024 25 for i in $SUPPLIES; do26 total_tests=$((total_tests + NUM_TESTS))27 done28 29 echo "$total_tests"30}31 32ktap_print_header33 34SYSFS_SUPPLIES=/sys/class/power_supply/35 36if [ $# -eq 0 ]; then37 supplies=$(ls "$SYSFS_SUPPLIES")38else39 supplies=$140fi41 42ktap_set_plan "$(count_tests "$supplies")"43 44for DEVNAME in $supplies; do45 ktap_print_msg Testing device "$DEVNAME"46 47 if [ ! -d "$SYSFS_SUPPLIES"/"$DEVNAME" ]; then48 ktap_test_fail "$DEVNAME".exists49 ktap_exit_fail_msg Device does not exist50 fi51 52 ktap_test_pass "$DEVNAME".exists53 54 test_uevent_prop NAME "$DEVNAME"55 56 test_sysfs_prop type57 SUPPLY_TYPE=$(cat "$SYSFS_SUPPLIES"/"$DEVNAME"/type)58 # This fails on kernels < 5.8 (needs 2ad3d74e3c69f)59 test_uevent_prop TYPE "$SUPPLY_TYPE"60 61 test_sysfs_prop_optional usb_type62 63 test_sysfs_prop_optional_range online 0 264 test_sysfs_prop_optional_range present 0 165 66 test_sysfs_prop_optional_list status "Unknown","Charging","Discharging","Not charging","Full"67 68 # Capacity is reported as percentage, thus any value less than 0 and69 # greater than 100 are not allowed.70 test_sysfs_prop_optional_range capacity 0 100 "%"71 72 test_sysfs_prop_optional_list capacity_level "Unknown","Critical","Low","Normal","High","Full"73 74 test_sysfs_prop_optional model_name75 test_sysfs_prop_optional manufacturer76 test_sysfs_prop_optional serial_number77 test_sysfs_prop_optional_list technology "Unknown","NiMH","Li-ion","Li-poly","LiFe","NiCd","LiMn"78 79 test_sysfs_prop_optional cycle_count80 81 test_sysfs_prop_optional_list scope "Unknown","System","Device"82 83 test_sysfs_prop_optional input_current_limit "uA"84 test_sysfs_prop_optional input_voltage_limit "uV"85 86 # Technically the power-supply class does not limit reported values.87 # E.g. one could expose an RTC backup-battery, which goes below 1.5V or88 # an electric vehicle battery with over 300V. But most devices do not89 # have a step-up capable regulator behind the battery and operate with90 # voltages considered safe to touch, so we limit the allowed range to91 # 1.8V-60V to catch drivers reporting incorrectly scaled values. E.g. a92 # common mistake is reporting data in mV instead of µV.93 test_sysfs_prop_optional_range voltage_now 1800000 60000000 "uV"94 test_sysfs_prop_optional_range voltage_min 1800000 60000000 "uV"95 test_sysfs_prop_optional_range voltage_max 1800000 60000000 "uV"96 test_sysfs_prop_optional_range voltage_min_design 1800000 60000000 "uV"97 test_sysfs_prop_optional_range voltage_max_design 1800000 60000000 "uV"98 99 # current based systems100 test_sysfs_prop_optional current_now "uA"101 test_sysfs_prop_optional current_max "uA"102 test_sysfs_prop_optional charge_now "uAh"103 test_sysfs_prop_optional charge_full "uAh"104 test_sysfs_prop_optional charge_full_design "uAh"105 106 # power based systems107 test_sysfs_prop_optional power_now "uW"108 test_sysfs_prop_optional energy_now "uWh"109 test_sysfs_prop_optional energy_full "uWh"110 test_sysfs_prop_optional energy_full_design "uWh"111 test_sysfs_prop_optional energy_full_design "uWh"112done113 114ktap_finished115