From patchwork Tue Feb 20 19:33:05 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: 10230785 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 51E07602A7 for ; Tue, 20 Feb 2018 19:34:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4107928895 for ; Tue, 20 Feb 2018 19:34:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 34F9728897; Tue, 20 Feb 2018 19:34:25 +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 C97DD28895 for ; Tue, 20 Feb 2018 19:34:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753264AbeBTTeM (ORCPT ); Tue, 20 Feb 2018 14:34:12 -0500 Received: from mail-wr0-f196.google.com ([209.85.128.196]:46707 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752347AbeBTTdl (ORCPT ); Tue, 20 Feb 2018 14:33:41 -0500 Received: by mail-wr0-f196.google.com with SMTP id 34so17606394wre.13; Tue, 20 Feb 2018 11:33:40 -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=ESy+MF8kq2J/NsjM12ifns0+TKa+xeo4ZiEv77mAAQU=; b=kw59YfzaJwksqZF/RC9HanuwN3YQTDdA3eUPo2B3+p+3TGCkgUldwVrQUEN3aWODHU UiBvyBEwiXfUoi8TQBR9a00jFaJ8i3dQQ3Sd87feLAjFdNvgKJOJZeczQXa0arcZKaWu O7ooryskXEiPj28JJ6U6itMfpr2B01feZfeu6W8k1DvFYf8LnfiOWRGP7UGPYMY/KyBX tWO0h1uznVCyY7r3mzpSVdMX9AYYUv+D7GndOgJrSP4cuY7b33E70/8C9Dm57eoW0Uyr arDFR4434B6d50gcTZO6sU2OLutu2cm5BQNPLe0CNL/N2wvyQU0xRJhVYg0y6kj8iPaD +9QQ== 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=ESy+MF8kq2J/NsjM12ifns0+TKa+xeo4ZiEv77mAAQU=; b=YbWu5CE8nJha1yNSuYJK/DsV34O0vhjQJUbLEUzoANqTPvazV+jT3PIwOvhnbKZ+N8 TMJg9rV1Bt+1fLiMPSYIQocNY3+eb+ndw+ULWHa63vMVsSS07Fj8iHKbDmcsw4cSBz6O C1DXtlB0bbTNpHf2lpXADSRYVUenmgMysyMWROyzWiOYGcVoI2GNDbikfePVtJgvjPD2 Lrz6P/lAk+hIQZ3SfH0/bk+8mH4qBOyOKGOglApz1aJx2t136tPt+YhVv+Rsa6gPAWCD t53kMiswJA1GTEVhSirgO9Lu5CMBh2b5hoC2uzPyGxFpm74iJIW4eUlcXY1JjWI2FdrY q6yA== X-Gm-Message-State: APf1xPDNW78TRThtuQhknU3/mpObgCxYZvUC1xNrhGQWtZ8YC0eOLHgM FfqcwpImk0IZejqi+OJHZPg= X-Google-Smtp-Source: AH8x224lVWySQFMguFE18fB+3Vu2voP+oMyLjD+wUhhrAJoEYHw2lenzA1UuWui5oRkHZMIojGg/pg== X-Received: by 10.223.170.79 with SMTP id q15mr715192wrd.46.1519155219779; Tue, 20 Feb 2018 11:33:39 -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.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Feb 2018 11:33:39 -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 2/3] HID: steam: add serial number information Date: Tue, 20 Feb 2018 20:33:05 +0100 Message-Id: <20180220193306.28748-3-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 This device has a feature report to send and receive commands. Use it to get the serial number and set the device's uniq value. Signed-off-by: Rodrigo Rivas Costa --- drivers/hid/hid-steam.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 3 deletions(-) diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c index 7b2f16b7bb49..c9d0f909c8a0 100644 --- a/drivers/hid/hid-steam.c +++ b/drivers/hid/hid-steam.c @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include "hid-ids.h" MODULE_LICENSE("GPL"); @@ -31,8 +33,99 @@ struct steam_device { unsigned long quirks; struct work_struct work_connect; bool connected; + char serial_no[11]; }; +static int steam_recv_report(struct steam_device *steam, + u8 *data, int size) +{ + struct hid_report *r; + u8 *buf; + int ret; + + r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0]; + if (hid_report_len(r) < 64) + return -EINVAL; + buf = hid_alloc_report_buf(r, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + /* + * The report ID is always 0, so strip the first byte from the output. + * hid_report_len() is not counting the report ID, so +1 to the length + * or else we get a EOVERFLOW. We are safe from a buffer overflow + * because hid_alloc_report_buf() allocates +7 bytes. + */ + ret = hid_hw_raw_request(steam->hdev, 0x00, + buf, hid_report_len(r) + 1, + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); + if (ret > 0) + memcpy(data, buf + 1, min(size, ret - 1)); + kfree(buf); + return ret; +} + +static int steam_send_report(struct steam_device *steam, + u8 *cmd, int size) +{ + struct hid_report *r; + u8 *buf; + int retry; + int ret; + + r = steam->hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0]; + if (hid_report_len(r) < 64) + return -EINVAL; + buf = hid_alloc_report_buf(r, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + /* The report ID is always 0 */ + memcpy(buf + 1, cmd, size); + + /* + * Sometimes the wireless controller fails with EPIPE + * when sending a feature report. + * Doing a HID_REQ_GET_REPORT and waiting for a while + * seems to fix that. + */ + for (retry = 0; retry < 10; ++retry) { + ret = hid_hw_raw_request(steam->hdev, 0, + buf, size + 1, + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); + if (ret != -EPIPE) + break; + steam_recv_report(steam, NULL, 0); + msleep(50); + } + kfree(buf); + if (ret < 0) + hid_err(steam->hdev, "%s: error %d (%*ph)\n", __func__, + ret, size, cmd); + return ret; +} + +static int steam_get_serial(struct steam_device *steam) +{ + /* + * Send: 0xae 0x15 0x01 + * Recv: 0xae 0x15 0x01 serialnumber (10 chars) + */ + int ret; + u8 cmd[] = {0xae, 0x15, 0x01}; + u8 reply[14]; + + ret = steam_send_report(steam, cmd, sizeof(cmd)); + if (ret < 0) + return ret; + ret = steam_recv_report(steam, reply, sizeof(reply)); + if (ret < 0) + return ret; + reply[13] = 0; + strcpy(steam->serial_no, reply + 3); + return 0; +} + static int steam_input_open(struct input_dev *dev) { struct steam_device *steam = input_get_drvdata(dev); @@ -53,7 +146,12 @@ static int steam_register(struct steam_device *steam) struct input_dev *input; int ret; - hid_info(hdev, "Steam Controller connected"); + ret = steam_get_serial(steam); + if (ret) + return ret; + + hid_info(hdev, "Steam Controller '%s' connected", + steam->serial_no); input = input_allocate_device(); if (!input) @@ -68,7 +166,7 @@ static int steam_register(struct steam_device *steam) "Wireless Steam Controller" : "Steam Controller"; input->phys = hdev->phys; - input->uniq = hdev->uniq; + input->uniq = steam->serial_no; input->id.bustype = hdev->bus; input->id.vendor = hdev->vendor; input->id.product = hdev->product; @@ -121,7 +219,8 @@ static int steam_register(struct steam_device *steam) static void steam_unregister(struct steam_device *steam) { if (steam->input) { - hid_info(steam->hdev, "Steam Controller disconnected"); + hid_info(steam->hdev, "Steam Controller '%s' disconnected", + steam->serial_no); input_unregister_device(steam->input); steam->input = NULL; }