From patchwork Wed Feb 28 18:43:20 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: 10249329 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 2FEA660211 for ; Wed, 28 Feb 2018 18:44:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 247C428660 for ; Wed, 28 Feb 2018 18:44:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1937E28E2F; Wed, 28 Feb 2018 18:44:34 +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 AEB5E28660 for ; Wed, 28 Feb 2018 18:44:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933136AbeB1Snt (ORCPT ); Wed, 28 Feb 2018 13:43:49 -0500 Received: from mail-wr0-f193.google.com ([209.85.128.193]:41155 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933120AbeB1Snq (ORCPT ); Wed, 28 Feb 2018 13:43:46 -0500 Received: by mail-wr0-f193.google.com with SMTP id f14so3498190wre.8; Wed, 28 Feb 2018 10:43:45 -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=vLlfvu33EhUzzpJO8bFAJ4/e+FDJBvGU5+k8ooCe7AQ=; b=Dz5TYTkbaRrh+usAYyEQkgEaBOgXzsQmnqtxt+wsYkw4A2OofKPt4hTalvdJbYQYqL K2GCTeFTjTxK8SlgT0DZZQDr4vhm1TSiyHmoAx+qxZa9cKK0pVkXSVWoafsAqQ5aB4rt nOIhn4Wd9LnpOMbQHdzZIl4/u0w6IWve+8wErsG0jCdyTY4pgBN/PlNCIyB0XhkUCiSc RIc+dBh9KOUPHHfhm3O8sKoEFuRHGEre2tkpBwAyUNBnI/DNkAE38EoOjmZBgChHYy6D hliOaffdkwD1uBPboAE8vORRw60R49XX3HqJ9HycT6ry5mzC0xcudizbWJFrrmZDWXC6 /ivA== 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=vLlfvu33EhUzzpJO8bFAJ4/e+FDJBvGU5+k8ooCe7AQ=; b=lzD90+kSYlX4AiWz8DUO7BoECi+Lo5XZOTeaBJPb3c087mumTOBohlPddZkhWbfgVG KYHswdrwSiSkg0JvvMwgWNtm59+G524sVh4+7Drfhl9E1veiHJjlK+QFUDUpRrBqRVLj hU1VxbDa8VeF++3vNnAYawdvm5s2mRuP97V14+UTbCPJ1VKKc14sF1Y2pdzfdm5wtjZg y5E8kQLnOYtDrHNpXoPgGrDR+AEygwZuHnDyPfLcCQwLylL3wvBmszv3YngXCVY/GWO5 HLVRDb5/UoRy/A2hrdh5eo2N/RRaHV5n+SajDBlojQcdodmwrImirzkmfJa0hK4dCdxn fwmA== X-Gm-Message-State: APf1xPAX6l69Y6PmXFITPH8Cw+izGHZgO9qC3eXIdjxQ6H+IBPCR0dDI VVUOHQq2pIkhlR/HplGMogC16QZBqJ4= X-Google-Smtp-Source: AH8x227w95rOFizD/xCJihT60m4esq/gKGsmH3T5NVBCuM1IktUwFqCDXkL/yS1QCfeSAfO/yP6tTA== X-Received: by 10.223.173.163 with SMTP id w32mr17591182wrc.204.1519843424725; Wed, 28 Feb 2018 10:43:44 -0800 (PST) Received: from localhost.localdomain ([2a01:c50e:5126:7a00:65e2:8463:c9d0:ead9]) by smtp.gmail.com with ESMTPSA id r136sm2766864wmf.18.2018.02.28.10.43.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Feb 2018 10:43:44 -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 v4 2/4] HID: steam: add serial number information. Date: Wed, 28 Feb 2018 19:43:20 +0100 Message-Id: <20180228184322.29636-3-rodrigorivascosta@gmail.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180228184322.29636-1-rodrigorivascosta@gmail.com> References: <20180228184322.29636-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 | 104 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c index 96256426c366..9e4b1f640bef 100644 --- a/drivers/hid/hid-steam.c +++ b/drivers/hid/hid-steam.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "hid-ids.h" MODULE_LICENSE("GPL"); @@ -41,8 +42,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); @@ -71,7 +163,12 @@ static int steam_register(struct steam_device *steam) return 0; } - 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) @@ -86,7 +183,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; @@ -157,7 +254,8 @@ static void steam_unregister(struct steam_device *steam) if (input) { RCU_INIT_POINTER(steam->input, NULL); synchronize_rcu(); - hid_info(steam->hdev, "Steam Controller disconnected"); + hid_info(steam->hdev, "Steam Controller '%s' disconnected", + steam->serial_no); input_unregister_device(input); } }