Skip to contentSkip to conversation

HID: hid-cmedia: use device managed resources

PATCH Superseded Marcus Folkesson 1 participant 2 messages Sep 15, 2022 – Sep 27, 2022 HID

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 HID: hid-cmedia: use device managed resources

When we do not need to free() any memory in .remove, we can remove cmhid_remove as well.
hid_device_remove() will call hid_hw_stop() as default .remove function if no function is specified.

---
 drivers/hid/hid-cmedia.c | 25 +++++--------------------
 1 file changed, 5 insertions(+), 20 deletions(-)
drivers/hid/hid-cmedia.c +5 -20
index cab42047bc99..87786a88b119 100644
@@ -145,11 +145,9 @@ static int cmhid_probe(struct hid_device *hid, const struct hid_device_id *id)
 	int ret;
 	struct cmhid *cm;
 
-	cm = kzalloc(sizeof(struct cmhid), GFP_KERNEL);
-	if (!cm) {
-		ret = -ENOMEM;
-		goto allocfail;
-	}
+	cm = devm_kzalloc(&hid->dev, sizeof(struct cmhid), GFP_KERNEL);
+	if (!cm)
+		return -ENOMEM;
 
 	cm->hid = hid;
 
@@ -159,28 +157,16 @@ static int cmhid_probe(struct hid_device *hid, const struct hid_device_id *id)
 	ret = hid_parse(hid);
 	if (ret) {
 		hid_err(hid, "parse failed\n");
-		goto fail;
+		return ret;
 	}
 
 	ret = hid_hw_start(hid, HID_CONNECT_DEFAULT | HID_CONNECT_HIDDEV_FORCE);
 	if (ret) {
 		hid_err(hid, "hw start failed\n");
-		goto fail;
+		return ret;
 	}
 
 	return 0;
-fail:
-	kfree(cm);
-allocfail:
-	return ret;
-}
-
-static void cmhid_remove(struct hid_device *hid)
-{
-	struct cmhid *cm = hid_get_drvdata(hid);
-
-	hid_hw_stop(hid);
-	kfree(cm);
 }
 
 static const struct hid_device_id cmhid_devices[] = {
@@ -195,7 +181,6 @@ static struct hid_driver cmhid_driver = {
 	.raw_event = cmhid_raw_event,
 	.input_configured = cmhid_input_configured,
 	.probe = cmhid_probe,
-	.remove = cmhid_remove,
 	.input_mapping = cmhid_input_mapping,
 };
 
Signature · 1 line
2.37.1
#1 lore raw

Hi,

Do you pick up this (and the similiar
[PATCH] HID: hid-alps: use default remove for hid device [PATCH] HID: hid-elan: use default remove for hid device )
patch as well?

Quoted 35 lines · Marcus Folkesson

When we do not need to free() any memory in .remove, we can remove cmhid_remove as well.
hid_device_remove() will call hid_hw_stop() as default .remove function if no function is specified.

---
 drivers/hid/hid-cmedia.c | 25 +++++--------------------
 1 file changed, 5 insertions(+), 20 deletions(-)
drivers/hid/hid-cmedia.c +4 -6
index cab42047bc99..87786a88b119 100644
@@ -145,11 +145,9 @@ static int cmhid_probe(struct hid_device *hid, const struct hid_device_id *id)
 	int ret;
 	struct cmhid *cm;
 
-	cm = kzalloc(sizeof(struct cmhid), GFP_KERNEL);
-	if (!cm) {
-		ret = -ENOMEM;
-		goto allocfail;
-	}
+	cm = devm_kzalloc(&hid->dev, sizeof(struct cmhid), GFP_KERNEL);
+	if (!cm)
+		return -ENOMEM;
 
 	cm->hid = hid;
 
@@ -159,28 +157,16 @@ static int cmhid_probe(struct hid_device *hid, const struct hid_device_id *id)
 	ret = hid_parse(hid);
 	if (ret) {
 		hid_err(hid, "parse failed\n");
-		goto fail;
+		return ret;

drivers/hid/hid-cmedia.c static int cmhid_probe(struct hid_device *hid, const struct hid_device_id *id) · line 169

Quoted 10 lines
drivers/hid/hid-cmedia.c +1 -1
 	}
 
 	ret = hid_hw_start(hid, HID_CONNECT_DEFAULT | HID_CONNECT_HIDDEV_FORCE);
 	if (ret) {
 		hid_err(hid, "hw start failed\n");
-		goto fail;
+		return ret;
 	}
 
 	return 0;

Outch, I just saw that we should just return ret here. IOW

  	if (ret)
  		hid_err(hid, "hw start failed\n");
    return ret;

Do you want me to send v2 or do you fix that when applying?

Thanks,
Marcus Folkesson