[PATCH v3 4/6] rpmsg: char: Add possibility to create and reuse default endpoint

From: Arnaud Pouliquen
Date: Thu Apr 29 2021 - 09:55:50 EST


This patch prepares the introduction of a rpmsg channel device for the
char device. The rpmsg channel device will require a default endpoint to
communicate to the remote processor.

Add the use_default_ept field in rpmsg_eptdev structure. This boolean
determines the behavior on rpmsg_eptdev_open and rpmsg_eptdev_release call.

- If use_default_ept == false:
Use the legacy behavior by creating a new endpoint each time
rpmsg_eptdev_open is called and release it when rpmsg_eptdev_release
is called on /dev/rpmsgX device open/close.

- if use_default_ept == true:
create a endpoint only on first rpmsg_eptdev_open call (if no default
endpoint already exists) and associate it to the default endpoint.
The endpoint is released when the rpmsg device is removed.

Signed-off-by: Arnaud Pouliquen <arnaud.pouliquen@xxxxxxxxxxx>

---
Update from V2

This patch is a merge of two patches proposed in V2
rpmsg: char: Introduce __rpmsg_chrdev_create_eptdev function
rpmsg: char: No dynamic endpoint management for the default one

With the introduction of the use_default_ept to determine the behaviour
on rpmsg_eptdev_open/release.
---
drivers/rpmsg/rpmsg_char.c | 46 +++++++++++++++++++++++++++++++-------
1 file changed, 38 insertions(+), 8 deletions(-)

diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c
index 3b07a050b75e..5c6a7da6e4d7 100644
--- a/drivers/rpmsg/rpmsg_char.c
+++ b/drivers/rpmsg/rpmsg_char.c
@@ -38,6 +38,8 @@ static DEFINE_IDA(rpmsg_minor_ida);
* @queue_lock: synchronization of @queue operations
* @queue: incoming message queue
* @readq: wait object for incoming queue
+ * @use_default_ept: specify if the endpoint has to be created at each device opening or
+ * if the default endpoint should be reused.
*/
struct rpmsg_eptdev {
struct device dev;
@@ -52,6 +54,8 @@ struct rpmsg_eptdev {
spinlock_t queue_lock;
struct sk_buff_head queue;
wait_queue_head_t readq;
+
+ bool use_default_ept;
};

int rpmsg_chrdev_eptdev_destroy(struct device *dev, void *data)
@@ -109,11 +113,29 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)

get_device(dev);

- ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo);
- if (!ept) {
- dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
- put_device(dev);
- return -EINVAL;
+ /*
+ * If the rpmsg device default endpoint is used, create it only the first time then reuse
+ * it. Else a new endpoint is created on open that will be destroyed on release.
+ */
+ if (eptdev->use_default_ept && rpdev->ept) {
+ ept = rpdev->ept;
+ } else {
+ ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo);
+ if (!ept) {
+ dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
+ put_device(dev);
+ return -EINVAL;
+ }
+
+ if (eptdev->use_default_ept) {
+ /*
+ * Assign the new endpoint as default endpoint and update the channel
+ * info source to reflect the allocation in the rpmsg class.
+ */
+ rpdev->ept = ept;
+ rpdev->src = ept->addr;
+ eptdev->chinfo.src = rpdev->src;
+ }
}

eptdev->ept = ept;
@@ -130,7 +152,8 @@ static int rpmsg_eptdev_release(struct inode *inode, struct file *filp)
/* Close the endpoint, if it's not already destroyed by the parent */
mutex_lock(&eptdev->ept_lock);
if (eptdev->ept) {
- rpmsg_destroy_ept(eptdev->ept);
+ if (!eptdev->use_default_ept)
+ rpmsg_destroy_ept(eptdev->ept);
eptdev->ept = NULL;
}
mutex_unlock(&eptdev->ept_lock);
@@ -316,8 +339,8 @@ static void rpmsg_eptdev_release_device(struct device *dev)
kfree(eptdev);
}

-int rpmsg_chrdev_eptdev_create(struct rpmsg_device *rpdev, struct device *parent,
- struct rpmsg_channel_info chinfo)
+static int __rpmsg_chrdev_eptdev_create(struct rpmsg_device *rpdev, struct device *parent,
+ struct rpmsg_channel_info chinfo, bool use_default_ept)
{
struct rpmsg_eptdev *eptdev;
struct device *dev;
@@ -330,6 +353,7 @@ int rpmsg_chrdev_eptdev_create(struct rpmsg_device *rpdev, struct device *parent
dev = &eptdev->dev;
eptdev->rpdev = rpdev;
eptdev->chinfo = chinfo;
+ eptdev->use_default_ept = use_default_ept;

mutex_init(&eptdev->ept_lock);
spin_lock_init(&eptdev->queue_lock);
@@ -381,6 +405,12 @@ int rpmsg_chrdev_eptdev_create(struct rpmsg_device *rpdev, struct device *parent

return ret;
}
+
+int rpmsg_chrdev_eptdev_create(struct rpmsg_device *rpdev, struct device *parent,
+ struct rpmsg_channel_info chinfo)
+{
+ return __rpmsg_chrdev_eptdev_create(rpdev, parent, chinfo, false);
+}
EXPORT_SYMBOL(rpmsg_chrdev_eptdev_create);

static int rpmsg_chrdev_init(void)
--
2.17.1