virtio-serial: support custom the virtio serial device name

By setting config CONFIG_DRIVERS_VIRTIO_SERIAL_NAME to "ttyXX0;ttyXX1;..."
to customize the virtio serial name.

For example:
If CONFIG_DRIVERS_VIRTIO_SERIAL_NAME="ttyBT;ttyTest0;ttyTest1",
virtio-serial will register three uart devices with names:
"/dev/ttyBT", "/dev/ttyTest0", "/dev/ttyTest1" to the VFS.

nsh> ls dev
/dev:
 console
 null
 telnet
 ttyBT
 ttyS0
 ttyTest0
 ttyTest1
 zero

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
This commit is contained in:
Bowen Wang 2024-01-09 19:16:35 +08:00 committed by Xiang Xiao
parent 84d2aae63b
commit 106040df6c
2 changed files with 65 additions and 13 deletions

View file

@ -95,6 +95,15 @@ config DRIVERS_VIRTIO_SERIAL_CONSOLE
---help---
This enables using first virtio serial device as console.
config DRIVERS_VIRTIO_SERIAL_NAME
string "Virtio serial driver name"
default ""
---help---
Using this config to custom the virtio serial registered device name,
using ";" to split the names.
For example, if DRIVERS_VIRTIO_SERIAL_NAME = "ttyBT;ttyTEL" and pass
three virtio-serial devices to the qemu, we will get three uart devices
with names: "/dev/ttyBT", "/dev/ttyTEL", "/dev/ttyV2"
endif
config DRIVERS_VIRTIO_SOUND

View file

@ -527,6 +527,61 @@ static void virtio_serial_uninit(FAR struct virtio_serial_priv_s *priv)
virtio_free_buf(vdev, priv->udev.recv.buffer);
}
/****************************************************************************
* Name: virtio_serial_uart_register
****************************************************************************/
static int virtio_serial_uart_register(FAR struct virtio_serial_priv_s *priv)
{
FAR const char *name = CONFIG_DRIVERS_VIRTIO_SERIAL_NAME;
bool found = false;
int start = 0;
int ret;
int i;
int j;
for (i = 0, j = 0; name[start] != '\0'; i++)
{
if (name[i] == ';' || name[i] == '\0')
{
if (j++ == g_virtio_serial_idx)
{
found = true;
break;
}
start = i + 1;
}
}
if (found)
{
snprintf(priv->name, NAME_MAX, "/dev/%.*s", i - start, &name[start]);
}
else
{
snprintf(priv->name, NAME_MAX, "/dev/ttyV%d", g_virtio_serial_idx);
}
ret = uart_register(priv->name, &priv->udev);
if (ret < 0)
{
return ret;
}
#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;
}
/****************************************************************************
* Name: virtio_serial_probe
****************************************************************************/
@ -554,25 +609,13 @@ static int virtio_serial_probe(FAR struct virtio_device *vdev)
/* Uart driver register */
snprintf(priv->name, NAME_MAX, "/dev/ttyV%d", g_virtio_serial_idx);
ret = uart_register(priv->name, &priv->udev);
ret = virtio_serial_uart_register(priv);
if (ret < 0)
{
vrterr("uart_register failed, ret=%d\n", ret);
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;
err_with_init: