2 * Copyright (c) 2011 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
8 * This provide a test serial port. It provides an emulated serial port where
9 * a test program and read out the serial output and inject serial input for
17 #include <linux/compiler.h>
18 #include <asm/state.h>
22 * serial_buf: A buffer that holds keyboard characters for the
26 * serial_buf_write == serial_buf_read -> empty buffer
27 * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
29 static char serial_buf[16];
30 static unsigned int serial_buf_write;
31 static unsigned int serial_buf_read;
33 static int sandbox_serial_init(void)
35 struct sandbox_state *state = state_get_current();
37 if (state->term_raw != STATE_TERM_COOKED)
38 os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
42 static void sandbox_serial_setbrg(void)
46 static void sandbox_serial_putc(const char ch)
51 static void sandbox_serial_puts(const char *str)
53 os_write(1, str, strlen(str));
56 static unsigned int increment_buffer_index(unsigned int index)
58 return (index + 1) % ARRAY_SIZE(serial_buf);
61 static int sandbox_serial_tstc(void)
63 const unsigned int next_index =
64 increment_buffer_index(serial_buf_write);
71 if (next_index == serial_buf_read)
72 return 1; /* buffer full */
74 count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
76 serial_buf_write = next_index;
77 return serial_buf_write != serial_buf_read;
80 static int sandbox_serial_getc(void)
84 while (!sandbox_serial_tstc())
87 result = serial_buf[serial_buf_read];
88 serial_buf_read = increment_buffer_index(serial_buf_read);
92 static struct serial_device sandbox_serial_drv = {
93 .name = "sandbox_serial",
94 .start = sandbox_serial_init,
96 .setbrg = sandbox_serial_setbrg,
97 .putc = sandbox_serial_putc,
98 .puts = sandbox_serial_puts,
99 .getc = sandbox_serial_getc,
100 .tstc = sandbox_serial_tstc,
103 void sandbox_serial_initialize(void)
105 serial_register(&sandbox_serial_drv);
108 __weak struct serial_device *default_serial_console(void)
110 return &sandbox_serial_drv;