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 strcpy(rpdev->id.name, "rpmsg_ns");
24 rpdev->driver_override = "rpmsg_ns";
25 rpdev->src = RPMSG_NS_ADDR;
26 rpdev->dst = RPMSG_NS_ADDR;
28 return rpmsg_register_device(rpdev);
30 EXPORT_SYMBOL(rpmsg_ns_register_device);
32 /* invoked when a name service announcement arrives */
33 static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
36 struct rpmsg_ns_msg *msg = data;
37 struct rpmsg_device *newch;
38 struct rpmsg_channel_info chinfo;
39 struct device *dev = rpdev->dev.parent;
42 #if defined(CONFIG_DYNAMIC_DEBUG)
43 dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
47 if (len != sizeof(*msg)) {
48 dev_err(dev, "malformed ns msg (%d)\n", len);
52 /* don't trust the remote processor for null terminating the name */
53 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
55 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
56 chinfo.src = RPMSG_ADDR_ANY;
57 chinfo.dst = rpmsg32_to_cpu(rpdev, msg->addr);
59 dev_info(dev, "%sing channel %s addr 0x%x\n",
60 rpmsg32_to_cpu(rpdev, msg->flags) & RPMSG_NS_DESTROY ?
61 "destroy" : "creat", msg->name, chinfo.dst);
63 if (rpmsg32_to_cpu(rpdev, msg->flags) & RPMSG_NS_DESTROY) {
64 ret = rpmsg_release_channel(rpdev, &chinfo);
66 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
68 newch = rpmsg_create_channel(rpdev, &chinfo);
70 dev_err(dev, "rpmsg_create_channel failed\n");
76 static int rpmsg_ns_probe(struct rpmsg_device *rpdev)
78 struct rpmsg_endpoint *ns_ept;
79 struct rpmsg_channel_info ns_chinfo = {
82 .name = "name_service",
86 * Create the NS announcement service endpoint associated to the RPMsg
87 * device. The endpoint will be automatically destroyed when the RPMsg
88 * device will be deleted.
90 ns_ept = rpmsg_create_ept(rpdev, rpmsg_ns_cb, NULL, ns_chinfo);
92 dev_err(&rpdev->dev, "failed to create the ns ept\n");
100 static struct rpmsg_driver rpmsg_ns_driver = {
101 .drv.name = KBUILD_MODNAME,
102 .probe = rpmsg_ns_probe,
105 static int rpmsg_ns_init(void)
109 ret = register_rpmsg_driver(&rpmsg_ns_driver);
111 pr_err("%s: Failed to register rpmsg driver\n", __func__);
115 postcore_initcall(rpmsg_ns_init);
117 static void rpmsg_ns_exit(void)
119 unregister_rpmsg_driver(&rpmsg_ns_driver);
121 module_exit(rpmsg_ns_exit);
123 MODULE_DESCRIPTION("Name service announcement rpmsg driver");
124 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
125 MODULE_ALIAS("rpmsg:" KBUILD_MODNAME);
126 MODULE_LICENSE("GPL v2");