xref: /openbmc/linux/drivers/rpmsg/rpmsg_ns.c (revision da1d9caf)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STMicroelectronics 2020 - All Rights Reserved
4  */
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>
11 
12 #include "rpmsg_internal.h"
13 
14 /**
15  * rpmsg_ns_register_device() - register name service device based on rpdev
16  * @rpdev: prepared rpdev to be used for creating endpoints
17  *
18  * This function wraps rpmsg_register_device() preparing the rpdev for use as
19  * basis for the rpmsg name service device.
20  */
21 int rpmsg_ns_register_device(struct rpmsg_device *rpdev)
22 {
23 	rpdev->src = RPMSG_NS_ADDR;
24 	rpdev->dst = RPMSG_NS_ADDR;
25 
26 	return rpmsg_register_device_override(rpdev, "rpmsg_ns");
27 }
28 EXPORT_SYMBOL(rpmsg_ns_register_device);
29 
30 /* invoked when a name service announcement arrives */
31 static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
32 		       void *priv, u32 src)
33 {
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;
38 	int ret;
39 
40 #if defined(CONFIG_DYNAMIC_DEBUG)
41 	dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
42 			 data, len, true);
43 #endif
44 
45 	if (len != sizeof(*msg)) {
46 		dev_err(dev, "malformed ns msg (%d)\n", len);
47 		return -EINVAL;
48 	}
49 
50 	/* don't trust the remote processor for null terminating the name */
51 	msg->name[RPMSG_NAME_SIZE - 1] = '\0';
52 
53 	strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
54 	chinfo.src = RPMSG_ADDR_ANY;
55 	chinfo.dst = rpmsg32_to_cpu(rpdev, msg->addr);
56 
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);
60 
61 	if (rpmsg32_to_cpu(rpdev, msg->flags) & RPMSG_NS_DESTROY) {
62 		ret = rpmsg_release_channel(rpdev, &chinfo);
63 		if (ret)
64 			dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
65 	} else {
66 		newch = rpmsg_create_channel(rpdev, &chinfo);
67 		if (!newch)
68 			dev_err(dev, "rpmsg_create_channel failed\n");
69 	}
70 
71 	return 0;
72 }
73 
74 static int rpmsg_ns_probe(struct rpmsg_device *rpdev)
75 {
76 	struct rpmsg_endpoint *ns_ept;
77 	struct rpmsg_channel_info ns_chinfo = {
78 		.src = RPMSG_NS_ADDR,
79 		.dst = RPMSG_NS_ADDR,
80 		.name = "name_service",
81 	};
82 
83 	/*
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.
87 	 */
88 	ns_ept = rpmsg_create_ept(rpdev, rpmsg_ns_cb, NULL, ns_chinfo);
89 	if (!ns_ept) {
90 		dev_err(&rpdev->dev, "failed to create the ns ept\n");
91 		return -ENOMEM;
92 	}
93 	rpdev->ept = ns_ept;
94 
95 	return 0;
96 }
97 
98 static struct rpmsg_driver rpmsg_ns_driver = {
99 	.drv.name = KBUILD_MODNAME,
100 	.probe = rpmsg_ns_probe,
101 };
102 
103 static int rpmsg_ns_init(void)
104 {
105 	int ret;
106 
107 	ret = register_rpmsg_driver(&rpmsg_ns_driver);
108 	if (ret < 0)
109 		pr_err("%s: Failed to register rpmsg driver\n", __func__);
110 
111 	return ret;
112 }
113 postcore_initcall(rpmsg_ns_init);
114 
115 static void rpmsg_ns_exit(void)
116 {
117 	unregister_rpmsg_driver(&rpmsg_ns_driver);
118 }
119 module_exit(rpmsg_ns_exit);
120 
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");
125