remoteproc_sysfs.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/remoteproc.h>
  14. #include "remoteproc_internal.h"
  15. #define to_rproc(d) container_of(d, struct rproc, dev)
  16. /* Expose the loaded / running firmware name via sysfs */
  17. static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
  18. char *buf)
  19. {
  20. struct rproc *rproc = to_rproc(dev);
  21. return sprintf(buf, "%s\n", rproc->firmware);
  22. }
  23. /* Change firmware name via sysfs */
  24. static ssize_t firmware_store(struct device *dev,
  25. struct device_attribute *attr,
  26. const char *buf, size_t count)
  27. {
  28. struct rproc *rproc = to_rproc(dev);
  29. char *p;
  30. int err, len = count;
  31. err = mutex_lock_interruptible(&rproc->lock);
  32. if (err) {
  33. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, err);
  34. return -EINVAL;
  35. }
  36. if (rproc->state != RPROC_OFFLINE) {
  37. dev_err(dev, "can't change firmware while running\n");
  38. err = -EBUSY;
  39. goto out;
  40. }
  41. len = strcspn(buf, "\n");
  42. if (!len) {
  43. dev_err(dev, "can't provide a NULL firmware\n");
  44. err = -EINVAL;
  45. goto out;
  46. }
  47. p = kstrndup(buf, len, GFP_KERNEL);
  48. if (!p) {
  49. err = -ENOMEM;
  50. goto out;
  51. }
  52. kfree(rproc->firmware);
  53. rproc->firmware = p;
  54. out:
  55. mutex_unlock(&rproc->lock);
  56. return err ? err : count;
  57. }
  58. static DEVICE_ATTR_RW(firmware);
  59. /*
  60. * A state-to-string lookup table, for exposing a human readable state
  61. * via sysfs. Always keep in sync with enum rproc_state
  62. */
  63. static const char * const rproc_state_string[] = {
  64. [RPROC_OFFLINE] = "offline",
  65. [RPROC_SUSPENDED] = "suspended",
  66. [RPROC_RUNNING] = "running",
  67. [RPROC_CRASHED] = "crashed",
  68. [RPROC_DELETED] = "deleted",
  69. [RPROC_LAST] = "invalid",
  70. };
  71. /* Expose the state of the remote processor via sysfs */
  72. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  73. char *buf)
  74. {
  75. struct rproc *rproc = to_rproc(dev);
  76. unsigned int state;
  77. state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
  78. return sprintf(buf, "%s\n", rproc_state_string[state]);
  79. }
  80. /* Change remote processor state via sysfs */
  81. static ssize_t state_store(struct device *dev,
  82. struct device_attribute *attr,
  83. const char *buf, size_t count)
  84. {
  85. struct rproc *rproc = to_rproc(dev);
  86. int ret = 0;
  87. if (sysfs_streq(buf, "start")) {
  88. if (rproc->state == RPROC_RUNNING)
  89. return -EBUSY;
  90. ret = rproc_boot(rproc);
  91. if (ret)
  92. dev_err(&rproc->dev, "Boot failed: %d\n", ret);
  93. } else if (sysfs_streq(buf, "stop")) {
  94. if (rproc->state != RPROC_RUNNING)
  95. return -EINVAL;
  96. rproc_shutdown(rproc);
  97. } else {
  98. dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
  99. ret = -EINVAL;
  100. }
  101. return ret ? ret : count;
  102. }
  103. static DEVICE_ATTR_RW(state);
  104. static struct attribute *rproc_attrs[] = {
  105. &dev_attr_firmware.attr,
  106. &dev_attr_state.attr,
  107. NULL
  108. };
  109. static const struct attribute_group rproc_devgroup = {
  110. .attrs = rproc_attrs
  111. };
  112. static const struct attribute_group *rproc_devgroups[] = {
  113. &rproc_devgroup,
  114. NULL
  115. };
  116. struct class rproc_class = {
  117. .name = "remoteproc",
  118. .dev_groups = rproc_devgroups,
  119. };
  120. int __init rproc_init_sysfs(void)
  121. {
  122. /* create remoteproc device class for sysfs */
  123. int err = class_register(&rproc_class);
  124. if (err)
  125. pr_err("remoteproc: unable to register class\n");
  126. return err;
  127. }
  128. void __exit rproc_exit_sysfs(void)
  129. {
  130. class_unregister(&rproc_class);
  131. }