Anastasia Klimchuk has uploaded this change for review.

View Change

jlink_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

TEST=builds
BUG=b:185191942

Change-Id: I71f64ed38154af670d4d28b8c7914d87fbc75679
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
---
M jlink_spi.c
1 file changed, 47 insertions(+), 19 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/08/52308/1
diff --git a/jlink_spi.c b/jlink_spi.c
index 3a73c01..fcdcd28 100644
--- a/jlink_spi.c
+++ b/jlink_spi.c
@@ -178,8 +178,7 @@
{
char *arg;
unsigned long speed = 0;
-
- register_shutdown(jlink_spi_shutdown, NULL);
+ int init_ret = 0;

arg = extract_programmer_param("spispeed");

@@ -269,7 +268,8 @@

if (ret != JAYLINK_OK) {
msg_perr("jaylink_discover_scan() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}

struct jaylink_device **devs;
@@ -278,7 +278,8 @@

if (ret != JAYLINK_OK) {
msg_perr("jaylink_get_devices() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}

if (!use_serial_number)
@@ -321,7 +322,8 @@

if (!device_found) {
msg_perr("No J-Link device found.\n");
- return 1;
+ init_ret = 1;
+ goto init_err;
}

size_t length;
@@ -332,7 +334,8 @@

if (ret != JAYLINK_OK) {
msg_perr("jaylink_get_firmware_version() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
} else if (length > 0) {
msg_pdbg("Firmware: %s\n", firmware_version);
free(firmware_version);
@@ -346,7 +349,8 @@
msg_pdbg("S/N: N/A\n");
} else {
msg_perr("jaylink_device_get_serial_number() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}

uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE];
@@ -356,7 +360,8 @@

if (ret != JAYLINK_OK) {
msg_perr("jaylink_get_caps() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}

if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
@@ -364,7 +369,8 @@

if (ret != JAYLINK_OK) {
msg_perr("jaylink_get_available_interfaces() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}
}

@@ -374,19 +380,22 @@

if (ret != JAYLINK_OK) {
msg_perr("jaylink_get_available_interfaces() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}

if (!(ifaces & (1 << JAYLINK_TIF_JTAG))) {
msg_perr("Device does not support JTAG interface.\n");
- return 1;
+ init_ret = 1;
+ goto init_err;
}

ret = jaylink_select_interface(jaylink_devh, JAYLINK_TIF_JTAG, NULL);

if (ret != JAYLINK_OK) {
msg_perr("jaylink_select_interface() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}

struct jaylink_hardware_status hwstat;
@@ -395,7 +404,8 @@

if (ret != JAYLINK_OK) {
msg_perr("jaylink_get_hardware_status() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}

msg_pdbg("VTarget: %u.%03u V\n", hwstat.target_voltage / 1000,
@@ -404,7 +414,8 @@
if (hwstat.target_voltage < MIN_TARGET_VOLTAGE) {
msg_perr("Target voltage is below %u.%03u V. You need to attach VTref to the I/O voltage of "
"the chip.\n", MIN_TARGET_VOLTAGE / 1000, MIN_TARGET_VOLTAGE % 1000);
- return 1;
+ init_ret = 1;
+ goto init_err;
}

struct jaylink_speed device_speeds;
@@ -417,7 +428,8 @@

if (ret != JAYLINK_OK) {
msg_perr("jaylink_get_speeds() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}
}

@@ -433,23 +445,39 @@
if (speed > (device_speeds.freq / device_speeds.div)) {
msg_perr("Specified SPI speed of %lu kHz is too high. Maximum is %" PRIu32 " kHz.\n", speed,
device_speeds.freq / device_speeds.div);
- return 1;
+ init_ret = 1;
+ goto init_err;
}

ret = jaylink_set_speed(jaylink_devh, speed);

if (ret != JAYLINK_OK) {
msg_perr("jaylink_set_speed() failed: %s.\n", jaylink_strerror(ret));
- return 1;
+ init_ret = 1;
+ goto init_err;
}

msg_pdbg("SPI speed: %lu kHz\n", speed);

/* Ensure that the CS signal is not active initially. */
- if (!deassert_cs())
- return 1;
+ if (!deassert_cs()) {
+ init_ret = 1;
+ goto init_err;
+ }

+ if (register_shutdown(jlink_spi_shutdown, NULL)) {
+ init_ret = 1;
+ goto init_err;
+ }
register_spi_master(&spi_master_jlink_spi);

return 0;
+
+init_err:
+ if (jaylink_devh)
+ jaylink_close(jaylink_devh);
+
+ jaylink_exit(jaylink_ctx);
+
+ return init_ret;
}

To view, visit change 52308. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I71f64ed38154af670d4d28b8c7914d87fbc75679
Gerrit-Change-Number: 52308
Gerrit-PatchSet: 1
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-MessageType: newchange