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