Received: by 10.213.65.16 with SMTP id m16csp24742imf; Sun, 11 Mar 2018 13:00:51 -0700 (PDT) X-Google-Smtp-Source: AG47ELvEoZyouD6TA2+UZdvQgaTDSUGI+sZukBc2uJ///kn4dd3fw48+GG8djJlc/sXzb7WqDVpk X-Received: by 10.99.119.5 with SMTP id s5mr4561671pgc.71.1520798451597; Sun, 11 Mar 2018 13:00:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520798451; cv=none; d=google.com; s=arc-20160816; b=nSvBF+ojA0oapTa+XWnXAt6LvhDGOXHEO4VAQKpbujXF/gJblaIqEbieyYOCgK7eo0 MRCy40IeUyVHsodJNvbU2LxA9rF5lPFl0rNDqXYYM18O5CmbJEU2RwGYFe4T6Uazw8Bp jOVQoK+SPMQMbUqZ63fnnbEmwsLeFjRog5xRisiXy+hs8DL2k89uP6yOcMUlL9VaBVuS 2ZrcG8xC8hde1lhnxpAOqBuLqiIj3REwGz9EMkC/32+iMYM++oNIo5hDrAQMtwbrJ0En 2bXMjy9ERywK8dCjQCzP4T+n9b/ON+2RqcnTNLGt/opQsIC8cWn4qUwPvDgn/xfIal8O Jnig== 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=f6OQYuS0ZKHtXwlxlEpRhYs680i9iwEqgu/ymZ7/eXs=; b=EAKIqJZ/NdR/p8tZq82D2bm6rMHzdAHuzHRBCpqcM+2MQYN7a7EdidRWUYqLkEkwgF ypqO1sCB0dNFSB2WwsdwxKZc5lq0IMrc1RM8rCdWjkQT4FOf/KsZ6Oxue7uGYTbtCTIk D4S0hK9KbOi7rK90CbadGUs1K5GLapPvXZFXxK6GJIm8y8/z4JenL1R+klEw0PF9dapE mWNof2j8NxrrX/AWjgg7CQ8r3ktRoJ0kr9qDpM8Jd9HjO97YoW99Jvt8+2dVBe2Pzh// scgQyi+hA7JR0U0K6UGsSXhEvoXQLGI8Wwv/BOzpKcmTlwrOjSSGp5N+W2tLxLdJep4x QIjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=lMLhfJo9; 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 p25-v6si364762pli.599.2018.03.11.13.00.37; Sun, 11 Mar 2018 13:00:51 -0700 (PDT) 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=lMLhfJo9; 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 S932361AbeCKT7B (ORCPT + 99 others); Sun, 11 Mar 2018 15:59:01 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:39985 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932311AbeCKT65 (ORCPT ); Sun, 11 Mar 2018 15:58:57 -0400 Received: by mail-wm0-f65.google.com with SMTP id t6so12350113wmt.5; Sun, 11 Mar 2018 12:58:56 -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=f6OQYuS0ZKHtXwlxlEpRhYs680i9iwEqgu/ymZ7/eXs=; b=lMLhfJo9+jc4ODD68qIVQaCsmqTglmvxVjiaEyldt4zQGH3syQoab4w8sgX1a7JEPi 1J3udm5fXPJj6QIkT50eV7/ppSJqERsVsUFabW/3khrPEgpaq6oNjo++Bgd3u0wcJpwd Bv0dIrhbAHGALKphgusLdeLQpOS5+8JfIXnC4vaIs169Gn9GpB6s6JQnzWR+nBHDKJdf 1YqVuA8UXJ3t8epHHE+3WARBXI4mav7IADOWh82IfJmqY2G0Y3W2e1RhB4yY3K8mrSpx DhoiaJTXxjeS9cB5EoVCDsdxUTy1N/yXGHM0hDc5vEoT14lgZXxGC5jbKRqxidwwvE6Y vKDg== 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=f6OQYuS0ZKHtXwlxlEpRhYs680i9iwEqgu/ymZ7/eXs=; b=RcMiZoX+tSwG8BqZMwh8L9sRMZb9iOLa9toQbsl3Llt2jGJqdBuX/aw2TMDkpgBRA6 /of+FrXCT8Kspy0dXfMoMhJOgyhKisjmIQBUuCVLQExcrUKSMwuvsAOX1HLoIxueXWSk SUe6e5JE1EjYAONI+fqFzwdZFZnvol95Uc71BqLN3hlBspXvJ6+jzM2DMTGANvaYcx1t HBolrNk/mYh6Cj8KwoyNYdrPtK6HKxV7ZCd3AmXbNwlRH334RRTS/vK1giYmTQpO5vg5 1oCkC8V7I+cxSZNPXItdyiRMNEtivYHFQnAZkQ4ecF4hvDiywIxqK0vmtKCpxN/Bgp9R lWJg== X-Gm-Message-State: AElRT7Hn55w9XNx6pPGWEORXfJAGYE0dAacGm+AiYZuuwdgyI8s4hyuc vztSysiPmNbF4/7HU5iYGao= X-Received: by 10.28.165.7 with SMTP id o7mr3409859wme.38.1520798335686; Sun, 11 Mar 2018 12:58:55 -0700 (PDT) Received: from localhost.localdomain ([2a01:c50e:5126:7a00:b895:42e2:2236:5d86]) by smtp.gmail.com with ESMTPSA id 8sm4900473wmf.13.2018.03.11.12.58.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Mar 2018 12:58:55 -0700 (PDT) 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 v5 2/4] HID: steam: add serial number information. Date: Sun, 11 Mar 2018 20:58:40 +0100 Message-Id: <20180311195842.5551-3-rodrigorivascosta@gmail.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180311195842.5551-1-rodrigorivascosta@gmail.com> References: <20180311195842.5551-1-rodrigorivascosta@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 108 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 3 deletions(-) diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c index bc8b706311a9..03eebc4c28ba 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"); @@ -25,6 +26,7 @@ MODULE_AUTHOR("Rodrigo Rivas Costa "); #define STEAM_QUIRK_WIRELESS BIT(0) +#define STEAM_SERIAL_LEN 10 /* Touch pads are 40 mm in diameter and 65535 units */ #define STEAM_PAD_RESOLUTION 1638 /* Trigger runs are about 5 mm and 256 units */ @@ -41,8 +43,102 @@ struct steam_device { unsigned long quirks; struct work_struct work_connect; bool connected; + char serial_no[STEAM_SERIAL_LEN + 1]; }; +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; + unsigned int retries = 10; + 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. + */ + do { + 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); + } while (--retries); + + 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[3 + STEAM_SERIAL_LEN + 1]; + + 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[3 + STEAM_SERIAL_LEN] = 0; + strlcpy(steam->serial_no, reply + 3, sizeof(steam->serial_no)); + return 0; +} + static int steam_input_open(struct input_dev *dev) { struct steam_device *steam = input_get_drvdata(dev); @@ -71,7 +167,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 +187,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 +258,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); } } -- 2.16.2