Received: by 10.223.185.116 with SMTP id b49csp2985816wrg; Sun, 25 Feb 2018 10:55:23 -0800 (PST) X-Google-Smtp-Source: AH8x227bZbAxtu02nzFy72I7PsTRfefQyvElrcI6YUuZi7qU34XiNI0OZ9KRwU7fq2DWT698omqY X-Received: by 2002:a17:902:b697:: with SMTP id c23-v6mr7407297pls.328.1519584923451; Sun, 25 Feb 2018 10:55:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519584923; cv=none; d=google.com; s=arc-20160816; b=HrJSdrfaWlWndD0QimzR0cv/Ai/z5A+Nln8AAyIQFKb/gBeSoaIsKr/GLEbqvgkoEt HF/H/VdEYRmqzliADvxMAzrooWb6ydKYCIHaNFQ9B68IqXeawrqZ6fw99TYnk4VvUQeO /l6ZuZ37SrLKVNLb2JriPn3g08UR/0bpDq4Ed0ckH3ymclp/HhWptFE+L+xsLRLQCsDt +2GPwFUNTBNoHmD3IEePvvF3NG7EKUQ1V7ZlQCQRmK9r8QFbH/uNMlVlMit8/grzW/Uy CoL1351Vu6qcm6S9VOPYWqs7X0e10ct/NI1cTkn0j1alkpA84/CAZrI/WgruXSzWa7ml emkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=1/zTq13BsiWPgKTTc5t6BXFQ42xg++HGvA/s6XGGqSU=; b=KZEWdtFlcQRa3AGo2vW0iQZGOMEumUqJOt7PKTUleituOeYvX8qxonh4suU0CAr+uw mkgDBuyND0/KBitnSEIBTXRgr8R1nfVAtAfFfaiz5CsfMfaI/nUVXmSqqXoGwoJ9LMBK yBQRkSdjSK0WBoVoSVUoxq5s5kLCm2dkF0YziCs8bDpS1bNzrJkF20sW0QS/QvEphvTO zIgLxPGDLQVJEUBQi0RpMXxqEzLgjFW6yrO2360OI8/9bJXXvpd/4BJMsaYtS5Z1ATgC pa0jJaFPqG5dC2XHdPI6HKxTOCDMkPtB75MME29dU35WUNDyi6ymtz+nW49kkvK4iqSR 8uEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RfoxzjtE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y10si4098478pgv.456.2018.02.25.10.55.09; Sun, 25 Feb 2018 10:55:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RfoxzjtE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751942AbeBYSxo (ORCPT + 99 others); Sun, 25 Feb 2018 13:53:44 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:40520 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751728AbeBYSxN (ORCPT ); Sun, 25 Feb 2018 13:53:13 -0500 Received: by mail-wm0-f66.google.com with SMTP id t6so4930711wmt.5; Sun, 25 Feb 2018 10:53:12 -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=1/zTq13BsiWPgKTTc5t6BXFQ42xg++HGvA/s6XGGqSU=; b=RfoxzjtEMJyqiqKhKESweJCXMhNwtO+lj3JiQVRhR47CB4X+rhW7IJJlLvx6S746ia 05dyjvsf6LNEfBXhtB+3bFr2pmOLfVWArrApzAoV8nhnCnfq0yN1Frx0AX9ULFae897u KI0/8/SyA77hLs+TA3mb7xIYDVeBJOj2SGJG/wc6mNrRmdUUio8fjuChoXztb/lfrCxP G9ibml8+xHL8g/qSXrVFWfinF+JkLNSh/2jbPJfikU83HE5UzV1oRvcpOgNoe6qUtHs0 xlApd0wfAHTSimKPKBrFW6mh003qNmSOJV4pWIJaqUS36AurxymhxALyISNEbjxCoyp4 4aFw== 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=1/zTq13BsiWPgKTTc5t6BXFQ42xg++HGvA/s6XGGqSU=; b=SYWizKNde21eDLL2gr/4pSjgYyiK3/AtZ09nYjRTo9K68NFeF8J9YL4NFdx1sg1WEr LnDz0q1gy/ofZmbq4BMzx5xKYJm6dHCQBfGP6cawl0wvyxDd0b2NwnD1k85e6mVka0Ca DTPj2vrM+lcz0TYlg6RKBfPYRVq818VcQkxdjHWlcxV01HTpmmtZOJC6cxxpyqSIt2D1 UTxjf/7NVCvZ/zmu3wmExHjVCH7psDR/8XO7rpj37fOMu/esbbnNU8xHFvqMfmU/RUy3 DLmAsX3+jMMw7QwxL6G5wdv/12TEs1IObHuaWzjlmuxeyNhWVr3fgbG7Q2stDjdCL2Bo Atkg== X-Gm-Message-State: APf1xPCETbDS3OKBpGhv8nlP5LhJfSKEdd9j/wXOtuxILtNEkOaivq5p k9zkhReaWNs1v8ICtLIwq4Y= X-Received: by 10.28.35.77 with SMTP id j74mr6001255wmj.96.1519584791423; Sun, 25 Feb 2018 10:53:11 -0800 (PST) Received: from localhost.localdomain ([2a01:c50e:5126:7a00:50d8:4f2:c65c:e8e9]) by smtp.gmail.com with ESMTPSA id 72sm6332850wmh.44.2018.02.25.10.53.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 25 Feb 2018 10:53:10 -0800 (PST) From: Rodrigo Rivas Costa To: Jiri Kosina , Benjamin Tissoires , "Pierre-Loup A. Griffais" , Cameron Gutman , =?UTF-8?q?Cl=C3=A9ment=20VUCHENER?= , linux-kernel@vger.kernel.org, linux-input@vger.kernel.org Cc: Rodrigo Rivas Costa Subject: [PATCH v3 3/3] HID: steam: add battery device. Date: Sun, 25 Feb 2018 19:52:50 +0100 Message-Id: <20180225185250.10759-4-rodrigorivascosta@gmail.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180225185250.10759-1-rodrigorivascosta@gmail.com> References: <20180225185250.10759-1-rodrigorivascosta@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 141 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 1 deletion(-) diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c index 405fae2d2e4b..6af828ecafe9 100644 --- a/drivers/hid/hid-steam.c +++ b/drivers/hid/hid-steam.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "hid-ids.h" MODULE_LICENSE("GPL"); @@ -43,6 +44,10 @@ struct steam_device { struct work_struct work_connect; bool connected; char serial_no[11]; + struct power_supply_desc battery_desc; + struct power_supply __rcu *battery; + u8 battery_charge; + u16 voltage; }; static int steam_recv_report(struct steam_device *steam, @@ -149,6 +154,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; + } + rcu_assign_pointer(steam->battery, battery); + power_supply_powers(battery, &steam->hdev->dev); + return 0; +} + static int steam_register(struct steam_device *steam) { struct hid_device *hdev = steam->hdev; @@ -234,6 +318,10 @@ static int steam_register(struct steam_device *steam) rcu_assign_pointer(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: @@ -244,11 +332,18 @@ static int steam_register(struct steam_device *steam) static void steam_unregister(struct steam_device *steam) { struct input_dev *input; + struct power_supply *battery; rcu_read_lock(); input = rcu_dereference(steam->input); + battery = rcu_dereference(steam->battery); rcu_read_unlock(); + if (battery) { + RCU_INIT_POINTER(steam->battery, NULL); + synchronize_rcu(); + power_supply_unregister(battery); + } if (input) { RCU_INIT_POINTER(steam->input, NULL); synchronize_rcu(); @@ -548,12 +643,44 @@ static void steam_do_input_event(struct steam_device *steam, 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, + struct power_supply *battery, u8 *data) +{ + unsigned long flags; + + s16 volts = le16_to_cpup((__le16 *)(data + 12)); + u8 batt = data[14]; + + /* Creating the battery may have failed */ + rcu_read_lock(); + battery = rcu_dereference(steam->battery); + if (likely(battery)) { + spin_lock_irqsave(&steam->lock, flags); + steam->voltage = volts; + steam->battery_charge = batt; + spin_unlock_irqrestore(&steam->lock, flags); + power_supply_changed(battery); + } + rcu_read_unlock(); +} + static int steam_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size) { struct steam_device *steam = hid_get_drvdata(hdev); struct input_dev *input; + struct power_supply *battery; if (!steam) return 0; @@ -606,7 +733,19 @@ static int steam_raw_event(struct hid_device *hdev, } break; case 0x04: - /* TODO battery status */ + if (steam->quirks & STEAM_QUIRK_WIRELESS) { + rcu_read_lock(); + battery = rcu_dereference(steam->battery); + if (likely(battery)) { + steam_do_battery_event(steam, battery, data); + } else { + dbg_hid( + "%s: battery data without connect event\n", + __func__); + steam_do_connect_event(steam, true); + } + rcu_read_unlock(); + } break; } return 0; -- 2.16.2