xref: /openbmc/linux/drivers/rpmsg/rpmsg_ctrl.c (revision 0b656310)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2022, STMicroelectronics
4  * Copyright (c) 2016, Linaro Ltd.
5  * Copyright (c) 2012, Michal Simek <monstr@monstr.eu>
6  * Copyright (c) 2012, PetaLogix
7  * Copyright (c) 2011, Texas Instruments, Inc.
8  * Copyright (c) 2011, Google, Inc.
9  *
10  * Based on rpmsg performance statistics driver by Michal Simek, which in turn
11  * was based on TI & Google OMX rpmsg driver.
12  */
13 
14 #define pr_fmt(fmt)		KBUILD_MODNAME ": " fmt
15 
16 #include <linux/cdev.h>
17 #include <linux/device.h>
18 #include <linux/fs.h>
19 #include <linux/idr.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/rpmsg.h>
23 #include <linux/skbuff.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
26 #include <uapi/linux/rpmsg.h>
27 
28 #include "rpmsg_char.h"
29 #include "rpmsg_internal.h"
30 
31 #define RPMSG_DEV_MAX	(MINORMASK + 1)
32 
33 static dev_t rpmsg_major;
34 
35 static DEFINE_IDA(rpmsg_ctrl_ida);
36 static DEFINE_IDA(rpmsg_minor_ida);
37 
38 #define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, dev)
39 #define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct rpmsg_ctrldev, cdev)
40 
41 /**
42  * struct rpmsg_ctrldev - control device for instantiating endpoint devices
43  * @rpdev:	underlaying rpmsg device
44  * @cdev:	cdev for the ctrl device
45  * @dev:	device for the ctrl device
46  * @ctrl_lock:	serialize the ioctrls.
47  */
48 struct rpmsg_ctrldev {
49 	struct rpmsg_device *rpdev;
50 	struct cdev cdev;
51 	struct device dev;
52 	struct mutex ctrl_lock;
53 };
54 
55 static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp)
56 {
57 	struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
58 
59 	get_device(&ctrldev->dev);
60 	filp->private_data = ctrldev;
61 
62 	return 0;
63 }
64 
65 static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp)
66 {
67 	struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
68 
69 	put_device(&ctrldev->dev);
70 
71 	return 0;
72 }
73 
74 static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd,
75 				unsigned long arg)
76 {
77 	struct rpmsg_ctrldev *ctrldev = fp->private_data;
78 	void __user *argp = (void __user *)arg;
79 	struct rpmsg_endpoint_info eptinfo;
80 	struct rpmsg_channel_info chinfo;
81 	struct rpmsg_device *rpdev;
82 	int ret = 0;
83 
84 	if (copy_from_user(&eptinfo, argp, sizeof(eptinfo)))
85 		return -EFAULT;
86 
87 	memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE);
88 	chinfo.name[RPMSG_NAME_SIZE - 1] = '\0';
89 	chinfo.src = eptinfo.src;
90 	chinfo.dst = eptinfo.dst;
91 
92 	mutex_lock(&ctrldev->ctrl_lock);
93 	switch (cmd) {
94 	case RPMSG_CREATE_EPT_IOCTL:
95 		ret = rpmsg_chrdev_eptdev_create(ctrldev->rpdev, &ctrldev->dev, chinfo);
96 		break;
97 
98 	case RPMSG_CREATE_DEV_IOCTL:
99 		rpdev = rpmsg_create_channel(ctrldev->rpdev, &chinfo);
100 		if (!rpdev) {
101 			dev_err(&ctrldev->dev, "failed to create %s channel\n", chinfo.name);
102 			ret = -ENXIO;
103 		}
104 		break;
105 
106 	case RPMSG_RELEASE_DEV_IOCTL:
107 		ret = rpmsg_release_channel(ctrldev->rpdev, &chinfo);
108 		if (ret)
109 			dev_err(&ctrldev->dev, "failed to release %s channel (%d)\n",
110 				chinfo.name, ret);
111 		break;
112 
113 	default:
114 		ret = -EINVAL;
115 	}
116 	mutex_unlock(&ctrldev->ctrl_lock);
117 
118 	return ret;
119 };
120 
121 static const struct file_operations rpmsg_ctrldev_fops = {
122 	.owner = THIS_MODULE,
123 	.open = rpmsg_ctrldev_open,
124 	.release = rpmsg_ctrldev_release,
125 	.unlocked_ioctl = rpmsg_ctrldev_ioctl,
126 	.compat_ioctl = compat_ptr_ioctl,
127 };
128 
129 static void rpmsg_ctrldev_release_device(struct device *dev)
130 {
131 	struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev);
132 
133 	ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
134 	ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
135 	kfree(ctrldev);
136 }
137 
138 static int rpmsg_ctrldev_probe(struct rpmsg_device *rpdev)
139 {
140 	struct rpmsg_ctrldev *ctrldev;
141 	struct device *dev;
142 	int ret;
143 
144 	ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL);
145 	if (!ctrldev)
146 		return -ENOMEM;
147 
148 	ctrldev->rpdev = rpdev;
149 
150 	dev = &ctrldev->dev;
151 	device_initialize(dev);
152 	dev->parent = &rpdev->dev;
153 	dev->class = rpmsg_class;
154 
155 	mutex_init(&ctrldev->ctrl_lock);
156 	cdev_init(&ctrldev->cdev, &rpmsg_ctrldev_fops);
157 	ctrldev->cdev.owner = THIS_MODULE;
158 
159 	ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
160 	if (ret < 0)
161 		goto free_ctrldev;
162 	dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
163 
164 	ret = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL);
165 	if (ret < 0)
166 		goto free_minor_ida;
167 	dev->id = ret;
168 	dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", ret);
169 
170 	ret = cdev_device_add(&ctrldev->cdev, &ctrldev->dev);
171 	if (ret)
172 		goto free_ctrl_ida;
173 
174 	/* We can now rely on the release function for cleanup */
175 	dev->release = rpmsg_ctrldev_release_device;
176 
177 	dev_set_drvdata(&rpdev->dev, ctrldev);
178 
179 	return ret;
180 
181 free_ctrl_ida:
182 	ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
183 free_minor_ida:
184 	ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
185 free_ctrldev:
186 	put_device(dev);
187 	kfree(ctrldev);
188 
189 	return ret;
190 }
191 
192 static void rpmsg_ctrldev_remove(struct rpmsg_device *rpdev)
193 {
194 	struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev);
195 	int ret;
196 
197 	mutex_lock(&ctrldev->ctrl_lock);
198 	/* Destroy all endpoints */
199 	ret = device_for_each_child(&ctrldev->dev, NULL, rpmsg_chrdev_eptdev_destroy);
200 	if (ret)
201 		dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret);
202 	mutex_unlock(&ctrldev->ctrl_lock);
203 
204 	cdev_device_del(&ctrldev->cdev, &ctrldev->dev);
205 	put_device(&ctrldev->dev);
206 }
207 
208 static struct rpmsg_driver rpmsg_ctrldev_driver = {
209 	.probe = rpmsg_ctrldev_probe,
210 	.remove = rpmsg_ctrldev_remove,
211 	.drv = {
212 		.name = "rpmsg_ctrl",
213 	},
214 };
215 
216 static int rpmsg_ctrldev_init(void)
217 {
218 	int ret;
219 
220 	ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg_ctrl");
221 	if (ret < 0) {
222 		pr_err("failed to allocate char dev region\n");
223 		return ret;
224 	}
225 
226 	ret = register_rpmsg_driver(&rpmsg_ctrldev_driver);
227 	if (ret < 0) {
228 		pr_err("failed to register rpmsg driver\n");
229 		unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
230 	}
231 
232 	return ret;
233 }
234 postcore_initcall(rpmsg_ctrldev_init);
235 
236 static void rpmsg_ctrldev_exit(void)
237 {
238 	unregister_rpmsg_driver(&rpmsg_ctrldev_driver);
239 	unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
240 }
241 module_exit(rpmsg_ctrldev_exit);
242 
243 MODULE_DESCRIPTION("rpmsg control interface");
244 MODULE_ALIAS("rpmsg:" KBUILD_MODNAME);
245 MODULE_LICENSE("GPL v2");
246