2021-08-02 12:56:16

by Hamza Mahfooz

[permalink] [raw]
Subject: [PATCH v4] HID: logitech-hidpp: battery: provide CAPACITY property for newer devices

For devices that only support the BATTERY_VOLTAGE (0x1001) feature, UPower
requires the additional information provided by this patch, to set them up.

Signed-off-by: Hamza Mahfooz <[email protected]>
---

v2: use ARRAY_SIZE() and set voltages[]'s size to 100

v3: add a check to ensure that exactly 100 elements are in voltages[]

v4: add a note regarding the voltage curve and introduce a bounds check on
the reported voltage
---
drivers/hid/hid-logitech-hidpp.c | 44 +++++++++++++++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid-logitech-hidpp.c
index 61635e629469..c4226883904b 100644
--- a/drivers/hid/hid-logitech-hidpp.c
+++ b/drivers/hid/hid-logitech-hidpp.c
@@ -1331,6 +1331,43 @@ static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
return 0;
}

+static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
+{
+ /* NB: This voltage curve doesn't necessarily map perfectly to all
+ * devices that implement the BATTERY_VOLTAGE feature. This is because
+ * there are a few devices that use different battery technology.
+ */
+
+ static const int voltages[] = {
+ 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
+ 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
+ 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
+ 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
+ 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
+ 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
+ 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
+ 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
+ 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
+ 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
+ };
+
+ int i;
+
+ BUILD_BUG_ON(ARRAY_SIZE(voltages) != 100);
+
+ if (unlikely(voltage < 3500 || voltage >= 5000))
+ hid_warn_once(hid_dev,
+ "%s: possibly using the wrong voltage curve\n",
+ __func__);
+
+ for (i = 0; i < ARRAY_SIZE(voltages); i++) {
+ if (voltage >= voltages[i])
+ return ARRAY_SIZE(voltages) - i;
+ }
+
+ return 0;
+}
+
static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
{
u8 feature_type;
@@ -1354,6 +1391,8 @@ static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)

hidpp->battery.status = status;
hidpp->battery.voltage = voltage;
+ hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
+ voltage);
hidpp->battery.level = level;
hidpp->battery.charge_type = charge_type;
hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
@@ -1378,6 +1417,8 @@ static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,

if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
hidpp->battery.voltage = voltage;
+ hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
+ voltage);
hidpp->battery.status = status;
hidpp->battery.level = level;
hidpp->battery.charge_type = charge_type;
@@ -3717,7 +3758,8 @@ static int hidpp_initialize_battery(struct hidpp_device *hidpp)
num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;

if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
- hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE)
+ hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
+ hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
battery_props[num_battery_props++] =
POWER_SUPPLY_PROP_CAPACITY;

--
2.32.0



2021-08-02 14:07:40

by Filipe Laíns

[permalink] [raw]
Subject: Re: [PATCH v4] HID: logitech-hidpp: battery: provide CAPACITY property for newer devices

On Mon, 2021-08-02 at 08:52 -0400, Hamza Mahfooz wrote:
> For devices that only support the BATTERY_VOLTAGE (0x1001) feature, UPower
> requires the additional information provided by this patch, to set them up.
>
> Signed-off-by: Hamza Mahfooz <[email protected]>

Thanks Hamza!

Reviewed-by: Filipe Laíns <[email protected]>

Cheers,
Filipe Laíns


Attachments:
signature.asc (849.00 B)
This is a digitally signed message part

2021-08-20 12:35:52

by Jiri Kosina

[permalink] [raw]
Subject: Re: [PATCH v4] HID: logitech-hidpp: battery: provide CAPACITY property for newer devices

On Mon, 2 Aug 2021, Hamza Mahfooz wrote:

> For devices that only support the BATTERY_VOLTAGE (0x1001) feature, UPower
> requires the additional information provided by this patch, to set them up.
>
> Signed-off-by: Hamza Mahfooz <[email protected]>
> ---
>
> v2: use ARRAY_SIZE() and set voltages[]'s size to 100
>
> v3: add a check to ensure that exactly 100 elements are in voltages[]
>
> v4: add a note regarding the voltage curve and introduce a bounds check on
> the reported voltage

Applied, thanks.

--
Jiri Kosina
SUSE Labs