remoteproc_sysfs.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. p = kstrndup(buf, len, GFP_KERNEL);
  43. if (!p) {
  44. err = -ENOMEM;
  45. goto out;
  46. }
  47. kfree(rproc->firmware);
  48. rproc->firmware = p;
  49. out:
  50. mutex_unlock(&rproc->lock);
  51. return err ? err : count;
  52. }
  53. static DEVICE_ATTR_RW(firmware);
  54. /*
  55. * A state-to-string lookup table, for exposing a human readable state
  56. * via sysfs. Always keep in sync with enum rproc_state
  57. */
  58. static const char * const rproc_state_string[] = {
  59. [RPROC_OFFLINE] = "offline",
  60. [RPROC_SUSPENDED] = "suspended",
  61. [RPROC_RUNNING] = "running",
  62. [RPROC_CRASHED] = "crashed",
  63. [RPROC_DELETED] = "deleted",
  64. [RPROC_LAST] = "invalid",
  65. };
  66. /* Expose the state of the remote processor via sysfs */
  67. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  68. char *buf)
  69. {
  70. struct rproc *rproc = to_rproc(dev);
  71. unsigned int state;
  72. state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
  73. return sprintf(buf, "%s\n", rproc_state_string[state]);
  74. }
  75. /* Change remote processor state via sysfs */
  76. static ssize_t state_store(struct device *dev,
  77. struct device_attribute *attr,
  78. const char *buf, size_t count)
  79. {
  80. struct rproc *rproc = to_rproc(dev);
  81. int ret = 0;
  82. if (sysfs_streq(buf, "start")) {
  83. if (rproc->state == RPROC_RUNNING)
  84. return -EBUSY;
  85. ret = rproc_boot(rproc);
  86. if (ret)
  87. dev_err(&rproc->dev, "Boot failed: %d\n", ret);
  88. } else if (sysfs_streq(buf, "stop")) {
  89. if (rproc->state != RPROC_RUNNING)
  90. return -EINVAL;
  91. rproc_shutdown(rproc);
  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. }