From patchwork Tue Feb 20 19:33:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Rivas Costa X-Patchwork-Id: 10230779 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7EB48602A7 for ; Tue, 20 Feb 2018 19:34:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F2D028895 for ; Tue, 20 Feb 2018 19:34:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 63C4928897; Tue, 20 Feb 2018 19:34:06 +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=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable 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 E62B828895 for ; Tue, 20 Feb 2018 19:34:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753143AbeBTTdv (ORCPT ); Tue, 20 Feb 2018 14:33:51 -0500 Received: from mail-wr0-f196.google.com ([209.85.128.196]:36451 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752441AbeBTTdn (ORCPT ); Tue, 20 Feb 2018 14:33:43 -0500 Received: by mail-wr0-f196.google.com with SMTP id u15so17660923wrg.3; Tue, 20 Feb 2018 11:33:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=9T+qEsJGfNSDFvt3yLM/5X0Tl2ayZwDdy+6NiE640fw=; b=HECGk0dZP2CtLzLXjlRkBYb3CH/KI0QHvEyPsRwVjrzFEmnLojl7oWLAmyOrd2Dvhq ZGfibxQ2Lu3vtQsXN4WnxepmMfzpqoCPzx3wHphT5PB8ocLlFFsG3GXlEtQA6fv1xK08 PIOf5xE/y6Vw7IMtpo7vrWhm7MhzoP6I8CWBe+F82gbBWXC2UOzS/07UnnN/p73T1S4M N1Ut0G/tR9hMcRW4gX2c/vogoSHGkTjjYf+IhqLZwOhsZ4y00uQFSQsa5jnY3yjgXV28 l8gzNDGJ4Z0lwyNQK3ld5dGQSrxuB11VYOu6grcIZZW0Z7xO8xTnu0rweCA9JCa4ArUQ WlFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=9T+qEsJGfNSDFvt3yLM/5X0Tl2ayZwDdy+6NiE640fw=; b=gHAxZMVq5B15KySmJgnJ8M9OtL+LT9mw6NbJ1W6nxbun4uYCxfEtOw3M0MKfy3v94O t8QnFej/uLX3axlOgjLhJAe/uSCxGavHGXBEg1vZxP8BE9YXjcFV8XL+tyzLOWKjHfG7 wYhfrEkFJnB8FGF/S0OCAS2sA/pu9EonN0uz19gikFVFwa9iMZsNzCxuY0KGuiDAMCMC Dj9OPUOfEE4BwKAPAqcwe4NgXOKlsEs4254p3gdnLeE+t2SOtZriuGVbK2uuei58JvSZ R/OD7KR/X9V/sVdC8g4A1OiL2yh+IUBHns+NRwx47xQvj/lqVCsGEZhJrpwaWR3pasXS OK6g== X-Gm-Message-State: APf1xPA92pdZa6Ov/2U5KQQekXzNynFUeklEFFwQnEdmf/xg8esR9Nje taEckoFR4L3RPDMRRcPuDQs= X-Google-Smtp-Source: AH8x227UpalGJpcmyMa4gZ4MDzGWFf68UFA7eLuVSB2iF//gBSv4MKxN3R4fCS+c7xTw/eALZn6u2w== X-Received: by 10.28.29.209 with SMTP id d200mr73354wmd.149.1519155221704; Tue, 20 Feb 2018 11:33:41 -0800 (PST) Received: from localhost.localdomain ([2a01:c50e:5126:7a00:c17:3826:4af9:bc38]) by smtp.gmail.com with ESMTPSA id k63sm17311520wrc.30.2018.02.20.11.33.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Feb 2018 11:33:41 -0800 (PST) From: Rodrigo Rivas Costa To: Jiri Kosina , Benjamin Tissoires , linux-kernel@vger.kernel.org, linux-input@vger.kernel.org Cc: Rodrigo Rivas Costa Subject: [PATCH v2 3/3] HID: steam: add battery device Date: Tue, 20 Feb 2018 20:33:06 +0100 Message-Id: <20180220193306.28748-4-rodrigorivascosta@gmail.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180220193306.28748-1-rodrigorivascosta@gmail.com> References: <20180220193306.28748-1-rodrigorivascosta@gmail.com> 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 The wireless Steam Controller is battery operated, so add the battery device and power information. Signed-off-by: Rodrigo Rivas Costa --- drivers/hid/hid-steam.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c index c9d0f909c8a0..d6926e86d664 100644 --- a/drivers/hid/hid-steam.c +++ b/drivers/hid/hid-steam.c @@ -34,6 +34,10 @@ struct steam_device { struct work_struct work_connect; bool connected; char serial_no[11]; + struct power_supply_desc battery_desc; + struct power_supply *battery; + u8 battery_charge; + u16 voltage; }; static int steam_recv_report(struct steam_device *steam, @@ -140,6 +144,85 @@ static void steam_input_close(struct input_dev *dev) hid_hw_close(steam->hdev); } +static enum power_supply_property steam_battery_props[] = { + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_SCOPE, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_CAPACITY, +}; + +static int steam_battery_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct steam_device *steam = power_supply_get_drvdata(psy); + unsigned long flags; + s16 volts; + u8 batt; + int ret = 0; + + spin_lock_irqsave(&steam->lock, flags); + volts = steam->voltage; + batt = steam->battery_charge; + spin_unlock_irqrestore(&steam->lock, flags); + + switch (psp) { + case POWER_SUPPLY_PROP_PRESENT: + val->intval = 1; + break; + case POWER_SUPPLY_PROP_SCOPE: + val->intval = POWER_SUPPLY_SCOPE_DEVICE; + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + val->intval = volts * 1000; /* mV -> uV */ + break; + case POWER_SUPPLY_PROP_CAPACITY: + val->intval = batt; + break; + default: + ret = -EINVAL; + break; + } + return ret; +} + +static int steam_battery_register(struct steam_device *steam) +{ + struct power_supply *battery; + struct power_supply_config battery_cfg = { .drv_data = steam, }; + unsigned long flags; + int ret; + + steam->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; + steam->battery_desc.properties = steam_battery_props; + steam->battery_desc.num_properties = ARRAY_SIZE(steam_battery_props); + steam->battery_desc.get_property = steam_battery_get_property; + steam->battery_desc.name = devm_kasprintf(&steam->hdev->dev, + GFP_KERNEL, "steam-controller-%s-battery", + steam->serial_no); + if (!steam->battery_desc.name) + return -ENOMEM; + + /* avoid the warning of 0% battery while waiting for the first info */ + spin_lock_irqsave(&steam->lock, flags); + steam->voltage = 3000; + steam->battery_charge = 100; + spin_unlock_irqrestore(&steam->lock, flags); + + battery = power_supply_register(&steam->hdev->dev, + &steam->battery_desc, &battery_cfg); + if (IS_ERR(battery)) { + ret = PTR_ERR(battery); + hid_err(steam->hdev, + "%s:power_supply_register failed with error %d\n", + __func__, ret); + return ret; + } + steam->battery = battery; + power_supply_powers(steam->battery, &steam->hdev->dev); + return 0; +} + static int steam_register(struct steam_device *steam) { struct hid_device *hdev = steam->hdev; @@ -209,6 +292,10 @@ static int steam_register(struct steam_device *steam) steam->input = input; + /* ignore battery errors, we can live without it */ + if (steam->quirks & STEAM_QUIRK_WIRELESS) + steam_battery_register(steam); + return 0; input_register_fail: @@ -218,6 +305,10 @@ static int steam_register(struct steam_device *steam) static void steam_unregister(struct steam_device *steam) { + if (steam->battery) { + power_supply_unregister(steam->battery); + steam->battery = NULL; + } if (steam->input) { hid_info(steam->hdev, "Steam Controller '%s' disconnected", steam->serial_no); @@ -500,6 +591,40 @@ static void steam_do_input_event(struct steam_device *steam, u8 *data) input_sync(input); } +/* + * The size for this message payload is 11. + * The known values are: + * Offset| Type | Meaning + * -------+-------+--------------------------- + * 4-7 | u32 | sequence number + * 8-11 | -- | always 0 + * 12-13 | u16 | voltage (mV) + * 14 | u8 | battery percent + */ +static void steam_do_battery_event(struct steam_device *steam, u8 *data) +{ + unsigned long flags; + s16 volts = le16_to_cpup((__le16 *)(data + 12)); + u8 batt = data[14]; + + if (unlikely(!steam->input)) { + dbg_hid("%s: battery data without connect event\n", __func__); + steam_do_connect_event(steam, true); + return; + } + + /* Creating the battery may have failed */ + if (unlikely(!steam->battery)) + return; + + spin_lock_irqsave(&steam->lock, flags); + steam->voltage = volts; + steam->battery_charge = batt; + spin_unlock_irqrestore(&steam->lock, flags); + + power_supply_changed(steam->battery); +} + static int steam_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size) @@ -545,7 +670,8 @@ static int steam_raw_event(struct hid_device *hdev, } break; case 0x04: - /* TODO battery status */ + if (steam->quirks & STEAM_QUIRK_WIRELESS) + steam_do_battery_event(steam, data); break; } return 0;