drm/nouveau: fix kernel-doc comments
[drm/drm-misc.git] / drivers / rpmsg / rpmsg_ctrl.c
blob28f57945ccd9f7f1e591e8bf4126cfa626713f10
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.
10 * Based on rpmsg performance statistics driver by Michal Simek, which in turn
11 * was based on TI & Google OMX rpmsg driver.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
28 #include "rpmsg_char.h"
29 #include "rpmsg_internal.h"
31 #define RPMSG_DEV_MAX (MINORMASK + 1)
33 static dev_t rpmsg_major;
35 static DEFINE_IDA(rpmsg_ctrl_ida);
36 static DEFINE_IDA(rpmsg_minor_ida);
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)
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.
48 struct rpmsg_ctrldev {
49 struct rpmsg_device *rpdev;
50 struct cdev cdev;
51 struct device dev;
52 struct mutex ctrl_lock;
55 static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp)
57 struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
59 get_device(&ctrldev->dev);
60 filp->private_data = ctrldev;
62 return 0;
65 static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp)
67 struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
69 put_device(&ctrldev->dev);
71 return 0;
74 static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd,
75 unsigned long arg)
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;
84 if (copy_from_user(&eptinfo, argp, sizeof(eptinfo)))
85 return -EFAULT;
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;
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;
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;
104 break;
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;
113 default:
114 ret = -EINVAL;
116 mutex_unlock(&ctrldev->ctrl_lock);
118 return ret;
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,
129 static void rpmsg_ctrldev_release_device(struct device *dev)
131 struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev);
133 ida_free(&rpmsg_ctrl_ida, dev->id);
134 ida_free(&rpmsg_minor_ida, MINOR(dev->devt));
135 kfree(ctrldev);
138 static int rpmsg_ctrldev_probe(struct rpmsg_device *rpdev)
140 struct rpmsg_ctrldev *ctrldev;
141 struct device *dev;
142 int ret;
144 ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL);
145 if (!ctrldev)
146 return -ENOMEM;
148 ctrldev->rpdev = rpdev;
150 dev = &ctrldev->dev;
151 device_initialize(dev);
152 dev->parent = &rpdev->dev;
153 dev->class = &rpmsg_class;
155 mutex_init(&ctrldev->ctrl_lock);
156 cdev_init(&ctrldev->cdev, &rpmsg_ctrldev_fops);
157 ctrldev->cdev.owner = THIS_MODULE;
159 ret = ida_alloc_max(&rpmsg_minor_ida, RPMSG_DEV_MAX - 1, GFP_KERNEL);
160 if (ret < 0)
161 goto free_ctrldev;
162 dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
164 ret = ida_alloc(&rpmsg_ctrl_ida, 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);
170 ret = cdev_device_add(&ctrldev->cdev, &ctrldev->dev);
171 if (ret)
172 goto free_ctrl_ida;
174 /* We can now rely on the release function for cleanup */
175 dev->release = rpmsg_ctrldev_release_device;
177 dev_set_drvdata(&rpdev->dev, ctrldev);
179 return ret;
181 free_ctrl_ida:
182 ida_free(&rpmsg_ctrl_ida, dev->id);
183 free_minor_ida:
184 ida_free(&rpmsg_minor_ida, MINOR(dev->devt));
185 free_ctrldev:
186 put_device(dev);
187 kfree(ctrldev);
189 return ret;
192 static void rpmsg_ctrldev_remove(struct rpmsg_device *rpdev)
194 struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev);
195 int ret;
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);
204 cdev_device_del(&ctrldev->cdev, &ctrldev->dev);
205 put_device(&ctrldev->dev);
208 static struct rpmsg_driver rpmsg_ctrldev_driver = {
209 .probe = rpmsg_ctrldev_probe,
210 .remove = rpmsg_ctrldev_remove,
211 .drv = {
212 .name = "rpmsg_ctrl",
216 static int rpmsg_ctrldev_init(void)
218 int ret;
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;
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);
232 return ret;
234 postcore_initcall(rpmsg_ctrldev_init);
236 static void rpmsg_ctrldev_exit(void)
238 unregister_rpmsg_driver(&rpmsg_ctrldev_driver);
239 unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
241 module_exit(rpmsg_ctrldev_exit);
243 MODULE_DESCRIPTION("rpmsg control interface");
244 MODULE_ALIAS("rpmsg:" KBUILD_MODNAME);
245 MODULE_LICENSE("GPL v2");