1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2020 - All Rights Reserved
5 #include <linux/device.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/rpmsg.h>
9 #include <linux/rpmsg/ns.h>
10 #include <linux/slab.h>
12 #include "rpmsg_internal.h"
15 * rpmsg_ns_register_device() - register name service device based on rpdev
16 * @rpdev: prepared rpdev to be used for creating endpoints
18 * This function wraps rpmsg_register_device() preparing the rpdev for use as
19 * basis for the rpmsg name service device.
21 int rpmsg_ns_register_device(struct rpmsg_device *rpdev)
23 rpdev->src = RPMSG_NS_ADDR;
24 rpdev->dst = RPMSG_NS_ADDR;
26 return rpmsg_register_device_override(rpdev, "rpmsg_ns");
28 EXPORT_SYMBOL(rpmsg_ns_register_device);
30 /* invoked when a name service announcement arrives */
31 static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
34 struct rpmsg_ns_msg *msg = data;
35 struct rpmsg_device *newch;
36 struct rpmsg_channel_info chinfo;
37 struct device *dev = rpdev->dev.parent;
40 #if defined(CONFIG_DYNAMIC_DEBUG)
41 dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
45 if (len != sizeof(*msg)) {
46 dev_err(dev, "malformed ns msg (%d)\n", len);
50 /* don't trust the remote processor for null terminating the name */
51 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
53 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
54 chinfo.src = RPMSG_ADDR_ANY;
55 chinfo.dst = rpmsg32_to_cpu(rpdev, msg->addr);
57 dev_info(dev, "%sing channel %s addr 0x%x\n",
58 rpmsg32_to_cpu(rpdev, msg->flags) & RPMSG_NS_DESTROY ?
59 "destroy" : "creat", msg->name, chinfo.dst);
61 if (rpmsg32_to_cpu(rpdev, msg->flags) & RPMSG_NS_DESTROY) {
62 ret = rpmsg_release_channel(rpdev, &chinfo);
64 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
66 newch = rpmsg_create_channel(rpdev, &chinfo);
68 dev_err(dev, "rpmsg_create_channel failed\n");
74 static int rpmsg_ns_probe(struct rpmsg_device *rpdev)
76 struct rpmsg_endpoint *ns_ept;
77 struct rpmsg_channel_info ns_chinfo = {
80 .name = "name_service",
84 * Create the NS announcement service endpoint associated to the RPMsg
85 * device. The endpoint will be automatically destroyed when the RPMsg
86 * device will be deleted.
88 ns_ept = rpmsg_create_ept(rpdev, rpmsg_ns_cb, NULL, ns_chinfo);
90 dev_err(&rpdev->dev, "failed to create the ns ept\n");
98 static struct rpmsg_driver rpmsg_ns_driver = {
99 .drv.name = KBUILD_MODNAME,
100 .probe = rpmsg_ns_probe,
103 static int rpmsg_ns_init(void)
107 ret = register_rpmsg_driver(&rpmsg_ns_driver);
109 pr_err("%s: Failed to register rpmsg driver\n", __func__);
113 postcore_initcall(rpmsg_ns_init);
115 static void rpmsg_ns_exit(void)
117 unregister_rpmsg_driver(&rpmsg_ns_driver);
119 module_exit(rpmsg_ns_exit);
121 MODULE_DESCRIPTION("Name service announcement rpmsg driver");
122 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
123 MODULE_ALIAS("rpmsg:" KBUILD_MODNAME);
124 MODULE_LICENSE("GPL v2");