remoteproc_cdev.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Character device interface driver for Remoteproc framework.
  4. *
  5. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  6. */
  7. #include <linux/cdev.h>
  8. #include <linux/compat.h>
  9. #include <linux/fs.h>
  10. #include <linux/module.h>
  11. #include <linux/remoteproc.h>
  12. #include <linux/uaccess.h>
  13. #include <uapi/linux/remoteproc_cdev.h>
  14. #include "remoteproc_internal.h"
  15. #define NUM_RPROC_DEVICES 64
  16. static dev_t rproc_major;
  17. static ssize_t rproc_cdev_write(struct file *filp, const char __user *buf, size_t len, loff_t *pos)
  18. {
  19. struct rproc *rproc = container_of(filp->f_inode->i_cdev, struct rproc, cdev);
  20. int ret = 0;
  21. char cmd[10];
  22. if (!len || len > sizeof(cmd))
  23. return -EINVAL;
  24. ret = copy_from_user(cmd, buf, len);
  25. if (ret)
  26. return -EFAULT;
  27. if (!strncmp(cmd, "start", len)) {
  28. ret = rproc_boot(rproc);
  29. } else if (!strncmp(cmd, "stop", len)) {
  30. ret = rproc_shutdown(rproc);
  31. } else if (!strncmp(cmd, "detach", len)) {
  32. ret = rproc_detach(rproc);
  33. } else {
  34. dev_err(&rproc->dev, "Unrecognized option\n");
  35. ret = -EINVAL;
  36. }
  37. return ret ? ret : len;
  38. }
  39. static long rproc_device_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  40. {
  41. struct rproc *rproc = container_of(filp->f_inode->i_cdev, struct rproc, cdev);
  42. void __user *argp = (void __user *)arg;
  43. s32 param;
  44. switch (ioctl) {
  45. case RPROC_SET_SHUTDOWN_ON_RELEASE:
  46. if (copy_from_user(&param, argp, sizeof(s32)))
  47. return -EFAULT;
  48. rproc->cdev_put_on_release = !!param;
  49. break;
  50. case RPROC_GET_SHUTDOWN_ON_RELEASE:
  51. param = (s32)rproc->cdev_put_on_release;
  52. if (copy_to_user(argp, &param, sizeof(s32)))
  53. return -EFAULT;
  54. break;
  55. default:
  56. dev_err(&rproc->dev, "Unsupported ioctl\n");
  57. return -EINVAL;
  58. }
  59. return 0;
  60. }
  61. static int rproc_cdev_release(struct inode *inode, struct file *filp)
  62. {
  63. struct rproc *rproc = container_of(inode->i_cdev, struct rproc, cdev);
  64. int ret = 0;
  65. if (!rproc->cdev_put_on_release)
  66. return 0;
  67. if (rproc->state == RPROC_RUNNING)
  68. rproc_shutdown(rproc);
  69. else if (rproc->state == RPROC_ATTACHED)
  70. ret = rproc_detach(rproc);
  71. return ret;
  72. }
  73. static const struct file_operations rproc_fops = {
  74. .write = rproc_cdev_write,
  75. .unlocked_ioctl = rproc_device_ioctl,
  76. .compat_ioctl = compat_ptr_ioctl,
  77. .release = rproc_cdev_release,
  78. };
  79. int rproc_char_device_add(struct rproc *rproc)
  80. {
  81. int ret;
  82. cdev_init(&rproc->cdev, &rproc_fops);
  83. rproc->cdev.owner = THIS_MODULE;
  84. rproc->dev.devt = MKDEV(MAJOR(rproc_major), rproc->index);
  85. cdev_set_parent(&rproc->cdev, &rproc->dev.kobj);
  86. ret = cdev_add(&rproc->cdev, rproc->dev.devt, 1);
  87. if (ret < 0)
  88. dev_err(&rproc->dev, "Failed to add char dev for %s\n", rproc->name);
  89. return ret;
  90. }
  91. void rproc_char_device_remove(struct rproc *rproc)
  92. {
  93. cdev_del(&rproc->cdev);
  94. }
  95. void __init rproc_init_cdev(void)
  96. {
  97. int ret;
  98. ret = alloc_chrdev_region(&rproc_major, 0, NUM_RPROC_DEVICES, "remoteproc");
  99. if (ret < 0)
  100. pr_err("Failed to alloc rproc_cdev region, err %d\n", ret);
  101. }