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