[PATCH 01/14] ALSA: 6fire: Use auto-cleanup for firmware loading

Takashi Iwai <[email protected]>
Newsgroups org.kernel.vger.linux-sound
Message-ID <[email protected]>
Clean up the code for managing the firmware loading in the 6fire
driver with __free(firmware) and __free(kfree), so that the loaded
firmware and the name string are cleaned up automatically.

Only the code refactoring, no functional changes.

Signed-off-by: Takashi Iwai <[email protected]>
---
 sound/usb/6fire/firmware.c | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)

diff --git a/sound/usb/6fire/firmware.c b/sound/usb/6fire/firmware.c
index 123c1c6539b8..d9dd8f44b047 100644
--- a/sound/usb/6fire/firmware.c
+++ b/sound/usb/6fire/firmware.c
@@ -194,23 +194,20 @@ static int usb6fire_fw_ezusb_upload(
 	int ret;
 	u8 data;
 	struct usb_device *device = interface_to_usbdev(intf);
-	const struct firmware *fw = NULL;
-	struct ihex_record *rec = kmalloc_obj(struct ihex_record);
+	struct ihex_record *rec __free(kfree) = kmalloc_obj(struct ihex_record);
 
 	if (!rec)
 		return -ENOMEM;
 
+	const struct firmware *fw __free(firmware) = NULL;
 	ret = request_firmware(&fw, fwname, &device->dev);
 	if (ret < 0) {
-		kfree(rec);
 		dev_err(&intf->dev,
 			"error requesting ezusb firmware %s.\n", fwname);
 		return ret;
 	}
 	ret = usb6fire_fw_ihex_init(fw, rec);
 	if (ret < 0) {
-		kfree(rec);
-		release_firmware(fw);
 		dev_err(&intf->dev,
 			"error validating ezusb firmware %s.\n", fwname);
 		return ret;
@@ -219,8 +216,6 @@ static int usb6fire_fw_ezusb_upload(
 	data = 0x01; /* stop ezusb cpu */
 	ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
 	if (ret) {
-		kfree(rec);
-		release_firmware(fw);
 		dev_err(&intf->dev,
 			"unable to upload ezusb firmware %s: begin message.\n",
 			fwname);
@@ -231,8 +226,6 @@ static int usb6fire_fw_ezusb_upload(
 		ret = usb6fire_fw_ezusb_write(device, 0xa0, rec->address,
 				rec->data, rec->len);
 		if (ret) {
-			kfree(rec);
-			release_firmware(fw);
 			dev_err(&intf->dev,
 				"unable to upload ezusb firmware %s: data urb.\n",
 				fwname);
@@ -240,8 +233,6 @@ static int usb6fire_fw_ezusb_upload(
 		}
 	}
 
-	release_firmware(fw);
-	kfree(rec);
 	if (postdata) { /* write data after firmware has been uploaded */
 		ret = usb6fire_fw_ezusb_write(device, 0xa0, postaddr,
 				postdata, postlen);
@@ -270,19 +261,18 @@ static int usb6fire_fw_fpga_upload(
 	int ret;
 	int i;
 	struct usb_device *device = interface_to_usbdev(intf);
-	u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
+	u8 *buffer __free(kfree) = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
 	const char *c;
 	const char *end;
-	const struct firmware *fw;
 
 	if (!buffer)
 		return -ENOMEM;
 
+	const struct firmware *fw __free(firmware) = NULL;
 	ret = request_firmware(&fw, fwname, &device->dev);
 	if (ret < 0) {
 		dev_err(&intf->dev, "unable to get fpga firmware %s.\n",
 				fwname);
-		kfree(buffer);
 		return -EIO;
 	}
 
@@ -291,8 +281,6 @@ static int usb6fire_fw_fpga_upload(
 
 	ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0);
 	if (ret) {
-		kfree(buffer);
-		release_firmware(fw);
 		dev_err(&intf->dev,
 			"unable to upload fpga firmware: begin urb.\n");
 		return ret;
@@ -304,15 +292,11 @@ static int usb6fire_fw_fpga_upload(
 
 		ret = usb6fire_fw_fpga_write(device, buffer, i);
 		if (ret < 0) {
-			release_firmware(fw);
-			kfree(buffer);
 			dev_err(&intf->dev,
 				"unable to upload fpga firmware: fw urb.\n");
 			return ret;
 		}
 	}
-	release_firmware(fw);
-	kfree(buffer);
 
 	ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0);
 	if (ret) {
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.