Use devres

Allow kernel to handle removal of devices when driver is unloaded.

Signed-off-by: Tim Crawford <tcrawford@system76.com>
This commit is contained in:
Tim Crawford 2025-06-09 15:44:44 -06:00
parent 9c210e3060
commit e457babab4
No known key found for this signature in database
GPG Key ID: 68E558D2BBD856E3
5 changed files with 15 additions and 53 deletions

View File

@ -98,12 +98,13 @@ static int __init ap_led_init(struct device *dev)
{
int err;
err = led_classdev_register(dev, &ap_led);
if (unlikely(err)) {
err = devm_led_classdev_register(dev, &ap_led);
if (err < 0) {
return err;
}
if (device_create_file(ap_led.dev, &ap_led_invert_dev_attr) != 0) {
err = device_create_file(ap_led.dev, &ap_led_invert_dev_attr);
if (err < 0) {
pr_err("failed to create ap_led_invert\n");
}
@ -115,8 +116,4 @@ static int __init ap_led_init(struct device *dev)
static void __exit ap_led_exit(void)
{
device_remove_file(ap_led.dev, &ap_led_invert_dev_attr);
if (!IS_ERR_OR_NULL(ap_led.dev)) {
led_classdev_unregister(&ap_led);
}
}

View File

@ -234,20 +234,16 @@ static struct notifier_block s76_hwmon_reboot_notifier = {
static int s76_hwmon_init(struct device *dev)
{
int ret;
s76_hwmon = kzalloc(sizeof(*s76_hwmon), GFP_KERNEL);
s76_hwmon = devm_kzalloc(dev, sizeof(*s76_hwmon), GFP_KERNEL);
if (!s76_hwmon)
return -ENOMEM;
s76_hwmon->dev = hwmon_device_register_with_groups(dev, S76_DRIVER_NAME, NULL, hwmon_default_groups);
s76_hwmon->dev = devm_hwmon_device_register_with_groups(dev, S76_DRIVER_NAME, NULL, hwmon_default_groups);
if (IS_ERR(s76_hwmon->dev)) {
ret = PTR_ERR(s76_hwmon->dev);
s76_hwmon->dev = NULL;
return ret;
return PTR_ERR(s76_hwmon->dev);
}
register_reboot_notifier(&s76_hwmon_reboot_notifier);
(void)devm_register_reboot_notifier(dev, &s76_hwmon_reboot_notifier);
s76_write_pwm_auto(0);
#ifdef EXPERIMENTAL
s76_write_pwm_auto(1);
@ -257,15 +253,13 @@ static int s76_hwmon_init(struct device *dev)
static int s76_hwmon_fini(struct device *dev)
{
if (!s76_hwmon || !s76_hwmon->dev)
if (!s76_hwmon || IS_ERR_OR_NULL(s76_hwmon->dev))
return 0;
s76_write_pwm_auto(0);
#ifdef EXPERIMENTAL
s76_write_pwm_auto(1);
#endif
unregister_reboot_notifier(&s76_hwmon_reboot_notifier);
hwmon_device_unregister(s76_hwmon->dev);
kfree(s76_hwmon);
return 0;
}

View File

@ -131,11 +131,10 @@ static void s76_input_close(struct input_dev *dev)
static int __init s76_input_init(struct device *dev)
{
int err;
u8 byte;
s76_input_device = input_allocate_device();
if (unlikely(!s76_input_device)) {
s76_input_device = devm_input_allocate_device(dev);
if (!s76_input_device) {
pr_err("Error allocating input device\n");
return -ENOMEM;
}
@ -143,8 +142,8 @@ static int __init s76_input_init(struct device *dev)
s76_input_device->name = "System76 Hotkeys";
s76_input_device->phys = "system76/input0";
s76_input_device->id.bustype = BUS_HOST;
s76_input_device->dev.parent = dev;
set_bit(EV_KEY, s76_input_device->evbit);
if (driver_flags & DRIVER_AP_KEY) {
set_bit(AIRPLANE_KEY, s76_input_device->keybit);
ec_read(0xDB, &byte);
@ -157,26 +156,5 @@ static int __init s76_input_init(struct device *dev)
s76_input_device->open = s76_input_open;
s76_input_device->close = s76_input_close;
err = input_register_device(s76_input_device);
if (unlikely(err)) {
pr_err("Error registering input device\n");
goto err_free_input_device;
}
return 0;
err_free_input_device:
input_free_device(s76_input_device);
return err;
}
static void __exit s76_input_exit(void)
{
if (unlikely(!s76_input_device)) {
return;
}
input_unregister_device(s76_input_device);
s76_input_device = NULL;
return input_register_device(s76_input_device);
}

View File

@ -316,7 +316,7 @@ static int __init kb_led_init(struct device *dev)
{
int err;
err = led_classdev_register(dev, &kb_led);
err = devm_led_classdev_register(dev, &kb_led);
if (unlikely(err)) {
return err;
}
@ -348,10 +348,6 @@ static void __exit kb_led_exit(void)
device_remove_file(kb_led.dev, &kb_led_color_right_dev_attr);
device_remove_file(kb_led.dev, &kb_led_color_center_dev_attr);
device_remove_file(kb_led.dev, &kb_led_color_left_dev_attr);
if (!IS_ERR_OR_NULL(kb_led.dev)) {
led_classdev_unregister(&kb_led);
}
}
static void kb_wmi_brightness(enum led_brightness value)

View File

@ -234,9 +234,6 @@ static int s76_remove(struct platform_device *dev)
s76_hwmon_fini(&dev->dev);
}
#endif
if (driver_flags & DRIVER_INPUT) {
s76_input_exit();
}
if (driver_flags & (DRIVER_KB_LED_WMI | DRIVER_KB_LED)) {
kb_led_exit();
}