--- v3
+++ v6
@@ -1,149 +1,144 @@
From: Roderick Colenbrander <roderick.colenbrander@sony.com>
-Retrieve DualSense hardware and firmware information using a vendor
-specific feature report. Report the data through sysfs and also
-report using hid_info as there can be signficant differences between
-versions.
+Add a ID allocator to assign player ids to ps_device instances.
+Utilize the player id to set a default color on the DualSense its
+player LED strip.
Signed-off-by: Roderick Colenbrander <roderick.colenbrander@sony.com>
---
- drivers/hid/hid-playstation.c | 81 +++++++++++++++++++++++++++++++++++
- 1 file changed, 81 insertions(+)
+ drivers/hid/hid-playstation.c | 70 ++++++++++++++++++++++++++++++++++-
+ 1 file changed, 69 insertions(+), 1 deletion(-)
diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c
-index 1cfad1f40b4e..ea5d3e69687c 100644
+index 2d96785c397d..973c1fe61e8a 100644
--- a/drivers/hid/hid-playstation.c
+++ b/drivers/hid/hid-playstation.c
-@@ -41,6 +41,8 @@ struct ps_device {
- int battery_status;
+@@ -9,6 +9,7 @@
+ #include <linux/crc32.h>
+ #include <linux/device.h>
+ #include <linux/hid.h>
++#include <linux/idr.h>
+ #include <linux/input/mt.h>
+ #include <linux/leds.h>
+ #include <linux/led-class-multicolor.h>
+@@ -22,6 +23,8 @@
+ static DEFINE_MUTEX(ps_devices_lock);
+ static LIST_HEAD(ps_devices_list);
- uint8_t mac_address[6]; /* Note: stored in little endian order. */
-+ uint32_t hw_version;
-+ uint32_t fw_version;
++static DEFINE_IDA(ps_player_id_allocator);
++
+ #define HID_PLAYSTATION_VERSION_PATCH 0x8000
- int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size);
- };
-@@ -77,6 +79,8 @@ struct ps_led_info {
- #define DS_FEATURE_REPORT_CALIBRATION_SIZE 41
- #define DS_FEATURE_REPORT_PAIRING_INFO 0x09
- #define DS_FEATURE_REPORT_PAIRING_INFO_SIZE 20
-+#define DS_FEATURE_REPORT_FIRMWARE_INFO 0x20
-+#define DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE 64
+ /* Base class for playstation devices. */
+@@ -30,6 +33,8 @@ struct ps_device {
+ struct hid_device *hdev;
+ spinlock_t lock;
- /* Button masks for DualSense input report. */
- #define DS_BUTTONS0_HAT_SWITCH GENMASK(3, 0)
-@@ -649,6 +653,40 @@ static struct input_dev *ps_touchpad_create(struct hid_device *hdev, int width,
- return touchpad;
++ uint32_t player_id;
++
+ struct power_supply_desc battery_desc;
+ struct power_supply *battery;
+ uint8_t battery_capacity;
+@@ -321,6 +326,24 @@ static int ps_devices_list_remove(struct ps_device *dev)
+ return 0;
}
-+static ssize_t ps_show_firmware_version(struct device *dev,
-+ struct device_attribute
-+ *attr, char *buf)
++static int ps_device_set_player_id(struct ps_device *dev)
+{
-+ struct hid_device *hdev = to_hid_device(dev);
-+ struct ps_device *ps_dev = hid_get_drvdata(hdev);
++ int ret = ida_alloc(&ps_player_id_allocator, GFP_KERNEL);
+
-+ return sysfs_emit(buf, "0x%08x\n", ps_dev->fw_version);
++ if (ret < 0)
++ return ret;
++
++ dev->player_id = ret;
++ return 0;
+}
+
-+static DEVICE_ATTR(firmware_version, 0444, ps_show_firmware_version, NULL);
++static void ps_device_release_player_id(struct ps_device *dev)
++{
++ ida_free(&ps_player_id_allocator, dev->player_id);
+
-+static ssize_t ps_show_hardware_version(struct device *dev,
-+ struct device_attribute
-+ *attr, char *buf)
-+{
-+ struct hid_device *hdev = to_hid_device(dev);
-+ struct ps_device *ps_dev = hid_get_drvdata(hdev);
-+
-+ return sysfs_emit(buf, "0x%08x\n", ps_dev->hw_version);
++ dev->player_id = U32_MAX;
+}
+
-+static DEVICE_ATTR(hardware_version, 0444, ps_show_hardware_version, NULL);
-+
-+static struct attribute *ps_device_attributes[] = {
-+ &dev_attr_firmware_version.attr,
-+ &dev_attr_hardware_version.attr,
-+ NULL
-+};
-+
-+static const struct attribute_group ps_device_attribute_group = {
-+ .attrs = ps_device_attributes,
-+};
-+
- static int dualsense_get_calibration_data(struct dualsense *ds)
+ static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix)
{
- short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
-@@ -739,6 +777,30 @@ static int dualsense_get_calibration_data(struct dualsense *ds)
- return ret;
+ struct input_dev *input_dev;
+@@ -1156,6 +1179,29 @@ static int dualsense_reset_leds(struct dualsense *ds)
+ return 0;
}
-+static int dualsense_get_firmware_info(struct dualsense *ds)
++static void dualsense_set_player_leds(struct dualsense *ds)
+{
-+ uint8_t *buf;
-+ int ret;
++ /*
++ * The DualSense controller has a row of 5 LEDs used for player ids.
++ * Behavior on the PlayStation 5 console is to center the player id
++ * across the LEDs, so e.g. player 1 would be "--x--" with x being 'on'.
++ * Follow a similar mapping here.
++ */
++ static const int player_ids[5] = {
++ BIT(2),
++ BIT(3) | BIT(1),
++ BIT(4) | BIT(2) | BIT(0),
++ BIT(4) | BIT(3) | BIT(1) | BIT(0),
++ BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0)
++ };
+
-+ buf = kzalloc(DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, GFP_KERNEL);
-+ if (!buf)
-+ return -ENOMEM;
++ uint8_t player_id = ds->base.player_id % ARRAY_SIZE(player_ids);
+
-+ ret = ps_get_report(ds->base.hdev, DS_FEATURE_REPORT_FIRMWARE_INFO, buf,
-+ DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE);
++ ds->update_player_leds = true;
++ ds->player_leds_state = player_ids[player_id];
++ schedule_work(&ds->output_worker);
++}
++
+ static struct ps_device *dualsense_create(struct hid_device *hdev)
+ {
+ struct dualsense *ds;
+@@ -1264,6 +1310,15 @@ static struct ps_device *dualsense_create(struct hid_device *hdev)
+ goto err;
+ }
+
++ ret = ps_device_set_player_id(ps_dev);
+ if (ret) {
-+ hid_err(ds->base.hdev, "Failed to retrieve DualSense firmware info: %d\n", ret);
-+ goto err_free;
++ hid_err(hdev, "Failed to assign player id for DualSense: %d\n", ret);
++ goto err;
+ }
+
-+ ds->base.hw_version = get_unaligned_le32(&buf[24]);
-+ ds->base.fw_version = get_unaligned_le32(&buf[28]);
-+
-+err_free:
-+ kfree(buf);
-+ return ret;
-+}
-+
- static int dualsense_get_mac_address(struct dualsense *ds)
- {
- uint8_t *buf;
-@@ -1240,6 +1302,12 @@ static struct ps_device *dualsense_create(struct hid_device *hdev)
- }
- snprintf(hdev->uniq, sizeof(hdev->uniq), "%pMR", ds->base.mac_address);
-
-+ ret = dualsense_get_firmware_info(ds);
-+ if (ret) {
-+ hid_err(hdev, "Failed to get firmware info from DualSense\n");
-+ return ERR_PTR(ret);
-+ }
-+
- ret = ps_devices_list_add(ps_dev);
- if (ret)
- return ERR_PTR(ret);
-@@ -1307,6 +1375,13 @@ static struct ps_device *dualsense_create(struct hid_device *hdev)
- /* Set player LEDs to our player id. */
- dualsense_set_player_leds(ds);
-
-+ /*
-+ * Reporting hardware and firmware is important as there are frequent updates, which
-+ * can change behavior.
-+ */
-+ hid_info(hdev, "Registered DualSense controller hw_version=0x%08x fw_version=0x%08x\n",
-+ ds->base.hw_version, ds->base.fw_version);
++ /* Set player LEDs to our player id. */
++ dualsense_set_player_leds(ds);
+
return &ds->base;
err:
-@@ -1357,6 +1432,12 @@ static int ps_probe(struct hid_device *hdev, const struct hid_device_id *id)
- }
- }
+@@ -1328,6 +1383,7 @@ static void ps_remove(struct hid_device *hdev)
+ struct ps_device *dev = hid_get_drvdata(hdev);
-+ ret = devm_device_add_group(&hdev->dev, &ps_device_attribute_group);
-+ if (ret) {
-+ hid_err(hdev, "Failed to register sysfs nodes.\n");
-+ goto err_close;
-+ }
+ ps_devices_list_remove(dev);
++ ps_device_release_player_id(dev);
+
+ hid_hw_close(hdev);
+ hid_hw_stop(hdev);
+@@ -1348,7 +1404,19 @@ static struct hid_driver ps_driver = {
+ .raw_event = ps_raw_event,
+ };
+
+-module_hid_driver(ps_driver);
++static int __init ps_init(void)
++{
++ return hid_register_driver(&ps_driver);
++}
+
- return ret;
++static void __exit ps_exit(void)
++{
++ hid_unregister_driver(&ps_driver);
++ ida_destroy(&ps_player_id_allocator);
++}
++
++module_init(ps_init);
++module_exit(ps_exit);
- err_close:
+ MODULE_AUTHOR("Sony Interactive Entertainment");
+ MODULE_DESCRIPTION("HID Driver for PlayStation peripherals.");
--
2.26.2