--- v3
+++ v6
@@ -1,118 +1,102 @@
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.
+Track devices in a list, so we can detect when a device is connected
+twice when using Bluetooth and USB.
Signed-off-by: Roderick Colenbrander <roderick.colenbrander@sony.com>
---
- drivers/hid/Kconfig | 1 +
- drivers/hid/hid-playstation.c | 41 +++++++++++++++++++++++++++++++++++
- 2 files changed, 42 insertions(+)
+ drivers/hid/hid-playstation.c | 46 +++++++++++++++++++++++++++++++++++
+ 1 file changed, 46 insertions(+)
-diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
-index 0c141f2312f7..b3ec01c7a0b7 100644
---- a/drivers/hid/Kconfig
-+++ b/drivers/hid/Kconfig
-@@ -856,6 +856,7 @@ config HID_PLANTRONICS
- config HID_PLAYSTATION
- tristate "PlayStation HID Driver"
- depends on HID
-+ select CRC32
- select POWER_SUPPLY
- help
- Provides support for Sony PS5 controllers including support for
diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c
-index a14c33a52c8f..bcf93836beb2 100644
+index b09ec604cd27..20fe29fc61c0 100644
--- a/drivers/hid/hid-playstation.c
+++ b/drivers/hid/hid-playstation.c
-@@ -6,6 +6,7 @@
- */
+@@ -15,10 +15,15 @@
- #include <linux/bits.h>
-+#include <linux/crc32.h>
- #include <linux/device.h>
- #include <linux/hid.h>
- #include <linux/input/mt.h>
-@@ -45,8 +46,14 @@ struct ps_calibration_data {
- int sens_denom;
+ #include "hid-ids.h"
+
++/* List of connected playstation devices. */
++static DEFINE_MUTEX(ps_devices_lock);
++static LIST_HEAD(ps_devices_list);
++
+ #define HID_PLAYSTATION_VERSION_PATCH 0x8000
+
+ /* Base class for playstation devices. */
+ struct ps_device {
++ struct list_head list;
+ struct hid_device *hdev;
+ spinlock_t lock;
+
+@@ -160,6 +165,38 @@ static const struct {int x; int y; } ps_gamepad_hat_mapping[] = {
+ {0, 0},
};
-+/* Seed values for DualShock4 / DualSense CRC32 for different report types. */
-+#define PS_INPUT_CRC32_SEED 0xA1
-+#define PS_FEATURE_CRC32_SEED 0xA3
++/*
++ * Add a new ps_device to ps_devices if it doesn't exist.
++ * Return error on duplicate device, which can happen if the same
++ * device is connected using both Bluetooth and USB.
++ */
++static int ps_devices_list_add(struct ps_device *dev)
++{
++ struct ps_device *entry;
+
- #define DS_INPUT_REPORT_USB 0x01
- #define DS_INPUT_REPORT_USB_SIZE 64
-+#define DS_INPUT_REPORT_BT 0x31
-+#define DS_INPUT_REPORT_BT_SIZE 78
-
- #define DS_FEATURE_REPORT_CALIBRATION 0x05
- #define DS_FEATURE_REPORT_CALIBRATION_SIZE 41
-@@ -292,6 +299,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;
-@@ -342,6 +360,17 @@ static int ps_get_report(struct hid_device *hdev, uint8_t report_id, uint8_t *bu
- return -EINVAL;
- }
-
-+ if (hdev->bus == BUS_BLUETOOTH) {
-+ /* Last 4 bytes contains crc32. */
-+ uint8_t crc_offset = size - 4;
-+ uint32_t report_crc = get_unaligned_le32(&buf[crc_offset]);
-+
-+ if (!ps_check_crc32(PS_FEATURE_CRC32_SEED, buf, crc_offset, report_crc)) {
-+ hid_err(hdev, "CRC check failed for reportID=%d\n", report_id);
-+ return -EILSEQ;
++ mutex_lock(&ps_devices_lock);
++ list_for_each_entry(entry, &ps_devices_list, list) {
++ if (!memcmp(entry->mac_address, dev->mac_address, sizeof(dev->mac_address))) {
++ hid_err(dev->hdev, "Duplicate device found for MAC address %pMR.\n",
++ dev->mac_address);
++ mutex_unlock(&ps_devices_lock);
++ return -EEXIST;
+ }
+ }
+
- return 0;
++ list_add_tail(&dev->list, &ps_devices_list);
++ mutex_unlock(&ps_devices_lock);
++ return 0;
++}
++
++static int ps_devices_list_remove(struct ps_device *dev)
++{
++ mutex_lock(&ps_devices_lock);
++ list_del(&dev->list);
++ mutex_unlock(&ps_devices_lock);
++ return 0;
++}
++
+ static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix)
+ {
+ struct input_dev *input_dev;
+@@ -675,6 +712,10 @@ static struct ps_device *dualsense_create(struct hid_device *hdev)
+ }
+ snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds->base.mac_address);
+
++ ret = ps_devices_list_add(ps_dev);
++ if (ret)
++ return ERR_PTR(ret);
++
+ ret = dualsense_get_calibration_data(ds);
+ if (ret) {
+ hid_err(hdev, "Failed to get calibration data from DualSense\n");
+@@ -707,6 +748,7 @@ static struct ps_device *dualsense_create(struct hid_device *hdev)
+ return &ds->base;
+
+ err:
++ ps_devices_list_remove(ps_dev);
+ return ERR_PTR(ret);
}
-@@ -543,6 +572,17 @@ static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *r
- if (hdev->bus == BUS_USB && report->id == DS_INPUT_REPORT_USB &&
- size == DS_INPUT_REPORT_USB_SIZE) {
- ds_report = (struct dualsense_input_report *)&data[1];
-+ } else if (hdev->bus == BUS_BLUETOOTH && report->id == DS_INPUT_REPORT_BT &&
-+ size == DS_INPUT_REPORT_BT_SIZE) {
-+ /* Last 4 bytes of input report contain crc32 */
-+ uint32_t report_crc = get_unaligned_le32(&data[size - 4]);
+@@ -764,6 +806,10 @@ static int ps_probe(struct hid_device *hdev, const struct hid_device_id *id)
+
+ static void ps_remove(struct hid_device *hdev)
+ {
++ struct ps_device *dev = hid_get_drvdata(hdev);
+
-+ if (!ps_check_crc32(PS_INPUT_CRC32_SEED, data, size - 4, report_crc)) {
-+ hid_err(hdev, "DualSense input CRC's check failed\n");
-+ return -EILSEQ;
-+ }
++ ps_devices_list_remove(dev);
+
-+ ds_report = (struct dualsense_input_report *)&data[2];
- } else {
- hid_err(hdev, "Unhandled reportID=%d\n", report->id);
- return -1;
-@@ -806,6 +846,7 @@ static void ps_remove(struct hid_device *hdev)
+ hid_hw_close(hdev);
+ hid_hw_stop(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) },
- { }
- };
--
2.26.2