Edward O'Callaghan has uploaded this change for review.

View Change

raiden_debug_spi.c: Clean up global state

The Chromium flashrom fork has very poor dispatch logic whereas
upstream has proper inversion of control with a generic 'data'
void * member for stuff long-lived state in. Leverage the member
to store the USB descriptor state in during the life-time of the
spi master.

BUG=b:140394053
BRANCH=none
TEST=builds

Change-Id: Ida9dce97fef2c6dfd68a278c879917fdd3ff7fef
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
---
M raiden_debug_spi.c
1 file changed, 48 insertions(+), 15 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/05/40105/1
diff --git a/raiden_debug_spi.c b/raiden_debug_spi.c
index 173e355..913ee45 100644
--- a/raiden_debug_spi.c
+++ b/raiden_debug_spi.c
@@ -161,9 +161,11 @@
*/
#define TRANSFER_TIMEOUT_MS (200 + 800)

-struct usb_device *device = NULL;
-uint8_t in_endpoint = 0;
-uint8_t out_endpoint = 0;
+struct raiden_debug_spi_data {
+ struct usb_device *dev;
+ uint8_t in_ep;
+ uint8_t out_ep;
+};

typedef struct {
int8_t write_count;
@@ -200,6 +202,12 @@
return true;
}

+static const struct raiden_debug_spi_data *
+ get_raiden_data_from_context(const struct flashctx *flash)
+{
+ return (const struct raiden_debug_spi_data *)flash->mst->spi.data;
+}
+
static int write_command(const struct flashctx *flash,
unsigned int write_count,
unsigned int read_count,
@@ -210,6 +218,7 @@
int transferred;
int ret;
usb_spi_command_t command_packet;
+ const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);

if (write_count > PAYLOAD_SIZE) {
msg_perr("Raiden: Invalid write_count of %d\n", write_count);
@@ -226,8 +235,8 @@

memcpy(command_packet.data, write_buffer, write_count);

- ret = LIBUSB(libusb_bulk_transfer(device->handle,
- out_endpoint,
+ ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
+ ctx_data->out_ep,
(void*)&command_packet,
write_count + PACKET_HEADER_SIZE,
&transferred,
@@ -258,9 +267,10 @@
int transferred;
int ret;
usb_spi_response_t response_packet;
+ const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);

- ret = LIBUSB(libusb_bulk_transfer(device->handle,
- in_endpoint,
+ ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
+ ctx_data->in_ep,
(void*)&response_packet,
read_count + PACKET_HEADER_SIZE,
&transferred,
@@ -403,16 +413,19 @@
return 0;
}

-static int shutdown(void * data)
+static int raiden_debug_spi_shutdown(void * data)
{
+ struct raiden_debug_spi_data * ctx_data =
+ (struct raiden_debug_spi_data *)data;
+
int ret = LIBUSB(libusb_control_transfer(
- device->handle,
+ ctx_data->dev->handle,
LIBUSB_ENDPOINT_OUT |
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_INTERFACE,
RAIDEN_DEBUG_SPI_REQ_DISABLE,
0,
- device->interface_descriptor->bInterfaceNumber,
+ ctx_data->dev->interface_descriptor->bInterfaceNumber,
NULL,
0,
TRANSFER_TIMEOUT_MS));
@@ -421,10 +434,9 @@
return ret;
}

- usb_device_free(device);
-
- device = NULL;
+ usb_device_free(ctx_data->dev);
libusb_exit(NULL);
+ free(ctx_data);

return 0;
}
@@ -458,11 +470,20 @@
dev = usb_device_free(dev);
}

+static int register_raiden_debug_spi_master(void *ptr)
+{
+ struct spi_master mst = spi_master_raiden_debug;
+ mst.data = ptr;
+
+ return register_spi_master(&mst);
+}
+
int raiden_debug_spi_init(void)
{
struct usb_match match;
char *serial = extract_programmer_param("serial");
struct usb_device *current;
+ struct usb_device *device = NULL;
int found = 0;
int ret;

@@ -489,6 +510,8 @@
return ret;
}

+ uint8_t in_endpoint = 0;
+ uint8_t out_endpoint = 0;
while (current) {
device = current;

@@ -576,8 +599,18 @@
(request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
usleep(50 * 1000);

- register_spi_master(&spi_master_raiden_debug);
- register_shutdown(shutdown, NULL);
+ struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data));
+ if (!data) {
+ msg_perr("Unable to allocate space for extra SPI master data.\n");
+ return SPI_GENERIC_ERROR;
+ }
+
+ data->dev = device;
+ data->in_ep = in_endpoint;
+ data->out_ep = out_endpoint;
+
+ register_raiden_debug_spi_master(data);
+ register_shutdown(raiden_debug_spi_shutdown, data);

return 0;
}

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

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: Ida9dce97fef2c6dfd68a278c879917fdd3ff7fef
Gerrit-Change-Number: 40105
Gerrit-PatchSet: 1
Gerrit-Owner: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-MessageType: newchange