Skip to content

drivers/rpmsg: add the rpmsg char driver - #21981

Open
CV-Bowen wants to merge 4 commits into
apache:masterfrom
CV-Bowen:up/P103-rpmsg-char
Open

CV-Bowen wants to merge 4 commits into
apache:masterfrom
CV-Bowen:up/P103-rpmsg-char

Conversation

@CV-Bowen

@CV-Bowen CV-Bowen commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

NuttX has no generic way for an application to talk to a remote rpmsg
service: each service needs its own driver. Linux has rpmsg_char for
this; this PR adds the same interface:

  • include/nuttx/rpmsg/rpmsg.h: the ioctls and struct rpmsg_endpoint_info of Linux <linux/rpmsg.h>
    (RPMSG_CREATE_EPT_IOCTL, RPMSG_CREATE_DEV_IOCTL,
    RPMSG_RELEASE_DEV_IOCTL, RPMSG_DESTROY_EPT_IOCTL, and the flow
    control ones).
  • drivers/rpmsg: add the rpmsg char driver (CONFIG_RPMSG_CHAR):
    RPMSG_CREATE_EPT_IOCTL / RPMSG_CREATE_DEV_IOCTL on
    /dev/rpmsg/<cpu> creates an endpoint and registers
    /dev/rpmsg-<name>, which supports read(), write() and poll();
    received messages are queued until read, and the peer's name service
    destroy makes read() return -ECONNRESET and poll() report
    POLLHUP.
  • rpmsg_char: prefix the endpoint name with "rpmsg-raw-", the service
    name the Linux driver binds to, so a NuttX and a Linux rpmsg char
    endpoint find each other.
  • rpmsg_char: copy into the reader's buffer directly when a reader is
    already waiting.

Impact

  • New Kconfig RPMSG_CHAR (default n) and RPMSG_CHAR_NPOLLWAITERS.
  • New public ioctls 8..13 in include/nuttx/rpmsg/rpmsg.h and struct rpmsg_endpoint_info; the existing ioctl numbers do not change.
  • No change unless RPMSG_CHAR is enabled.

Testing

Host: Ubuntu 22.04.5 LTS x86_64, QEMU version 10.2.1, sim: cc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0, qemu-armv8a: aarch64-none-elf-gcc (Arm GNU Toolchain 12.2.Rel1 (Build arm-12.24)) 12.2.1 20221205

qemu-armv8a:rpserver + qemu-armv8a:rpproxy (rptun ivshmem), both with
CONFIG_RPMSG_CHAR. The server runs the rpmsgchar test program
(nuttx-apps examples/rpmsgchar, apache/nuttx-apps#4081); the proxy
runs a small test-only echo server (below, not in this PR) that creates
the same endpoints with RPMSG_CREATE_DEV_IOCTL, writes back every
message, and releases an endpoint once the peer destroys it.

test result (3 runs)
rpmsgchar -t echo -c /dev/rpmsg/proxy -n t0: 1000 write/read round trips of 12 bytes over /dev/rpmsg-t0 Finish the echo test, the echo server counts 1000 messages
rpmsgchar -t destroy -c /dev/rpmsg/proxy -n d: create 100 endpoints, one round trip each, then release them all End the destroy test; no /dev/rpmsg-d* left on the server, heap back to the same number of chunks

On master RPMSG_CHAR does not exist.

log
echo: serving /dev/rpmsg-t0
Open rpmsg char device /dev/rpmsg-t0
Start the echo test
Finish the echo test
echo: exit after 1000 messages
echo: serving 100 endpoints
Start the destroy test
End the destroy test
test-only echo server
#include <nuttx/config.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <nuttx/rpmsg/rpmsg.h>

static int create(int ctrl, const char *name)
{
  struct rpmsg_endpoint_info info;
  char path[64];

  memset(&info, 0, sizeof(info));
  strlcpy(info.name, name, sizeof(info.name));
  info.src = RPMSG_ADDR_ANY;
  info.dst = RPMSG_ADDR_ANY;
  if (ioctl(ctrl, RPMSG_CREATE_DEV_IOCTL, &info) < 0)
    {
      return -1;
    }

  snprintf(path, sizeof(path), "/dev/rpmsg-%s", name);
  return open(path, O_RDWR);
}

int main(int argc, char *argv[])
{
  struct rpmsg_endpoint_info info;
  char name[RPMSG_NAME_SIZE];
  char buf[512];
  ssize_t n;
  long cnt = 0;
  int ctrl;
  int fd;
  int i;

  if (argc < 3)
    {
      printf("usage: %s <ctrl> <name> [count]\n", argv[0]);
      return 1;
    }

  ctrl = open(argv[1], O_RDWR);
  if (ctrl < 0)
    {
      printf("echo: open %s failed\n", argv[1]);
      return 1;
    }

  if (argc == 3)
    {
      fd = create(ctrl, argv[2]);
      if (fd < 0)
        {
          printf("echo: create %s failed\n", argv[2]);
          return 1;
        }

      printf("echo: serving /dev/rpmsg-%s\n", argv[2]);
      while ((n = read(fd, buf, sizeof(buf))) > 0)
        {
          if (write(fd, buf, n) != n)
            {
              break;
            }

          cnt++;
        }

      printf("echo: exit after %ld messages\n", cnt);
      close(fd);
      return 0;
    }

  int num = atoi(argv[3]);
  struct pollfd *fds = calloc(num, sizeof(*fds));
  int left = num;

  for (i = 0; i < num; i++)
    {
      snprintf(name, sizeof(name), "%s%d", argv[2], i);
      fds[i].fd = create(ctrl, name);
      fds[i].events = POLLIN;
      if (fds[i].fd < 0)
        {
          printf("echo: create %s failed\n", name);
          return 1;
        }
    }

  printf("echo: serving %d endpoints\n", num);
  while (left > 0 && poll(fds, num, -1) > 0)
    {
      for (i = 0; i < num; i++)
        {
          if (fds[i].fd < 0 || fds[i].revents == 0)
            {
              continue;
            }

          /* Echo, and keep the endpoint until the peer releases it */

          n = read(fds[i].fd, buf, sizeof(buf));
          if (n > 0)
            {
              if (write(fds[i].fd, buf, n) == n)
                {
                  cnt++;
                }

              continue;
            }

          close(fds[i].fd);
          snprintf(name, sizeof(name), "%s%d", argv[2], i);
          memset(&info, 0, sizeof(info));
          strlcpy(info.name, name, sizeof(info.name));
          info.src = RPMSG_ADDR_ANY;
          info.dst = RPMSG_ADDR_ANY;
          ioctl(ctrl, RPMSG_RELEASE_DEV_IOCTL, &info);
          fds[i].fd = -1;
          left--;
        }
    }

  printf("echo: %ld of %d endpoints echoed and released\n", cnt, num);
  free(fds);
  return 0;
}

Every commit builds with no warning on sim:rpserver with and without
CONFIG_RPMSG_CHAR.

No regression in rpmsg: sim:rpserver/rpproxy and qemu-armv8a:rpserver/rpproxy boot and pass the fixed rpmsg cases (dump, ping, rpmsgfs; 15/15). qemu-armv8a was built with LIBC_ENVPATH and BINFMT_CHECKEXEC disabled, which current master needs for nxinit to start nsh there; unrelated to this change.

CV-Bowen and others added 4 commits November 18, 2026 12:20
Add the ioctl commands and struct rpmsg_endpoint_info of the Linux
<linux/rpmsg.h> interface, for the rpmsg char driver that follows:
RPMSG_CREATE_EPT_IOCTL, RPMSG_CREATE_DEV_IOCTL and RPMSG_RELEASE_DEV_IOCTL
on the rpmsg device, RPMSG_DESTROY_EPT_IOCTL and the flow control ioctls
on an endpoint device.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
Let an application talk to a remote service without a dedicated
driver, with the Linux rpmsg_char interface: RPMSG_CREATE_EPT_IOCTL on
/dev/rpmsg/<cpu> creates an endpoint and registers /dev/rpmsg-<name>,
which then supports read(), write() and poll(). Received messages are
queued per endpoint device until read.

Enabled by CONFIG_RPMSG_CHAR.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
The Linux rpmsg_char driver binds to the "rpmsg-raw" service. Create the
endpoint as "rpmsg-raw-<name>" so that a NuttX rpmsg char endpoint and a
Linux one find each other through the name service.

Signed-off-by: chenzihan1 <chenzihan1@xiaomi.com>
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
When a reader is waiting in read(), the endpoint callback now copies
the message into the reader's buffer instead of allocating an rx buffer
and copying it a second time in read().

Signed-off-by: chenzihan1 <chenzihan1@xiaomi.com>
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: L The size of the change in this PR is large labels Nov 18, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants