From 3fecf46097f48aa072dbf2184f021f69fb161fde Mon Sep 17 00:00:00 2001 From: Yanfeng Liu Date: Wed, 24 Jul 2024 14:51:00 +0800 Subject: [PATCH] virtio/serial: initial CONSOLE support This adds DRIVERS_VIRTIO_SERIAL_CONSOLE config and related logic to virtio serial so that it can be used as console device. Note that due to its dependency on OS services, this console is available late so it is not proper for debugging too early booting issues. Signed-off-by: Yanfeng Liu --- drivers/virtio/Kconfig | 8 ++++++++ drivers/virtio/virtio-serial.c | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index dbb87a140f..2d386ad88a 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -74,6 +74,14 @@ if DRIVERS_VIRTIO_SERIAL config DRIVERS_VIRTIO_SERIAL_BUFSIZE int "Virtio serial driver buffer size" default 256 + +config DRIVERS_VIRTIO_SERIAL_CONSOLE + bool "Virtio serial console" + default n + select SERIAL_CONSOLE + ---help--- + This enables using first virtio serial device as console. + endif config DRIVERS_VIRTIO_SOUND diff --git a/drivers/virtio/virtio-serial.c b/drivers/virtio/virtio-serial.c index 3e3153016b..a7123ca8ee 100644 --- a/drivers/virtio/virtio-serial.c +++ b/drivers/virtio/virtio-serial.c @@ -132,6 +132,10 @@ static const struct uart_ops_s g_virtio_serial_ops = static int g_virtio_serial_idx = 0; +#ifdef CONFIG_DRIVERS_VIRTIO_SERIAL_CONSOLE +static struct uart_dev_s *g_virtio_console; +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -558,6 +562,16 @@ static int virtio_serial_probe(FAR struct virtio_device *vdev) goto err_with_init; } +#ifdef CONFIG_DRIVERS_VIRTIO_SERIAL_CONSOLE + if (g_virtio_console == NULL) + { + DEBUGVERIFY(uart_register("/dev/console", &priv->udev)); + g_virtio_console = &priv->udev; + g_virtio_console->isconsole = true; + } + +#endif + g_virtio_serial_idx++; return ret; @@ -595,3 +609,26 @@ int virtio_register_serial_driver(void) int ret2 = virtio_register_driver(&g_virtio_rprocserial_driver); return ret1 < 0 ? ret1 : ret2; } + +#ifdef CONFIG_DRIVERS_VIRTIO_SERIAL_CONSOLE +/**************************************************************************** + * Name: up_putc + ****************************************************************************/ + +int up_putc(int ch) +{ + if (g_virtio_console != NULL) + { + if (ch == '\n') + { + /* Add CR */ + + virtio_serial_send(g_virtio_console, '\r'); + } + + virtio_serial_send(g_virtio_console, ch); + } + + return ch; +} +#endif