From patchwork Tue Feb 13 12:03:07 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: 10215963 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 C7677601C2 for ; Tue, 13 Feb 2018 12:04:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B875128DC5 for ; Tue, 13 Feb 2018 12:04:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AD12828DCE; Tue, 13 Feb 2018 12:04:11 +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 47CCB28DC5 for ; Tue, 13 Feb 2018 12:04:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934924AbeBMMDn (ORCPT ); Tue, 13 Feb 2018 07:03:43 -0500 Received: from mail-wr0-f193.google.com ([209.85.128.193]:46341 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934835AbeBMMDk (ORCPT ); Tue, 13 Feb 2018 07:03:40 -0500 Received: by mail-wr0-f193.google.com with SMTP id 34so8114125wre.13; Tue, 13 Feb 2018 04:03:39 -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=LWji5FeUPBryqp6aAwFSE/QlvWB+LBCCtqwXDzim0Sk=; b=ml2qauXt/BLboohdLd3QnTVknMgLqj2qBXGo8OTDA/QRKgRjfosibvGIb43HmZzvD9 pElerZxPG4p/8v7O6muAm0P7FJzNE780PiROKWqju5D9Rye+UchiaOEb/yT0sgcOQaud XTyqzv+K8qgG4kjnyEyizaKBqM/MDVsHdLEzHaOXiVwZxph6WpzAzVZvt+MRApwC1vWy PakrHLmpSJ8VVq3rQdhBOXDLy4VKr6NTxKAEh7k+OAfADVS/yBDv/emHO7AsVkMOZdHc LdEIlPlcg/1xFU7ErAWrmkWFONz5YI9JR6nOYW/Eys1WBTS1sj+HT+HSfOnghlfNoyIx t1+A== 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=LWji5FeUPBryqp6aAwFSE/QlvWB+LBCCtqwXDzim0Sk=; b=qpB2VV7g4QZtGcsPvziT7McqOFqVFeWrSxW+aVXZjhy4bX2LCU8waOaGJCmcJQfhE+ hhARb+YrxKq7ogQbZ1Wu/+7EuKd7t5qIDK7OsoLkTIrpltwwBkXxlDmzZ7AhtxOCLxDA hGUyZs/2PlxA8f2eyOktq8Xu7xtq6B+em8i2sUubCTPg6lLbW2X5uIEbsKM0RIAsiQwR O7WaKJK7rr40xRM/elLvgMje1pkoWd5YaU8fBm2SLSBeUyunDw33o+L3+FkQZqUCFCQ2 g2s9EQT0rK0fa2beqqtI+4lJKVXNFg4zQK4Y30AGRD9SNhKNiyHiNwAuVAS4NIz9gFF3 XORw== X-Gm-Message-State: APf1xPDVKKQitKv9CYy3UO5cmhvJPsRYZWm289szBoL5FY0ifP37xg66 JP3mg2vNWtF/zvL837gYvxg= X-Google-Smtp-Source: AH8x224Qk8N50+KOCm9MspRuZv483XupbbGXhgGTGGFy2NlAFxLSQ+zqwi07VFEt+53a5UlvIlXyCQ== X-Received: by 10.223.163.16 with SMTP id c16mr192013wrb.18.1518523419101; Tue, 13 Feb 2018 04:03:39 -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.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Feb 2018 04:03:38 -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 2/3] HID: steam: add serial number information. Date: Tue, 13 Feb 2018 13:03:07 +0100 Message-Id: <20180213120308.23879-2-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-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 | 89 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 3 deletions(-) diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c index 03f912ab5484..f269a986e2be 100644 --- a/drivers/hid/hid-steam.c +++ b/drivers/hid/hid-steam.c @@ -34,12 +34,17 @@ struct steam_device { unsigned long quirks; struct work_struct work_connect; bool connected; + char serial_no[11]; }; static int steam_register(struct steam_device *steam); static void steam_unregister(struct steam_device *steam); static void steam_do_connect_event(struct steam_device *steam, bool connected); static void steam_do_input_event(struct steam_device *steam, u8 *data); +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_input_open(struct input_dev *dev) { @@ -55,6 +60,78 @@ static void steam_input_close(struct input_dev *dev) hid_hw_close(steam->hid_dev); } +#define STEAM_FEATURE_REPORT_SIZE 65 + +static int steam_send_report(struct steam_device *steam, + u8 *cmd, int size) +{ + int retry; + int ret; + u8 *buf = kzalloc(STEAM_FEATURE_REPORT_SIZE, 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->hid_dev, 0, + buf, size + 1, + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); + if (ret != -EPIPE) + break; + dbg_hid("%s: failed, retrying (%d times)\n", __func__, retry+1); + steam_recv_report(steam, NULL, 0); + msleep(50); + } + kfree(buf); + return ret; +} + +static int steam_recv_report(struct steam_device *steam, + u8 *data, int size) +{ + int ret; + u8 *buf = kzalloc(STEAM_FEATURE_REPORT_SIZE, GFP_KERNEL); + + if (!buf) + return -ENOMEM; + + /* The report ID is always 0 */ + ret = hid_hw_raw_request(steam->hid_dev, 0x00, + buf, STEAM_FEATURE_REPORT_SIZE, + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); + memcpy(data, buf + 1, size); + kfree(buf); + 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 void steam_work_connect_cb(struct work_struct *work) { struct steam_device *steam = container_of(work, struct steam_device, @@ -385,7 +462,12 @@ static int steam_register(struct steam_device *steam) dbg_hid("%s\n", __func__); - hid_info(hdev, "Steam Controller connected"); + ret = steam_get_serial(steam); + if (ret) + return ret; + + hid_info(hdev, "Steam Controller SN: '%s' connected", + steam->serial_no); input = input_allocate_device(); if (!input) @@ -398,7 +480,7 @@ static int steam_register(struct steam_device *steam) input->name = "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; @@ -447,7 +529,8 @@ static void steam_unregister(struct steam_device *steam) dbg_hid("%s\n", __func__); if (steam->input_dev) { - hid_info(steam->hid_dev, "Steam Controller disconnected"); + hid_info(steam->hid_dev, "Steam Controller SN: '%s' disconnected", + steam->serial_no); input_unregister_device(steam->input_dev); steam->input_dev = NULL; }