You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/08/17 10:34:04 UTC
[incubator-nuttx] branch master updated: rpmsgdev: add rpmsg-device support.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 52a1692cfa rpmsgdev: add rpmsg-device support.
52a1692cfa is described below
commit 52a1692cfae1521be58238bbc4d7842bb2211ef5
Author: wangbowen6 <wa...@xiaomi.com>
AuthorDate: Tue Aug 9 16:20:26 2022 +0800
rpmsgdev: add rpmsg-device support.
Signed-off-by: wangbowen6 <wa...@xiaomi.com>
---
boards/sim/sim/sim/configs/rpproxy/defconfig | 5 +
boards/sim/sim/sim/configs/rpserver/defconfig | 5 +
boards/sim/sim/sim/src/sim_bringup.c | 6 +
drivers/drivers_initialize.c | 5 +
drivers/misc/Kconfig | 10 +
drivers/misc/Make.defs | 8 +
drivers/misc/rpmsgdev.c | 1037 +++++++++++++++++++++++++
drivers/misc/rpmsgdev.h | 103 +++
drivers/misc/rpmsgdev_server.c | 352 +++++++++
include/nuttx/drivers/rpmsgdev.h | 90 +++
10 files changed, 1621 insertions(+)
diff --git a/boards/sim/sim/sim/configs/rpproxy/defconfig b/boards/sim/sim/sim/configs/rpproxy/defconfig
index af465845f9..5841640176 100644
--- a/boards/sim/sim/sim/configs/rpproxy/defconfig
+++ b/boards/sim/sim/sim/configs/rpproxy/defconfig
@@ -13,7 +13,11 @@ CONFIG_ARCH_CHIP="sim"
CONFIG_ARCH_SIM=y
CONFIG_BOARDCTL_POWEROFF=y
CONFIG_BUILTIN=y
+CONFIG_DEBUG_ASSERTIONS=y
+CONFIG_DEBUG_ERROR=y
+CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_SYMBOLS=y
+CONFIG_DEV_RPMSG=y
CONFIG_DEV_SIMPLE_ADDRENV=y
CONFIG_EXAMPLES_RPMSGSOCKET=y
CONFIG_FS_PROCFS=y
@@ -22,6 +26,7 @@ CONFIG_IDLETHREAD_STACKSIZE=4096
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INPUT=y
CONFIG_LIBC_HOSTNAME="proxy"
+CONFIG_NDEBUG=y
CONFIG_NET=y
CONFIG_NETDB_DNSCLIENT=y
CONFIG_NETDB_DNSCLIENT_MAXRESPONSE=176
diff --git a/boards/sim/sim/sim/configs/rpserver/defconfig b/boards/sim/sim/sim/configs/rpserver/defconfig
index c040e4140f..e46e2f517e 100644
--- a/boards/sim/sim/sim/configs/rpserver/defconfig
+++ b/boards/sim/sim/sim/configs/rpserver/defconfig
@@ -14,7 +14,11 @@ CONFIG_BOARDCTL_POWEROFF=y
CONFIG_BUILTIN=y
CONFIG_CLK=y
CONFIG_CLK_RPMSG=y
+CONFIG_DEBUG_ASSERTIONS=y
+CONFIG_DEBUG_ERROR=y
+CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_SYMBOLS=y
+CONFIG_DEV_RPMSG_SERVER=y
CONFIG_DEV_SIMPLE_ADDRENV=y
CONFIG_EXAMPLES_RPMSGSOCKET=y
CONFIG_FS_HOSTFS=y
@@ -26,6 +30,7 @@ CONFIG_INPUT=y
CONFIG_IOEXPANDER=y
CONFIG_IOEXPANDER_RPMSG=y
CONFIG_LIBC_HOSTNAME="server"
+CONFIG_NDEBUG=y
CONFIG_NET=y
CONFIG_NETDB_DNSCLIENT=y
CONFIG_NETDB_DNSCLIENT_MAXRESPONSE=176
diff --git a/boards/sim/sim/sim/src/sim_bringup.c b/boards/sim/sim/sim/src/sim_bringup.c
index 0b57627215..11f8eba4ef 100644
--- a/boards/sim/sim/sim/src/sim_bringup.c
+++ b/boards/sim/sim/sim/src/sim_bringup.c
@@ -34,6 +34,7 @@
#include <nuttx/mtd/mtd.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/nxffs.h>
+#include <nuttx/drivers/rpmsgdev.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/spi/spi_transfer.h>
#include <nuttx/rc/lirc_dev.h>
@@ -446,6 +447,11 @@ int sim_bringup(void)
#else
up_rptun_init("server-proxy", "server", false);
#endif
+
+#ifdef CONFIG_DEV_RPMSG
+ rpmsgdev_register("server", "/dev/console", "/dev/server-console");
+ rpmsgdev_register("server", "/dev/null", "/dev/server-null");
+#endif
#endif
#ifdef CONFIG_SIM_WTGAHRS2_UARTN
diff --git a/drivers/drivers_initialize.c b/drivers/drivers_initialize.c
index 334d3ecf5c..605f35a1a4 100644
--- a/drivers/drivers_initialize.c
+++ b/drivers/drivers_initialize.c
@@ -25,6 +25,7 @@
#include <nuttx/clk/clk_provider.h>
#include <nuttx/crypto/crypto.h>
#include <nuttx/drivers/drivers.h>
+#include <nuttx/drivers/rpmsgdev.h>
#include <nuttx/fs/loop.h>
#include <nuttx/input/uinput.h>
#include <nuttx/net/loopback.h>
@@ -162,4 +163,8 @@ void drivers_initialize(void)
#ifdef CONFIG_SENSORS_RPMSG
sensor_rpmsg_initialize();
#endif
+
+#ifdef CONFIG_DEV_RPMSG_SERVER
+ rpmsgdev_server_init();
+#endif
}
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index 780f805f40..95be70c993 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -15,6 +15,16 @@ config DEV_ZERO
bool "Enable /dev/zero"
default n
+config DEV_RPMSG
+ bool "RPMSG Device Client Support"
+ default n
+ depends on RPTUN
+
+config DEV_RPMSG_SERVER
+ bool "RPMSG Device Server Support"
+ default n
+ depends on RPTUN
+
config DRVR_MKRD
bool "RAM disk wrapper (mkrd)"
default n
diff --git a/drivers/misc/Make.defs b/drivers/misc/Make.defs
index ae0e3b6537..30ef53ec16 100644
--- a/drivers/misc/Make.defs
+++ b/drivers/misc/Make.defs
@@ -47,6 +47,14 @@ else ifeq ($(CONFIG_DRVR_READAHEAD),y)
CSRCS += rwbuffer.c
endif
+ifeq ($(CONFIG_DEV_RPMSG),y)
+ CSRCS += rpmsgdev.c
+endif
+
+ifeq ($(CONFIG_DEV_RPMSG_SERVER),y)
+ CSRCS += rpmsgdev_server.c
+endif
+
# Include build support
DEPPATH += --dep-path misc
diff --git a/drivers/misc/rpmsgdev.c b/drivers/misc/rpmsgdev.c
new file mode 100644
index 0000000000..6d8628334b
--- /dev/null
+++ b/drivers/misc/rpmsgdev.c
@@ -0,0 +1,1037 @@
+/****************************************************************************
+ * drivers/misc/rpmsgdev.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/types.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <limits.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/fs/ioctl.h>
+#include <nuttx/video/fb.h>
+#include <nuttx/mutex.h>
+#include <nuttx/rptun/openamp.h>
+
+#include "rpmsgdev.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifndef rpmsgdeverr
+# define rpmsgdeverr(fmt, ...)
+#endif
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct rpmsgdev_s
+{
+ struct rpmsg_endpoint ept; /* Rpmsg endpoint */
+ FAR const char *remotecpu; /* The server cpu name */
+ FAR const char *remotepath; /* The device path in the server cpu */
+ sem_t wait; /* Wait sem, used for preventing any
+ * opreation until the connection
+ * between two cpu established.
+ */
+ mutex_t excl; /* Exclusive mutex, used to support thread
+ * safe.
+ */
+ int open_count; /* Device open count */
+};
+
+/* Rpmsg device cookie used to handle the response from the remote cpu */
+
+struct rpmsgdev_cookie_s
+{
+ sem_t sem; /* Semaphore used fo rpmsg */
+ int result; /* The return value of the remote call */
+ FAR void *data; /* The return data buffer of the remote call */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* The file operation functions */
+
+static int rpmsgdev_open(FAR struct file *filep);
+static int rpmsgdev_close(FAR struct file *filep);
+static ssize_t rpmsgdev_read(FAR struct file *filep, FAR char *buffer,
+ size_t buflen);
+static ssize_t rpmsgdev_write(FAR struct file *filep, FAR const char *buffer,
+ size_t buflen);
+static off_t rpmsgdev_seek(FAR struct file *filep, off_t offset,
+ int whence);
+static size_t rpmsgdev_ioctl_arglen(int cmd);
+static int rpmsgdev_ioctl(FAR struct file *filep, int cmd,
+ unsigned long arg);
+
+/* Functions for sending data to the remote cpu */
+
+static int rpmsgdev_send_recv(FAR struct rpmsgdev_s *priv,
+ uint32_t command, bool copy,
+ FAR struct rpmsgdev_header_s *msg,
+ int len, FAR void *data);
+static FAR void *rpmsgdev_get_tx_payload_buffer(FAR struct rpmsgdev_s *priv,
+ FAR uint32_t *len);
+
+/* Functions handle the responses from the remote cpu */
+
+static int rpmsgdev_default_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv);
+static int rpmsgdev_read_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv);
+static int rpmsgdev_ioctl_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv);
+
+/* Functions for creating communication with remote cpu */
+
+static void rpmsgdev_device_created(struct rpmsg_device *rdev,
+ FAR void *priv_);
+static void rpmsgdev_device_destroy(struct rpmsg_device *rdev,
+ FAR void *priv_);
+static int rpmsgdev_ept_cb(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len, uint32_t src,
+ FAR void *priv);
+static void rpmsgdev_ns_bound(struct rpmsg_endpoint *ept);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Rpmsg device response handler table */
+
+static const rpmsg_ept_cb g_rpmsgdev_handler[] =
+{
+ [RPMSGDEV_OPEN] = rpmsgdev_default_handler,
+ [RPMSGDEV_CLOSE] = rpmsgdev_default_handler,
+ [RPMSGDEV_READ] = rpmsgdev_read_handler,
+ [RPMSGDEV_WRITE] = rpmsgdev_default_handler,
+ [RPMSGDEV_LSEEK] = rpmsgdev_default_handler,
+ [RPMSGDEV_IOCTL] = rpmsgdev_ioctl_handler,
+};
+
+/* File operations */
+
+const struct file_operations g_rpmsgdev_ops =
+{
+ rpmsgdev_open, /* open */
+ rpmsgdev_close, /* close */
+ rpmsgdev_read, /* read */
+ rpmsgdev_write, /* write */
+ rpmsgdev_seek, /* seek */
+ rpmsgdev_ioctl, /* ioctl */
+ NULL /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+ , NULL /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: rpmsgdev_open
+ *
+ * Description:
+ * Rpmsg-device open operation
+ *
+ * Parameters:
+ * filep - the file instance
+ *
+ * Returned Values:
+ * OK on success; A negated errno value is returned on any failure.
+ *
+ ****************************************************************************/
+
+static int rpmsgdev_open(FAR struct file *filep)
+{
+ FAR struct rpmsgdev_s *dev;
+ struct rpmsgdev_open_s msg;
+ int ret;
+
+ /* Sanity checks */
+
+ DEBUGASSERT(filep->f_inode != NULL);
+
+ /* Get the mountpoint inode reference from the file structure and the
+ * mountpoint private data from the inode structure
+ */
+
+ dev = filep->f_inode->i_private;
+ DEBUGASSERT(dev != NULL);
+
+ /* Take the semaphore */
+
+ ret = nxmutex_lock(&dev->excl);
+ if (ret < 0)
+ {
+ rpmsgdeverr("semtake error, ret=%d\n", ret);
+ return ret;
+ }
+
+ /* Check if this is the first time that the driver has been opened. */
+
+ if (dev->open_count++ == 0)
+ {
+ /* Try to open the file in the host file system */
+
+ msg.flags = filep->f_oflags;
+
+ ret = rpmsgdev_send_recv(dev, RPMSGDEV_OPEN, true, &msg.header,
+ sizeof(msg), NULL);
+ if (ret < 0)
+ {
+ rpmsgdeverr("open failed\n");
+ dev->open_count--;
+ }
+ }
+
+ nxmutex_unlock(&dev->excl);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_close
+ *
+ * Description:
+ * Rpmsg-device close operation
+ *
+ * Parameters:
+ * filep - the file instance
+ *
+ * Returned Values:
+ * OK on success; A negated errno value is returned on any failure.
+ *
+ ****************************************************************************/
+
+static int rpmsgdev_close(FAR struct file *filep)
+{
+ FAR struct rpmsgdev_s *dev;
+ struct rpmsgdev_close_s msg;
+ int ret;
+
+ /* Sanity checks */
+
+ DEBUGASSERT(filep->f_inode != NULL);
+
+ /* Recover our private data from the struct file instance */
+
+ dev = filep->f_inode->i_private;
+ DEBUGASSERT(dev != NULL);
+
+ /* Take the semaphore */
+
+ ret = nxmutex_lock(&dev->excl);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* There are no more references to the port */
+
+ if (--dev->open_count == 0)
+ {
+ ret = rpmsgdev_send_recv(dev, RPMSGDEV_CLOSE, true, &msg.header,
+ sizeof(msg), NULL);
+ if (ret < 0)
+ {
+ dev->open_count++;
+ }
+ }
+
+ nxmutex_unlock(&dev->excl);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_read
+ *
+ * Description:
+ * Rpmsg-device read operation
+ *
+ * Parameters:
+ * filep - the file instance
+ * buffer - the read buffer pointer
+ * buflen - the read buffer length
+ *
+ * Returned Values:
+ * The positive non-zero number of bytes read on success, 0 on if an
+ * end-of-file condition, or a negated errno value on any failure.
+ *
+ ****************************************************************************/
+
+static ssize_t rpmsgdev_read(FAR struct file *filep, FAR char *buffer,
+ size_t buflen)
+{
+ FAR struct rpmsgdev_s *dev;
+ struct rpmsgdev_read_s msg;
+ struct iovec read;
+ ssize_t ret;
+
+ if (buffer == NULL)
+ {
+ return -EINVAL;
+ }
+
+ /* Sanity checks */
+
+ DEBUGASSERT(filep->f_inode != NULL);
+
+ /* Recover our private data from the struct file instance */
+
+ dev = filep->f_inode->i_private;
+ DEBUGASSERT(dev != NULL);
+
+ /* Take the semaphore */
+
+ ret = nxmutex_lock(&dev->excl);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* Call the host to perform the read */
+
+ read.iov_base = buffer;
+ read.iov_len = 0;
+
+ msg.count = buflen;
+
+ ret = rpmsgdev_send_recv(dev, RPMSGDEV_READ, true, &msg.header,
+ sizeof(msg) - 1, &read);
+
+ nxmutex_unlock(&dev->excl);
+
+ return read.iov_len ? read.iov_len : ret;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_write
+ *
+ * Description:
+ * Rpmsg-device write operation
+ *
+ * Parameters:
+ * filep - the file instance
+ * buffer - the write buffer pointer
+ * buflen - the write buffer length
+ *
+ * Returned Values:
+ * On success, the number of bytes written are returned (zero indicates
+ * nothing was written). On any failure, a negated errno value is returned
+ * (see comments withwrite() for a description of the appropriate errno
+ * values).
+ *
+ ****************************************************************************/
+
+static ssize_t rpmsgdev_write(FAR struct file *filep, const char *buffer,
+ size_t buflen)
+{
+ FAR struct rpmsgdev_s *dev;
+ FAR struct rpmsgdev_write_s *msg;
+ struct rpmsgdev_cookie_s cookie;
+ uint32_t space;
+ size_t written = 0;
+ int ret;
+
+ if (buffer == NULL)
+ {
+ return -EINVAL;
+ }
+
+ DEBUGASSERT(filep->f_inode != NULL);
+
+ /* Recover our private data from the struct file instance */
+
+ dev = filep->f_inode->i_private;
+ DEBUGASSERT(dev != NULL);
+
+ /* Take the semaphore */
+
+ ret = nxmutex_lock(&dev->excl);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* Perform the rpmsg write */
+
+ memset(&cookie, 0, sizeof(cookie));
+ nxsem_init(&cookie.sem, 0, 0);
+ nxsem_set_protocol(&cookie.sem, SEM_PRIO_NONE);
+
+ while (written < buflen)
+ {
+ msg = rpmsgdev_get_tx_payload_buffer(dev, &space);
+ if (msg == NULL)
+ {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ space -= sizeof(*msg) - 1;
+ if (space >= buflen - written)
+ {
+ /* Send complete, set cookie is valid, need ack */
+
+ space = buflen - written;
+ msg->header.cookie = (uintptr_t)&cookie;
+ }
+ else
+ {
+ /* Not send complete, set cookie invalid, do not need ack */
+
+ msg->header.cookie = 0;
+ }
+
+ msg->header.command = RPMSGDEV_WRITE;
+ msg->header.result = -ENXIO;
+ msg->count = space;
+ memcpy(msg->buf, buffer + written, space);
+
+ ret = rpmsg_send_nocopy(&dev->ept, msg, sizeof(*msg) - 1 + space);
+ if (ret < 0)
+ {
+ goto out;
+ }
+
+ written += space;
+ }
+
+ ret = rpmsg_wait(&dev->ept, &cookie.sem);
+ if (ret < 0)
+ {
+ goto out;
+ }
+
+ ret = cookie.result;
+
+out:
+ nxsem_destroy(&cookie.sem);
+ nxmutex_unlock(&dev->excl);
+
+ return ret < 0 ? ret : buflen;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_seek
+ *
+ * Description:
+ * Rpmsg-device seek operation
+ *
+ * Parameters:
+ * file File structure instance
+ * offset Defines the offset to position to
+ * whence Defines how to use offset
+ *
+ * Returned Values:
+ * The resulting offset on success. A negated errno value is returned on
+ * any failure (see lseek comments).
+ *
+ ****************************************************************************/
+
+static off_t rpmsgdev_seek(FAR struct file *filep, off_t offset, int whence)
+{
+ FAR struct rpmsgdev_s *dev;
+ struct rpmsgdev_lseek_s msg;
+ off_t ret;
+
+ /* Sanity checks */
+
+ DEBUGASSERT(filep->f_inode != NULL);
+
+ /* Recover our private data from the struct file instance */
+
+ dev = filep->f_inode->i_private;
+ DEBUGASSERT(dev != NULL);
+
+ /* Take the semaphore */
+
+ ret = nxmutex_lock(&dev->excl);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* Call our internal routine to perform the seek */
+
+ msg.offset = offset;
+ msg.whence = whence;
+
+ ret = rpmsgdev_send_recv(dev, RPMSGDEV_LSEEK, true, &msg.header,
+ sizeof(msg), NULL);
+
+ nxmutex_unlock(&dev->excl);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ioctl_arglen
+ *
+ * Description:
+ * Get rpmsg device ioctl argument length according to the command
+ *
+ * Parameters:
+ * cmd - the ioctl command
+ *
+ * Returned Values:
+ * 0 - ioctl command not support
+ * positive - the argument length
+ *
+ ****************************************************************************/
+
+static size_t rpmsgdev_ioctl_arglen(int cmd)
+{
+ switch (cmd)
+ {
+ case FIONBIO:
+ case FIONWRITE:
+ case FIONREAD:
+ case FIONSPACE:
+ case FBIOSET_POWER:
+ case FBIOGET_POWER:
+ return sizeof(int);
+ default:
+ return 0;
+ }
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ioctl
+ *
+ * Description:
+ * Rpmsg-device ioctl operation
+ *
+ * Parameters:
+ * filep - the file instance
+ * cmd - the ioctl command
+ * arg - the ioctl arguments
+ *
+ * Returned Values:
+ * OK on success; A negated errno value is returned on any failure.
+ *
+ ****************************************************************************/
+
+static int rpmsgdev_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
+{
+ FAR struct rpmsgdev_s *dev;
+ FAR struct rpmsgdev_ioctl_s *msg;
+ uint32_t space;
+ size_t arglen;
+ size_t msglen;
+ int ret;
+
+ /* Sanity checks */
+
+ DEBUGASSERT(filep->f_inode != NULL);
+
+ /* Recover our private data from the struct file instance */
+
+ dev = filep->f_inode->i_private;
+ DEBUGASSERT(dev != NULL);
+
+ /* Take the semaphore */
+
+ ret = nxmutex_lock(&dev->excl);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* Call our internal routine to perform the ioctl */
+
+ arglen = rpmsgdev_ioctl_arglen(cmd);
+ msglen = sizeof(*msg) + arglen - 1;
+
+ msg = rpmsgdev_get_tx_payload_buffer(dev, &space);
+ if (msg == NULL)
+ {
+ return -ENOMEM;
+ }
+
+ msg->request = cmd;
+ msg->arg = arg;
+ msg->arglen = arglen;
+
+ if (arglen > 0)
+ {
+ memcpy(msg->buf, (FAR void *)(uintptr_t)arg, arglen);
+ }
+
+ ret = rpmsgdev_send_recv(dev, RPMSGDEV_IOCTL, false, &msg->header,
+ msglen, arglen > 0 ? (FAR void *)arg : NULL);
+
+ nxmutex_unlock(&dev->excl);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_get_tx_payload_buffer
+ *
+ * Description:
+ * Get the rpmsg device tx payload, the buffer is from the rpmsg
+ * share memory that can be accessed by local and remote cpu.
+ *
+ * Parameters:
+ * priv - The rpmsg-device handle
+ * len - The got memroy size
+ *
+ * Returned Values:
+ * NULL - failure
+ * not NULL - success
+ *
+ ****************************************************************************/
+
+static FAR void *rpmsgdev_get_tx_payload_buffer(FAR struct rpmsgdev_s *priv,
+ FAR uint32_t *len)
+{
+ int sval;
+
+ nxsem_get_value(&priv->wait, &sval);
+ if (sval <= 0)
+ {
+ rpmsg_wait(&priv->ept, &priv->wait);
+ rpmsg_post(&priv->ept, &priv->wait);
+ }
+
+ return rpmsg_get_tx_payload_buffer(&priv->ept, len, true);
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_send_recv
+ *
+ * Description:
+ * Send and receive the rpmsg data.
+ *
+ * Parameters:
+ * priv - rpmsg device handle
+ * command - the command, RPMSGDEV_OPEN, RPMSGDEV_CLOSE, RPMSGDEV_READ,
+ * RPMSGDEV_WRITE, RPMSGDEV_IOCTL
+ * copy - true, send a message across to the remote processor, and the
+ * tx buffer will be alloced inside function rpmsg_send()
+ * false, send a message in tx buffer reserved by
+ * rpmsg_get_tx_payload_buffer() across to the remote
+ * processor.
+ * msg - the message header
+ * len - length of the payload
+ * data - the data
+ *
+ * Returned Values:
+ * OK on success; A negated errno value is returned on any failure.
+ *
+ ****************************************************************************/
+
+static int rpmsgdev_send_recv(FAR struct rpmsgdev_s *priv,
+ uint32_t command, bool copy,
+ FAR struct rpmsgdev_header_s *msg,
+ int len, FAR void *data)
+{
+ struct rpmsgdev_cookie_s cookie;
+ int ret;
+
+ memset(&cookie, 0, sizeof(cookie));
+ nxsem_init(&cookie.sem, 0, 0);
+ nxsem_set_protocol(&cookie.sem, SEM_PRIO_NONE);
+
+ if (data != NULL)
+ {
+ cookie.data = data;
+ }
+ else if (copy)
+ {
+ cookie.data = msg;
+ }
+
+ msg->command = command;
+ msg->result = -ENXIO;
+ msg->cookie = (uintptr_t)&cookie;
+
+ if (copy)
+ {
+ ret = rpmsg_send(&priv->ept, msg, len);
+ }
+ else
+ {
+ ret = rpmsg_send_nocopy(&priv->ept, msg, len);
+ }
+
+ if (ret < 0)
+ {
+ goto fail;
+ }
+
+ ret = rpmsg_wait(&priv->ept, &cookie.sem);
+ if (ret >= 0)
+ {
+ ret = cookie.result;
+ }
+
+fail:
+ nxsem_destroy(&cookie.sem);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_default_handler
+ *
+ * Description:
+ * Default rpmsg-device response handler, this function will be called to
+ * process the return message of rpmsgdev_open(), rpmsgdev_close() and
+ * rpmsgdev_write().
+ *
+ * Parameters:
+ * ept - The rpmsg endpoint
+ * data - The return message
+ * len - The return message length
+ * src - unknow
+ * priv - unknow
+ *
+ * Returned Values:
+ * Always OK
+ *
+ ****************************************************************************/
+
+static int rpmsgdev_default_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv)
+{
+ FAR struct rpmsgdev_header_s *header = data;
+ FAR struct rpmsgdev_cookie_s *cookie =
+ (FAR struct rpmsgdev_cookie_s *)(uintptr_t)header->cookie;
+
+ cookie->result = header->result;
+ if (cookie->result >= 0 && cookie->data)
+ {
+ memcpy(cookie->data, data, len);
+ }
+
+ rpmsg_post(ept, &cookie->sem);
+ return 0;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_read_handler
+ *
+ * Description:
+ * Rpmsg-device read response handler, this function will be called to
+ * process the return message of rpmsgdev_read().
+ *
+ * Parameters:
+ * ept - The rpmsg endpoint
+ * data - The return message
+ * len - The return message length
+ * src - unknow
+ * priv - unknow
+ *
+ * Returned Values:
+ * Always OK
+ *
+ ****************************************************************************/
+
+static int rpmsgdev_read_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv)
+{
+ FAR struct rpmsgdev_header_s *header = data;
+ FAR struct rpmsgdev_cookie_s *cookie =
+ (FAR struct rpmsgdev_cookie_s *)(uintptr_t)header->cookie;
+ FAR struct rpmsgdev_read_s *rsp = data;
+ FAR struct iovec *read = cookie->data;
+
+ cookie->result = header->result;
+ if (cookie->result > 0)
+ {
+ memcpy(read->iov_base + read->iov_len, rsp->buf, cookie->result);
+ read->iov_len += cookie->result;
+ }
+
+ if (cookie->result <= 0 || read->iov_len >= rsp->count)
+ {
+ rpmsg_post(ept, &cookie->sem);
+ }
+
+ return 0;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ioctl_handler
+ *
+ * Description:
+ * Rpmsg-device ioctl response handler, this function will be called to
+ * process the return message of rpmsgdev_ioctl().
+ *
+ * Parameters:
+ * ept - The rpmsg endpoint
+ * data - The return message
+ * len - The return message length
+ * src - unknow
+ * priv - unknow
+ *
+ * Returned Values:
+ * Always OK
+ *
+ ****************************************************************************/
+
+static int rpmsgdev_ioctl_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv)
+{
+ FAR struct rpmsgdev_header_s *header = data;
+ FAR struct rpmsgdev_cookie_s *cookie =
+ (FAR struct rpmsgdev_cookie_s *)(uintptr_t)header->cookie;
+ FAR struct rpmsgdev_ioctl_s *rsp = data;
+
+ if (cookie->result >= 0 && rsp->arglen > 0)
+ {
+ memcpy(cookie->data, (FAR void *)(uintptr_t)rsp->buf, rsp->arglen);
+ }
+
+ rpmsg_post(ept, &cookie->sem);
+ return 0;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ns_bound
+ *
+ * Description:
+ * Rpmsg device end point service bound callback function , called when
+ * remote end point address is received.
+ *
+ * Parameters:
+ * ept - The rpmsg-device end point
+ *
+ * Returned Values:
+ * None
+ *
+ ****************************************************************************/
+
+static void rpmsgdev_ns_bound(FAR struct rpmsg_endpoint *ept)
+{
+ FAR struct rpmsgdev_s *priv = ept->priv;
+
+ rpmsg_post(&priv->ept, &priv->wait);
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_device_created
+ *
+ * Description:
+ * Rpmsg device create function, this function will be called by rptun to
+ * create a rpmsg-device end point.
+ *
+ * Parameters:
+ * rdev - The rpmsg-device end point
+ * priv_ - Rpmsg-device handle
+ *
+ * Returned Values:
+ * None
+ *
+ ****************************************************************************/
+
+static void rpmsgdev_device_created(FAR struct rpmsg_device *rdev,
+ FAR void *priv_)
+{
+ FAR struct rpmsgdev_s *priv = priv_;
+ char buf[RPMSG_NAME_SIZE];
+
+ if (strcmp(priv->remotecpu, rpmsg_get_cpuname(rdev)) == 0)
+ {
+ priv->ept.priv = priv;
+ priv->ept.ns_bound_cb = rpmsgdev_ns_bound;
+ snprintf(buf, sizeof(buf), "%s%s", RPMSGDEV_NAME_PREFIX,
+ priv->remotepath);
+ rpmsg_create_ept(&priv->ept, rdev, buf,
+ RPMSG_ADDR_ANY, RPMSG_ADDR_ANY,
+ rpmsgdev_ept_cb, NULL);
+ }
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_device_destroy
+ *
+ * Description:
+ * Rpmsg device destroy function, this function will be called by rptun to
+ * destroy rpmsg-device end point.
+ *
+ * Parameters:
+ * rdev - The rpmsg-device end point
+ * priv_ - Rpmsg-device handle
+ *
+ * Returned Values:
+ * None
+ *
+ ****************************************************************************/
+
+static void rpmsgdev_device_destroy(FAR struct rpmsg_device *rdev,
+ FAR void *priv_)
+{
+ FAR struct rpmsgdev_s *priv = priv_;
+
+ if (strcmp(priv->remotecpu, rpmsg_get_cpuname(rdev)) == 0)
+ {
+ rpmsg_destroy_ept(&priv->ept);
+ }
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ept_cb
+ *
+ * Description:
+ * Rpmsg device end point callback function, this function will be called
+ * when receive the remote cpu message.
+ *
+ * Parameters:
+ * ept - The rpmsg-device end point
+ * data - The received data
+ * len - The received data length
+ * src - unknow
+ * priv - unknow
+ *
+ * Returned Values:
+ * OK on success; A negated errno value is returned on any failure.
+ *
+ ****************************************************************************/
+
+static int rpmsgdev_ept_cb(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len, uint32_t src,
+ FAR void *priv)
+{
+ FAR struct rpmsgdev_header_s *header = data;
+ uint32_t command = header->command;
+
+ if (command < ARRAY_SIZE(g_rpmsgdev_handler))
+ {
+ return g_rpmsgdev_handler[command](ept, data, len, src, priv);
+ }
+
+ return -EINVAL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: rpmsgdev_register
+ *
+ * Description:
+ * Rpmsg-device client initialize function, the client cpu should call
+ * this function in the board initialize process.
+ *
+ * Parameters:
+ * remotecpu - the server cpu name
+ * remotepath - the device you want to access in the remote cpu
+ * localpath - the device path in local cpu, if NULL, the localpath is
+ * same as the remotepath, provide this argument to supoort
+ * custom device path
+ *
+ * Returned Values:
+ * OK on success; A negated errno value is returned on any failure.
+ *
+ ****************************************************************************/
+
+int rpmsgdev_register(FAR const char *remotecpu, FAR const char *remotepath,
+ FAR const char *localpath)
+{
+ FAR struct rpmsgdev_s *dev;
+ int ret;
+
+ /* Arguments check */
+
+ if (remotecpu == NULL || remotepath == NULL)
+ {
+ return -EINVAL;
+ }
+
+ dev = kmm_zalloc(sizeof(*dev));
+ if (dev == NULL)
+ {
+ return -ENOMEM;
+ }
+
+ /* Initialize the rpmsg device */
+
+ dev->remotecpu = remotecpu;
+ dev->remotepath = remotepath;
+
+ nxmutex_init(&dev->excl);
+
+ nxsem_init(&dev->wait, 0, 0);
+ nxsem_set_protocol(&dev->wait, SEM_PRIO_NONE);
+
+ /* Register the rpmsg callback */
+
+ ret = rpmsg_register_callback(dev,
+ rpmsgdev_device_created,
+ rpmsgdev_device_destroy,
+ NULL,
+ NULL);
+ if (ret < 0)
+ {
+ rpmsgdeverr("register callback failed, ret=%d\n", ret);
+ goto fail;
+ }
+
+ /* Register driver, using the remotepath if localpath is NULL */
+
+ if (localpath == NULL)
+ {
+ localpath = remotepath;
+ }
+
+ ret = register_driver(localpath, &g_rpmsgdev_ops, 0666, dev);
+ if (ret < 0)
+ {
+ rpmsgdeverr("register driver failed, ret=%d\n", ret);
+ goto fail_with_rpmsg;
+ }
+
+ return OK;
+
+fail_with_rpmsg:
+ rpmsg_unregister_callback(dev,
+ rpmsgdev_device_created,
+ rpmsgdev_device_destroy,
+ NULL,
+ NULL);
+
+fail:
+ nxmutex_destroy(&dev->excl);
+ nxsem_destroy(&dev->wait);
+ kmm_free(dev);
+
+ return ret;
+}
diff --git a/drivers/misc/rpmsgdev.h b/drivers/misc/rpmsgdev.h
new file mode 100644
index 0000000000..f51a7056f0
--- /dev/null
+++ b/drivers/misc/rpmsgdev.h
@@ -0,0 +1,103 @@
+/****************************************************************************
+ * drivers/misc/rpmsgdev.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __DRIVERS_MISC_RPMSGDEV_H
+#define __DRIVERS_MISC_RPMSGDEV_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/compiler.h>
+
+/****************************************************************************
+ * Pre-processor definitions
+ ****************************************************************************/
+
+#ifndef ARRAY_SIZE
+# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#endif
+
+#define RPMSGDEV_NAME_PREFIX "rpmsgdev-"
+#define RPMSGDEV_NAME_PREFIX_LEN 9
+
+#define RPMSGDEV_OPEN 1
+#define RPMSGDEV_CLOSE 2
+#define RPMSGDEV_READ 3
+#define RPMSGDEV_WRITE 4
+#define RPMSGDEV_LSEEK 5
+#define RPMSGDEV_IOCTL 6
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+begin_packed_struct struct rpmsgdev_header_s
+{
+ uint32_t command;
+ int32_t result;
+ uint64_t cookie;
+} end_packed_struct;
+
+begin_packed_struct struct rpmsgdev_open_s
+{
+ struct rpmsgdev_header_s header;
+ int32_t flags;
+} end_packed_struct;
+
+begin_packed_struct struct rpmsgdev_close_s
+{
+ struct rpmsgdev_header_s header;
+} end_packed_struct;
+
+begin_packed_struct struct rpmsgdev_read_s
+{
+ struct rpmsgdev_header_s header;
+ uint32_t count;
+ char buf[1];
+} end_packed_struct;
+
+#define rpmsgdev_write_s rpmsgdev_read_s
+
+begin_packed_struct struct rpmsgdev_lseek_s
+{
+ struct rpmsgdev_header_s header;
+ int32_t whence;
+ int32_t offset;
+} end_packed_struct;
+
+begin_packed_struct struct rpmsgdev_ioctl_s
+{
+ struct rpmsgdev_header_s header;
+ int32_t request;
+ uint64_t arg;
+ uint32_t arglen;
+ char buf[1];
+} end_packed_struct;
+
+/****************************************************************************
+ * Internal function prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Internal data
+ ****************************************************************************/
+
+#endif /* __DRIVERS_MISC_RPMSGDEV_H */
diff --git a/drivers/misc/rpmsgdev_server.c b/drivers/misc/rpmsgdev_server.c
new file mode 100644
index 0000000000..6561f8d7f0
--- /dev/null
+++ b/drivers/misc/rpmsgdev_server.c
@@ -0,0 +1,352 @@
+/****************************************************************************
+ * drivers/misc/rpmsgdev_server.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/drivers/rpmsgdev.h>
+#include <nuttx/rptun/openamp.h>
+
+#include "rpmsgdev.h"
+
+/****************************************************************************
+ * Pre-processor definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct rpmsgdev_server_s
+{
+ struct rpmsg_endpoint ept;
+ struct file file;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Functions handle the messages from the client cpu */
+
+static int rpmsgdev_open_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv);
+static int rpmsgdev_close_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv);
+static int rpmsgdev_read_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv);
+static int rpmsgdev_write_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv);
+static int rpmsgdev_lseek_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv);
+static int rpmsgdev_ioctl_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv);
+
+/* Functions for creating communication with client cpu */
+
+static bool rpmsgdev_ns_match(FAR struct rpmsg_device *rdev,
+ FAR void *priv, FAR const char *name,
+ uint32_t dest);
+static void rpmsgdev_ns_bind(FAR struct rpmsg_device *rdev,
+ FAR void *priv, FAR const char *name,
+ uint32_t dest);
+static void rpmsgdev_ns_unbind(FAR struct rpmsg_endpoint *ept);
+static int rpmsgdev_ept_cb(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len, uint32_t src,
+ FAR void *priv);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const rpmsg_ept_cb g_rpmsgdev_handler[] =
+{
+ [RPMSGDEV_OPEN] = rpmsgdev_open_handler,
+ [RPMSGDEV_CLOSE] = rpmsgdev_close_handler,
+ [RPMSGDEV_READ] = rpmsgdev_read_handler,
+ [RPMSGDEV_WRITE] = rpmsgdev_write_handler,
+ [RPMSGDEV_LSEEK] = rpmsgdev_lseek_handler,
+ [RPMSGDEV_IOCTL] = rpmsgdev_ioctl_handler,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: rpmsgdev_open_handler
+ ****************************************************************************/
+
+static int rpmsgdev_open_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv)
+{
+ FAR struct rpmsgdev_server_s *server = ept->priv;
+ FAR struct rpmsgdev_open_s *msg = data;
+
+ msg->header.result = file_open(&server->file,
+ &ept->name[RPMSGDEV_NAME_PREFIX_LEN],
+ msg->flags, 0);
+
+ return rpmsg_send(ept, msg, sizeof(*msg));
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_close_handler
+ ****************************************************************************/
+
+static int rpmsgdev_close_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv)
+{
+ FAR struct rpmsgdev_server_s *server = ept->priv;
+ FAR struct rpmsgdev_close_s *msg = data;
+
+ msg->header.result = file_close(&server->file);
+
+ return rpmsg_send(ept, msg, sizeof(*msg));
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_read_handler
+ ****************************************************************************/
+
+static int rpmsgdev_read_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv)
+{
+ FAR struct rpmsgdev_server_s *server = ept->priv;
+ FAR struct rpmsgdev_read_s *msg = data;
+ FAR struct rpmsgdev_read_s *rsp;
+ int ret = -ENOENT;
+ size_t read = 0;
+ uint32_t space;
+
+ while (read < msg->count)
+ {
+ rsp = rpmsg_get_tx_payload_buffer(ept, &space, true);
+ if (rsp == NULL)
+ {
+ return -ENOMEM;
+ }
+
+ *rsp = *msg;
+
+ space -= sizeof(*msg) - 1;
+ if (space > msg->count - read)
+ {
+ space = msg->count - read;
+ }
+
+ ret = file_read(&server->file, rsp->buf, space);
+
+ rsp->header.result = ret;
+ rpmsg_send_nocopy(ept, rsp, (ret < 0 ? 0 : ret) + sizeof(*rsp) - 1);
+ if (ret <= 0)
+ {
+ break;
+ }
+
+ read += ret;
+ }
+
+ return 0;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_write_handler
+ ****************************************************************************/
+
+static int rpmsgdev_write_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv)
+{
+ FAR struct rpmsgdev_server_s *server = ept->priv;
+ FAR struct rpmsgdev_write_s *msg = data;
+ size_t written = 0;
+ int ret = -ENOENT;
+
+ while (written < msg->count)
+ {
+ ret = file_write(&server->file, msg->buf + written,
+ msg->count - written);
+ if (ret <= 0)
+ {
+ break;
+ }
+
+ written += ret;
+ }
+
+ if (msg->header.cookie != 0)
+ {
+ msg->header.result = ret < 0 ? ret : written;
+ rpmsg_send(ept, msg, sizeof(*msg) - 1);
+ }
+
+ return 0;
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_lseek_handler
+ ****************************************************************************/
+
+static int rpmsgdev_lseek_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv)
+{
+ FAR struct rpmsgdev_server_s *server = ept->priv;
+ FAR struct rpmsgdev_lseek_s *msg = data;
+
+ msg->header.result = file_seek(&server->file, msg->offset, msg->whence);
+
+ return rpmsg_send(ept, msg, len);
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ioctl_handler
+ ****************************************************************************/
+
+static int rpmsgdev_ioctl_handler(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len,
+ uint32_t src, FAR void *priv)
+{
+ FAR struct rpmsgdev_server_s *server = ept->priv;
+ FAR struct rpmsgdev_ioctl_s *msg = data;
+
+ msg->header.result = file_ioctl(&server->file, msg->request,
+ msg->arglen > 0 ? (unsigned long)msg->buf :
+ msg->arg);
+
+ return rpmsg_send(ept, msg, len);
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ns_match
+ ****************************************************************************/
+
+static bool rpmsgdev_ns_match(FAR struct rpmsg_device *rdev,
+ FAR void *priv, FAR const char *name,
+ uint32_t dest)
+{
+ return !strncmp(name, RPMSGDEV_NAME_PREFIX, RPMSGDEV_NAME_PREFIX_LEN);
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ns_bind
+ ****************************************************************************/
+
+static void rpmsgdev_ns_bind(FAR struct rpmsg_device *rdev,
+ FAR void *priv, FAR const char *name,
+ uint32_t dest)
+{
+ FAR struct rpmsgdev_server_s *server;
+ int ret;
+
+ server = kmm_zalloc(sizeof(*server));
+ if (server == NULL)
+ {
+ return;
+ }
+
+ server->ept.priv = server;
+
+ ret = rpmsg_create_ept(&server->ept, rdev, name,
+ RPMSG_ADDR_ANY, dest,
+ rpmsgdev_ept_cb, rpmsgdev_ns_unbind);
+ if (ret < 0)
+ {
+ kmm_free(server);
+ }
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ns_unbind
+ ****************************************************************************/
+
+static void rpmsgdev_ns_unbind(FAR struct rpmsg_endpoint *ept)
+{
+ FAR struct rpmsgdev_server_s *server = ept->priv;
+
+ file_close(&server->file);
+ rpmsg_destroy_ept(&server->ept);
+
+ kmm_free(server);
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ept_cb
+ ****************************************************************************/
+
+static int rpmsgdev_ept_cb(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len, uint32_t src,
+ FAR void *priv)
+{
+ FAR struct rpmsgdev_header_s *header = data;
+ uint32_t command = header->command;
+
+ if (command < ARRAY_SIZE(g_rpmsgdev_handler))
+ {
+ return g_rpmsgdev_handler[command](ept, data, len, src, priv);
+ }
+
+ return -EINVAL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: rpmsgdev_server_init
+ *
+ * Description:
+ * Rpmsg-device server initialize function, the server cpu should call
+ * this function.
+ *
+ * Parameters:
+ * None
+ *
+ * Returned Values:
+ * OK on success; A negated errno value is returned on any failure.
+ *
+ ****************************************************************************/
+
+int rpmsgdev_server_init(void)
+{
+ return rpmsg_register_callback(NULL,
+ NULL,
+ NULL,
+ rpmsgdev_ns_match,
+ rpmsgdev_ns_bind);
+}
diff --git a/include/nuttx/drivers/rpmsgdev.h b/include/nuttx/drivers/rpmsgdev.h
new file mode 100644
index 0000000000..e3d86f2160
--- /dev/null
+++ b/include/nuttx/drivers/rpmsgdev.h
@@ -0,0 +1,90 @@
+/****************************************************************************
+ * include/nuttx/drivers/rpmsgdev.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_DRIVERS_RPMSGDEV_H
+#define __INCLUDE_NUTTX_DRIVERS_RPMSGDEV_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Name: rpmsgdev_server_init
+ *
+ * Description:
+ * Rpmsg-device server initialize function, the server cpu should call
+ * this function.
+ *
+ * Parameters:
+ * None
+ *
+ * Returned Values:
+ * OK on success; A negated errno value is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_DEV_RPMSG_SERVER
+int rpmsgdev_server_init(void);
+#endif
+
+/****************************************************************************
+ * Name: rpmsgdev_register
+ *
+ * Description:
+ * Rpmsg-device client initialize function, the client cpu should call
+ * this function in the board initialize process.
+ *
+ * Parameters:
+ * remotecpu - the server cpu name
+ * remotepath - the device you want to access in the remote cpu
+ * localpath - the device path in local cpu, if NULL, the localpath is
+ * same as the remotepath, provide this argument to supoort
+ * custom device path
+ *
+ * Returned Values:
+ * OK on success; A negated errno value is returned on any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_DEV_RPMSG
+int rpmsgdev_register(FAR const char *remotecpu, FAR const char *remotepath,
+ FAR const char *localpath);
+#endif
+
+#undef EXTERN
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_DRIVERS_RPMSGDEV_H */