Received: by 10.223.185.116 with SMTP id b49csp3540970wrg; Tue, 13 Feb 2018 04:04:52 -0800 (PST) X-Google-Smtp-Source: AH8x224GY0vIo0OBisrRWDcYAcMicVqMP3hC7qW8PnzGGxjxvQAaCE5OoXB7sojKeWfkde66QfRT X-Received: by 2002:a17:902:396a:: with SMTP id e39-v6mr946847plg.324.1518523492585; Tue, 13 Feb 2018 04:04:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518523492; cv=none; d=google.com; s=arc-20160816; b=UAnmqEuoGmWUbSARAtXPlEMLBsG/eE6fshQ03A18SAgvaSAnbevShPvDY2g6knqgR0 FxrCbfTb/cGfBfzXNFRyyyW1L5p8aI2+fIPrmEYHctp11YJFJRiD8wcxx9j5Siiy/jtB wl5hktQZlCthS00d4UC1Iu9LOjb4chWWEP64eKgggi9uuB9tqMP+nyPQYMPt86TdzGoj 1aC51KhqDNsAbU8KwDrprIxn4VoQjJjCcqAHgvQ7NBJaq7uKvPUKAQyr/PVe7vhWAyzk rdggQRW5/NLrWnEKZtHDDywxMcLsfnnOvxIum44MIpsZexT6iZI46OjWDDJwMYE68K5b DFlA== 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=9a+hW9Tmv3bZ65vKjLAO+y0nYjaFJFDuDy38EUCBgWs=; b=bWp/gh0LaHHXhymFim771IXYzkmqW1H/bsQs3Mq7Z7yN1fZiVWHR2Xa6oc68wgloIg sLeWqCw3SMZs07yZjKndEznmz9yNa3PIlcUqVxlr2J0pT0uXUricNyJqd7TJQKXVFmE8 8xGONq1k6ApuQ7HF4DEtaJZ4pyfoCUSeG55ggkt/iRxaGdetU7DIljlmbB4KLp8kKLR0 VRyMOTkbBgBpv5MW9lgtoxh0wmlkbvTxPLdkvIyjK8tGPutkKlIYGRLa/CMBVIbMT2d+ VhdUO4KiQoEHq10TzZJ1Z2y6yusqHHViFgKrr2tVU8pYmiJPFN0SMmUOMns9WBlXzGvl 7sqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WFD21a/R; 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 p69si609298pfl.73.2018.02.13.04.04.37; Tue, 13 Feb 2018 04:04:52 -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=WFD21a/R; 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 S935020AbeBMMDy (ORCPT + 99 others); Tue, 13 Feb 2018 07:03:54 -0500 Received: from mail-wr0-f196.google.com ([209.85.128.196]:39971 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934946AbeBMMDp (ORCPT ); Tue, 13 Feb 2018 07:03:45 -0500 Received: by mail-wr0-f196.google.com with SMTP id o76so15256667wrb.7; Tue, 13 Feb 2018 04:03:44 -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=9a+hW9Tmv3bZ65vKjLAO+y0nYjaFJFDuDy38EUCBgWs=; b=WFD21a/Rq0cCgdba471C02m1p35qVFeveiXVHb5BteJL2aaQJ6LcV47X5ZppwXTPPP usCKwUGbzpWCXh9hj9H4Hg0Zs2Q7XgRCtsFaldjhd4yX3D66pKx5zA8xFlnYvHYoEiqj nkSBgYSjZ4iIp5oR6Poe1sWG9q5mtCGqeD1Vq/m8PY/R/vUMQWkrwbBbg9eKYNy6K+rQ yY4RxoOmCyYeueD2X8NA2YdM/2Zj87ChqFhhCKPPKEprhN0Vcvtn6SjDAxAkpVglGiEB Cq+p79DGuDc++CwjSf/ZixCc4NpDmUe9nqvWnBScoSvHzUpO4pUMLJCkZgLP2tUtC2FM wBZA== 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=9a+hW9Tmv3bZ65vKjLAO+y0nYjaFJFDuDy38EUCBgWs=; b=BqY7pNVfBTSgFcsT0qY58hPX67R619m4SOvdm/XZOLyljfqj5aoc74D+Uph5vfm3AM Co0Yo3brQSuwcsEJQbW6ZJN7fMtbw0J+2oqQRdVc6nv81CO3nHnXajrt4EGkZZxSdSYf GI7jL1oADCCYuo1UR8kNJrA0gioD7YG6pvmwb9dcy7o2Kl/5EkX2L9jqU/N1Dla7Say1 oOrN6L1c7C9seel4eOg2mFT3NNEsBsO20NL3OrzV5GeszLHD4fpzGfmih1m6m7DsuvEp fkvLHVDbQnaK7W8Ifm1RgerQLaVVX4G1QvMamTKQjwWPl2Jx6oWDqd4AlYUVmD46KTcQ VVyw== X-Gm-Message-State: APf1xPDvR6s6rP2LwtOJKL8ln/C+VR/SBrkPTaZqWljSSQQOO/q/t0+b z2wK4vFQcHlIKRYU4SvYrgXxFsg+N10= X-Received: by 10.223.186.67 with SMTP id t3mr979808wrg.201.1518523423711; Tue, 13 Feb 2018 04:03:43 -0800 (PST) Received: from localhost.localdomain ([2a01:c50e:5126:7a00:bca6:624f:b432:9029]) by smtp.gmail.com with ESMTPSA id 62sm11641550wrg.81.2018.02.13.04.03.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Feb 2018 04:03:43 -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 3/3] HID: steam: add battery device. Date: Tue, 13 Feb 2018 13:03:08 +0100 Message-Id: <20180213120308.23879-3-rodrigorivascosta@gmail.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180213120308.23879-1-rodrigorivascosta@gmail.com> References: <20180213120308.23879-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 | 139 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 138 insertions(+), 1 deletion(-) diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c index f269a986e2be..eb2424688092 100644 --- a/drivers/hid/hid-steam.c +++ b/drivers/hid/hid-steam.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "hid-ids.h" MODULE_LICENSE("GPL"); @@ -35,6 +36,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_register(struct steam_device *steam); @@ -45,6 +50,8 @@ static int steam_send_report(struct steam_device *steam, u8 *cmd, int size); static int steam_recv_report(struct steam_device *steam, u8 *data, int size); +static int steam_battery_register(struct steam_device *steam); +static void steam_do_battery_event(struct steam_device *steam, u8 *data); static int steam_input_open(struct input_dev *dev) { @@ -311,7 +318,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; @@ -517,6 +525,10 @@ static int steam_register(struct steam_device *steam) steam->input_dev = input; + /* ignore battery errors, we can live without it */ + if (steam->quirks & STEAM_QUIRK_WIRELESS) + steam_battery_register(steam); + return 0; input_register_fail: @@ -528,6 +540,12 @@ static void steam_unregister(struct steam_device *steam) { dbg_hid("%s\n", __func__); + if (steam->battery) { + power_supply_unregister(steam->battery); + steam->battery = NULL; + kfree(steam->battery_desc.name); + steam->battery_desc.name = NULL; + } if (steam->input_dev) { hid_info(steam->hid_dev, "Steam Controller SN: '%s' disconnected", steam->serial_no); @@ -536,6 +554,125 @@ static void steam_unregister(struct steam_device *steam) } } +/* 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]; + + dbg_hid("%s: %d %d\n", __func__, volts, batt); + + if (unlikely(!steam->battery)) { + dbg_hid("%s: battery data without connect event\n", __func__); + steam_do_connect_event(steam, true); + 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 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; + + dbg_hid("%s\n", __func__); + + 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 = kasprintf(GFP_KERNEL, + "steam-controller-%s-battery", steam->serial_no); + if (!steam->battery_desc.name) { + ret = -ENOMEM; + goto print_name_fail; + } + + /* 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->hid_dev->dev, + &steam->battery_desc, &battery_cfg); + if (IS_ERR(battery)) { + ret = PTR_ERR(battery); + hid_err(steam->hid_dev, + "%s:power_supply_register returned error %d\n", + __func__, ret); + goto power_supply_reg_fail; + } + steam->battery = battery; + power_supply_powers(steam->battery, &steam->hid_dev->dev); + return 0; + +power_supply_reg_fail: + kfree(steam->battery_desc.name); + steam->battery_desc.name = NULL; +print_name_fail: + return ret; +} + static const struct hid_device_id steam_controllers[] = { { /* Wired Steam Controller */ HID_USB_DEVICE(USB_VENDOR_ID_VALVE, -- 2.16.1