diff mbox series

[-next,17/19] hwmon: (nzxt-kraken2) Use devm_hid_hw_start_and_open in kraken2_probe()

Message ID 20240904123607.3407364-18-lizetao1@huawei.com (mailing list archive)
State Superseded
Headers show
Series HID: convert to devm_hid_hw_start_and_open() | expand

Commit Message

Li Zetao Sept. 4, 2024, 12:36 p.m. UTC
Currently, the nzxt-kraken2 module needs to maintain hid resources
by itself. Consider using devm_hid_hw_start_and_open helper to ensure
that hid resources are consistent with the device life cycle, and release
hid resources before device is released. At the same time, it can avoid
the goto-release encoding, drop the fail_and_close and fail_and_stop
lables, and directly return the error code when an error occurs.

Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
 drivers/hwmon/nzxt-kraken2.c | 23 +++--------------------
 1 file changed, 3 insertions(+), 20 deletions(-)

Comments

Guenter Roeck Sept. 4, 2024, 2:17 p.m. UTC | #1
On 9/4/24 05:36, Li Zetao wrote:
> Currently, the nzxt-kraken2 module needs to maintain hid resources
> by itself. Consider using devm_hid_hw_start_and_open helper to ensure
> that hid resources are consistent with the device life cycle, and release
> hid resources before device is released. At the same time, it can avoid
> the goto-release encoding, drop the fail_and_close and fail_and_stop
> lables, and directly return the error code when an error occurs.
> 
> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> ---
>   drivers/hwmon/nzxt-kraken2.c | 23 +++--------------------
>   1 file changed, 3 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/hwmon/nzxt-kraken2.c b/drivers/hwmon/nzxt-kraken2.c
> index 7caf387eb144..aaaf857b42ed 100644
> --- a/drivers/hwmon/nzxt-kraken2.c
> +++ b/drivers/hwmon/nzxt-kraken2.c
> @@ -158,17 +158,9 @@ static int kraken2_probe(struct hid_device *hdev,
>   	/*
>   	 * Enable hidraw so existing user-space tools can continue to work.
>   	 */
> -	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
> -	if (ret) {
> -		hid_err(hdev, "hid hw start failed with %d\n", ret);
> +	ret = devm_hid_hw_start_and_open(hdev, HID_CONNECT_HIDRAW);
> +	if (ret)
>   		return ret;
> -	}
> -
> -	ret = hid_hw_open(hdev);
> -	if (ret) {
> -		hid_err(hdev, "hid hw open failed with %d\n", ret);
> -		goto fail_and_stop;
> -	}
>   
>   	priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "kraken2",
>   							  priv, &kraken2_chip_info,
> @@ -176,16 +168,10 @@ static int kraken2_probe(struct hid_device *hdev,
>   	if (IS_ERR(priv->hwmon_dev)) {
>   		ret = PTR_ERR(priv->hwmon_dev);
>   		hid_err(hdev, "hwmon registration failed with %d\n", ret);
> -		goto fail_and_close;
> +		return ret;
>   	}
>   
>   	return 0;

	struct device *hwmon_dev;	// also drop from struct kraken2_priv_data
	...
	hwmon_dev = devm_hwmon_device_register_with_info(...);
	return PTR_ERR_OR_ZERO(hwmon_dev);

and drop the remove function.

Thanks,
Guenter

> -
> -fail_and_close:
> -	hid_hw_close(hdev);
> -fail_and_stop:
> -	hid_hw_stop(hdev);
> -	return ret;
>   }
>   
>   static void kraken2_remove(struct hid_device *hdev)
> @@ -193,9 +179,6 @@ static void kraken2_remove(struct hid_device *hdev)
>   	struct kraken2_priv_data *priv = hid_get_drvdata(hdev);
>   
>   	hwmon_device_unregister(priv->hwmon_dev);
> -
> -	hid_hw_close(hdev);
> -	hid_hw_stop(hdev);
>   }
>   
>   static const struct hid_device_id kraken2_table[] = {
diff mbox series

Patch

diff --git a/drivers/hwmon/nzxt-kraken2.c b/drivers/hwmon/nzxt-kraken2.c
index 7caf387eb144..aaaf857b42ed 100644
--- a/drivers/hwmon/nzxt-kraken2.c
+++ b/drivers/hwmon/nzxt-kraken2.c
@@ -158,17 +158,9 @@  static int kraken2_probe(struct hid_device *hdev,
 	/*
 	 * Enable hidraw so existing user-space tools can continue to work.
 	 */
-	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
-	if (ret) {
-		hid_err(hdev, "hid hw start failed with %d\n", ret);
+	ret = devm_hid_hw_start_and_open(hdev, HID_CONNECT_HIDRAW);
+	if (ret)
 		return ret;
-	}
-
-	ret = hid_hw_open(hdev);
-	if (ret) {
-		hid_err(hdev, "hid hw open failed with %d\n", ret);
-		goto fail_and_stop;
-	}
 
 	priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "kraken2",
 							  priv, &kraken2_chip_info,
@@ -176,16 +168,10 @@  static int kraken2_probe(struct hid_device *hdev,
 	if (IS_ERR(priv->hwmon_dev)) {
 		ret = PTR_ERR(priv->hwmon_dev);
 		hid_err(hdev, "hwmon registration failed with %d\n", ret);
-		goto fail_and_close;
+		return ret;
 	}
 
 	return 0;
-
-fail_and_close:
-	hid_hw_close(hdev);
-fail_and_stop:
-	hid_hw_stop(hdev);
-	return ret;
 }
 
 static void kraken2_remove(struct hid_device *hdev)
@@ -193,9 +179,6 @@  static void kraken2_remove(struct hid_device *hdev)
 	struct kraken2_priv_data *priv = hid_get_drvdata(hdev);
 
 	hwmon_device_unregister(priv->hwmon_dev);
-
-	hid_hw_close(hdev);
-	hid_hw_stop(hdev);
 }
 
 static const struct hid_device_id kraken2_table[] = {