diff mbox series

[12/13] HID: playstation: DualSense set LEDs to default player id.

Message ID 20201219062336.72568-13-roderick@gaikai.com (mailing list archive)
State Superseded
Delegated to: Jiri Kosina
Headers show
Series HID: new driver for PS5 'DualSense' controller | expand

Commit Message

Roderick Colenbrander Dec. 19, 2020, 6:23 a.m. UTC
From: Roderick Colenbrander <roderick.colenbrander@sony.com>

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 | 58 +++++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 2 deletions(-)

Comments

Samuel Čavoj Dec. 27, 2020, 12:08 a.m. UTC | #1
Hi Roderick,

this one is very much just a nitpick, yet not completely pointless.

On 18.12.2020 22:23, Roderick Colenbrander wrote:
> @@ -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;

This change should be merged into the previous patch, as it doesn't
compile without it (typo, I suppose). Could lead to annoyment during
git bisect and all that.

Regards,
Samuel
Roderick Colenbrander Dec. 27, 2020, 11:07 p.m. UTC | #2
On Sat, Dec 26, 2020 at 4:08 PM Samuel Čavoj <sammko@sammserver.com> wrote:
>
> Hi Roderick,
>
> this one is very much just a nitpick, yet not completely pointless.
>
> On 18.12.2020 22:23, Roderick Colenbrander wrote:
> > @@ -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;
>
> This change should be merged into the previous patch, as it doesn't
> compile without it (typo, I suppose). Could lead to annoyment during
> git bisect and all that.
>

Yep that doesn't belong here. Rebase artifact and I didn't compile
that particular patch after the fact apparently... Will run a 'rebase
-x' with a compile script on the next revision to make sure nothing
got messed up...

Thanks,
Roderick
diff mbox series

Patch

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);