summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/console.c
blob: 58260cf7554925def5ef045eefc8e2c2251d9b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <device.h>
#include <drivers/uart.h>
#include <init.h>
#include <kernel.h>
#include <shell/shell.h>
#include <stdbool.h>
#include <string.h>
#include <sys/printk.h>
#include <zephyr.h>

#include "console.h"
#include "printf.h"
#include "uart.h"

static const struct device *uart_dev;
#ifdef CONFIG_UART_CONSOLE_ON_DEV_NAME
static int init_uart_dev(const struct device *unused)
{
	ARG_UNUSED(unused);
	uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
	return 0;
}
SYS_INIT(init_uart_dev, POST_KERNEL, 50);
#endif

int zshim_run_ec_console_command(int (*handler)(int argc, char **argv),
				 const struct shell *shell, size_t argc,
				 char **argv, const char *help_str,
				 const char *argdesc)
{
	ARG_UNUSED(shell);

	for (int i = 1; i < argc; i++) {
		if (!help_str && !argdesc)
			break;
		if (!strcmp(argv[i], "-h")) {
			if (help_str)
				printk("%s\n", help_str);
			if (argdesc)
				printk("Usage: %s\n", argdesc);
			return 0;
		}
	}

	return handler(argc, argv);
}

#if DT_NODE_EXISTS(DT_PATH(ec_console))
#define EC_CONSOLE DT_PATH(ec_console)

static const char * const disabled_channels[] = DT_PROP(EC_CONSOLE, disabled);
static const size_t disabled_channel_count = DT_PROP_LEN(EC_CONSOLE, disabled);
static int init_ec_console(const struct device *unused)
{
	for (size_t i = 0; i < disabled_channel_count; i++)
		console_channel_disable(disabled_channels[i]);

	return 0;
} SYS_INIT(init_ec_console, PRE_KERNEL_1, 50);
#endif

/*
 * Minimal implementation of a few uart_* functions we need.
 * TODO(b/178033156): probably need to swap this for something more
 * robust in order to handle UART buffering.
 */

int uart_init_done(void)
{
	return true;
}

void uart_tx_start(void)
{
}

int uart_tx_ready(void)
{
	return 1;
}

int uart_tx_char_raw(void *context, int c)
{
	uart_write_char(c);
	return 0;
}

void uart_write_char(char c)
{
	printk("%c", c);

	if (IS_ENABLED(CONFIG_PLATFORM_EC_HOSTCMD_CONSOLE))
		console_buf_notify_char(c);
}

void uart_flush_output(void)
{
}

void uart_tx_flush(void)
{
}

int uart_getc(void)
{
	uint8_t c;

	if (uart_dev && !uart_poll_in(uart_dev, &c))
		return c;
	return -1;
}

void uart_clear_input(void)
{
	/* Not needed since we're not stopping the shell. */
}