Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

HID: google: switch to devm when registering keyboard backlight LED

We can use devm to register keyboard backlight LED on hammer devices, this
will allow us to use HID's driver data for something else later.

Link: https://lore.kernel.org/r/20220228075446.466016-2-dmitry.torokhov@gmail.com
Tested-by: Stephen Boyd <swboyd@chromium.org> # coachz, wormdingler
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+13 -25
+13 -25
drivers/hid/hid-google-hammer.c
··· 340 340 static int hammer_register_leds(struct hid_device *hdev) 341 341 { 342 342 struct hammer_kbd_leds *kbd_backlight; 343 - int error; 344 343 345 - kbd_backlight = kzalloc(sizeof(*kbd_backlight), GFP_KERNEL); 344 + kbd_backlight = devm_kzalloc(&hdev->dev, sizeof(*kbd_backlight), 345 + GFP_KERNEL); 346 346 if (!kbd_backlight) 347 347 return -ENOMEM; 348 348 ··· 356 356 /* Set backlight to 0% initially. */ 357 357 hammer_kbd_brightness_set_blocking(&kbd_backlight->cdev, 0); 358 358 359 - error = led_classdev_register(&hdev->dev, &kbd_backlight->cdev); 360 - if (error) 361 - goto err_free_mem; 362 - 363 - hid_set_drvdata(hdev, kbd_backlight); 364 - return 0; 365 - 366 - err_free_mem: 367 - kfree(kbd_backlight); 368 - return error; 369 - } 370 - 371 - static void hammer_unregister_leds(struct hid_device *hdev) 372 - { 373 - struct hammer_kbd_leds *kbd_backlight = hid_get_drvdata(hdev); 374 - 375 - if (kbd_backlight) { 376 - led_classdev_unregister(&kbd_backlight->cdev); 377 - kfree(kbd_backlight); 378 - } 359 + return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev); 379 360 } 380 361 381 362 #define HID_UP_GOOGLEVENDOR 0xffd10000 ··· 493 512 kfree(buf); 494 513 } 495 514 515 + static void hammer_stop(void *hdev) 516 + { 517 + hid_hw_stop(hdev); 518 + } 519 + 496 520 static int hammer_probe(struct hid_device *hdev, 497 521 const struct hid_device_id *id) 498 522 { ··· 508 522 return error; 509 523 510 524 error = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 525 + if (error) 526 + return error; 527 + 528 + error = devm_add_action(&hdev->dev, hammer_stop, hdev); 511 529 if (error) 512 530 return error; 513 531 ··· 567 577 spin_unlock_irqrestore(&cbas_ec_lock, flags); 568 578 } 569 579 570 - hammer_unregister_leds(hdev); 571 - 572 - hid_hw_stop(hdev); 580 + /* Unregistering LEDs and stopping the hardware is done via devm */ 573 581 } 574 582 575 583 static const struct hid_device_id hammer_devices[] = {