xref: /openbmc/linux/drivers/rpmsg/rpmsg_char.c (revision 608edd96049b142de7944413cd7c24cb3f203d37)
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/poll.h>
23 #include <linux/rpmsg.h>
24 #include <linux/skbuff.h>
25 #include <linux/slab.h>
26 #include <linux/uaccess.h>
27 #include <uapi/linux/rpmsg.h>
28 
29 #include "rpmsg_char.h"
30 #include "rpmsg_internal.h"
31 
32 #define RPMSG_DEV_MAX	(MINORMASK + 1)
33 
34 static dev_t rpmsg_major;
35 
36 static DEFINE_IDA(rpmsg_ctrl_ida);
37 static DEFINE_IDA(rpmsg_ept_ida);
38 static DEFINE_IDA(rpmsg_minor_ida);
39 
40 #define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev)
41 #define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct rpmsg_eptdev, cdev)
42 
43 #define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, dev)
44 #define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct rpmsg_ctrldev, cdev)
45 
46 /**
47  * struct rpmsg_ctrldev - control device for instantiating endpoint devices
48  * @rpdev:	underlaying rpmsg device
49  * @cdev:	cdev for the ctrl device
50  * @dev:	device for the ctrl device
51  */
52 struct rpmsg_ctrldev {
53 	struct rpmsg_device *rpdev;
54 	struct cdev cdev;
55 	struct device dev;
56 };
57 
58 /**
59  * struct rpmsg_eptdev - endpoint device context
60  * @dev:	endpoint device
61  * @cdev:	cdev for the endpoint device
62  * @rpdev:	underlaying rpmsg device
63  * @chinfo:	info used to open the endpoint
64  * @ept_lock:	synchronization of @ept modifications
65  * @ept:	rpmsg endpoint reference, when open
66  * @queue_lock:	synchronization of @queue operations
67  * @queue:	incoming message queue
68  * @readq:	wait object for incoming queue
69  */
70 struct rpmsg_eptdev {
71 	struct device dev;
72 	struct cdev cdev;
73 
74 	struct rpmsg_device *rpdev;
75 	struct rpmsg_channel_info chinfo;
76 
77 	struct mutex ept_lock;
78 	struct rpmsg_endpoint *ept;
79 
80 	spinlock_t queue_lock;
81 	struct sk_buff_head queue;
82 	wait_queue_head_t readq;
83 };
84 
85 int rpmsg_chrdev_eptdev_destroy(struct device *dev, void *data)
86 {
87 	struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
88 
89 	mutex_lock(&eptdev->ept_lock);
90 	if (eptdev->ept) {
91 		rpmsg_destroy_ept(eptdev->ept);
92 		eptdev->ept = NULL;
93 	}
94 	mutex_unlock(&eptdev->ept_lock);
95 
96 	/* wake up any blocked readers */
97 	wake_up_interruptible(&eptdev->readq);
98 
99 	cdev_device_del(&eptdev->cdev, &eptdev->dev);
100 	put_device(&eptdev->dev);
101 
102 	return 0;
103 }
104 EXPORT_SYMBOL(rpmsg_chrdev_eptdev_destroy);
105 
106 static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len,
107 			void *priv, u32 addr)
108 {
109 	struct rpmsg_eptdev *eptdev = priv;
110 	struct sk_buff *skb;
111 
112 	skb = alloc_skb(len, GFP_ATOMIC);
113 	if (!skb)
114 		return -ENOMEM;
115 
116 	skb_put_data(skb, buf, len);
117 
118 	spin_lock(&eptdev->queue_lock);
119 	skb_queue_tail(&eptdev->queue, skb);
120 	spin_unlock(&eptdev->queue_lock);
121 
122 	/* wake up any blocking processes, waiting for new data */
123 	wake_up_interruptible(&eptdev->readq);
124 
125 	return 0;
126 }
127 
128 static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
129 {
130 	struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
131 	struct rpmsg_endpoint *ept;
132 	struct rpmsg_device *rpdev = eptdev->rpdev;
133 	struct device *dev = &eptdev->dev;
134 
135 	if (eptdev->ept)
136 		return -EBUSY;
137 
138 	get_device(dev);
139 
140 	ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo);
141 	if (!ept) {
142 		dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
143 		put_device(dev);
144 		return -EINVAL;
145 	}
146 
147 	eptdev->ept = ept;
148 	filp->private_data = eptdev;
149 
150 	return 0;
151 }
152 
153 static int rpmsg_eptdev_release(struct inode *inode, struct file *filp)
154 {
155 	struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
156 	struct device *dev = &eptdev->dev;
157 
158 	/* Close the endpoint, if it's not already destroyed by the parent */
159 	mutex_lock(&eptdev->ept_lock);
160 	if (eptdev->ept) {
161 		rpmsg_destroy_ept(eptdev->ept);
162 		eptdev->ept = NULL;
163 	}
164 	mutex_unlock(&eptdev->ept_lock);
165 
166 	/* Discard all SKBs */
167 	skb_queue_purge(&eptdev->queue);
168 
169 	put_device(dev);
170 
171 	return 0;
172 }
173 
174 static ssize_t rpmsg_eptdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
175 {
176 	struct file *filp = iocb->ki_filp;
177 	struct rpmsg_eptdev *eptdev = filp->private_data;
178 	unsigned long flags;
179 	struct sk_buff *skb;
180 	int use;
181 
182 	if (!eptdev->ept)
183 		return -EPIPE;
184 
185 	spin_lock_irqsave(&eptdev->queue_lock, flags);
186 
187 	/* Wait for data in the queue */
188 	if (skb_queue_empty(&eptdev->queue)) {
189 		spin_unlock_irqrestore(&eptdev->queue_lock, flags);
190 
191 		if (filp->f_flags & O_NONBLOCK)
192 			return -EAGAIN;
193 
194 		/* Wait until we get data or the endpoint goes away */
195 		if (wait_event_interruptible(eptdev->readq,
196 					     !skb_queue_empty(&eptdev->queue) ||
197 					     !eptdev->ept))
198 			return -ERESTARTSYS;
199 
200 		/* We lost the endpoint while waiting */
201 		if (!eptdev->ept)
202 			return -EPIPE;
203 
204 		spin_lock_irqsave(&eptdev->queue_lock, flags);
205 	}
206 
207 	skb = skb_dequeue(&eptdev->queue);
208 	spin_unlock_irqrestore(&eptdev->queue_lock, flags);
209 	if (!skb)
210 		return -EFAULT;
211 
212 	use = min_t(size_t, iov_iter_count(to), skb->len);
213 	if (copy_to_iter(skb->data, use, to) != use)
214 		use = -EFAULT;
215 
216 	kfree_skb(skb);
217 
218 	return use;
219 }
220 
221 static ssize_t rpmsg_eptdev_write_iter(struct kiocb *iocb,
222 				       struct iov_iter *from)
223 {
224 	struct file *filp = iocb->ki_filp;
225 	struct rpmsg_eptdev *eptdev = filp->private_data;
226 	size_t len = iov_iter_count(from);
227 	void *kbuf;
228 	int ret;
229 
230 	kbuf = kzalloc(len, GFP_KERNEL);
231 	if (!kbuf)
232 		return -ENOMEM;
233 
234 	if (!copy_from_iter_full(kbuf, len, from)) {
235 		ret = -EFAULT;
236 		goto free_kbuf;
237 	}
238 
239 	if (mutex_lock_interruptible(&eptdev->ept_lock)) {
240 		ret = -ERESTARTSYS;
241 		goto free_kbuf;
242 	}
243 
244 	if (!eptdev->ept) {
245 		ret = -EPIPE;
246 		goto unlock_eptdev;
247 	}
248 
249 	if (filp->f_flags & O_NONBLOCK) {
250 		ret = rpmsg_trysendto(eptdev->ept, kbuf, len, eptdev->chinfo.dst);
251 		if (ret == -ENOMEM)
252 			ret = -EAGAIN;
253 	} else {
254 		ret = rpmsg_sendto(eptdev->ept, kbuf, len, eptdev->chinfo.dst);
255 	}
256 
257 unlock_eptdev:
258 	mutex_unlock(&eptdev->ept_lock);
259 
260 free_kbuf:
261 	kfree(kbuf);
262 	return ret < 0 ? ret : len;
263 }
264 
265 static __poll_t rpmsg_eptdev_poll(struct file *filp, poll_table *wait)
266 {
267 	struct rpmsg_eptdev *eptdev = filp->private_data;
268 	__poll_t mask = 0;
269 
270 	if (!eptdev->ept)
271 		return EPOLLERR;
272 
273 	poll_wait(filp, &eptdev->readq, wait);
274 
275 	if (!skb_queue_empty(&eptdev->queue))
276 		mask |= EPOLLIN | EPOLLRDNORM;
277 
278 	mask |= rpmsg_poll(eptdev->ept, filp, wait);
279 
280 	return mask;
281 }
282 
283 static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd,
284 			       unsigned long arg)
285 {
286 	struct rpmsg_eptdev *eptdev = fp->private_data;
287 
288 	if (cmd != RPMSG_DESTROY_EPT_IOCTL)
289 		return -EINVAL;
290 
291 	return rpmsg_chrdev_eptdev_destroy(&eptdev->dev, NULL);
292 }
293 
294 static const struct file_operations rpmsg_eptdev_fops = {
295 	.owner = THIS_MODULE,
296 	.open = rpmsg_eptdev_open,
297 	.release = rpmsg_eptdev_release,
298 	.read_iter = rpmsg_eptdev_read_iter,
299 	.write_iter = rpmsg_eptdev_write_iter,
300 	.poll = rpmsg_eptdev_poll,
301 	.unlocked_ioctl = rpmsg_eptdev_ioctl,
302 	.compat_ioctl = compat_ptr_ioctl,
303 };
304 
305 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
306 			 char *buf)
307 {
308 	struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
309 
310 	return sprintf(buf, "%s\n", eptdev->chinfo.name);
311 }
312 static DEVICE_ATTR_RO(name);
313 
314 static ssize_t src_show(struct device *dev, struct device_attribute *attr,
315 			 char *buf)
316 {
317 	struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
318 
319 	return sprintf(buf, "%d\n", eptdev->chinfo.src);
320 }
321 static DEVICE_ATTR_RO(src);
322 
323 static ssize_t dst_show(struct device *dev, struct device_attribute *attr,
324 			 char *buf)
325 {
326 	struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
327 
328 	return sprintf(buf, "%d\n", eptdev->chinfo.dst);
329 }
330 static DEVICE_ATTR_RO(dst);
331 
332 static struct attribute *rpmsg_eptdev_attrs[] = {
333 	&dev_attr_name.attr,
334 	&dev_attr_src.attr,
335 	&dev_attr_dst.attr,
336 	NULL
337 };
338 ATTRIBUTE_GROUPS(rpmsg_eptdev);
339 
340 static void rpmsg_eptdev_release_device(struct device *dev)
341 {
342 	struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
343 
344 	ida_simple_remove(&rpmsg_ept_ida, dev->id);
345 	ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt));
346 	kfree(eptdev);
347 }
348 
349 int rpmsg_chrdev_eptdev_create(struct rpmsg_device *rpdev, struct device *parent,
350 			       struct rpmsg_channel_info chinfo)
351 {
352 	struct rpmsg_eptdev *eptdev;
353 	struct device *dev;
354 	int ret;
355 
356 	eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL);
357 	if (!eptdev)
358 		return -ENOMEM;
359 
360 	dev = &eptdev->dev;
361 	eptdev->rpdev = rpdev;
362 	eptdev->chinfo = chinfo;
363 
364 	mutex_init(&eptdev->ept_lock);
365 	spin_lock_init(&eptdev->queue_lock);
366 	skb_queue_head_init(&eptdev->queue);
367 	init_waitqueue_head(&eptdev->readq);
368 
369 	device_initialize(dev);
370 	dev->class = rpmsg_class;
371 	dev->parent = parent;
372 	dev->groups = rpmsg_eptdev_groups;
373 	dev_set_drvdata(dev, eptdev);
374 
375 	cdev_init(&eptdev->cdev, &rpmsg_eptdev_fops);
376 	eptdev->cdev.owner = THIS_MODULE;
377 
378 	ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
379 	if (ret < 0)
380 		goto free_eptdev;
381 	dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
382 
383 	ret = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL);
384 	if (ret < 0)
385 		goto free_minor_ida;
386 	dev->id = ret;
387 	dev_set_name(dev, "rpmsg%d", ret);
388 
389 	ret = cdev_device_add(&eptdev->cdev, &eptdev->dev);
390 	if (ret)
391 		goto free_ept_ida;
392 
393 	/* We can now rely on the release function for cleanup */
394 	dev->release = rpmsg_eptdev_release_device;
395 
396 	return ret;
397 
398 free_ept_ida:
399 	ida_simple_remove(&rpmsg_ept_ida, dev->id);
400 free_minor_ida:
401 	ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
402 free_eptdev:
403 	put_device(dev);
404 	kfree(eptdev);
405 
406 	return ret;
407 }
408 EXPORT_SYMBOL(rpmsg_chrdev_eptdev_create);
409 
410 static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp)
411 {
412 	struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
413 
414 	get_device(&ctrldev->dev);
415 	filp->private_data = ctrldev;
416 
417 	return 0;
418 }
419 
420 static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp)
421 {
422 	struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
423 
424 	put_device(&ctrldev->dev);
425 
426 	return 0;
427 }
428 
429 static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd,
430 				unsigned long arg)
431 {
432 	struct rpmsg_ctrldev *ctrldev = fp->private_data;
433 	void __user *argp = (void __user *)arg;
434 	struct rpmsg_endpoint_info eptinfo;
435 	struct rpmsg_channel_info chinfo;
436 
437 	if (cmd != RPMSG_CREATE_EPT_IOCTL)
438 		return -EINVAL;
439 
440 	if (copy_from_user(&eptinfo, argp, sizeof(eptinfo)))
441 		return -EFAULT;
442 
443 	memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE);
444 	chinfo.name[RPMSG_NAME_SIZE-1] = '\0';
445 	chinfo.src = eptinfo.src;
446 	chinfo.dst = eptinfo.dst;
447 
448 	return rpmsg_chrdev_eptdev_create(ctrldev->rpdev, &ctrldev->dev, chinfo);
449 };
450 
451 static const struct file_operations rpmsg_ctrldev_fops = {
452 	.owner = THIS_MODULE,
453 	.open = rpmsg_ctrldev_open,
454 	.release = rpmsg_ctrldev_release,
455 	.unlocked_ioctl = rpmsg_ctrldev_ioctl,
456 	.compat_ioctl = compat_ptr_ioctl,
457 };
458 
459 static void rpmsg_ctrldev_release_device(struct device *dev)
460 {
461 	struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev);
462 
463 	ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
464 	ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
465 	kfree(ctrldev);
466 }
467 
468 static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
469 {
470 	struct rpmsg_ctrldev *ctrldev;
471 	struct device *dev;
472 	int ret;
473 
474 	ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL);
475 	if (!ctrldev)
476 		return -ENOMEM;
477 
478 	ctrldev->rpdev = rpdev;
479 
480 	dev = &ctrldev->dev;
481 	device_initialize(dev);
482 	dev->parent = &rpdev->dev;
483 	dev->class = rpmsg_class;
484 
485 	cdev_init(&ctrldev->cdev, &rpmsg_ctrldev_fops);
486 	ctrldev->cdev.owner = THIS_MODULE;
487 
488 	ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
489 	if (ret < 0)
490 		goto free_ctrldev;
491 	dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
492 
493 	ret = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL);
494 	if (ret < 0)
495 		goto free_minor_ida;
496 	dev->id = ret;
497 	dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", ret);
498 
499 	ret = cdev_device_add(&ctrldev->cdev, &ctrldev->dev);
500 	if (ret)
501 		goto free_ctrl_ida;
502 
503 	/* We can now rely on the release function for cleanup */
504 	dev->release = rpmsg_ctrldev_release_device;
505 
506 	dev_set_drvdata(&rpdev->dev, ctrldev);
507 
508 	return ret;
509 
510 free_ctrl_ida:
511 	ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
512 free_minor_ida:
513 	ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
514 free_ctrldev:
515 	put_device(dev);
516 	kfree(ctrldev);
517 
518 	return ret;
519 }
520 
521 static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
522 {
523 	struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev);
524 	int ret;
525 
526 	/* Destroy all endpoints */
527 	ret = device_for_each_child(&ctrldev->dev, NULL, rpmsg_chrdev_eptdev_destroy);
528 	if (ret)
529 		dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret);
530 
531 	cdev_device_del(&ctrldev->cdev, &ctrldev->dev);
532 	put_device(&ctrldev->dev);
533 }
534 
535 static struct rpmsg_driver rpmsg_chrdev_driver = {
536 	.probe = rpmsg_chrdev_probe,
537 	.remove = rpmsg_chrdev_remove,
538 	.drv = {
539 		.name = "rpmsg_chrdev",
540 	},
541 };
542 
543 static int rpmsg_chrdev_init(void)
544 {
545 	int ret;
546 
547 	ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg");
548 	if (ret < 0) {
549 		pr_err("failed to allocate char dev region\n");
550 		return ret;
551 	}
552 
553 	ret = register_rpmsg_driver(&rpmsg_chrdev_driver);
554 	if (ret < 0) {
555 		pr_err("failed to register rpmsg driver\n");
556 		unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
557 	}
558 
559 	return ret;
560 }
561 postcore_initcall(rpmsg_chrdev_init);
562 
563 static void rpmsg_chrdev_exit(void)
564 {
565 	unregister_rpmsg_driver(&rpmsg_chrdev_driver);
566 	unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
567 }
568 module_exit(rpmsg_chrdev_exit);
569 
570 MODULE_ALIAS("rpmsg:rpmsg_chrdev");
571 MODULE_LICENSE("GPL v2");
572