diff mbox series

[v2,07/13] HID: playstation: add DualSense Bluetooth support.

Message ID 20210102223109.996781-8-roderick@gaikai.com
State New
Headers show
Series None | expand

Commit Message

Roderick Colenbrander Jan. 2, 2021, 10:31 p.m. UTC
From: Roderick Colenbrander <roderick.colenbrander@sony.com>

This patch adds support for the DualSense when operating in Bluetooth mode.
The device has the same behavior as the DualShock 4 in that by default it
sends a limited input report (0x1), but after requesting calibration data,
it switches to an extended input report (report 49), which adds data for
touchpad, motion sensors, battery and more.

Signed-off-by: Roderick Colenbrander <roderick.colenbrander@sony.com>
---
 drivers/hid/hid-playstation.c | 35 +++++++++++++++++++++++++++++++++++
 drivers/hid/hid-quirks.c      |  1 +
 2 files changed, 36 insertions(+)

Comments

Barnabás Pőcze Jan. 7, 2021, 8:18 p.m. UTC | #1
Hi


2021. január 2., szombat 23:31 keltezéssel, Roderick Colenbrander írta:

> From: Roderick Colenbrander <roderick.colenbrander@sony.com>

>

> This patch adds support for the DualSense when operating in Bluetooth mode.

> The device has the same behavior as the DualShock 4 in that by default it

> sends a limited input report (0x1), but after requesting calibration data,

> it switches to an extended input report (report 49), which adds data for

> touchpad, motion sensors, battery and more.

>

> Signed-off-by: Roderick Colenbrander <roderick.colenbrander@sony.com>

> [...]

>  static struct input_dev *ps_gamepad_create(struct hid_device *hdev)

>  {

>  	struct input_dev *gamepad;

> @@ -406,6 +418,18 @@  static int dualsense_get_calibration_data(struct dualsense *ds)

>  		goto err_free;

>  	}

>

> +	if (ds->base.hdev->bus == BUS_BLUETOOTH) {

> +		/* Last 4 bytes contains crc32 */


Most other comments have a period at the end. I know this is a minor thing,
but I believe being consistent is better than not.

And shouldn't this CRC be checked in `dualsense_get_mac_address()`?
If so, maybe a helper function could be created that wraps the `hid_hw_raw_request()`
call?


> +		uint8_t crc_offset = DS_FEATURE_REPORT_CALIBRATION_SIZE - 4;

> +		uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]);

> +

> +		if (!ps_check_crc32(0xa3, buf, crc_offset, report_crc)) {


Maybe that 0xa3 could be named?


> +			hid_err(ds->base.hdev, "DualSense calibration report CRC's check failed\n");

> +			ret = -EILSEQ;

> +			goto err_free;

> +		}

> +	}

> +

>  	gyro_pitch_bias  = get_unaligned_le16(&buf[1]);

>  	gyro_yaw_bias    = get_unaligned_le16(&buf[3]);

>  	gyro_roll_bias   = get_unaligned_le16(&buf[5]);

> @@ -515,6 +539,16 @@  static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *r

>  	 */

>  	if (report->id == DS_INPUT_REPORT_USB && hdev->bus == BUS_USB) {

>  		ds_report = (struct dualsense_input_report *)&data[1];

> +	} else if (report->id == DS_INPUT_REPORT_BT && hdev->bus == BUS_BLUETOOTH) {

> +		/* Last 4 bytes of input report contain crc32 */

> +		uint32_t report_crc = get_unaligned_le32(&data[size - 4]);

> +

> +		if (!ps_check_crc32(0xa1, data, size - 4, report_crc)) {


And this 0xa1 as well?


> +			hid_err(hdev, "DualSense input CRC's check failed, size=%d\n", size);

> +			return -EILSEQ;

> +		}

> +

> +		ds_report = (struct dualsense_input_report *)&data[2];

>  	} else {

>  		hid_err(hdev, "Unhandled reportID=%d\n", report->id);

>  		return -1;

> [...]



Regards,
Barnabás Pőcze
diff mbox series

Patch

diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c
index 91f3ed005fce..552a52a50b78 100644
--- a/drivers/hid/hid-playstation.c
+++ b/drivers/hid/hid-playstation.c
@@ -47,6 +47,7 @@  struct ps_calibration_data {
 };
 
 #define DS_INPUT_REPORT_USB			0x01
+#define DS_INPUT_REPORT_BT			0x31
 
 #define DS_FEATURE_REPORT_CALIBRATION		5
 #define DS_FEATURE_REPORT_CALIBRATION_SIZE	41
@@ -285,6 +286,17 @@  static int ps_device_register_battery(struct ps_device *dev)
 	return 0;
 }
 
+/* Compute crc32 of HID data and compare against expected CRC. */
+static bool ps_check_crc32(uint8_t seed, uint8_t *data, size_t len, uint32_t report_crc)
+{
+	uint32_t crc;
+
+	crc = crc32_le(0xFFFFFFFF, &seed, 1);
+	crc = ~crc32_le(crc, data, len);
+
+	return crc == report_crc;
+}
+
 static struct input_dev *ps_gamepad_create(struct hid_device *hdev)
 {
 	struct input_dev *gamepad;
@@ -406,6 +418,18 @@  static int dualsense_get_calibration_data(struct dualsense *ds)
 		goto err_free;
 	}
 
+	if (ds->base.hdev->bus == BUS_BLUETOOTH) {
+		/* Last 4 bytes contains crc32 */
+		uint8_t crc_offset = DS_FEATURE_REPORT_CALIBRATION_SIZE - 4;
+		uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]);
+
+		if (!ps_check_crc32(0xa3, buf, crc_offset, report_crc)) {
+			hid_err(ds->base.hdev, "DualSense calibration report CRC's check failed\n");
+			ret = -EILSEQ;
+			goto err_free;
+		}
+	}
+
 	gyro_pitch_bias  = get_unaligned_le16(&buf[1]);
 	gyro_yaw_bias    = get_unaligned_le16(&buf[3]);
 	gyro_roll_bias   = get_unaligned_le16(&buf[5]);
@@ -515,6 +539,16 @@  static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *r
 	 */
 	if (report->id == DS_INPUT_REPORT_USB && hdev->bus == BUS_USB) {
 		ds_report = (struct dualsense_input_report *)&data[1];
+	} else if (report->id == DS_INPUT_REPORT_BT && hdev->bus == BUS_BLUETOOTH) {
+		/* Last 4 bytes of input report contain crc32 */
+		uint32_t report_crc = get_unaligned_le32(&data[size - 4]);
+
+		if (!ps_check_crc32(0xa1, data, size - 4, report_crc)) {
+			hid_err(hdev, "DualSense input CRC's check failed, size=%d\n", size);
+			return -EILSEQ;
+		}
+
+		ds_report = (struct dualsense_input_report *)&data[2];
 	} else {
 		hid_err(hdev, "Unhandled reportID=%d\n", report->id);
 		return -1;
@@ -774,6 +808,7 @@  static void ps_remove(struct hid_device *hdev)
 }
 
 static const struct hid_device_id ps_devices[] = {
+	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
 	{ }
 };
diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
index 1ca46cb445be..541c8837debd 100644
--- a/drivers/hid/hid-quirks.c
+++ b/drivers/hid/hid-quirks.c
@@ -567,6 +567,7 @@  static const struct hid_device_id hid_have_special_driver[] = {
 #endif
 #if IS_ENABLED(CONFIG_HID_PLAYSTATION)
 	{ HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
++	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS5_CONTROLLER) },
 #endif
 #if IS_ENABLED(CONFIG_HID_PRIMAX)
 	{ HID_USB_DEVICE(USB_VENDOR_ID_PRIMAX, USB_DEVICE_ID_PRIMAX_KEYBOARD) },