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