summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWei-Han Chen <stimim@google.com>2018-08-16 11:47:58 +0800
committerchrome-bot <chrome-bot@chromium.org>2018-08-20 21:43:06 -0700
commit3f19d870f81f062410e30d166848d47a2a20e5d9 (patch)
treeb8af51b5b8f312bbf25caa53e45c3385094589a0
parentb059ccdead89713485fc031faa622115d0839d57 (diff)
downloadchrome-ec-3f19d870f81f062410e30d166848d47a2a20e5d9.tar.gz
uart_buffering: expose interface to support non-host command
UART buffer could only be accessed by host command. For detachable devices, we don't want to enable all host commands. Refactor and expose an API to support accessing UART buffer directly, host command is not necessary. BRANCH=none BUG=b:70482333 TEST=tested on whiskers TEST=tested on nocturne Signed-off-by: Wei-Han Chen <stimim@chromium.org> Change-Id: I8a9bbad23fbd3c02df54cd7b5d59b0e8376756ac Reviewed-on: https://chromium-review.googlesource.com/1177094 Commit-Ready: Wei-Han Chen <stimim@chromium.org> Tested-by: Wei-Han Chen <stimim@chromium.org> Reviewed-by: Nicolas Boichat <drinkcat@chromium.org>
-rw-r--r--common/uart_buffering.c110
-rw-r--r--include/uart.h32
2 files changed, 96 insertions, 46 deletions
diff --git a/common/uart_buffering.c b/common/uart_buffering.c
index 1f9f57f18a..7b4b864c09 100644
--- a/common/uart_buffering.c
+++ b/common/uart_buffering.c
@@ -330,6 +330,51 @@ DECLARE_HOOK(HOOK_INIT, uart_rx_dma_init, HOOK_PRIO_DEFAULT);
static int host_command_console_snapshot(struct host_cmd_handler_args *args)
{
+ return uart_console_read_buffer_init();
+}
+DECLARE_HOST_COMMAND(EC_CMD_CONSOLE_SNAPSHOT,
+ host_command_console_snapshot,
+ EC_VER_MASK(0));
+
+
+static int host_command_console_read(struct host_cmd_handler_args *args)
+{
+ if (args->version == 0) {
+ /*
+ * Prior versions of this command only support reading from
+ * an entire snapshot, not just the output since the last
+ * snapshot.
+ */
+ return uart_console_read_buffer(
+ CONSOLE_READ_NEXT,
+ (char *)args->response,
+ args->response_max,
+ &args->response_size);
+#ifdef CONFIG_CONSOLE_ENABLE_READ_V1
+ } else if (args->version == 1) {
+ const struct ec_params_console_read_v1 *p;
+
+ /* Check the params to figure out where to start reading. */
+ p = args->params;
+ return uart_console_read_buffer(
+ p->subcmd,
+ (char *)args->response,
+ args->response_max,
+ &args->response_size);
+#endif
+ }
+ return EC_RES_INVALID_PARAM;
+}
+DECLARE_HOST_COMMAND(EC_CMD_CONSOLE_READ,
+ host_command_console_read,
+ EC_VER_MASK(0)
+#ifdef CONFIG_CONSOLE_ENABLE_READ_V1
+ | EC_VER_MASK(1)
+#endif
+ );
+
+int uart_console_read_buffer_init(void)
+{
/* Assume the whole circular buffer is full */
tx_snapshot_head = tx_buf_head;
tx_snapshot_tail = TX_BUF_NEXT(tx_snapshot_head);
@@ -355,26 +400,31 @@ static int host_command_console_snapshot(struct host_cmd_handler_args *args)
return EC_RES_SUCCESS;
}
-DECLARE_HOST_COMMAND(EC_CMD_CONSOLE_SNAPSHOT,
- host_command_console_snapshot,
- EC_VER_MASK(0));
-/*
- * Common code for host_command_console_read and
- * host_command_console_read_recent.
- */
-static int console_read_helper(struct host_cmd_handler_args *args,
- int *tail)
+int uart_console_read_buffer(uint8_t type,
+ char *dest,
+ uint16_t dest_size,
+ uint16_t *write_count)
{
- char *dest = (char *)args->response;
+ int *tail;
+
+ switch (type) {
+ case CONSOLE_READ_NEXT:
+ tail = &tx_snapshot_tail;
+ break;
+ case CONSOLE_READ_RECENT:
+ tail = &tx_last_snapshot_head;
+ break;
+ default:
+ return EC_RES_INVALID_PARAM;
+ }
/* If no snapshot data, return empty response */
if (tx_snapshot_head == *tail)
return EC_RES_SUCCESS;
/* Copy data to response */
- while (*tail != tx_snapshot_head &&
- args->response_size < args->response_max - 1) {
+ while (*tail != tx_snapshot_head && *write_count < dest_size - 1) {
/*
* Copy only non-zero bytes, so that we don't copy unused
@@ -382,7 +432,7 @@ static int console_read_helper(struct host_cmd_handler_args *args,
*/
if (tx_buf[*tail]) {
*(dest++) = tx_buf[*tail];
- args->response_size++;
+ (*write_count)++;
}
*tail = TX_BUF_NEXT(*tail);
@@ -390,39 +440,7 @@ static int console_read_helper(struct host_cmd_handler_args *args,
/* Null-terminate */
*(dest++) = '\0';
- args->response_size++;
+ (*write_count)++;
return EC_RES_SUCCESS;
}
-
-static int host_command_console_read(struct host_cmd_handler_args *args)
-{
- if (args->version == 0) {
- /*
- * Prior versions of this command only support reading from
- * an entire snapshot, not just the output since the last
- * snapshot.
- */
- return console_read_helper(args, &tx_snapshot_tail);
-#ifdef CONFIG_CONSOLE_ENABLE_READ_V1
- } else if (args->version == 1) {
- const struct ec_params_console_read_v1 *p;
-
- /* Check the params to figure out where to start reading. */
- p = args->params;
- if (p->subcmd == CONSOLE_READ_NEXT)
- return console_read_helper(args, &tx_snapshot_tail);
- else if (p->subcmd == CONSOLE_READ_RECENT)
- return console_read_helper(args,
- &tx_last_snapshot_head);
-#endif
- }
- return EC_RES_INVALID_PARAM;
-}
-DECLARE_HOST_COMMAND(EC_CMD_CONSOLE_READ,
- host_command_console_read,
- EC_VER_MASK(0)
-#ifdef CONFIG_CONSOLE_ENABLE_READ_V1
- | EC_VER_MASK(1)
-#endif
- );
diff --git a/include/uart.h b/include/uart.h
index d17b161ae5..e40b0e866f 100644
--- a/include/uart.h
+++ b/include/uart.h
@@ -300,4 +300,36 @@ int uart_alt_pad_write_read(uint8_t *tx, int tx_len, uint8_t *rx, int rx_len,
*/
void uart_default_pad_rx_interrupt(enum gpio_signal signal);
+/**
+ * Prepare for following `uart_console_read_buffer()` call. It will create a
+ * snapshot of current uart buffer.
+ *
+ * @return result status (EC_RES_*)
+ */
+int uart_console_read_buffer_init(void);
+
+/**
+ * Read from uart buffer.
+ *
+ * `uart_console_read_buffer_init()` must be called first.
+ *
+ * If `type` is CONSOLE_READ_NEXT, this will return data starting from the
+ * beginning of the last snapshot created by `uart_console_read_buffer_init()`.
+ *
+ * If `type` is CONSOLE_READ_RECENT, this will start from the end of the
+ * previous snapshot (so if current snapshot and previous snapshot has overlaps,
+ * only new content will be returned).
+ *
+ * @param type an ec_console_read_subcmd value.
+ * @param dest output buffer, it will be a null-terminated string.
+ * @param dest_size size of output buffer.
+ * @param write_count number of bytes written (including '\0').
+ *
+ * @return result status (EC_RES_*)
+ */
+int uart_console_read_buffer(uint8_t type,
+ char *dest,
+ uint16_t dest_size,
+ uint16_t *write_count);
+
#endif /* __CROS_EC_UART_H */