From patchwork Mon Apr 16 12:27:03 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: 10342857 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 518066039A for ; Mon, 16 Apr 2018 12:28:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 418F52865F for ; Mon, 16 Apr 2018 12:28:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 36354286BE; Mon, 16 Apr 2018 12:28:32 +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=-7.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 9B8B62865F for ; Mon, 16 Apr 2018 12:28:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754268AbeDPM22 (ORCPT ); Mon, 16 Apr 2018 08:28:28 -0400 Received: from mail-wr0-f195.google.com ([209.85.128.195]:41173 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753931AbeDPM2Y (ORCPT ); Mon, 16 Apr 2018 08:28:24 -0400 Received: by mail-wr0-f195.google.com with SMTP id v24so8406910wra.8; Mon, 16 Apr 2018 05:28:23 -0700 (PDT) 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=3/LKM4R7Day2NmqYNzUECri3yKpXCON3IRXmFVS0Hq0=; b=kINMQA6dLkfgZNe4KgaBheABpWWT1myiMuzPqeNC36qPC4nitXH0pP1VytZ1iPPjE+ ym/BHYsA8babNSf1+1sd0LiXYnZ8d0giAWSp9V7Y6ZTf3NBBOdyXYYFYUD+cOXJyFBXn jTR4HfD6EpXZ53F3HLAhVoLKYibvhc0UYdrQj3M9zuCBMHNFoAfXIR2CmCExlCDTpZb4 5MTccLaMDIegx9OCTdiCggzsoRl6xTcbLR0xoH9Q6suqKRmmRVr/F+rPwZVKJBXIi1XB aNRlG+MarWtUQSLUzOaUYtpmLurj9saVTpzIDCfVF+54V5shfyduXZLwGnzFethHX3z+ pl/w== 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=3/LKM4R7Day2NmqYNzUECri3yKpXCON3IRXmFVS0Hq0=; b=DYH9au3JosSwl6+nz5+lxuHsR3Nc7XcsCxBTyEfb3dTjB4Xolz9Jk7tqhMNdtgGNds o6qlbIJfwqN3eqt1hYdkDHbTqr3Bu23d3y6hZ/kh5ainGA8ODkIEvfwyj9+NgAD2kYVV Lj38NCQAkeLvKekBVCnHCwg0gVwUySZnPxKVeosY10410dbtTvFEeDsaVr17hGd78Kg+ cFpMnTZvKajsslKLjByrKLTR2TCVB/mRXWQQa+QpEC0GEIsW/Ae6jQfmKCCukY9xVbR0 UucwDmYiRfOmb1JglXFUFvziShCx+iS5J6u2RWdZozIsYZaRiAEhSnMn8qY7I+XMbtQa B9Nw== X-Gm-Message-State: ALQs6tBDqG8bl9S4zjxSgwUAwReJbxPEhBLjAQE696Wg80LJmqPO0u97 2JGJ8aiAh9V9Y1/z61UqT+c= X-Google-Smtp-Source: AIpwx4/GmpsPRL/y4p8QHrzh0bI4Du9725iBd5j8fm5Yx8rUvDYft2fjiJrb2TiVOM4m0mm4FNCwHw== X-Received: by 10.28.218.70 with SMTP id r67mr10778246wmg.6.1523881702942; Mon, 16 Apr 2018 05:28:22 -0700 (PDT) Received: from localhost.localdomain ([2a01:c50e:5126:7a00:3036:bcff:fec8:b31f]) by smtp.gmail.com with ESMTPSA id v111sm9289153wrb.30.2018.04.16.05.28.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Apr 2018 05:28:22 -0700 (PDT) From: Rodrigo Rivas Costa To: Benjamin Tissoires , "Pierre-Loup A. Griffais" , =?UTF-8?q?Cl=C3=A9ment=20VUCHENER?= , Jiri Kosina , Cameron Gutman , lkml , linux-input Cc: Rodrigo Rivas Costa Subject: [PATCH v9 2/2] HID: steam: add battery device. Date: Mon, 16 Apr 2018 14:27:03 +0200 Message-Id: <20180416122703.22306-3-rodrigorivascosta@gmail.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180416122703.22306-1-rodrigorivascosta@gmail.com> References: <20180416122703.22306-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. --- 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 36fc85714ea5..af7ebb618867 100644 --- a/drivers/hid/hid-steam.c +++ b/drivers/hid/hid-steam.c @@ -40,6 +40,7 @@ #include #include #include +#include #include "hid-ids.h" MODULE_LICENSE("GPL"); @@ -118,6 +119,10 @@ struct steam_device { struct work_struct work_connect; bool connected; char serial_no[STEAM_SERIAL_LEN + 1]; + 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, @@ -316,6 +321,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; @@ -409,6 +493,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: @@ -419,11 +507,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(); @@ -851,12 +946,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 = steam_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; @@ -914,7 +1041,19 @@ static int steam_raw_event(struct hid_device *hdev, } break; case STEAM_EV_BATTERY: - /* TODO: battery info */ + 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;