From 2ddc2ae6750af478cc104342ccc355fad1046bd5 Mon Sep 17 00:00:00 2001 From: Mary Ruthven Date: Wed, 12 Oct 2016 13:50:47 -0700 Subject: [PATCH] g: check that the rx fifo is ready before reading from it The USB stream should check that there are bytes in the rx fifo to read before trying to read them. This should have been in here already. Checking if rx is valid in usb-stream makes the rx_valid call in usb_spi unnecessary so that is removed. BUG=none BRANCH=none TEST=manual Test CCD functionality still works on gru and reef AP/EC consoles sudo flashrom -p raiden_debug_spi:[AP|EC] -r img.bin usb updater Change-Id: Ieb77e35cc471b1f97d540ea4560591f0f40dd600 Signed-off-by: Mary Ruthven Reviewed-on: https://chromium-review.googlesource.com/397858 Reviewed-by: Bill Richardson --- chip/g/usb-stream.c | 4 ++++ chip/g/usb_spi.c | 57 +++++++++++++++++++++------------------------ chip/g/usb_spi.h | 3 --- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/chip/g/usb-stream.c b/chip/g/usb-stream.c index 21a06c94e3..025f71e7fc 100644 --- a/chip/g/usb-stream.c +++ b/chip/g/usb-stream.c @@ -57,6 +57,10 @@ int rx_stream_handler(struct usb_stream_config const *config) */ static int rx_handled; + /* If the HW FIFO isn't ready, then we're waiting for more bytes */ + if (!rx_fifo_is_ready(config)) + return 0; + /* * How many of the HW FIFO bytes have we not yet handled? We need to * know both where we are in the buffer and how many bytes we haven't diff --git a/chip/g/usb_spi.c b/chip/g/usb_spi.c index bab7bae6c4..e03d2935e3 100644 --- a/chip/g/usb_spi.c +++ b/chip/g/usb_spi.c @@ -42,14 +42,12 @@ static void usb_spi_write_packet(struct usb_spi_config const *config, QUEUE_ADD_UNITS(config->tx_queue, config->buffer, count); } -static int rx_valid(struct usb_spi_config const *config) -{ - return (config->usb->out_desc->flags & DOEPDMA_BS_MASK) == - DOEPDMA_BS_DMA_DONE; -} - void usb_spi_deferred(struct usb_spi_config const *config) { + uint16_t count; + uint8_t write_count; + uint8_t read_count; + uint16_t res; /* * If our overall enabled state has changed we call the board specific * enable or disable routines and save our new state. @@ -70,34 +68,31 @@ void usb_spi_deferred(struct usb_spi_config const *config) * And if there is a USB packet waiting we process it and generate a * response. */ - if (!rx_valid(config)) { - uint16_t count = usb_spi_read_packet(config); - uint8_t write_count = config->buffer[0]; - uint8_t read_count = config->buffer[1]; - uint16_t res; + count = usb_spi_read_packet(config); + write_count = config->buffer[0]; + read_count = config->buffer[1]; - if (!read_count && !write_count) - return; + if (!count || (!read_count && !write_count)) + return; - if (!config->state->enabled) { - res = USB_SPI_DISABLED; - } else if (write_count > USB_SPI_MAX_WRITE_COUNT || - write_count != (count - HEADER_SIZE)) { - res = USB_SPI_WRITE_COUNT_INVALID; - } else if (read_count > USB_SPI_MAX_READ_COUNT) { - res = USB_SPI_READ_COUNT_INVALID; - } else { - res = usb_spi_map_error( - spi_transaction(SPI_FLASH_DEVICE, - config->buffer + HEADER_SIZE, - write_count, - config->buffer + HEADER_SIZE, - read_count)); - } - - memcpy(config->buffer, &res, HEADER_SIZE); - usb_spi_write_packet(config, read_count + HEADER_SIZE); + if (!config->state->enabled) { + res = USB_SPI_DISABLED; + } else if (write_count > USB_SPI_MAX_WRITE_COUNT || + write_count != (count - HEADER_SIZE)) { + res = USB_SPI_WRITE_COUNT_INVALID; + } else if (read_count > USB_SPI_MAX_READ_COUNT) { + res = USB_SPI_READ_COUNT_INVALID; + } else { + res = usb_spi_map_error( + spi_transaction(SPI_FLASH_DEVICE, + config->buffer + HEADER_SIZE, + write_count, + config->buffer + HEADER_SIZE, + read_count)); } + + memcpy(config->buffer, &res, HEADER_SIZE); + usb_spi_write_packet(config, read_count + HEADER_SIZE); } static void usb_spi_written(struct consumer const *consumer, size_t count) diff --git a/chip/g/usb_spi.h b/chip/g/usb_spi.h index 7a31e41d81..6aa6c203ea 100644 --- a/chip/g/usb_spi.h +++ b/chip/g/usb_spi.h @@ -109,8 +109,6 @@ struct usb_spi_config { */ struct usb_spi_state *state; - struct usb_stream_config const *usb; - /* * Interface and endpoint indicies. */ @@ -170,7 +168,6 @@ extern struct consumer_ops const usb_spi_consumer_ops; static struct usb_spi_state CONCAT2(NAME, _state_); \ struct usb_spi_config const NAME = { \ .state = &CONCAT2(NAME, _state_), \ - .usb = &CONCAT2(NAME, _usb_), \ .interface = INTERFACE, \ .endpoint = ENDPOINT, \ .deferred = &CONCAT2(NAME, _deferred__data), \