Message ID | 20201219062336.72568-10-roderick@gaikai.com (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | Jiri Kosina |
Headers | show |
Series | HID: new driver for PS5 'DualSense' controller | expand |
Hi 2020. december 19., szombat 7:23 keltezéssel, Roderick Colenbrander írta: > [...] > diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c > index 0b62bcb28d8a..f8cf82a27d43 100644 > [...] > +/* Create a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */ > +static struct led_classdev_mc *ps_lightbar_create(struct ps_device *ps_dev, > + int (*brightness_set)(struct led_classdev *, enum led_brightness)) > +{ > + struct hid_device *hdev = ps_dev->hdev; > + struct led_classdev_mc *lightbar_mc_dev; > + struct mc_subled *mc_led_info; > + struct led_classdev *led_cdev; > + int ret; > + > + lightbar_mc_dev = devm_kzalloc(&hdev->dev, sizeof(*lightbar_mc_dev), GFP_KERNEL); > + if (!lightbar_mc_dev) > + return ERR_PTR(-ENOMEM); > + > + mc_led_info = devm_kzalloc(&hdev->dev, 3*sizeof(*mc_led_info), GFP_KERNEL); > + if (!mc_led_info) > + return ERR_PTR(-ENOMEM); > + Is there any reason why these are dynamically allocated? > + 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); I guess the double colons are used because the MAC address has ':' in it; but as far as I know this doesn't follow the naming scheme for LED devices, so I'm not sure if this is the best way to go about it. > + 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 ERR_PTR(ret); > + } > + > + return lightbar_mc_dev; > +} > [...] > +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 = 2; /* Fade light out. */ Maybe it'd be better to name that '2'? > + dualsense_send_output_report(ds, &report); > + > + kfree(buf); > + return 0; > +} > [...] Regards, Barnabás Pőcze
Hi Barnabás, Thanks for your review. On Sun, Dec 27, 2020 at 6:41 AM Barnabás Pőcze <pobrn@protonmail.com> wrote: > > Hi > > > 2020. december 19., szombat 7:23 keltezéssel, Roderick Colenbrander írta: > > > [...] > > diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c > > index 0b62bcb28d8a..f8cf82a27d43 100644 > > [...] > > +/* Create a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */ > > +static struct led_classdev_mc *ps_lightbar_create(struct ps_device *ps_dev, > > + int (*brightness_set)(struct led_classdev *, enum led_brightness)) > > +{ > > + struct hid_device *hdev = ps_dev->hdev; > > + struct led_classdev_mc *lightbar_mc_dev; > > + struct mc_subled *mc_led_info; > > + struct led_classdev *led_cdev; > > + int ret; > > + > > + lightbar_mc_dev = devm_kzalloc(&hdev->dev, sizeof(*lightbar_mc_dev), GFP_KERNEL); > > + if (!lightbar_mc_dev) > > + return ERR_PTR(-ENOMEM); > > + > > + mc_led_info = devm_kzalloc(&hdev->dev, 3*sizeof(*mc_led_info), GFP_KERNEL); > > + if (!mc_led_info) > > + return ERR_PTR(-ENOMEM); > > + > > Is there any reason why these are dynamically allocated? No particular reason. I should probably at least not dynamically allocate 'mc_dev' and pass it in similar to regular LED registration (previously I had my regular LEDs dynamically allocated). The mc_led_info I will probably keep dynamic. It feels a bit nasty to have the caller be aware of these internal details. > > > > + 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); > > I guess the double colons are used because the MAC address has ':' in it; but > as far as I know this doesn't follow the naming scheme for LED devices, so I'm > not sure if this is the best way to go about it. Actually it was Benjamin who suggested this type of naming. He wasn't a fan of the previous hid-sony device naming (neither was I). This was the main idea so far. > > > + 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 ERR_PTR(ret); > > + } > > + > > + return lightbar_mc_dev; > > +} > > [...] > > +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 = 2; /* Fade light out. */ > > Maybe it'd be better to name that '2'? Will document this one. > > > > + dualsense_send_output_report(ds, &report); > > + > > + kfree(buf); > > + return 0; > > +} > > [...] > > > Regards, > Barnabás Pőcze Thanks, Roderick
2020. december 28., hétfő 22:26 keltezéssel, Roderick Colenbrander írta: > [...] > > > +/* Create a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */ > > > +static struct led_classdev_mc *ps_lightbar_create(struct ps_device *ps_dev, > > > + int (*brightness_set)(struct led_classdev *, enum led_brightness)) > > > +{ > > > + struct hid_device *hdev = ps_dev->hdev; > > > + struct led_classdev_mc *lightbar_mc_dev; > > > + struct mc_subled *mc_led_info; > > > + struct led_classdev *led_cdev; > > > + int ret; > > > + > > > + lightbar_mc_dev = devm_kzalloc(&hdev->dev, sizeof(*lightbar_mc_dev), GFP_KERNEL); > > > + if (!lightbar_mc_dev) > > > + return ERR_PTR(-ENOMEM); > > > + > > > + mc_led_info = devm_kzalloc(&hdev->dev, 3*sizeof(*mc_led_info), GFP_KERNEL); > > > + if (!mc_led_info) > > > + return ERR_PTR(-ENOMEM); > > > + > > > > Is there any reason why these are dynamically allocated? > > No particular reason. I should probably at least not dynamically > allocate 'mc_dev' and pass it in similar to regular LED registration > (previously I had my regular LEDs dynamically allocated). The > mc_led_info I will probably keep dynamic. It feels a bit nasty to have > the caller be aware of these internal details. > [...] Could you please elaborate what you mean by "It feels a bit nasty to have the caller be aware of these internal details."? I don't think I fully understand what you're referring to.
On Tue, Dec 29, 2020 at 10:59 AM Barnabás Pőcze <pobrn@protonmail.com> wrote: > > 2020. december 28., hétfő 22:26 keltezéssel, Roderick Colenbrander írta: > > > [...] > > > > +/* Create a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */ > > > > +static struct led_classdev_mc *ps_lightbar_create(struct ps_device *ps_dev, > > > > + int (*brightness_set)(struct led_classdev *, enum led_brightness)) > > > > +{ > > > > + struct hid_device *hdev = ps_dev->hdev; > > > > + struct led_classdev_mc *lightbar_mc_dev; > > > > + struct mc_subled *mc_led_info; > > > > + struct led_classdev *led_cdev; > > > > + int ret; > > > > + > > > > + lightbar_mc_dev = devm_kzalloc(&hdev->dev, sizeof(*lightbar_mc_dev), GFP_KERNEL); > > > > + if (!lightbar_mc_dev) > > > > + return ERR_PTR(-ENOMEM); > > > > + > > > > + mc_led_info = devm_kzalloc(&hdev->dev, 3*sizeof(*mc_led_info), GFP_KERNEL); > > > > + if (!mc_led_info) > > > > + return ERR_PTR(-ENOMEM); > > > > + > > > > > > Is there any reason why these are dynamically allocated? > > > > No particular reason. I should probably at least not dynamically > > allocate 'mc_dev' and pass it in similar to regular LED registration > > (previously I had my regular LEDs dynamically allocated). The > > mc_led_info I will probably keep dynamic. It feels a bit nasty to have > > the caller be aware of these internal details. > > [...] > > Could you please elaborate what you mean by "It feels a bit nasty to have > the caller be aware of these internal details."? I don't think I fully understand > what you're referring to. > Maybe I misunderstood your original comment. The question was why both 'lightbar_mc_dev' and 'mc_led_info' were dynamically allocated. I interpreted it as getting rid of some of the dynamic allocation as some wasn't needed, but not sure what you had in mind. The code now looks like: struct dualsense { ... /* RGB lightbar */ struct led_classdev_mc lightbar; (not a pointer anymore) } 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; ... In here I kept 'mc_led_info' as dynamically allocated. I didn't think it made sense to have the caller know about it. What was your original idea? Regards, Roderick
2020. december 29., kedd 20:54 keltezéssel, Roderick Colenbrander írta: > On Tue, Dec 29, 2020 at 10:59 AM Barnabás Pőcze <pobrn@protonmail.com> wrote: > > > > 2020. december 28., hétfő 22:26 keltezéssel, Roderick Colenbrander írta: > > > > > [...] > > > > > +/* Create a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */ > > > > > +static struct led_classdev_mc *ps_lightbar_create(struct ps_device *ps_dev, > > > > > + int (*brightness_set)(struct led_classdev *, enum led_brightness)) > > > > > +{ > > > > > + struct hid_device *hdev = ps_dev->hdev; > > > > > + struct led_classdev_mc *lightbar_mc_dev; > > > > > + struct mc_subled *mc_led_info; > > > > > + struct led_classdev *led_cdev; > > > > > + int ret; > > > > > + > > > > > + lightbar_mc_dev = devm_kzalloc(&hdev->dev, sizeof(*lightbar_mc_dev), GFP_KERNEL); > > > > > + if (!lightbar_mc_dev) > > > > > + return ERR_PTR(-ENOMEM); > > > > > + > > > > > + mc_led_info = devm_kzalloc(&hdev->dev, 3*sizeof(*mc_led_info), GFP_KERNEL); > > > > > + if (!mc_led_info) > > > > > + return ERR_PTR(-ENOMEM); > > > > > + > > > > > > > > Is there any reason why these are dynamically allocated? > > > > > > No particular reason. I should probably at least not dynamically > > > allocate 'mc_dev' and pass it in similar to regular LED registration > > > (previously I had my regular LEDs dynamically allocated). The > > > mc_led_info I will probably keep dynamic. It feels a bit nasty to have > > > the caller be aware of these internal details. > > > [...] > > > > Could you please elaborate what you mean by "It feels a bit nasty to have > > the caller be aware of these internal details."? I don't think I fully understand > > what you're referring to. > > > > Maybe I misunderstood your original comment. The question was why both > 'lightbar_mc_dev' and 'mc_led_info' were dynamically allocated. I > interpreted it as getting rid of some of the dynamic allocation as > some wasn't needed, but not sure what you had in mind. The code now > looks like: > > struct dualsense { > ... > /* RGB lightbar */ > struct led_classdev_mc lightbar; (not a pointer anymore) > } > > 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; > ... > > In here I kept 'mc_led_info' as dynamically allocated. I didn't think > it made sense to have the caller know about it. What was your original > idea? > [...] Thanks, I thought you meant something different, but this clears it up. By the way, my original idea is really the simplest: have a `struct mc_subled[3]` in the dualsense struct in addition to the multicolor LED, thus no dynamic allocation (apart from allocating the dualsense struct) is necessary.
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 0b62bcb28d8a..f8cf82a27d43 100644 --- a/drivers/hid/hid-playstation.c +++ b/drivers/hid/hid-playstation.c @@ -8,6 +8,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 <linux/crc32.h> #include <asm/unaligned.h> @@ -77,6 +78,9 @@ 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) /* DualSense hardware limits */ #define DS_ACC_RES_PER_G 8192 @@ -106,6 +110,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. */ @@ -396,6 +407,50 @@ static struct input_dev *ps_gamepad_create(struct hid_device *hdev, return gamepad; } +/* Create a DualSense/DualShock4 RGB lightbar represented by a multicolor LED. */ +static struct led_classdev_mc *ps_lightbar_create(struct ps_device *ps_dev, + int (*brightness_set)(struct led_classdev *, enum led_brightness)) +{ + struct hid_device *hdev = ps_dev->hdev; + struct led_classdev_mc *lightbar_mc_dev; + struct mc_subled *mc_led_info; + struct led_classdev *led_cdev; + int ret; + + lightbar_mc_dev = devm_kzalloc(&hdev->dev, sizeof(*lightbar_mc_dev), GFP_KERNEL); + if (!lightbar_mc_dev) + return ERR_PTR(-ENOMEM); + + mc_led_info = devm_kzalloc(&hdev->dev, 3*sizeof(*mc_led_info), GFP_KERNEL); + if (!mc_led_info) + return ERR_PTR(-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 ERR_PTR(ret); + } + + return lightbar_mc_dev; +} + static struct input_dev *ps_sensors_create(struct hid_device *hdev, int accel_range, int accel_res, int gyro_range, int gyro_res) { @@ -580,6 +635,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) { @@ -661,6 +737,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); @@ -841,6 +926,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 = 2; /* 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; @@ -908,6 +1017,21 @@ 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; + + ds->lightbar = ps_lightbar_create((struct ps_device *)ds, + dualsense_lightbar_set_brightness); + if (IS_ERR(ds->lightbar)) { + ret = PTR_ERR(ds->lightbar); + goto err; + } + return (struct ps_device *)ds; err: