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_LAST] = "invalid",
  64. };
  65. /* Expose the state of the remote processor via sysfs */
  66. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  67. char *buf)
  68. {
  69. struct rproc *rproc = to_rproc(dev);
  70. unsigned int state;
  71. state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
  72. return sprintf(buf, "%s\n", rproc_state_string[state]);
  73. }
  74. /* Change remote processor state via sysfs */
  75. static ssize_t state_store(struct device *dev,
  76. struct device_attribute *attr,
  77. const char *buf, size_t count)
  78. {
  79. struct rproc *rproc = to_rproc(dev);
  80. int ret = 0;
  81. if (sysfs_streq(buf, "start")) {
  82. if (rproc->state == RPROC_RUNNING)
  83. return -EBUSY;
  84. ret = rproc_boot(rproc);
  85. if (ret)
  86. dev_err(&rproc->dev, "Boot failed: %d\n", ret);
  87. } else if (sysfs_streq(buf, "stop")) {
  88. if (rproc->state != RPROC_RUNNING)
  89. return -EINVAL;
  90. rproc_shutdown(rproc);
  91. } else {
  92. dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
  93. ret = -EINVAL;
  94. }
  95. return ret ? ret : count;
  96. }
  97. static DEVICE_ATTR_RW(state);
  98. static struct attribute *rproc_attrs[] = {
  99. &dev_attr_firmware.attr,
  100. &dev_attr_state.attr,
  101. NULL
  102. };
  103. static const struct attribute_group rproc_devgroup = {
  104. .attrs = rproc_attrs
  105. };
  106. static const struct attribute_group *rproc_devgroups[] = {
  107. &rproc_devgroup,
  108. NULL
  109. };
  110. struct class rproc_class = {
  111. .name = "remoteproc",
  112. .dev_groups = rproc_devgroups,
  113. };
  114. int __init rproc_init_sysfs(void)
  115. {
  116. /* create remoteproc device class for sysfs */
  117. int err = class_register(&rproc_class);
  118. if (err)
  119. pr_err("remoteproc: unable to register class\n");
  120. return err;
  121. }
  122. void __exit rproc_exit_sysfs(void)
  123. {
  124. class_unregister(&rproc_class);
  125. }