diff options
author | Roderick Colenbrander <roderick@gaikai.com> | 2022-10-29 20:48:46 +0200 |
---|---|---|
committer | Jiri Kosina <jkosina@suse.cz> | 2022-11-11 11:07:09 +0100 |
commit | 4521109a8f40621ced642d62f1e887bd6b2d1946 (patch) | |
tree | 1e1050f78dfe1f1bb73ccdc809c241e27653a8db /drivers/hid | |
parent | HID: playstation: make LED brightness adjustable in ps_led_register. (diff) | |
download | linux-4521109a8f40621ced642d62f1e887bd6b2d1946.tar.xz linux-4521109a8f40621ced642d62f1e887bd6b2d1946.zip |
HID: playstation: support DualShock4 lightbar.
Expose the lightbar LEDs in the same manner as hid-sony through
individual LEDs for backwards compatibility reasons. There is a
slight change in LED naming to use the input device name as opposed
to the MAC address like hid-sony did. This is expected to not
cause any issues and should make the naming more compliant.
In addition set a default lightbar color based on player ID.
Signed-off-by: Roderick Colenbrander <roderick.colenbrander@sony.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Diffstat (limited to 'drivers/hid')
-rw-r--r-- | drivers/hid/hid-playstation.c | 141 |
1 files changed, 138 insertions, 3 deletions
diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c index 662c6f220571..d42fda13580a 100644 --- a/drivers/hid/hid-playstation.c +++ b/drivers/hid/hid-playstation.c @@ -311,6 +311,8 @@ struct dualsense_output_report { /* Flags for DualShock4 output report. */ #define DS4_OUTPUT_VALID_FLAG0_MOTOR 0x01 +#define DS4_OUTPUT_VALID_FLAG0_LED 0x02 +#define DS4_OUTPUT_VALID_FLAG0_LED_BLINK 0x04 /* DualShock4 hardware limits */ #define DS4_ACC_RES_PER_G 8192 @@ -339,6 +341,14 @@ struct dualshock4 { uint8_t motor_left; uint8_t motor_right; + /* Lightbar leds */ + bool update_lightbar; + bool lightbar_enabled; /* For use by global LED control. */ + uint8_t lightbar_red; + uint8_t lightbar_green; + uint8_t lightbar_blue; + struct led_classdev lightbar_leds[4]; + struct work_struct output_worker; bool output_worker_initialized; void *output_report_dmabuf; @@ -697,8 +707,14 @@ static int ps_led_register(struct ps_device *ps_dev, struct led_classdev *led, { int ret; - led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL, - "%s:%s:%s", ps_dev->input_dev_name, led_info->color, led_info->name); + if (led_info->name) { + led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL, + "%s:%s:%s", ps_dev->input_dev_name, led_info->color, led_info->name); + } else { + /* Backwards compatible mode for hid-sony, but not compliant with LED class spec. */ + led->name = devm_kasprintf(&ps_dev->hdev->dev, GFP_KERNEL, + "%s:%s", ps_dev->input_dev_name, led_info->color); + } if (!led->name) return -ENOMEM; @@ -1746,6 +1762,60 @@ err_free: return ret; } +static enum led_brightness dualshock4_led_get_brightness(struct led_classdev *led) +{ + struct hid_device *hdev = to_hid_device(led->dev->parent); + struct dualshock4 *ds4 = hid_get_drvdata(hdev); + unsigned int led_index; + + led_index = led - ds4->lightbar_leds; + switch (led_index) { + case 0: + return ds4->lightbar_red; + case 1: + return ds4->lightbar_green; + case 2: + return ds4->lightbar_blue; + case 3: + return ds4->lightbar_enabled; + } + + return -1; +} + +static int dualshock4_led_set_brightness(struct led_classdev *led, enum led_brightness value) +{ + struct hid_device *hdev = to_hid_device(led->dev->parent); + struct dualshock4 *ds4 = hid_get_drvdata(hdev); + unsigned long flags; + unsigned int led_index; + + spin_lock_irqsave(&ds4->base.lock, flags); + + led_index = led - ds4->lightbar_leds; + switch (led_index) { + case 0: + ds4->lightbar_red = value; + break; + case 1: + ds4->lightbar_green = value; + break; + case 2: + ds4->lightbar_blue = value; + break; + case 3: + ds4->lightbar_enabled = !!value; + } + + ds4->update_lightbar = true; + + spin_unlock_irqrestore(&ds4->base.lock, flags); + + dualshock4_schedule_work(ds4); + + return 0; +} + static void dualshock4_init_output_report(struct dualshock4 *ds4, struct dualshock4_output_report *rp, void *buf) { @@ -1784,6 +1854,18 @@ static void dualshock4_output_worker(struct work_struct *work) ds4->update_rumble = false; } + if (ds4->update_lightbar) { + common->valid_flag0 |= DS4_OUTPUT_VALID_FLAG0_LED; + /* Comptabile behavior with hid-sony, which used a dummy global LED to + * allow enabling/disabling the lightbar. The global LED maps to + * lightbar_enabled. + */ + common->lightbar_red = ds4->lightbar_enabled ? ds4->lightbar_red : 0; + common->lightbar_green = ds4->lightbar_enabled ? ds4->lightbar_green : 0; + common->lightbar_blue = ds4->lightbar_enabled ? ds4->lightbar_blue : 0; + ds4->update_lightbar = false; + } + spin_unlock_irqrestore(&ds4->base.lock, flags); hid_hw_output_report(ds4->base.hdev, report.data, report.len); @@ -1998,12 +2080,52 @@ static inline void dualshock4_schedule_work(struct dualshock4 *ds4) spin_unlock_irqrestore(&ds4->base.lock, flags); } +/* Set default lightbar color based on player. */ +static void dualshock4_set_default_lightbar_colors(struct dualshock4 *ds4) +{ + /* Use same player colors as PlayStation 4. + * Array of colors is in RGB. + */ + static const int player_colors[4][3] = { + { 0x00, 0x00, 0x40 }, /* Blue */ + { 0x40, 0x00, 0x00 }, /* Red */ + { 0x00, 0x40, 0x00 }, /* Green */ + { 0x20, 0x00, 0x20 } /* Pink */ + }; + + uint8_t player_id = ds4->base.player_id % ARRAY_SIZE(player_colors); + + ds4->lightbar_enabled = true; + ds4->lightbar_red = player_colors[player_id][0]; + ds4->lightbar_green = player_colors[player_id][1]; + ds4->lightbar_blue = player_colors[player_id][2]; + + ds4->update_lightbar = true; + dualshock4_schedule_work(ds4); +} + static struct ps_device *dualshock4_create(struct hid_device *hdev) { struct dualshock4 *ds4; struct ps_device *ps_dev; uint8_t max_output_report_size; - int ret; + int i, ret; + + /* The DualShock4 has an RGB lightbar, which the original hid-sony driver + * exposed as a set of 4 LEDs for the 3 color channels and a global control. + * Ideally this should have used the multi-color LED class, which didn't exist + * yet. In addition the driver used a naming scheme not compliant with the LED + * naming spec by using "<mac_address>:<color>", which contained many colons. + * We use a more compliant by using "<device_name>:<color>" name now. Ideally + * would have been "<device_name>:<color>:indicator", but that would break + * existing applications (e.g. Android). Nothing matches against MAC address. + */ + static const struct ps_led_info lightbar_leds_info[] = { + { NULL, "red", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, + { NULL, "green", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, + { NULL, "blue", 255, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, + { NULL, "global", 1, dualshock4_led_get_brightness, dualshock4_led_set_brightness }, + }; ds4 = devm_kzalloc(&hdev->dev, sizeof(*ds4), GFP_KERNEL); if (!ds4) @@ -2060,6 +2182,9 @@ static struct ps_device *dualshock4_create(struct hid_device *hdev) goto err; } + /* Use gamepad input device name as primary device name for e.g. LEDs */ + ps_dev->input_dev_name = dev_name(&ds4->gamepad->dev); + ds4->sensors = ps_sensors_create(hdev, DS4_ACC_RANGE, DS4_ACC_RES_PER_G, DS4_GYRO_RANGE, DS4_GYRO_RES_PER_DEG_S); if (IS_ERR(ds4->sensors)) { @@ -2077,12 +2202,22 @@ static struct ps_device *dualshock4_create(struct hid_device *hdev) if (ret) goto err; + for (i = 0; i < ARRAY_SIZE(lightbar_leds_info); i++) { + const struct ps_led_info *led_info = &lightbar_leds_info[i]; + + ret = ps_led_register(ps_dev, &ds4->lightbar_leds[i], led_info); + if (ret < 0) + goto err; + } + ret = ps_device_set_player_id(ps_dev); if (ret) { hid_err(hdev, "Failed to assign player id for DualShock4: %d\n", ret); goto err; } + dualshock4_set_default_lightbar_colors(ds4); + /* * Reporting hardware and firmware is important as there are frequent updates, which * can change behavior. |