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