From patchwork Wed Jun 5 19:45:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Vanzella X-Patchwork-Id: 10977705 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E984B15E6 for ; Wed, 5 Jun 2019 19:46:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DCB2F28608 for ; Wed, 5 Jun 2019 19:46:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D0A24288EE; Wed, 5 Jun 2019 19:46:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A56928757 for ; Wed, 5 Jun 2019 19:46:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726608AbfFETqG (ORCPT ); Wed, 5 Jun 2019 15:46:06 -0400 Received: from out4-smtp.messagingengine.com ([66.111.4.28]:48271 "EHLO out4-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726477AbfFETqG (ORCPT ); Wed, 5 Jun 2019 15:46:06 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id EEB1222398; Wed, 5 Jun 2019 15:46:04 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Wed, 05 Jun 2019 15:46:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= pedrovanzella.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; s=fm3; bh=84Wq34OlrA0uXIDD58OYDtLGL1/1BjAAHMmIQuNro1M=; b=mDSZ0 h60eAUG0cCyWTsOUvjRrTjoqzEonGBdjPxaih+CJwhHFCVLsbfBGJ9U7YpbovL8P El2OKkMuEaDrdNqVAq8stk2KuA8Poz2LIjcujSPXwvmNbyRVetaEymAQJbd7ebhT Vm31LVfjs4M8PqoJ3/+PD3xwv08H1zcQuOsPf28rMrXbGIJ9t1KC+hNEuvvW/39l LXuGoWpulXqrJRojZrNLdy1WhyYUpZ1ib1phy4m9AY+TLyiw4F0Q5KgiAt4ghj4K v7Wyg4MuRXBaKgoD+6GiGf9K1XpipbfT56z1Tm40qA3HDR1+r3ez1QVsSHxKMD+D +3uRH0lFXZi4noMnw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=84Wq34OlrA0uXIDD58OYDtLGL1/1BjAAHMmIQuNro1M=; b=DvAvnepR Oo0GAunGuNki1fwA+dH5o7JyFYE07xIpI+3+8/U1xfQbQTrB9M05OnpN5PdFoJXX Q1uQMhplozZ6PPSwIAUjs8a2pTyAfjXjkE8WKneu0tZq4IhIGDeLTfXsZ+Um+t1v pwczDJj8kOi/aOd7KmexDwQCky0JVOg39LJjicXtupjCnLwnM6R0mtSH5WPz85nH 09d71e5SuDSbElg5DzHRZb5sTqnEnJ9Y472oj5FKqxBKhoYnyksml1PgVeebBcFc khJVNQZlxNhoPbmyDL++iY7SqfrVNnvUP8tvCOKj3cGCPF0T7nAJQFQwVHJGgjaL Y/RM4jQfAoSmAQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrudegvddgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvffufffkofgjfhgggfestdekredtredttdenucfhrhhomheprfgvughr ohcugggrnhiivghllhgruceophgvughrohesphgvughrohhvrghniigvlhhlrgdrtghomh eqnecukfhppeejtddrvdejrddvjedrudegleenucfrrghrrghmpehmrghilhhfrhhomhep phgvughrohesphgvughrohhvrghniigvlhhlrgdrtghomhenucevlhhushhtvghrufhiii gvpedt X-ME-Proxy: Received: from localhost (toroon020aw-lp130-02-70-27-27-149.dsl.bell.ca [70.27.27.149]) by mail.messagingengine.com (Postfix) with ESMTPA id 6FE24380085; Wed, 5 Jun 2019 15:46:04 -0400 (EDT) From: Pedro Vanzella To: linux-input@vger.kernel.org Cc: Pedro Vanzella , Jiri Kosina , Benjamin Tissoires , linux-kernel@vger.kernel.org Subject: [PATCH v2 2/4] HID: hid-logitech-hidpp: add function to query battery voltage Date: Wed, 5 Jun 2019 15:45:31 -0400 Message-Id: <20190605194533.18717-3-pedro@pedrovanzella.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190605194533.18717-1-pedro@pedrovanzella.com> References: <20190605194533.18717-1-pedro@pedrovanzella.com> MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When the device is brought up, if it's one of the devices we know supports battery voltage checking, figure out the feature index and get the battery voltage and status. If everything went correctly, record the fact that we're capable of querying battery voltage. Signed-off-by: Pedro Vanzella --- drivers/hid/hid-logitech-hidpp.c | 94 ++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid-logitech-hidpp.c index 8b38c14725b8..31e99363ab65 100644 --- a/drivers/hid/hid-logitech-hidpp.c +++ b/drivers/hid/hid-logitech-hidpp.c @@ -92,6 +92,7 @@ MODULE_PARM_DESC(disable_tap_to_click, #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1) #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2) #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3) +#define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4) /* * There are two hidpp protocols in use, the first version hidpp10 is known @@ -140,12 +141,14 @@ struct hidpp_report { struct hidpp_battery { u8 feature_index; u8 solar_feature_index; + u8 voltage_feature_index; struct power_supply_desc desc; struct power_supply *ps; char name[64]; int status; int capacity; int level; + int voltage; /* in millivolts */ bool online; }; @@ -1224,6 +1227,92 @@ static int hidpp20_battery_event(struct hidpp_device *hidpp, return 0; } +/* -------------------------------------------------------------------------- */ +/* 0x1001: Battery voltage */ +/* -------------------------------------------------------------------------- */ + +#define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001 + +#define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00 + +static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage) +{ + int status; + + switch (data[2]) { + case 0x00: /* discharging */ + status = POWER_SUPPLY_STATUS_DISCHARGING; + break; + case 0x10: /* wireless charging */ + case 0x80: /* charging */ + status = POWER_SUPPLY_STATUS_CHARGING; + break; + case 0x81: /* fully charged */ + status = POWER_SUPPLY_STATUS_FULL; + break; + default: + status = POWER_SUPPLY_STATUS_NOT_CHARGING; + } + + *voltage = (data[0] << 8) + data[1]; + + return status; +} + +static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp, + u8 feature_index, + int *status, int *voltage) +{ + struct hidpp_report response; + int ret; + u8 *params = (u8 *)response.fap.params; + + ret = hidpp_send_fap_command_sync(hidpp, feature_index, + CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE, + NULL, 0, &response); + + if (ret > 0) { + hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", + __func__, ret); + return -EPROTO; + } + if (ret) + return ret; + + hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE; + + *status = hidpp20_battery_map_status_voltage(params, voltage); + + return 0; +} + +static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp) +{ + u8 feature_type; + int ret; + int status, voltage; + + if (hidpp->battery.voltage_feature_index == 0xff) { + ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE, + &hidpp->battery.voltage_feature_index, + &feature_type); + if (ret) + return ret; + } + + ret = hidpp20_battery_get_battery_voltage(hidpp, + hidpp->battery.voltage_feature_index, + &status, &voltage); + + if (ret) + return ret; + + hidpp->battery.status = status; + hidpp->battery.voltage = voltage; + hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING; + + return 0; +} static enum power_supply_property hidpp_battery_props[] = { POWER_SUPPLY_PROP_ONLINE, POWER_SUPPLY_PROP_STATUS, @@ -3209,10 +3298,13 @@ static int hidpp_initialize_battery(struct hidpp_device *hidpp) hidpp->battery.feature_index = 0xff; hidpp->battery.solar_feature_index = 0xff; + hidpp->battery.voltage_feature_index = 0xff; if (hidpp->protocol_major >= 2) { if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750) ret = hidpp_solar_request_battery_event(hidpp); + else if (hidpp->quirks & HIDPP_QUIRK_BATTERY_VOLTAGE_X1001) + ret = hidpp20_query_battery_voltage_info(hidpp); else ret = hidpp20_query_battery_info(hidpp); @@ -3413,6 +3505,8 @@ static void hidpp_connect_event(struct hidpp_device *hidpp) hidpp10_query_battery_status(hidpp); } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) { hidpp20_query_battery_info(hidpp); + if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE) + hidpp20_query_battery_voltage_info(hidpp); } if (hidpp->battery.ps) power_supply_changed(hidpp->battery.ps);