diff options
author | Namyoon Woo <namyoon@chromium.org> | 2019-06-05 16:34:30 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-06-14 00:56:38 +0000 |
commit | 79e88d81f3e090d959669b4de6215cef96c2a3ed (patch) | |
tree | 9b1f2c93d0929cee07d072a3aaa180523a16da49 /chip/g | |
parent | 8ca44cb4eca69d44e9fce0b93b58be9c7d9d19f3 (diff) | |
download | chrome-ec-79e88d81f3e090d959669b4de6215cef96c2a3ed.tar.gz |
g: consolidate USB console transfer codes to usb-stream
usb-console.c and usb_stream.c include some amount of
copied/pasted code. This patch converts USB console
implementation to USB stream to reduce the redundancy.
Flash usage decreases by 260 bytes.
BUG=None
BRANCH=None
TEST=Ran uart_stress_tester.sh (http://crrev.com/c/1586581/23) on bob.
This CL slightly increases CR50 console char loss rate: 5.5% -> 6.5% in
average.
Change-Id: Ibdc16867618d67ae33574e802c1949aa6b5cf1a6
Signed-off-by: Namyoon Woo <namyoon@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1648921
Reviewed-by: Mary Ruthven <mruthven@chromium.org>
Diffstat (limited to 'chip/g')
-rw-r--r-- | chip/g/usb-stream.c | 8 | ||||
-rw-r--r-- | chip/g/usb-stream.h | 10 | ||||
-rw-r--r-- | chip/g/usb_console.c | 86 |
3 files changed, 91 insertions, 13 deletions
diff --git a/chip/g/usb-stream.c b/chip/g/usb-stream.c index 8241eaf43c..e5de173b6d 100644 --- a/chip/g/usb-stream.c +++ b/chip/g/usb-stream.c @@ -114,8 +114,7 @@ void usb_stream_rx(struct usb_stream_config const *config) GR_USB_DOEPINT(config->endpoint) = 0xffffffff; } -/* True if the Tx/IN FIFO can take some bytes from us. */ -static inline int tx_fifo_is_ready(struct usb_stream_config const *config) +int tx_fifo_is_ready(struct usb_stream_config const *config) { uint32_t status; struct g_usb_desc *in_desc = config->in_desc; @@ -265,3 +264,8 @@ struct producer_ops const usb_stream_producer_ops = { struct consumer_ops const usb_stream_consumer_ops = { .written = usb_written, }; + +int usb_stream_is_reset(struct usb_stream_config const *config) +{ + return !!(*config->is_reset); +} diff --git a/chip/g/usb-stream.h b/chip/g/usb-stream.h index 020f6995c5..22a54f7369 100644 --- a/chip/g/usb-stream.h +++ b/chip/g/usb-stream.h @@ -228,4 +228,14 @@ void usb_stream_tx(struct usb_stream_config const *config); void usb_stream_rx(struct usb_stream_config const *config); void usb_stream_reset(struct usb_stream_config const *config); +/* + * True if the USB Tx/IN FIFO is ready for the transfer. + * False if it is busy transferring data. + */ +int tx_fifo_is_ready(struct usb_stream_config const *config); + +/* + * Return 1 if the USB stream is reset or 0 otherwise + */ +int usb_stream_is_reset(struct usb_stream_config const *config); #endif /* __CROS_EC_USB_STREAM_H */ diff --git a/chip/g/usb_console.c b/chip/g/usb_console.c index 11bef1de89..e0e4e8c035 100644 --- a/chip/g/usb_console.c +++ b/chip/g/usb_console.c @@ -15,15 +15,17 @@ #include "timer.h" #include "util.h" #include "usb_descriptor.h" +#include "usb-stream.h" #include "usb_hw.h" /* Console output macro */ #define CPRINTF(format, args...) cprintf(CC_USB, format, ## args) #define USB_CONSOLE_TIMEOUT_US (30 * MSEC) -static int last_tx_ok = 1; +#define QUEUE_SIZE_USB_TX 4096 +#define QUEUE_SIZE_USB_RX USB_MAX_PACKET_SIZE -static int is_reset; +static int last_tx_ok = 1; /* * Start enabled, so we can queue early debug output before the board gets @@ -37,6 +39,62 @@ static int is_enabled = 1; */ static int is_readonly = 1; +#ifdef CONFIG_STREAM_USB +struct usb_stream_config const usb_console; + +/* + * This is a usb_console producer policy, which wakes up CONSOLE task whenever + * rx_q gets new data added. This shall be called by rx_stream_handler() in + * usb-stream.c. + */ +static void usb_console_written(struct queue_policy const *policy, size_t count) +{ + task_wake(TASK_ID_CONSOLE); +} + +static void uart_console_read(struct queue_policy const *policy, size_t count) +{ + /* do nothing */ +} + +static struct queue_policy const usb_console_policy = { + .add = usb_console_written, + .remove = uart_console_read, +}; + +static struct queue const tx_q = QUEUE_NULL(QUEUE_SIZE_USB_TX, uint8_t); +static struct queue const rx_q = QUEUE(QUEUE_SIZE_USB_RX, uint8_t, + usb_console_policy); + +USB_STREAM_CONFIG(usb_console, + USB_IFACE_CONSOLE, + USB_STR_CONSOLE_NAME, + USB_EP_CONSOLE, + USB_MAX_PACKET_SIZE, + USB_MAX_PACKET_SIZE, + rx_q, + tx_q) + +static inline void handle_output(void) +{ + /* Wake up the Tx FIFO handler */ + hook_call_deferred(usb_console.deferred_tx, 0); +} + +static inline int tx_fifo_is_ready_(void) +{ + return tx_fifo_is_ready(&usb_console); +} + +static inline int usb_console_is_reset(void) +{ + return usb_stream_is_reset(&usb_console); +} + +#else /* #ifdef CONFIG_STREAM_USB ... */ + +static int is_reset; + /* USB-Serial descriptors */ const struct usb_interface_descriptor USB_IFACE_DESC(USB_IFACE_CONSOLE) = { @@ -74,8 +132,8 @@ static uint8_t ep_buf_rx[USB_MAX_PACKET_SIZE]; static struct g_usb_desc ep_out_desc; static struct g_usb_desc ep_in_desc; -static struct queue const tx_q = QUEUE_NULL(4096, uint8_t); -static struct queue const rx_q = QUEUE_NULL(USB_MAX_PACKET_SIZE, uint8_t); +static struct queue const tx_q = QUEUE_NULL(QUEUE_SIZE_USB_TX, uint8_t); +static struct queue const rx_q = QUEUE_NULL(QUEUE_SIZE_USB_RX, uint8_t); /* Let the USB HW IN-to-host FIFO transmit some bytes */ @@ -177,13 +235,19 @@ static void con_ep_rx(void) /* clear the RX/OUT interrupts */ GR_USB_DOEPINT(USB_EP_CONSOLE) = 0xffffffff; } + /* True if the Tx/IN FIFO can take some bytes from us. */ -static inline int tx_fifo_is_ready(void) +static inline int tx_fifo_is_ready_(void) { uint32_t status = ep_in_desc.flags & DIEPDMA_BS_MASK; return status == DIEPDMA_BS_DMA_DONE || status == DIEPDMA_BS_HOST_BSY; } +static inline int usb_console_is_reset(void) +{ + return is_reset; +} + /* Try to send some bytes to the host */ static void tx_fifo_handler(void) { @@ -193,7 +257,7 @@ static void tx_fifo_handler(void) return; /* If the HW FIFO isn't ready, then we can't do anything right now. */ - if (!tx_fifo_is_ready()) + if (!tx_fifo_is_ready_()) return; count = QUEUE_REMOVE_UNITS(&tx_q, ep_buf_tx, USB_MAX_PACKET_SIZE); @@ -245,13 +309,14 @@ static void ep_reset(void) USB_DECLARE_EP(USB_EP_CONSOLE, con_ep_tx, con_ep_rx, ep_reset); +#endif /* #ifdef CONFIG_STREAM_USB ... #else ... */ static int usb_wait_console(void) { timestamp_t deadline = get_time(); int wait_time_us = 1; - if (!is_enabled || !tx_fifo_is_ready()) + if (!is_enabled || !tx_fifo_is_ready_()) return EC_SUCCESS; deadline.val += USB_CONSOLE_TIMEOUT_US; @@ -264,7 +329,8 @@ static int usb_wait_console(void) * we should wait so that we don't clobber the buffer. */ if (last_tx_ok) { - while (queue_space(&tx_q) < USB_MAX_PACKET_SIZE || !is_reset) { + while (queue_space(&tx_q) < USB_MAX_PACKET_SIZE || + !usb_console_is_reset()) { if (timestamp_expired(deadline, NULL) || in_interrupt_context()) { last_tx_ok = 0; @@ -276,12 +342,10 @@ static int usb_wait_console(void) usleep(wait_time_us); wait_time_us *= 2; } - - return EC_SUCCESS; } else { last_tx_ok = queue_space(&tx_q); - return EC_SUCCESS; } + return EC_SUCCESS; } #ifdef CONFIG_USB_CONSOLE_CRC |