mbox series

[-next,00/19] HID: convert to devm_hid_hw_start_and_open()

Message ID 20240904123607.3407364-1-lizetao1@huawei.com
Headers show
Series HID: convert to devm_hid_hw_start_and_open() | expand

Message

Li Zetao Sept. 4, 2024, 12:35 p.m. UTC
Hi, all

This patchset is dedicated to using the life cycle approach to manage
hid resources. By keeping hid resources consistent with the life cycle
of the device, we ensure that resources are available during the life
cycle and the hid resources can be released before device release.

Going one step further, since the module does not need to recycle hid
resources by itself, the goto-release resource release coding can be
avoided. It also reduces the risk of resources not being released.

Li Zetao (19):
  HID: core: Use devm_add_action_or_reset helper to manage hid resources
  HID: cp2112: Use devm_hid_hw_start_and_open in cp2112_probe()
  HID: ft260: Use devm_hid_hw_start_and_open in ft260_probe()
  HID: mcp2200: Use devm_hid_hw_start_and_open in mcp2200_probe()
  HID: mcp2221: Use devm_hid_hw_start_and_open in mcp2221_probe()
  HID: nintendo: Use devm_hid_hw_start_and_open in nintendo_hid_probe()
  HID: shield: Use devm_hid_hw_start_and_open in shield_probe()
  HID: hid-picolcd: Use devm_hid_hw_start_and_open in picolcd_probe()
  HID: playstation: Use devm_hid_hw_start_and_open in ps_probe()
  HID: hid-steam: Use devm_hid_hw_start_and_open in steam_probe()
  HID: wiimote: Use devm_hid_hw_start_and_open in wiimote_hid_probe()
  hwmon: (aquacomputer_d5next) Use devm_hid_hw_start_and_open in
    aqc_probe()
  hwmon: Use devm_hid_hw_start_and_open in rog_ryujin_probe()
  hwmon: (corsair-cpro) Use devm_hid_hw_start_and_open in ccp_probe()
  hwmon: (corsair-psu) Use devm_hid_hw_start_and_open in
    corsairpsu_probe()
  hwmon: (gigabyte_waterforce) Use devm_hid_hw_start_and_open in
    waterforce_probe()
  hwmon: (nzxt-kraken2) Use devm_hid_hw_start_and_open in
    kraken2_probe()
  hwmon: (nzxt-kraken3) Use devm_hid_hw_start_and_open in
    kraken3_probe()
  hwmon: (nzxt-smart2) Use devm_hid_hw_start_and_open in
    nzxt_smart2_hid_probe()

 drivers/hid/hid-core.c              | 40 +++++++++++++++++++++++++++++
 drivers/hid/hid-cp2112.c            | 26 +++----------------
 drivers/hid/hid-ft260.c             | 32 +++++------------------
 drivers/hid/hid-mcp2200.c           | 22 ++--------------
 drivers/hid/hid-mcp2221.c           | 26 ++-----------------
 drivers/hid/hid-nintendo.c          | 23 +++--------------
 drivers/hid/hid-nvidia-shield.c     | 20 +++------------
 drivers/hid/hid-picolcd_core.c      | 22 +++-------------
 drivers/hid/hid-playstation.c       | 27 +++----------------
 drivers/hid/hid-steam.c             | 18 ++-----------
 drivers/hid/hid-wiimote-core.c      | 20 +++------------
 drivers/hwmon/aquacomputer_d5next.c | 39 +++++++---------------------
 drivers/hwmon/asus_rog_ryujin.c     | 29 ++++-----------------
 drivers/hwmon/corsair-cpro.c        | 24 ++++-------------
 drivers/hwmon/corsair-psu.c         | 24 ++++-------------
 drivers/hwmon/gigabyte_waterforce.c | 29 ++++-----------------
 drivers/hwmon/nzxt-kraken2.c        | 23 +++--------------
 drivers/hwmon/nzxt-kraken3.c        | 34 +++++-------------------
 drivers/hwmon/nzxt-smart2.c         | 22 +++-------------
 include/linux/hid.h                 |  2 ++
 20 files changed, 118 insertions(+), 384 deletions(-)

Comments

Li Zetao Sept. 5, 2024, 3:39 p.m. UTC | #1
Hi,

在 2024/9/4 22:14, Guenter Roeck 写道:
> On 9/4/24 05:36, Li Zetao wrote:
>> Currently, the nzxt-smart2 module needs to maintain hid resources
>> by itself. Consider using devm_hid_hw_start_and_open helper to ensure
> 
> For all patches:
> 
> s/Consider using/Use/
ok
> 
>> 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 out_hw_close and out_hw_stop
>> lables, and directly return the error code when an error occurs.
>>
>> Signed-off-by: Li Zetao <lizetao1@huawei.com>
>> ---
>>   drivers/hwmon/nzxt-smart2.c | 22 +++-------------------
>>   1 file changed, 3 insertions(+), 19 deletions(-)
>>
>> diff --git a/drivers/hwmon/nzxt-smart2.c b/drivers/hwmon/nzxt-smart2.c
>> index df6fa72a6b59..b5721a42c0d3 100644
>> --- a/drivers/hwmon/nzxt-smart2.c
>> +++ b/drivers/hwmon/nzxt-smart2.c
>> @@ -750,14 +750,10 @@ static int nzxt_smart2_hid_probe(struct 
>> hid_device *hdev,
>>       if (ret)
>>           return ret;
>> -    ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
>> +    ret = devm_hid_hw_start_and_open(hdev, HID_CONNECT_HIDRAW);
>>       if (ret)
>>           return ret;
>> -    ret = hid_hw_open(hdev);
>> -    if (ret)
>> -        goto out_hw_stop;
>> -
>>       hid_device_io_start(hdev);
>>       init_device(drvdata, UPDATE_INTERVAL_DEFAULT_MS);
>> @@ -765,19 +761,10 @@ static int nzxt_smart2_hid_probe(struct 
>> hid_device *hdev,
>>       drvdata->hwmon =
>>           hwmon_device_register_with_info(&hdev->dev, "nzxtsmart2", 
>> drvdata,
>>                           &nzxt_smart2_chip_info, NULL);
>> -    if (IS_ERR(drvdata->hwmon)) {
>> -        ret = PTR_ERR(drvdata->hwmon);
>> -        goto out_hw_close;
>> -    }
>> +    if (IS_ERR(drvdata->hwmon))
>> +        return PTR_ERR(drvdata->hwmon);
>>       return 0;
> 
>      return PTR_ERR_OR_ZERO(drvdata->hwmon);
> 
> Also, this can be optimized further.
> 
>      struct device *hwmon;    // and drop from struct drvdata
>      ...
>      hwmon = devm_hwmon_device_register_with_info(&hdev->dev, 
> "nzxtsmart2", drvdata,
>                               &nzxt_smart2_chip_info, NULL);
> 
>      return PTR_ERR_OR_ZERO(hwmon);
> 
> and drop the remove function entirely.
Benjamin mentioned that there are unsafe scenarios in 
hid_hw_close_and_stop, and it is necessary to ensure that the driver can 
not use manual kzalloc/kfree. So, to be extra safe, I would delete 
.remove in v2.
> 
> Thanks,
> Guenter
> 
>> -
>> -out_hw_close:
>> -    hid_hw_close(hdev);
>> -
>> -out_hw_stop:
>> -    hid_hw_stop(hdev);
>> -    return ret;
>>   }
>>   static void nzxt_smart2_hid_remove(struct hid_device *hdev)
>> @@ -785,9 +772,6 @@ static void nzxt_smart2_hid_remove(struct 
>> hid_device *hdev)
>>       struct drvdata *drvdata = hid_get_drvdata(hdev);
>>       hwmon_device_unregister(drvdata->hwmon);
>> -
>> -    hid_hw_close(hdev);
>> -    hid_hw_stop(hdev);
>>   }
>>   static const struct hid_device_id nzxt_smart2_hid_id_table[] = {
>