mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
To: Jiri Kosina <jikos@kernel.org>,
	Benjamin Tissoires <benjamin.tissoires@redhat.com>,
	linux-kernel@vger.kernel.org, linux-input@vger.kernel.org
Cc: Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
Subject: [PATCH 2/3] HID: steam: add serial number information.
Date: Tue, 13 Feb 2018 13:03:07 +0100	[thread overview]
Message-ID: <20180213120308.23879-2-rodrigorivascosta@gmail.com> (raw)
In-Reply-To: <20180213120308.23879-1-rodrigorivascosta@gmail.com>

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 <rodrigorivascosta@gmail.com>
---
 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;
 	}
-- 
2.16.1

  reply	other threads:[~2018-02-13 12:03 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-13 12:03 [PATCH 1/3] HID: add driver for Valve Steam Controller Rodrigo Rivas Costa
2018-02-13 12:03 ` Rodrigo Rivas Costa [this message]
2018-02-14 14:51   ` [PATCH 2/3] HID: steam: add serial number information Benjamin Tissoires
2018-02-15 22:16     ` Rodrigo Rivas Costa
2018-02-16  8:44       ` Benjamin Tissoires
2018-02-16  9:02         ` Rodrigo Rivas Costa
2018-02-16  9:31           ` Benjamin Tissoires
2018-02-16  9:57             ` Rodrigo Rivas Costa
2018-02-16 10:38               ` Benjamin Tissoires
2018-02-16 20:59                 ` Rodrigo Rivas Costa
2018-02-20 16:49                   ` Benjamin Tissoires
2018-02-20 17:45                     ` Rodrigo Rivas Costa
2018-02-13 12:03 ` [PATCH 3/3] HID: steam: add battery device Rodrigo Rivas Costa
2018-02-14 14:54   ` Benjamin Tissoires
2018-02-14 14:45 ` [PATCH 1/3] HID: add driver for Valve Steam Controller Benjamin Tissoires
2018-02-14 21:28   ` Philippe Ombredanne
2018-02-14 23:29   ` Rodrigo Rivas Costa
2018-02-22  0:19     ` Pierre-Loup A. Griffais

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180213120308.23879-2-rodrigorivascosta@gmail.com \
    --to=rodrigorivascosta@gmail.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®