UPSTREAM: linux_spi.c: Separate shutdown from failed init cleanup

Shutdown function was covering two different jobs here:
1) the actual shutdown which is run at the end of the driver's
lifecycle and 2) cleanup in cases when initialisation failed.
Now, shutdown is only doing its main job (#1), and the driver
itself is doing cleanup when init fails (#2).

The good thing is that now resources are released/closed immediately
in cases when init fails (vs shutdown function which was run at some
point later), and the driver leaves clean space after itself if init fails.

And very importantly this unlocks API change which plans to move
register_shutdown inside register master API, see this
https://review.coreboot.org/c/flashrom/+/51761

BUG=b:185191942
TEST=emerge-samus flashrom
BRANCH=none

Original-Change-Id: I1c8da2878cd0e85a1e43ba9b4b8e6f3d9f38ae5c
Original-Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/52284
Original-Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Original-Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
(cherry picked from commit 6b5736c991863789ee2b516d0aab238355f781fe)

Change-Id: I1781807bf808e1d2d3f3280d33378193dd10f956
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/flashrom/+/2845631
Reviewed-by: Sean Paul <seanpaul@chromium.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Tested-by: Edward O'Callaghan <quasisec@chromium.org>
Commit-Queue: Nikolai Artemiev <nartemiev@google.com>
diff --git a/linux_spi.c b/linux_spi.c
index 12c3fe1..b925707 100644
--- a/linux_spi.c
+++ b/linux_spi.c
@@ -242,6 +242,7 @@
 	/* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
 	const uint8_t mode = SPI_MODE_0;
 	const uint8_t bits = 8;
+	int ret = 0;
 
 	p = extract_programmer_param("spispeed");
 	if (p && strlen(p)) {
@@ -277,34 +278,44 @@
 	}
 	free(dev);
 
-	if (register_shutdown(linux_spi_shutdown, NULL))
-		return 1;
-	/* We rely on the shutdown function for cleanup from here on. */
-
 	if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
 		msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n",
 			 __func__, speed_hz, strerror(errno));
-		return 1;
+		ret = 1;
+		goto init_err;
 	}
 	msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000);
 
 	if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
 		msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
 			 __func__, mode, strerror(errno));
-		return 1;
+		ret = 1;
+		goto init_err;
 	}
 
 	if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
 		msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
 			 __func__, bits == 0 ? 8 : bits, strerror(errno));
-		return 1;
+		ret = 1;
+		goto init_err;
 	}
 
 	max_kernel_buf_size = get_max_kernel_buf_size();
 	msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
 
+	if (register_shutdown(linux_spi_shutdown, NULL)) {
+		ret = 1;
+		goto init_err;
+	}
 	register_spi_master(&spi_master_linux);
 	return 0;
+
+init_err:
+	if (fd != -1) {
+		close(fd);
+		fd = -1;
+	}
+	return ret;
 }
 
 #endif // CONFIG_LINUX_SPI == 1