rpmsgdev: add rpmsg-device support.

Signed-off-by: wangbowen6 <wangbowen6@xiaomi.com>
This commit is contained in:
wangbowen6 2022-08-09 16:20:26 +08:00 committed by Xiang Xiao
parent 704b03f833
commit 52a1692cfa
10 changed files with 1621 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
}

View file

@ -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

View file

@ -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

1037
drivers/misc/rpmsgdev.c Normal file

File diff suppressed because it is too large Load diff

103
drivers/misc/rpmsgdev.h Normal file
View file

@ -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 */

View file

@ -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);
}

View file

@ -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 */