1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, Linaro Ltd.
5 * Copyright (c) 2012, PetaLogix
6 * Copyright (c) 2011, Texas Instruments, Inc.
7 * Copyright (c) 2011, Google, Inc.
9 * Based on rpmsg performance statistics driver by Michal Simek, which in turn
10 * was based on TI & Google OMX rpmsg driver.
12 #include <linux/cdev.h>
13 #include <linux/device.h>
15 #include <linux/idr.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/rpmsg.h>
20 #include <linux/skbuff.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <uapi/linux/rpmsg.h>
25 #define RPMSG_DEV_MAX (MINORMASK + 1)
27 static dev_t rpmsg_major;
28 static struct class *rpmsg_class;
30 static DEFINE_IDA(rpmsg_ctrl_ida);
31 static DEFINE_IDA(rpmsg_ept_ida);
32 static DEFINE_IDA(rpmsg_minor_ida);
34 #define dev_to_eptdev(dev) container_of(dev, struct rpmsg_eptdev, dev)
35 #define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct rpmsg_eptdev, cdev)
37 #define dev_to_ctrldev(dev) container_of(dev, struct rpmsg_ctrldev, dev)
38 #define cdev_to_ctrldev(i_cdev) container_of(i_cdev, struct rpmsg_ctrldev, cdev)
41 * struct rpmsg_ctrldev - control device for instantiating endpoint devices
42 * @rpdev: underlaying rpmsg device
43 * @cdev: cdev for the ctrl device
44 * @dev: device for the ctrl device
46 struct rpmsg_ctrldev {
47 struct rpmsg_device *rpdev;
53 * struct rpmsg_eptdev - endpoint device context
54 * @dev: endpoint device
55 * @cdev: cdev for the endpoint device
56 * @rpdev: underlaying rpmsg device
57 * @chinfo: info used to open the endpoint
58 * @ept_lock: synchronization of @ept modifications
59 * @ept: rpmsg endpoint reference, when open
60 * @queue_lock: synchronization of @queue operations
61 * @queue: incoming message queue
62 * @readq: wait object for incoming queue
68 struct rpmsg_device *rpdev;
69 struct rpmsg_channel_info chinfo;
71 struct mutex ept_lock;
72 struct rpmsg_endpoint *ept;
74 spinlock_t queue_lock;
75 struct sk_buff_head queue;
76 wait_queue_head_t readq;
79 static int rpmsg_eptdev_destroy(struct device *dev, void *data)
81 struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
83 mutex_lock(&eptdev->ept_lock);
85 rpmsg_destroy_ept(eptdev->ept);
88 mutex_unlock(&eptdev->ept_lock);
90 /* wake up any blocked readers */
91 wake_up_interruptible(&eptdev->readq);
93 device_del(&eptdev->dev);
94 put_device(&eptdev->dev);
99 static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len,
100 void *priv, u32 addr)
102 struct rpmsg_eptdev *eptdev = priv;
105 skb = alloc_skb(len, GFP_ATOMIC);
109 skb_put_data(skb, buf, len);
111 spin_lock(&eptdev->queue_lock);
112 skb_queue_tail(&eptdev->queue, skb);
113 spin_unlock(&eptdev->queue_lock);
115 /* wake up any blocking processes, waiting for new data */
116 wake_up_interruptible(&eptdev->readq);
121 static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
123 struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
124 struct rpmsg_endpoint *ept;
125 struct rpmsg_device *rpdev = eptdev->rpdev;
126 struct device *dev = &eptdev->dev;
133 ept = rpmsg_create_ept(rpdev, rpmsg_ept_cb, eptdev, eptdev->chinfo);
135 dev_err(dev, "failed to open %s\n", eptdev->chinfo.name);
141 filp->private_data = eptdev;
146 static int rpmsg_eptdev_release(struct inode *inode, struct file *filp)
148 struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
149 struct device *dev = &eptdev->dev;
151 /* Close the endpoint, if it's not already destroyed by the parent */
152 mutex_lock(&eptdev->ept_lock);
154 rpmsg_destroy_ept(eptdev->ept);
157 mutex_unlock(&eptdev->ept_lock);
159 /* Discard all SKBs */
160 skb_queue_purge(&eptdev->queue);
167 static ssize_t rpmsg_eptdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
169 struct file *filp = iocb->ki_filp;
170 struct rpmsg_eptdev *eptdev = filp->private_data;
178 spin_lock_irqsave(&eptdev->queue_lock, flags);
180 /* Wait for data in the queue */
181 if (skb_queue_empty(&eptdev->queue)) {
182 spin_unlock_irqrestore(&eptdev->queue_lock, flags);
184 if (filp->f_flags & O_NONBLOCK)
187 /* Wait until we get data or the endpoint goes away */
188 if (wait_event_interruptible(eptdev->readq,
189 !skb_queue_empty(&eptdev->queue) ||
193 /* We lost the endpoint while waiting */
197 spin_lock_irqsave(&eptdev->queue_lock, flags);
200 skb = skb_dequeue(&eptdev->queue);
201 spin_unlock_irqrestore(&eptdev->queue_lock, flags);
205 use = min_t(size_t, iov_iter_count(to), skb->len);
206 if (copy_to_iter(skb->data, use, to) != use)
214 static ssize_t rpmsg_eptdev_write_iter(struct kiocb *iocb,
215 struct iov_iter *from)
217 struct file *filp = iocb->ki_filp;
218 struct rpmsg_eptdev *eptdev = filp->private_data;
219 size_t len = iov_iter_count(from);
223 kbuf = kzalloc(len, GFP_KERNEL);
227 if (!copy_from_iter_full(kbuf, len, from)) {
232 if (mutex_lock_interruptible(&eptdev->ept_lock)) {
242 if (filp->f_flags & O_NONBLOCK)
243 ret = rpmsg_trysendto(eptdev->ept, kbuf, len, eptdev->chinfo.dst);
245 ret = rpmsg_sendto(eptdev->ept, kbuf, len, eptdev->chinfo.dst);
248 mutex_unlock(&eptdev->ept_lock);
252 return ret < 0 ? ret : len;
255 static __poll_t rpmsg_eptdev_poll(struct file *filp, poll_table *wait)
257 struct rpmsg_eptdev *eptdev = filp->private_data;
263 poll_wait(filp, &eptdev->readq, wait);
265 if (!skb_queue_empty(&eptdev->queue))
266 mask |= EPOLLIN | EPOLLRDNORM;
268 mask |= rpmsg_poll(eptdev->ept, filp, wait);
273 static long rpmsg_eptdev_ioctl(struct file *fp, unsigned int cmd,
276 struct rpmsg_eptdev *eptdev = fp->private_data;
278 if (cmd != RPMSG_DESTROY_EPT_IOCTL)
281 return rpmsg_eptdev_destroy(&eptdev->dev, NULL);
284 static const struct file_operations rpmsg_eptdev_fops = {
285 .owner = THIS_MODULE,
286 .open = rpmsg_eptdev_open,
287 .release = rpmsg_eptdev_release,
288 .read_iter = rpmsg_eptdev_read_iter,
289 .write_iter = rpmsg_eptdev_write_iter,
290 .poll = rpmsg_eptdev_poll,
291 .unlocked_ioctl = rpmsg_eptdev_ioctl,
292 .compat_ioctl = compat_ptr_ioctl,
295 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
298 struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
300 return sprintf(buf, "%s\n", eptdev->chinfo.name);
302 static DEVICE_ATTR_RO(name);
304 static ssize_t src_show(struct device *dev, struct device_attribute *attr,
307 struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
309 return sprintf(buf, "%d\n", eptdev->chinfo.src);
311 static DEVICE_ATTR_RO(src);
313 static ssize_t dst_show(struct device *dev, struct device_attribute *attr,
316 struct rpmsg_eptdev *eptdev = dev_get_drvdata(dev);
318 return sprintf(buf, "%d\n", eptdev->chinfo.dst);
320 static DEVICE_ATTR_RO(dst);
322 static struct attribute *rpmsg_eptdev_attrs[] = {
328 ATTRIBUTE_GROUPS(rpmsg_eptdev);
330 static void rpmsg_eptdev_release_device(struct device *dev)
332 struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
334 ida_simple_remove(&rpmsg_ept_ida, dev->id);
335 ida_simple_remove(&rpmsg_minor_ida, MINOR(eptdev->dev.devt));
336 cdev_del(&eptdev->cdev);
340 static int rpmsg_eptdev_create(struct rpmsg_ctrldev *ctrldev,
341 struct rpmsg_channel_info chinfo)
343 struct rpmsg_device *rpdev = ctrldev->rpdev;
344 struct rpmsg_eptdev *eptdev;
348 eptdev = kzalloc(sizeof(*eptdev), GFP_KERNEL);
353 eptdev->rpdev = rpdev;
354 eptdev->chinfo = chinfo;
356 mutex_init(&eptdev->ept_lock);
357 spin_lock_init(&eptdev->queue_lock);
358 skb_queue_head_init(&eptdev->queue);
359 init_waitqueue_head(&eptdev->readq);
361 device_initialize(dev);
362 dev->class = rpmsg_class;
363 dev->parent = &ctrldev->dev;
364 dev->groups = rpmsg_eptdev_groups;
365 dev_set_drvdata(dev, eptdev);
367 cdev_init(&eptdev->cdev, &rpmsg_eptdev_fops);
368 eptdev->cdev.owner = THIS_MODULE;
370 ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
373 dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
375 ret = ida_simple_get(&rpmsg_ept_ida, 0, 0, GFP_KERNEL);
379 dev_set_name(dev, "rpmsg%d", ret);
381 ret = cdev_add(&eptdev->cdev, dev->devt, 1);
385 /* We can now rely on the release function for cleanup */
386 dev->release = rpmsg_eptdev_release_device;
388 ret = device_add(dev);
390 dev_err(dev, "device_add failed: %d\n", ret);
397 ida_simple_remove(&rpmsg_ept_ida, dev->id);
399 ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
407 static int rpmsg_ctrldev_open(struct inode *inode, struct file *filp)
409 struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
411 get_device(&ctrldev->dev);
412 filp->private_data = ctrldev;
417 static int rpmsg_ctrldev_release(struct inode *inode, struct file *filp)
419 struct rpmsg_ctrldev *ctrldev = cdev_to_ctrldev(inode->i_cdev);
421 put_device(&ctrldev->dev);
426 static long rpmsg_ctrldev_ioctl(struct file *fp, unsigned int cmd,
429 struct rpmsg_ctrldev *ctrldev = fp->private_data;
430 void __user *argp = (void __user *)arg;
431 struct rpmsg_endpoint_info eptinfo;
432 struct rpmsg_channel_info chinfo;
434 if (cmd != RPMSG_CREATE_EPT_IOCTL)
437 if (copy_from_user(&eptinfo, argp, sizeof(eptinfo)))
440 memcpy(chinfo.name, eptinfo.name, RPMSG_NAME_SIZE);
441 chinfo.name[RPMSG_NAME_SIZE-1] = '\0';
442 chinfo.src = eptinfo.src;
443 chinfo.dst = eptinfo.dst;
445 return rpmsg_eptdev_create(ctrldev, chinfo);
448 static const struct file_operations rpmsg_ctrldev_fops = {
449 .owner = THIS_MODULE,
450 .open = rpmsg_ctrldev_open,
451 .release = rpmsg_ctrldev_release,
452 .unlocked_ioctl = rpmsg_ctrldev_ioctl,
453 .compat_ioctl = compat_ptr_ioctl,
456 static void rpmsg_ctrldev_release_device(struct device *dev)
458 struct rpmsg_ctrldev *ctrldev = dev_to_ctrldev(dev);
460 ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
461 ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
462 cdev_del(&ctrldev->cdev);
466 static int rpmsg_chrdev_probe(struct rpmsg_device *rpdev)
468 struct rpmsg_ctrldev *ctrldev;
472 ctrldev = kzalloc(sizeof(*ctrldev), GFP_KERNEL);
476 ctrldev->rpdev = rpdev;
479 device_initialize(dev);
480 dev->parent = &rpdev->dev;
481 dev->class = rpmsg_class;
483 cdev_init(&ctrldev->cdev, &rpmsg_ctrldev_fops);
484 ctrldev->cdev.owner = THIS_MODULE;
486 ret = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX, GFP_KERNEL);
489 dev->devt = MKDEV(MAJOR(rpmsg_major), ret);
491 ret = ida_simple_get(&rpmsg_ctrl_ida, 0, 0, GFP_KERNEL);
495 dev_set_name(&ctrldev->dev, "rpmsg_ctrl%d", ret);
497 ret = cdev_add(&ctrldev->cdev, dev->devt, 1);
501 /* We can now rely on the release function for cleanup */
502 dev->release = rpmsg_ctrldev_release_device;
504 ret = device_add(dev);
506 dev_err(&rpdev->dev, "device_add failed: %d\n", ret);
510 dev_set_drvdata(&rpdev->dev, ctrldev);
515 ida_simple_remove(&rpmsg_ctrl_ida, dev->id);
517 ida_simple_remove(&rpmsg_minor_ida, MINOR(dev->devt));
525 static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev)
527 struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev);
530 /* Destroy all endpoints */
531 ret = device_for_each_child(&ctrldev->dev, NULL, rpmsg_eptdev_destroy);
533 dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret);
535 device_del(&ctrldev->dev);
536 put_device(&ctrldev->dev);
539 static struct rpmsg_driver rpmsg_chrdev_driver = {
540 .probe = rpmsg_chrdev_probe,
541 .remove = rpmsg_chrdev_remove,
543 .name = "rpmsg_chrdev",
547 static int rpmsg_chrdev_init(void)
551 ret = alloc_chrdev_region(&rpmsg_major, 0, RPMSG_DEV_MAX, "rpmsg");
553 pr_err("rpmsg: failed to allocate char dev region\n");
557 rpmsg_class = class_create(THIS_MODULE, "rpmsg");
558 if (IS_ERR(rpmsg_class)) {
559 pr_err("failed to create rpmsg class\n");
560 unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
561 return PTR_ERR(rpmsg_class);
564 ret = register_rpmsg_driver(&rpmsg_chrdev_driver);
566 pr_err("rpmsgchr: failed to register rpmsg driver\n");
567 class_destroy(rpmsg_class);
568 unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
573 postcore_initcall(rpmsg_chrdev_init);
575 static void rpmsg_chrdev_exit(void)
577 unregister_rpmsg_driver(&rpmsg_chrdev_driver);
578 class_destroy(rpmsg_class);
579 unregister_chrdev_region(rpmsg_major, RPMSG_DEV_MAX);
581 module_exit(rpmsg_chrdev_exit);
583 MODULE_ALIAS("rpmsg:rpmsg_chrdev");
584 MODULE_LICENSE("GPL v2");