Skip to content

Commit 4ab2bb3

Browse files
FFY00bentiss
authored andcommitted
HID: logitech-hidpp: BatteryVoltage: only read chargeStatus if extPower is active
In the HID++ 2.0 function getBatteryInfo() from the BatteryVoltage (0x1001) feature, chargeStatus is only valid if extPower is active. Previously we were ignoring extPower, which resulted in wrong values. Example: With an unplugged mouse $ cat /sys/class/power_supply/hidpp_battery_0/status Charging This patch fixes that, it also renames charge_sts to flags as charge_sts can be confused with chargeStatus from the spec. Spec: +--------+-------------------------------------------------------------------------+ | byte | 2 | +--------+--------------+------------+------------+----------+----------+----------+ | bit | 0..2 | 3 | 4 | 5 | 6 | 7 | +--------+--------------+------------+------------+----------+----------+----------+ | buffer | chargeStatus | fastCharge | slowCharge | critical | (unused) | extPower | +--------+--------------+------------+------------+----------+----------+----------+ Table 1 - battery voltage (0x1001), getBatteryInfo() (ASE 0), 3rd byte +-------+--------------------------------------+ | value | meaning | +-------+--------------------------------------+ | 0 | Charging | +-------+--------------------------------------+ | 1 | End of charge (100% charged) | +-------+--------------------------------------+ | 2 | Charge stopped (any "normal" reason) | +-------+--------------------------------------+ | 7 | Hardware error | +-------+--------------------------------------+ Table 2 - chargeStatus value Signed-off-by: Filipe Laíns <[email protected]> Tested-by: Pedro Vanzella <[email protected]> Reviewed-by: Pedro Vanzella <[email protected]> Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent 12fb2b9 commit 4ab2bb3

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

drivers/hid/hid-logitech-hidpp.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,36 +1256,35 @@ static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
12561256
{
12571257
int status;
12581258

1259-
long charge_sts = (long)data[2];
1259+
long flags = (long) data[2];
12601260

1261-
*level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1262-
switch (data[2] & 0xe0) {
1263-
case 0x00:
1264-
status = POWER_SUPPLY_STATUS_CHARGING;
1265-
break;
1266-
case 0x20:
1267-
status = POWER_SUPPLY_STATUS_FULL;
1268-
*level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1269-
break;
1270-
case 0x40:
1261+
if (flags & 0x80)
1262+
switch (flags & 0x07) {
1263+
case 0:
1264+
status = POWER_SUPPLY_STATUS_CHARGING;
1265+
break;
1266+
case 1:
1267+
status = POWER_SUPPLY_STATUS_FULL;
1268+
*level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1269+
break;
1270+
case 2:
1271+
status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1272+
break;
1273+
default:
1274+
status = POWER_SUPPLY_STATUS_UNKNOWN;
1275+
break;
1276+
}
1277+
else
12711278
status = POWER_SUPPLY_STATUS_DISCHARGING;
1272-
break;
1273-
case 0xe0:
1274-
status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1275-
break;
1276-
default:
1277-
status = POWER_SUPPLY_STATUS_UNKNOWN;
1278-
}
12791279

12801280
*charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1281-
if (test_bit(3, &charge_sts)) {
1281+
if (test_bit(3, &flags)) {
12821282
*charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
12831283
}
1284-
if (test_bit(4, &charge_sts)) {
1284+
if (test_bit(4, &flags)) {
12851285
*charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
12861286
}
1287-
1288-
if (test_bit(5, &charge_sts)) {
1287+
if (test_bit(5, &flags)) {
12891288
*level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
12901289
}
12911290

0 commit comments

Comments
 (0)