From: Roderick Colenbrander <roderick.colenbrander@xxxxxxxx> 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@xxxxxxxx> --- drivers/hid/hid-playstation.c | 58 +++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c index 384449e3095d..a55375ac79a9 100644 --- a/drivers/hid/hid-playstation.c +++ b/drivers/hid/hid-playstation.c @@ -20,12 +20,16 @@ static DEFINE_MUTEX(ps_devices_lock); static LIST_HEAD(ps_devices_list); +static DEFINE_IDA(ps_player_id_allocator); + /* Base class for playstation devices. */ struct ps_device { struct list_head list; struct hid_device *hdev; spinlock_t lock; + uint32_t player_id; + struct power_supply_desc battery_desc; struct power_supply *battery; uint8_t battery_capacity; @@ -288,6 +292,24 @@ static int ps_devices_list_remove(struct ps_device *dev) return 0; } +static int ps_device_set_player_id(struct ps_device *dev) +{ + int ret = ida_alloc(&ps_player_id_allocator, GFP_KERNEL); + + if (ret < 0) + return ret; + + dev->player_id = ret; + return 0; +} + +static void ps_device_release_player_id(struct ps_device *dev) +{ + ida_free(&ps_player_id_allocator, dev->player_id); + + dev->player_id = -1; +} + static struct input_dev *ps_allocate_input_dev(struct hid_device *hdev, const char *name_suffix) { struct input_dev *input_dev; @@ -837,8 +859,8 @@ static void dualsense_output_worker(struct work_struct *work) } if (ds->update_player_leds) { - r->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE; - r->player_leds = ds->player_leds_state; + common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE; + common->player_leds = ds->player_leds_state; ds->update_player_leds = false; } @@ -1084,6 +1106,28 @@ static int dualsense_reset_leds(struct dualsense *ds) return 0; } +static void dualsense_set_player_leds(struct dualsense *ds) +{ + /* 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. + */ + 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) + }; + + uint8_t player_id = ds->base.player_id % 5; + + 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; @@ -1188,6 +1232,15 @@ static struct ps_device *dualsense_create(struct hid_device *hdev) goto err; } + ret = ps_device_set_player_id((struct ps_device *)ds); + if (ret < 0) { + hid_err(hdev, "Failed to assign player id for DualSense\n"); + goto err; + } + + /* Set player LEDs to our player id. */ + dualsense_set_player_leds(ds); + return (struct ps_device *)ds; err: @@ -1256,6 +1309,7 @@ static void ps_remove(struct hid_device *hdev) struct ps_device *dev = hid_get_drvdata(hdev); ps_devices_list_remove(dev); + ps_device_release_player_id(dev); hid_hw_close(hdev); hid_hw_stop(hdev); -- 2.26.2