Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1204970pxv; Fri, 23 Jul 2021 02:33:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7L8iurSOOOoEtV46ETfh0yu3wUzQ4Mmh1KRZ8wDmF7P5kMltuVXqv61YQQsNeUgNljGI1 X-Received: by 2002:a05:6638:22f:: with SMTP id f15mr3320964jaq.141.1627032821725; Fri, 23 Jul 2021 02:33:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627032821; cv=none; d=google.com; s=arc-20160816; b=D/8VTkSW6ceu3lOY2T2h8SDbFzSsphSxeOQmKCyec0yndjxTG5v5hpbwwNR9827saI 8umMFGo3z7ayKks6X+1sF0SxOiPkmE5wmzkW3WY57LYTltOCU3S5y7mDLdLUEN/arkbp 7TbqWg3aXqSuLYB76WTeIK66yHU1TIT4kjEQwAlwCUgjkmU8XGQgzLEV0P8AdEfHKb82 eWfSpVW/bMu7XbzESWXhD1ytNi3D8x08+TR6RWW9qE61rYnTNI7lb0o+9nwYGDQDVOhk 6tXyuwUbWEVl/8jmFB8/Ku1YuuxSjX8z3Cs522sWw0qKyGVqnTjNFpXKNiqNuSqX7fwe iP6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=HRmj9Gkjs/hQhepGvArHLqqvdChtfYGVSMhFJtfCpf4=; b=JV2QAFn9AzndBxOuvpTGihyHe7QYF8wnzrxhtzWBarWvFQ72pu3fWWcvyDKO+ieWnA l7UqNXVKX9SeUauaZhUlbusG4NnDib4ijjkCfNorAIg1U5mfVPhANcq07zJeSudu4VpR RX9oJnV1kOWFtGhDSEGHwZIrhBmxs9gALaE+yumpx4Mnuq19aRPU4fyAXFnPuF9fZrY/ 7168foT+ym/wslKUhSdWpjzQot8A1ZBV5/h6IlTmhSAuqANITZtXCqj35kdLYyYjpkWE qMdJljgNNsbI6MMu8r6ZCuPYf5RY/0vixQ2wXP0UaC64jtMbG5g1IhgnIAG2BR9fmPx5 ivSA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x12si15837629iom.57.2021.07.23.02.33.30; Fri, 23 Jul 2021 02:33:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232518AbhGWIu7 (ORCPT + 99 others); Fri, 23 Jul 2021 04:50:59 -0400 Received: from relay8-d.mail.gandi.net ([217.70.183.201]:37809 "EHLO relay8-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229949AbhGWIu6 (ORCPT ); Fri, 23 Jul 2021 04:50:58 -0400 Received: (Authenticated sender: hadess@hadess.net) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id ED1781BF20B; Fri, 23 Jul 2021 09:31:29 +0000 (UTC) Message-ID: Subject: Re: [PATCH] HID: logitech-hidpp: battery: provide CAPACITY property for newer devices From: Bastien Nocera To: Hamza Mahfooz , linux-kernel@vger.kernel.org Cc: Jiri Kosina , Benjamin Tissoires , linux-input@vger.kernel.org, Filipe =?ISO-8859-1?Q?La=EDns?= Date: Fri, 23 Jul 2021 11:31:29 +0200 In-Reply-To: <20210723062346.86259-1-someguy@effective-light.com> References: <20210723062346.86259-1-someguy@effective-light.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.2 (3.40.2-1.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hey Hamza, On Fri, 2021-07-23 at 02:23 -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 > --- >  drivers/hid/hid-logitech-hidpp.c | 31 > ++++++++++++++++++++++++++++++- >  1 file changed, 30 insertions(+), 1 deletion(-) > > diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid- > logitech-hidpp.c > index 61635e629469..662c335e9c17 100644 > --- a/drivers/hid/hid-logitech-hidpp.c > +++ b/drivers/hid/hid-logitech-hidpp.c > @@ -1331,6 +1331,32 @@ static int > hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp, >         return 0; >  } >   > +static int hidpp20_map_battery_capacity(int voltage) > +{ > +       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, > +               3500 > +       }; > + > +       int i; > + > +       for (i = 0; i < (sizeof(voltages) / sizeof(int)); i++) { Use ARRAY_SIZE() > +               if (voltage >= voltages[i]) > +                       return 100 - i; I don't really like this part, which seems to rely on things which aren't asserted. Is there a way to make sure that voltages[] is 100 items and they're all initialised? If you tested this, could you also mention which devices you tested this on in the commit message? Thanks for picking this up! > +       } > + > +       return 0; > +} > + >  static int hidpp20_query_battery_voltage_info(struct hidpp_device > *hidpp) >  { >         u8 feature_type; > @@ -1354,6 +1380,7 @@ 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(voltage); >         hidpp->battery.level = level; >         hidpp->battery.charge_type = charge_type; >         hidpp->battery.online = status != > POWER_SUPPLY_STATUS_NOT_CHARGING; > @@ -1378,6 +1405,7 @@ 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(voltage); >                 hidpp->battery.status = status; >                 hidpp->battery.level = level; >                 hidpp->battery.charge_type = charge_type; > @@ -3717,7 +3745,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; >