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>
21 * serial_buf: A buffer that holds keyboard characters for the
25 * serial_buf_write == serial_buf_read -> empty buffer
26 * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
28 static char serial_buf[16];
29 static unsigned int serial_buf_write;
30 static unsigned int serial_buf_read;
32 static int sandbox_serial_init(void)
38 static void sandbox_serial_setbrg(void)
42 static void sandbox_serial_putc(const char ch)
47 static void sandbox_serial_puts(const char *str)
49 os_write(1, str, strlen(str));
52 static unsigned int increment_buffer_index(unsigned int index)
54 return (index + 1) % ARRAY_SIZE(serial_buf);
57 static int sandbox_serial_tstc(void)
59 const unsigned int next_index =
60 increment_buffer_index(serial_buf_write);
67 if (next_index == serial_buf_read)
68 return 1; /* buffer full */
70 count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
72 serial_buf_write = next_index;
73 return serial_buf_write != serial_buf_read;
76 static int sandbox_serial_getc(void)
80 while (!sandbox_serial_tstc())
83 result = serial_buf[serial_buf_read];
84 serial_buf_read = increment_buffer_index(serial_buf_read);
88 static struct serial_device sandbox_serial_drv = {
89 .name = "sandbox_serial",
90 .start = sandbox_serial_init,
92 .setbrg = sandbox_serial_setbrg,
93 .putc = sandbox_serial_putc,
94 .puts = sandbox_serial_puts,
95 .getc = sandbox_serial_getc,
96 .tstc = sandbox_serial_tstc,
99 void sandbox_serial_initialize(void)
101 serial_register(&sandbox_serial_drv);
104 __weak struct serial_device *default_serial_console(void)
106 return &sandbox_serial_drv;