diff mbox series

[v2,09/13] HID: playstation: add DualSense lightbar support

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

Commit Message

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

Expose the DualSense its RGB lightbar using the new multicolor LED
framework.

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

Comments

Barnabás Pőcze Jan. 7, 2021, 9:01 p.m. UTC | #1
2021. január 2., szombat 23:31 keltezéssel, Roderick Colenbrander írta:

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

>

> Expose the DualSense its RGB lightbar using the new multicolor LED

> framework.

>

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

> [...]

> +/* Register a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */

> +static int ps_lightbar_register(struct ps_device *ps_dev, struct led_classdev_mc *lightbar_mc_dev,

> +	int (*brightness_set)(struct led_classdev *, enum led_brightness))

> +{

> +	struct hid_device *hdev = ps_dev->hdev;

> +	struct mc_subled *mc_led_info;

> +	struct led_classdev *led_cdev;

> +	int ret;

> +

> +	mc_led_info = devm_kzalloc(&hdev->dev, 3*sizeof(*mc_led_info), GFP_KERNEL);


I believe `devm_kmalloc_array()` would be more appropriate. Have you looked at it?


> +	if (!mc_led_info)

> +		return -ENOMEM;

> +

> +	mc_led_info[0].color_index = LED_COLOR_ID_RED;

> +	mc_led_info[0].channel = 0;

> +	mc_led_info[1].color_index = LED_COLOR_ID_GREEN;

> +	mc_led_info[1].channel = 1;

> +	mc_led_info[2].color_index = LED_COLOR_ID_BLUE;

> +	mc_led_info[2].channel = 2;


Just a small note, as far as I'm aware, the `channel` member is not used by
multicolor LED functions, and it's not even used in this module as far as I see.


> +

> +	lightbar_mc_dev->subled_info = mc_led_info;

> +	lightbar_mc_dev->num_colors = 3;

> +

> +	led_cdev = &lightbar_mc_dev->led_cdev;

> +	led_cdev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "playstation::%pMR::rgb",

> +			ps_dev->mac_address);

> +	led_cdev->brightness = 255;

> +	led_cdev->max_brightness = 255;

> +	led_cdev->brightness_set_blocking = brightness_set;

> +

> +	ret = devm_led_classdev_multicolor_register(&hdev->dev, lightbar_mc_dev);

> +	if (ret < 0) {

> +		hid_err(hdev, "Cannot register multicolor LED device\n");

> +		return ret;

> +	}

> +

> +	return 0;

> +}

> +

>  static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res,

>  		int gyro_range, int gyro_res)

>  {

> @@ -601,6 +652,27 @@  static int dualsense_get_mac_address(struct dualsense *ds)

>  	return ret;

>  }

>

> +static int dualsense_lightbar_set_brightness(struct led_classdev *cdev,

> +	enum led_brightness brightness)

> +{

> +	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);

> +	struct hid_device *hdev = to_hid_device(cdev->dev->parent);

> +	struct dualsense *ds = hid_get_drvdata(hdev);


I think the previous two lines could be replaced with

```
struct dualsense *ds = container_of(mc_cdev, struct dualsense, lightbar);
```


> +	unsigned long flags;

> +

> +	led_mc_calc_color_components(mc_cdev, brightness);

> +

> +	spin_lock_irqsave(&ds->base.lock, flags);

> +	ds->update_lightbar = true;

> +	ds->lightbar_red = mc_cdev->subled_info[0].brightness;

> +	ds->lightbar_green = mc_cdev->subled_info[1].brightness;

> +	ds->lightbar_blue = mc_cdev->subled_info[2].brightness;

> +	spin_unlock_irqrestore(&ds->base.lock, flags);

> +

> +	schedule_work(&ds->output_worker);

> +	return 0;

> +}

> +

>  static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp,

>  		void *buf)

>  {

> @@ -682,6 +754,15 @@  static void dualsense_output_worker(struct work_struct *work)

>  		ds->update_rumble = false;

>  	}

>

> +	if (ds->update_lightbar) {

> +		common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE;

> +		common->lightbar_red = ds->lightbar_red;

> +		common->lightbar_green = ds->lightbar_green;

> +		common->lightbar_blue = ds->lightbar_blue;

> +

> +		ds->update_lightbar = false;

> +	}

> +

>  	spin_unlock_irqrestore(&ds->base.lock, flags);

>

>  	dualsense_send_output_report(ds, &report);

> @@ -861,6 +942,30 @@  static int dualsense_play_effect(struct input_dev *dev, void *data, struct ff_ef

>  	return 0;

>  }

> [...]

>  static struct ps_device *dualsense_create(struct hid_device *hdev)

>  {

>  	struct dualsense *ds;

> @@ -930,6 +1035,18 @@  static struct ps_device *dualsense_create(struct hid_device *hdev)

>  	if (ret < 0)

>  		goto err;

>

> +	/* The hardware may have control over the LEDs (e.g. in Bluetooth on startup).

> +	 * Reset the LEDs (lightbar, mute, player leds), so we can control them

> +	 * from software.

> +	 */

> +	ret = dualsense_reset_leds(ds);

> +	if (ret < 0)


I believe if `dualsense_reset_leds()` can only return 0 on success, or an errno,
then `if (ret)` would be better.


> +		goto err;

> +

> +	ret = ps_lightbar_register(ps_dev, &ds->lightbar, dualsense_lightbar_set_brightness);

> +	if (ret < 0)

> [...]


Same here.


Regards,
Barnabás Pőcze
diff mbox series

Patch

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index e6c67aaa1a1a..c80c81916f4a 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -857,6 +857,7 @@  config HID_PLAYSTATION
 	tristate "PlayStation HID Driver"
 	default !EXPERT
 	depends on HID
+	select LEDS_CLASS_MULTICOLOR
 	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 36a904b2f93f..3b26445acbb9 100644
--- a/drivers/hid/hid-playstation.c
+++ b/drivers/hid/hid-playstation.c
@@ -10,6 +10,7 @@ 
 #include <linux/device.h>
 #include <linux/hid.h>
 #include <linux/input/mt.h>
+#include <linux/led-class-multicolor.h>
 #include <linux/module.h>
 
 #include <asm/unaligned.h>
@@ -87,6 +88,10 @@  struct ps_calibration_data {
 /* Flags for DualSense output report. */
 #define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0)
 #define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1)
+#define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2)
+#define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3)
+#define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1)
+#define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1)
 
 /* DualSense hardware limits */
 #define DS_ACC_RES_PER_G	8192
@@ -116,6 +121,13 @@  struct dualsense {
 	uint8_t motor_left;
 	uint8_t motor_right;
 
+	/* RGB lightbar */
+	struct led_classdev_mc lightbar;
+	bool update_lightbar;
+	uint8_t lightbar_red;
+	uint8_t lightbar_green;
+	uint8_t lightbar_blue;
+
 	struct work_struct output_worker;
 	void *output_report_dmabuf;
 	uint8_t output_seq; /* Sequence number for output report. */
@@ -407,6 +419,45 @@  static struct input_dev *ps_gamepad_create(struct hid_device *hdev,
 	return gamepad;
 }
 
+/* Register a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */
+static int ps_lightbar_register(struct ps_device *ps_dev, struct led_classdev_mc *lightbar_mc_dev,
+	int (*brightness_set)(struct led_classdev *, enum led_brightness))
+{
+	struct hid_device *hdev = ps_dev->hdev;
+	struct mc_subled *mc_led_info;
+	struct led_classdev *led_cdev;
+	int ret;
+
+	mc_led_info = devm_kzalloc(&hdev->dev, 3*sizeof(*mc_led_info), GFP_KERNEL);
+	if (!mc_led_info)
+		return -ENOMEM;
+
+	mc_led_info[0].color_index = LED_COLOR_ID_RED;
+	mc_led_info[0].channel = 0;
+	mc_led_info[1].color_index = LED_COLOR_ID_GREEN;
+	mc_led_info[1].channel = 1;
+	mc_led_info[2].color_index = LED_COLOR_ID_BLUE;
+	mc_led_info[2].channel = 2;
+
+	lightbar_mc_dev->subled_info = mc_led_info;
+	lightbar_mc_dev->num_colors = 3;
+
+	led_cdev = &lightbar_mc_dev->led_cdev;
+	led_cdev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "playstation::%pMR::rgb",
+			ps_dev->mac_address);
+	led_cdev->brightness = 255;
+	led_cdev->max_brightness = 255;
+	led_cdev->brightness_set_blocking = brightness_set;
+
+	ret = devm_led_classdev_multicolor_register(&hdev->dev, lightbar_mc_dev);
+	if (ret < 0) {
+		hid_err(hdev, "Cannot register multicolor LED device\n");
+		return ret;
+	}
+
+	return 0;
+}
+
 static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res,
 		int gyro_range, int gyro_res)
 {
@@ -601,6 +652,27 @@  static int dualsense_get_mac_address(struct dualsense *ds)
 	return ret;
 }
 
+static int dualsense_lightbar_set_brightness(struct led_classdev *cdev,
+	enum led_brightness brightness)
+{
+	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
+	struct hid_device *hdev = to_hid_device(cdev->dev->parent);
+	struct dualsense *ds = hid_get_drvdata(hdev);
+	unsigned long flags;
+
+	led_mc_calc_color_components(mc_cdev, brightness);
+
+	spin_lock_irqsave(&ds->base.lock, flags);
+	ds->update_lightbar = true;
+	ds->lightbar_red = mc_cdev->subled_info[0].brightness;
+	ds->lightbar_green = mc_cdev->subled_info[1].brightness;
+	ds->lightbar_blue = mc_cdev->subled_info[2].brightness;
+	spin_unlock_irqrestore(&ds->base.lock, flags);
+
+	schedule_work(&ds->output_worker);
+	return 0;
+}
+
 static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp,
 		void *buf)
 {
@@ -682,6 +754,15 @@  static void dualsense_output_worker(struct work_struct *work)
 		ds->update_rumble = false;
 	}
 
+	if (ds->update_lightbar) {
+		common->valid_flag1 |= DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE;
+		common->lightbar_red = ds->lightbar_red;
+		common->lightbar_green = ds->lightbar_green;
+		common->lightbar_blue = ds->lightbar_blue;
+
+		ds->update_lightbar = false;
+	}
+
 	spin_unlock_irqrestore(&ds->base.lock, flags);
 
 	dualsense_send_output_report(ds, &report);
@@ -861,6 +942,30 @@  static int dualsense_play_effect(struct input_dev *dev, void *data, struct ff_ef
 	return 0;
 }
 
+static int dualsense_reset_leds(struct dualsense *ds)
+{
+	struct dualsense_output_report report;
+	uint8_t *buf;
+
+	buf = kzalloc(sizeof(struct dualsense_output_report_bt), GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	dualsense_init_output_report(ds, &report, buf);
+	/* On Bluetooth the DualSense outputs an animation on the lightbar
+	 * during startup and maintains a color afterwards. We need to explicitly
+	 * reconfigure the lightbar before we can do any programming later on.
+	 * In USB the lightbar is not on by default, but redoing the setup there
+	 * doesn't hurt.
+	 */
+	report.common->valid_flag2 = DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE;
+	report.common->lightbar_setup = DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT; /* Fade light out. */
+	dualsense_send_output_report(ds, &report);
+
+	kfree(buf);
+	return 0;
+}
+
 static struct ps_device *dualsense_create(struct hid_device *hdev)
 {
 	struct dualsense *ds;
@@ -930,6 +1035,18 @@  static struct ps_device *dualsense_create(struct hid_device *hdev)
 	if (ret < 0)
 		goto err;
 
+	/* The hardware may have control over the LEDs (e.g. in Bluetooth on startup).
+	 * Reset the LEDs (lightbar, mute, player leds), so we can control them
+	 * from software.
+	 */
+	ret = dualsense_reset_leds(ds);
+	if (ret < 0)
+		goto err;
+
+	ret = ps_lightbar_register(ps_dev, &ds->lightbar, dualsense_lightbar_set_brightness);
+	if (ret < 0)
+		goto err;
+
 	return &ds->base;
 
 err: