usb: xhci: Fix build warning seen with CONFIG_PM=n
[linux/fpc-iii.git] / drivers / remoteproc / remoteproc_sysfs.c
blob7f8536b73295d50d85dae7203e7fea8443a6941b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Remote Processor Framework
4 */
6 #include <linux/remoteproc.h>
8 #include "remoteproc_internal.h"
10 #define to_rproc(d) container_of(d, struct rproc, dev)
12 /* Expose the loaded / running firmware name via sysfs */
13 static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
14 char *buf)
16 struct rproc *rproc = to_rproc(dev);
18 return sprintf(buf, "%s\n", rproc->firmware);
21 /* Change firmware name via sysfs */
22 static ssize_t firmware_store(struct device *dev,
23 struct device_attribute *attr,
24 const char *buf, size_t count)
26 struct rproc *rproc = to_rproc(dev);
27 char *p;
28 int err, len = count;
30 err = mutex_lock_interruptible(&rproc->lock);
31 if (err) {
32 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, err);
33 return -EINVAL;
36 if (rproc->state != RPROC_OFFLINE) {
37 dev_err(dev, "can't change firmware while running\n");
38 err = -EBUSY;
39 goto out;
42 len = strcspn(buf, "\n");
43 if (!len) {
44 dev_err(dev, "can't provide a NULL firmware\n");
45 err = -EINVAL;
46 goto out;
49 p = kstrndup(buf, len, GFP_KERNEL);
50 if (!p) {
51 err = -ENOMEM;
52 goto out;
55 kfree(rproc->firmware);
56 rproc->firmware = p;
57 out:
58 mutex_unlock(&rproc->lock);
60 return err ? err : count;
62 static DEVICE_ATTR_RW(firmware);
65 * A state-to-string lookup table, for exposing a human readable state
66 * via sysfs. Always keep in sync with enum rproc_state
68 static const char * const rproc_state_string[] = {
69 [RPROC_OFFLINE] = "offline",
70 [RPROC_SUSPENDED] = "suspended",
71 [RPROC_RUNNING] = "running",
72 [RPROC_CRASHED] = "crashed",
73 [RPROC_DELETED] = "deleted",
74 [RPROC_LAST] = "invalid",
77 /* Expose the state of the remote processor via sysfs */
78 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
79 char *buf)
81 struct rproc *rproc = to_rproc(dev);
82 unsigned int state;
84 state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
85 return sprintf(buf, "%s\n", rproc_state_string[state]);
88 /* Change remote processor state via sysfs */
89 static ssize_t state_store(struct device *dev,
90 struct device_attribute *attr,
91 const char *buf, size_t count)
93 struct rproc *rproc = to_rproc(dev);
94 int ret = 0;
96 if (sysfs_streq(buf, "start")) {
97 if (rproc->state == RPROC_RUNNING)
98 return -EBUSY;
100 ret = rproc_boot(rproc);
101 if (ret)
102 dev_err(&rproc->dev, "Boot failed: %d\n", ret);
103 } else if (sysfs_streq(buf, "stop")) {
104 if (rproc->state != RPROC_RUNNING)
105 return -EINVAL;
107 rproc_shutdown(rproc);
108 } else {
109 dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
110 ret = -EINVAL;
112 return ret ? ret : count;
114 static DEVICE_ATTR_RW(state);
116 /* Expose the name of the remote processor via sysfs */
117 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
118 char *buf)
120 struct rproc *rproc = to_rproc(dev);
122 return sprintf(buf, "%s\n", rproc->name);
124 static DEVICE_ATTR_RO(name);
126 static struct attribute *rproc_attrs[] = {
127 &dev_attr_firmware.attr,
128 &dev_attr_state.attr,
129 &dev_attr_name.attr,
130 NULL
133 static const struct attribute_group rproc_devgroup = {
134 .attrs = rproc_attrs
137 static const struct attribute_group *rproc_devgroups[] = {
138 &rproc_devgroup,
139 NULL
142 struct class rproc_class = {
143 .name = "remoteproc",
144 .dev_groups = rproc_devgroups,
147 int __init rproc_init_sysfs(void)
149 /* create remoteproc device class for sysfs */
150 int err = class_register(&rproc_class);
152 if (err)
153 pr_err("remoteproc: unable to register class\n");
154 return err;
157 void __exit rproc_exit_sysfs(void)
159 class_unregister(&rproc_class);