spi_master: Use new API to register shutdown function
This allows spi masters to register shutdown function in spi_master
struct, which means there is no need to call register_shutdown in init
function, since this call is now a part of register_spi_master.
As a consequence of using new API, two things are happening here:
1) No resource leakage anymore in case register_shutdown() would fail,
2) Fixed propagation of register_spi_master() return values.
Basic testing: when I comment out free(data) in linux_spi_shutdown, test
fails with error
../linux_spi.c:235: note: block 0x55a4db276510 allocated here
ERROR: linux_spi_init_and_shutdown_test_success leaked 1 block(s)
Means, shutdown function is invoked.
Tested: 1) builds and ninja test including CB:56911
2) On ARMv7 device
flashrom -p linux_spi -V
-> using linux_spi, chip found
3) On x86_64 AMD device
flashrom -p internal -V
-> this is actually using sb600spi, chip found
flashrom-stable: Updated `dirtyjtag_spi` which was added earlier.
Change-Id: Ib60300f9ddb295a255d5ef3f8da0e07064207140
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/56103
Original-Reviewed-by: Nico Huber <nico.h@gmx.de>
Original-Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Original-Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72231
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
diff --git a/buspirate_spi.c b/buspirate_spi.c
index 9c29b80..efd3e8b 100644
--- a/buspirate_spi.c
+++ b/buspirate_spi.c
@@ -135,6 +135,7 @@
const unsigned char *writearr, unsigned char *readarr);
static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
const unsigned char *writearr, unsigned char *readarr);
+static int buspirate_spi_shutdown(void *data);
static struct spi_master spi_master_buspirate = {
.features = SPI_MASTER_4BA,
@@ -145,6 +146,7 @@
.read = default_spi_read,
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = buspirate_spi_shutdown,
};
static const struct buspirate_speeds spispeeds[] = {
@@ -605,13 +607,7 @@
goto init_err_cleanup_exit;
}
- if (register_shutdown(buspirate_spi_shutdown, bp_data) != 0) {
- ret = 1;
- goto init_err_cleanup_exit;
- }
- register_spi_master(&spi_master_buspirate, bp_data);
-
- return 0;
+ return register_spi_master(&spi_master_buspirate, bp_data);
init_err_cleanup_exit:
buspirate_spi_shutdown(bp_data);
diff --git a/ch341a_spi.c b/ch341a_spi.c
index e26f9e9..3f27298 100644
--- a/ch341a_spi.c
+++ b/ch341a_spi.c
@@ -385,6 +385,8 @@
return 0;
}
+static int ch341a_spi_shutdown(void *data);
+
static const struct spi_master spi_master_ch341a_spi = {
.features = SPI_MASTER_4BA,
/* flashrom's current maximum is 256 B. CH341A was tested on Linux and Windows to accept at least
@@ -397,6 +399,7 @@
.read = default_spi_read,
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = ch341a_spi_shutdown,
};
static int ch341a_spi_shutdown(void *data)
@@ -506,10 +509,7 @@
if ((config_stream(CH341A_STM_I2C_100K) < 0) || (enable_pins(true) < 0))
goto dealloc_transfers;
- register_shutdown(ch341a_spi_shutdown, NULL);
- register_spi_master(&spi_master_ch341a_spi, NULL);
-
- return 0;
+ return register_spi_master(&spi_master_ch341a_spi, NULL);
dealloc_transfers:
for (i = 0; i < USB_IN_TRANSFERS; i++) {
diff --git a/dediprog.c b/dediprog.c
index af1f23f..c7f96c6 100644
--- a/dediprog.c
+++ b/dediprog.c
@@ -1010,6 +1010,8 @@
return millivolt;
}
+static int dediprog_shutdown(void *data);
+
static struct spi_master spi_master_dediprog = {
.features = SPI_MASTER_NO_4BA_MODES,
.max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
@@ -1019,6 +1021,7 @@
.read = dediprog_spi_read,
.write_256 = dediprog_spi_write_256,
.write_aai = dediprog_spi_write_aai,
+ .shutdown = dediprog_shutdown,
};
/*
@@ -1302,9 +1305,6 @@
if (protocol(dp_data) >= PROTOCOL_V2)
spi_master_dediprog.features |= SPI_MASTER_4BA;
- if (register_shutdown(dediprog_shutdown, dp_data))
- goto init_err_cleanup_exit;
-
if (dediprog_set_leds(LED_NONE, dp_data))
goto init_err_cleanup_exit;
diff --git a/digilent_spi.c b/digilent_spi.c
index d39d686..128c95c 100644
--- a/digilent_spi.c
+++ b/digilent_spi.c
@@ -312,6 +312,8 @@
return 0;
}
+static int digilent_spi_shutdown(void *data);
+
static const struct spi_master spi_master_digilent_spi = {
.features = SPI_MASTER_4BA,
.max_data_read = 252,
@@ -321,6 +323,7 @@
.read = default_spi_read,
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = digilent_spi_shutdown,
};
@@ -446,10 +449,7 @@
if (spi_set_mode(0x00) != 0)
goto close_handle;
- register_shutdown(digilent_spi_shutdown, NULL);
- register_spi_master(&spi_master_digilent_spi, NULL);
-
- return 0;
+ return register_spi_master(&spi_master_digilent_spi, NULL);
close_handle:
libusb_close(handle);
diff --git a/dirtyjtag_spi.c b/dirtyjtag_spi.c
index e7d8664..47b553d 100644
--- a/dirtyjtag_spi.c
+++ b/dirtyjtag_spi.c
@@ -198,6 +198,7 @@
.read = default_spi_read,
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = dirtyjtag_spi_shutdown,
};
static int dirtyjtag_spi_init(void)
@@ -297,9 +298,6 @@
goto cleanup_libusb_handle;
}
- if (register_shutdown(dirtyjtag_spi_shutdown, djtag_data))
- goto cleanup_libusb_handle;
-
return register_spi_master(&spi_master_dirtyjtag_spi, djtag_data);
cleanup_libusb_handle:
diff --git a/ft2232_spi.c b/ft2232_spi.c
index b37a60e..78dbb69 100644
--- a/ft2232_spi.c
+++ b/ft2232_spi.c
@@ -298,6 +298,7 @@
.read = default_spi_read,
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = ft2232_shutdown,
};
/* Returns 0 upon success, a negative number upon errors. */
@@ -696,13 +697,7 @@
spi_data->aux_bits = aux_bits;
spi_data->pindir = pindir;
- if (register_shutdown(ft2232_shutdown, spi_data)) {
- ret = -9;
- goto ftdi_err;
- }
- register_spi_master(&spi_master_ft2232, spi_data);
-
- return 0;
+ return register_spi_master(&spi_master_ft2232, spi_data);
ftdi_err:
if ((f = ftdi_usb_close(ftdic)) < 0) {
diff --git a/jlink_spi.c b/jlink_spi.c
index 589447a..fd14908 100644
--- a/jlink_spi.c
+++ b/jlink_spi.c
@@ -156,6 +156,8 @@
return 0;
}
+static int jlink_spi_shutdown(void *data);
+
static const struct spi_master spi_master_jlink_spi = {
/* Maximum data read size in one go (excluding opcode+address). */
.max_data_read = JTAG_MAX_TRANSFER_SIZE - 5,
@@ -167,6 +169,7 @@
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
.features = SPI_MASTER_4BA,
+ .shutdown = jlink_spi_shutdown,
};
static int jlink_spi_shutdown(void *data)
@@ -515,11 +518,7 @@
if (!deassert_cs(jlink_data))
goto init_err;
- if (register_shutdown(jlink_spi_shutdown, jlink_data))
- goto init_err;
- register_spi_master(&spi_master_jlink_spi, jlink_data);
-
- return 0;
+ return register_spi_master(&spi_master_jlink_spi, jlink_data);
init_err:
if (jaylink_devh)
diff --git a/linux_spi.c b/linux_spi.c
index ea43f9d..390038f 100644
--- a/linux_spi.c
+++ b/linux_spi.c
@@ -70,6 +70,7 @@
.read = linux_spi_read,
.write_256 = linux_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = linux_spi_shutdown,
};
/* Read max buffer size from sysfs, or use page size as fallback. */
@@ -187,12 +188,7 @@
spi_data->fd = fd;
spi_data->max_kernel_buf_size = max_kernel_buf_size;
- if (register_shutdown(linux_spi_shutdown, spi_data)) {
- free(spi_data);
- goto init_err;
- }
- register_spi_master(&spi_master_linux, spi_data);
- return 0;
+ return register_spi_master(&spi_master_linux, spi_data);
init_err:
close(fd);
diff --git a/mstarddc_spi.c b/mstarddc_spi.c
index 460a382..cf00898 100644
--- a/mstarddc_spi.c
+++ b/mstarddc_spi.c
@@ -142,8 +142,6 @@
goto out;
}
}
- // Register shutdown function
- register_shutdown(mstarddc_spi_shutdown, NULL);
// Register programmer
register_spi_master(&spi_master_mstarddc, NULL);
@@ -227,6 +225,7 @@
.read = default_spi_read,
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = mstarddc_spi_shutdown,
};
const struct programmer_entry programmer_mstarddc_spi = {
diff --git a/ni845x_spi.c b/ni845x_spi.c
index 6e274ac..75e5312 100644
--- a/ni845x_spi.c
+++ b/ni845x_spi.c
@@ -476,14 +476,7 @@
return 1;
}
- if (register_shutdown(ni845x_spi_shutdown, NULL)) {
- ni845x_spi_shutdown(NULL);
- return 1;
- }
-
- register_spi_master(&spi_programmer_ni845x, NULL);
-
- return 0;
+ return register_spi_master(&spi_programmer_ni845x, NULL);
}
static int ni845x_spi_shutdown(void *data)
@@ -639,6 +632,7 @@
.read = default_spi_read,
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = ni845x_spi_shutdown,
};
const struct programmer_entry programmer_ni845x_spi = {
diff --git a/pickit2_spi.c b/pickit2_spi.c
index 62641f2..bcf48fc 100644
--- a/pickit2_spi.c
+++ b/pickit2_spi.c
@@ -342,6 +342,8 @@
return millivolt;
}
+static int pickit2_shutdown(void *data);
+
static const struct spi_master spi_master_pickit2 = {
.max_data_read = 40,
.max_data_write = 40,
@@ -350,6 +352,7 @@
.read = default_spi_read,
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = pickit2_shutdown,
};
static int pickit2_shutdown(void *data)
@@ -497,11 +500,7 @@
goto init_err_cleanup_exit;
}
- if (register_shutdown(pickit2_shutdown, pickit2_data))
- goto init_err_cleanup_exit;
- register_spi_master(&spi_master_pickit2, pickit2_data);
-
- return 0;
+ return register_spi_master(&spi_master_pickit2, pickit2_data);
init_err_cleanup_exit:
pickit2_shutdown(pickit2_data);
diff --git a/stlinkv3_spi.c b/stlinkv3_spi.c
index 50b5c85..c7ee33b 100644
--- a/stlinkv3_spi.c
+++ b/stlinkv3_spi.c
@@ -445,6 +445,7 @@
.read = default_spi_read,
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
+ .shutdown = stlinkv3_spi_shutdown,
};
static int stlinkv3_spi_init(void)
@@ -504,17 +505,7 @@
if (stlinkv3_spi_open(sck_freq_kHz))
goto init_err_exit;
- if (register_shutdown(stlinkv3_spi_shutdown, NULL))
- goto init_err_cleanup_exit;
-
- if (register_spi_master(&spi_programmer_stlinkv3, NULL))
- return 1; /* shutdown function does cleanup */
-
- return 0;
-
-init_err_cleanup_exit:
- stlinkv3_spi_shutdown(NULL);
- return 1;
+ return register_spi_master(&spi_programmer_stlinkv3, NULL);
init_err_exit:
if (stlinkv3_handle)