remoteproc_sysfs.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Remote Processor Framework
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/remoteproc.h>
  15. #include "remoteproc_internal.h"
  16. #define to_rproc(d) container_of(d, struct rproc, dev)
  17. /* Expose the loaded / running firmware name via sysfs */
  18. static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
  19. char *buf)
  20. {
  21. struct rproc *rproc = to_rproc(dev);
  22. return sprintf(buf, "%s\n", rproc->firmware);
  23. }
  24. /* Change firmware name via sysfs */
  25. static ssize_t firmware_store(struct device *dev,
  26. struct device_attribute *attr,
  27. const char *buf, size_t count)
  28. {
  29. struct rproc *rproc = to_rproc(dev);
  30. int err;
  31. /* restrict sysfs operation for userspace-based loader */
  32. if (rproc->use_userspace_loader)
  33. return -EPERM;
  34. err = rproc_set_firmware(rproc, buf);
  35. return err ? err : count;
  36. }
  37. static DEVICE_ATTR_RW(firmware);
  38. /*
  39. * A state-to-string lookup table, for exposing a human readable state
  40. * via sysfs. Always keep in sync with enum rproc_state
  41. */
  42. static const char * const rproc_state_string[] = {
  43. [RPROC_OFFLINE] = "offline",
  44. [RPROC_SUSPENDED] = "suspended",
  45. [RPROC_RUNNING] = "running",
  46. [RPROC_CRASHED] = "crashed",
  47. [RPROC_DELETED] = "deleted",
  48. [RPROC_LAST] = "invalid",
  49. };
  50. /* Expose the state of the remote processor via sysfs */
  51. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  52. char *buf)
  53. {
  54. struct rproc *rproc = to_rproc(dev);
  55. unsigned int state;
  56. state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
  57. return sprintf(buf, "%s\n", rproc_state_string[state]);
  58. }
  59. /* Change remote processor state via sysfs */
  60. static ssize_t state_store(struct device *dev,
  61. struct device_attribute *attr,
  62. const char *buf, size_t count)
  63. {
  64. struct rproc *rproc = to_rproc(dev);
  65. int ret = 0;
  66. /* restrict sysfs operation for userspace-based loader */
  67. if (rproc->use_userspace_loader)
  68. return -EPERM;
  69. if (sysfs_streq(buf, "start")) {
  70. if (rproc->state == RPROC_RUNNING)
  71. return -EBUSY;
  72. /*
  73. * prevent underlying implementation from being removed
  74. * when remoteproc does not support auto-boot
  75. */
  76. if (!rproc->auto_boot &&
  77. !try_module_get(dev->parent->driver->owner))
  78. return -EINVAL;
  79. ret = rproc_boot(rproc);
  80. if (ret) {
  81. dev_err(&rproc->dev, "Boot failed: %d\n", ret);
  82. if (!rproc->auto_boot)
  83. module_put(dev->parent->driver->owner);
  84. }
  85. } else if (sysfs_streq(buf, "stop")) {
  86. if (rproc->state != RPROC_RUNNING &&
  87. rproc->state != RPROC_SUSPENDED)
  88. return -EINVAL;
  89. rproc_shutdown(rproc);
  90. if (!rproc->auto_boot)
  91. module_put(dev->parent->driver->owner);
  92. } else {
  93. dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
  94. ret = -EINVAL;
  95. }
  96. return ret ? ret : count;
  97. }
  98. static DEVICE_ATTR_RW(state);
  99. static struct attribute *rproc_attrs[] = {
  100. &dev_attr_firmware.attr,
  101. &dev_attr_state.attr,
  102. NULL
  103. };
  104. static const struct attribute_group rproc_devgroup = {
  105. .attrs = rproc_attrs
  106. };
  107. static const struct attribute_group *rproc_devgroups[] = {
  108. &rproc_devgroup,
  109. NULL
  110. };
  111. struct class rproc_class = {
  112. .name = "remoteproc",
  113. .dev_groups = rproc_devgroups,
  114. };
  115. int __init rproc_init_sysfs(void)
  116. {
  117. /* create remoteproc device class for sysfs */
  118. int err = class_register(&rproc_class);
  119. if (err)
  120. pr_err("remoteproc: unable to register class\n");
  121. return err;
  122. }
  123. void __exit rproc_exit_sysfs(void)
  124. {
  125. class_unregister(&rproc_class);
  126. }