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