2021. január 2., szombat 23:31 keltezéssel, Roderick Colenbrander írta: > From: Roderick Colenbrander <roderick.colenbrander@xxxxxxxx> > > Expose the DualSense its RGB lightbar using the new multicolor LED > framework. > > Signed-off-by: Roderick Colenbrander <roderick.colenbrander@xxxxxxxx> > [...] > +/* 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