[PATCH v5 1/4] HID: add driver for Valve Steam Controller

2018-03-11 Thread Rodrigo Rivas Costa
There are two ways to connect the Steam Controller: directly to the USB
or with the USB wireless adapter.  Both methods are similar, but the
wireless adapter can connect up to 4 devices at the same time.

The wired device will appear as 3 interfaces: a virtual mouse, a virtual
keyboard and a custom HID device.

The wireless device will appear as 5 interfaces: a virtual keyboard and
4 custom HID devices, that will remain silent until a device is actually
connected.

The custom HID device has a report descriptor with all vendor specific
usages, so the hid-generic is not very useful. In a PC/SteamBox Valve
Steam Client provices a software translation by using direct USB access
and a creates a uinput virtual gamepad.

This driver was reverse engineered to provide direct kernel support in
case you cannot, or do not want to, use Valve Steam Client. It disables
the virtual keyboard and mouse, as they are not so useful when you have
a working gamepad.

Working: buttons, axes, pads, wireless connect/disconnect.

TO-DO: Battery, force-feedback, accelerometer/gyro, led, beeper...

Signed-off-by: Rodrigo Rivas Costa 
---
 drivers/hid/Kconfig |   8 +
 drivers/hid/Makefile|   1 +
 drivers/hid/hid-ids.h   |   4 +
 drivers/hid/hid-steam.c | 537 
 4 files changed, 550 insertions(+)
 create mode 100644 drivers/hid/hid-steam.c

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 19c499f5623d..6e80fbf04e03 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -823,6 +823,14 @@ config HID_SPEEDLINK
---help---
Support for Speedlink Vicious and Divine Cezanne mouse.
 
+config HID_STEAM
+   tristate "Steam Controller support"
+   depends on HID
+   ---help---
+   Say Y here if you have a Steam Controller if you want to use it
+   without running the Steam Client. It supports both the wired and
+   the wireless adaptor.
+
 config HID_STEELSERIES
tristate "Steelseries SRW-S1 steering wheel support"
depends on HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index eb13b9e92d85..60a8abf84682 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -95,6 +95,7 @@ obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o
 obj-$(CONFIG_HID_SMARTJOYPLUS) += hid-sjoy.o
 obj-$(CONFIG_HID_SONY) += hid-sony.o
 obj-$(CONFIG_HID_SPEEDLINK)+= hid-speedlink.o
+obj-$(CONFIG_HID_STEAM)+= hid-steam.o
 obj-$(CONFIG_HID_STEELSERIES)  += hid-steelseries.o
 obj-$(CONFIG_HID_SUNPLUS)  += hid-sunplus.o
 obj-$(CONFIG_HID_GREENASIA)+= hid-gaff.o
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 43ddcdfbd0da..be31a3c20818 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -988,6 +988,10 @@
 #define USB_VENDOR_ID_STANTUM_SITRONIX 0x1403
 #define USB_DEVICE_ID_MTP_SITRONIX 0x5001
 
+#define USB_VENDOR_ID_VALVE0x28de
+#define USB_DEVICE_ID_STEAM_CONTROLLER 0x1102
+#define USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS0x1142
+
 #define USB_VENDOR_ID_STEELSERIES  0x1038
 #define USB_DEVICE_ID_STEELSERIES_SRWS10x1410
 
diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c
new file mode 100644
index ..bc8b706311a9
--- /dev/null
+++ b/drivers/hid/hid-steam.c
@@ -0,0 +1,537 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * HID driver for Valve Steam Controller
+ *
+ * Copyright (c) 2018 Rodrigo Rivas Costa 
+ *
+ * Supports both the wired and wireless interfaces.
+ *
+ * For additional functions, such as disabling the virtual mouse/keyboard or
+ * changing the right-pad margin you can use the user-space tool at:
+ *
+ *   https://github.com/rodrigorc/steamctrl
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "hid-ids.h"
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Rodrigo Rivas Costa ");
+
+#define STEAM_QUIRK_WIRELESS   BIT(0)
+
+/* Touch pads are 40 mm in diameter and 65535 units */
+#define STEAM_PAD_RESOLUTION 1638
+/* Trigger runs are about 5 mm and 256 units */
+#define STEAM_TRIGGER_RESOLUTION 51
+/* Joystick runs are about 5 mm and 256 units */
+#define STEAM_JOYSTICK_RESOLUTION 51
+
+#define STEAM_PAD_FUZZ 256
+
+struct steam_device {
+   spinlock_t lock;
+   struct hid_device *hdev;
+   struct input_dev __rcu *input;
+   unsigned long quirks;
+   struct work_struct work_connect;
+   bool connected;
+};
+
+static int steam_input_open(struct input_dev *dev)
+{
+   struct steam_device *steam = input_get_drvdata(dev);
+
+   return hid_hw_open(steam->hdev);
+}
+
+static void steam_input_close(struct input_dev *dev)
+{
+   struct steam_device *steam = input_get_drvdata(dev);
+
+   hid_hw_close(steam->hdev);
+}
+
+static int steam_register(struct steam_device *steam)
+{
+   struct hid_device *hdev = steam->hdev;
+

[PATCH v5 1/4] HID: add driver for Valve Steam Controller

2018-03-11 Thread Rodrigo Rivas Costa
There are two ways to connect the Steam Controller: directly to the USB
or with the USB wireless adapter.  Both methods are similar, but the
wireless adapter can connect up to 4 devices at the same time.

The wired device will appear as 3 interfaces: a virtual mouse, a virtual
keyboard and a custom HID device.

The wireless device will appear as 5 interfaces: a virtual keyboard and
4 custom HID devices, that will remain silent until a device is actually
connected.

The custom HID device has a report descriptor with all vendor specific
usages, so the hid-generic is not very useful. In a PC/SteamBox Valve
Steam Client provices a software translation by using direct USB access
and a creates a uinput virtual gamepad.

This driver was reverse engineered to provide direct kernel support in
case you cannot, or do not want to, use Valve Steam Client. It disables
the virtual keyboard and mouse, as they are not so useful when you have
a working gamepad.

Working: buttons, axes, pads, wireless connect/disconnect.

TO-DO: Battery, force-feedback, accelerometer/gyro, led, beeper...

Signed-off-by: Rodrigo Rivas Costa 
---
 drivers/hid/Kconfig |   8 +
 drivers/hid/Makefile|   1 +
 drivers/hid/hid-ids.h   |   4 +
 drivers/hid/hid-steam.c | 537 
 4 files changed, 550 insertions(+)
 create mode 100644 drivers/hid/hid-steam.c

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 19c499f5623d..6e80fbf04e03 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -823,6 +823,14 @@ config HID_SPEEDLINK
---help---
Support for Speedlink Vicious and Divine Cezanne mouse.
 
+config HID_STEAM
+   tristate "Steam Controller support"
+   depends on HID
+   ---help---
+   Say Y here if you have a Steam Controller if you want to use it
+   without running the Steam Client. It supports both the wired and
+   the wireless adaptor.
+
 config HID_STEELSERIES
tristate "Steelseries SRW-S1 steering wheel support"
depends on HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index eb13b9e92d85..60a8abf84682 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -95,6 +95,7 @@ obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o
 obj-$(CONFIG_HID_SMARTJOYPLUS) += hid-sjoy.o
 obj-$(CONFIG_HID_SONY) += hid-sony.o
 obj-$(CONFIG_HID_SPEEDLINK)+= hid-speedlink.o
+obj-$(CONFIG_HID_STEAM)+= hid-steam.o
 obj-$(CONFIG_HID_STEELSERIES)  += hid-steelseries.o
 obj-$(CONFIG_HID_SUNPLUS)  += hid-sunplus.o
 obj-$(CONFIG_HID_GREENASIA)+= hid-gaff.o
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 43ddcdfbd0da..be31a3c20818 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -988,6 +988,10 @@
 #define USB_VENDOR_ID_STANTUM_SITRONIX 0x1403
 #define USB_DEVICE_ID_MTP_SITRONIX 0x5001
 
+#define USB_VENDOR_ID_VALVE0x28de
+#define USB_DEVICE_ID_STEAM_CONTROLLER 0x1102
+#define USB_DEVICE_ID_STEAM_CONTROLLER_WIRELESS0x1142
+
 #define USB_VENDOR_ID_STEELSERIES  0x1038
 #define USB_DEVICE_ID_STEELSERIES_SRWS10x1410
 
diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c
new file mode 100644
index ..bc8b706311a9
--- /dev/null
+++ b/drivers/hid/hid-steam.c
@@ -0,0 +1,537 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * HID driver for Valve Steam Controller
+ *
+ * Copyright (c) 2018 Rodrigo Rivas Costa 
+ *
+ * Supports both the wired and wireless interfaces.
+ *
+ * For additional functions, such as disabling the virtual mouse/keyboard or
+ * changing the right-pad margin you can use the user-space tool at:
+ *
+ *   https://github.com/rodrigorc/steamctrl
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "hid-ids.h"
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Rodrigo Rivas Costa ");
+
+#define STEAM_QUIRK_WIRELESS   BIT(0)
+
+/* Touch pads are 40 mm in diameter and 65535 units */
+#define STEAM_PAD_RESOLUTION 1638
+/* Trigger runs are about 5 mm and 256 units */
+#define STEAM_TRIGGER_RESOLUTION 51
+/* Joystick runs are about 5 mm and 256 units */
+#define STEAM_JOYSTICK_RESOLUTION 51
+
+#define STEAM_PAD_FUZZ 256
+
+struct steam_device {
+   spinlock_t lock;
+   struct hid_device *hdev;
+   struct input_dev __rcu *input;
+   unsigned long quirks;
+   struct work_struct work_connect;
+   bool connected;
+};
+
+static int steam_input_open(struct input_dev *dev)
+{
+   struct steam_device *steam = input_get_drvdata(dev);
+
+   return hid_hw_open(steam->hdev);
+}
+
+static void steam_input_close(struct input_dev *dev)
+{
+   struct steam_device *steam = input_get_drvdata(dev);
+
+   hid_hw_close(steam->hdev);
+}
+
+static int steam_register(struct steam_device *steam)
+{
+   struct hid_device *hdev = steam->hdev;
+   struct input_dev *input;
+   int ret;
+
+   rcu_read_lock();
+   input