]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/remoteproc/remoteproc_sysfs.c
Merge tag 'block-5.7-2020-05-16' of git://git.kernel.dk/linux-block
[thirdparty/linux.git] / drivers / remoteproc / remoteproc_sysfs.c
CommitLineData
1802d0be 1// SPDX-License-Identifier: GPL-2.0-only
2aefbef0
MR
2/*
3 * Remote Processor Framework
2aefbef0
MR
4 */
5
6#include <linux/remoteproc.h>
bf89a7c0 7#include <linux/slab.h>
2aefbef0
MR
8
9#include "remoteproc_internal.h"
10
11#define to_rproc(d) container_of(d, struct rproc, dev)
12
13/* Expose the loaded / running firmware name via sysfs */
14static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
15 char *buf)
16{
17 struct rproc *rproc = to_rproc(dev);
18
19 return sprintf(buf, "%s\n", rproc->firmware);
20}
21
22/* Change firmware name via sysfs */
23static ssize_t firmware_store(struct device *dev,
24 struct device_attribute *attr,
25 const char *buf, size_t count)
26{
27 struct rproc *rproc = to_rproc(dev);
28 char *p;
29 int err, len = count;
30
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
37 if (rproc->state != RPROC_OFFLINE) {
38 dev_err(dev, "can't change firmware while running\n");
39 err = -EBUSY;
40 goto out;
41 }
42
43 len = strcspn(buf, "\n");
faeadbb6
SA
44 if (!len) {
45 dev_err(dev, "can't provide a NULL firmware\n");
46 err = -EINVAL;
47 goto out;
48 }
2aefbef0
MR
49
50 p = kstrndup(buf, len, GFP_KERNEL);
51 if (!p) {
52 err = -ENOMEM;
53 goto out;
54 }
55
56 kfree(rproc->firmware);
57 rproc->firmware = p;
58out:
59 mutex_unlock(&rproc->lock);
60
61 return err ? err : count;
62}
63static DEVICE_ATTR_RW(firmware);
64
65/*
66 * A state-to-string lookup table, for exposing a human readable state
67 * via sysfs. Always keep in sync with enum rproc_state
68 */
69static const char * const rproc_state_string[] = {
70 [RPROC_OFFLINE] = "offline",
71 [RPROC_SUSPENDED] = "suspended",
72 [RPROC_RUNNING] = "running",
73 [RPROC_CRASHED] = "crashed",
608d7921 74 [RPROC_DELETED] = "deleted",
2aefbef0
MR
75 [RPROC_LAST] = "invalid",
76};
77
78/* Expose the state of the remote processor via sysfs */
79static ssize_t state_show(struct device *dev, struct device_attribute *attr,
80 char *buf)
81{
82 struct rproc *rproc = to_rproc(dev);
83 unsigned int state;
84
85 state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
86 return sprintf(buf, "%s\n", rproc_state_string[state]);
87}
88
89/* Change remote processor state via sysfs */
90static ssize_t state_store(struct device *dev,
91 struct device_attribute *attr,
92 const char *buf, size_t count)
93{
94 struct rproc *rproc = to_rproc(dev);
95 int ret = 0;
96
97 if (sysfs_streq(buf, "start")) {
98 if (rproc->state == RPROC_RUNNING)
99 return -EBUSY;
100
101 ret = rproc_boot(rproc);
102 if (ret)
103 dev_err(&rproc->dev, "Boot failed: %d\n", ret);
104 } else if (sysfs_streq(buf, "stop")) {
105 if (rproc->state != RPROC_RUNNING)
106 return -EINVAL;
107
108 rproc_shutdown(rproc);
109 } else {
110 dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
111 ret = -EINVAL;
112 }
113 return ret ? ret : count;
114}
115static DEVICE_ATTR_RW(state);
116
6ed756aa
SA
117/* Expose the name of the remote processor via sysfs */
118static ssize_t name_show(struct device *dev, struct device_attribute *attr,
119 char *buf)
120{
121 struct rproc *rproc = to_rproc(dev);
122
123 return sprintf(buf, "%s\n", rproc->name);
124}
125static DEVICE_ATTR_RO(name);
126
2aefbef0
MR
127static struct attribute *rproc_attrs[] = {
128 &dev_attr_firmware.attr,
129 &dev_attr_state.attr,
6ed756aa 130 &dev_attr_name.attr,
2aefbef0
MR
131 NULL
132};
133
134static const struct attribute_group rproc_devgroup = {
135 .attrs = rproc_attrs
136};
137
138static const struct attribute_group *rproc_devgroups[] = {
139 &rproc_devgroup,
140 NULL
141};
142
143struct class rproc_class = {
144 .name = "remoteproc",
145 .dev_groups = rproc_devgroups,
146};
147
148int __init rproc_init_sysfs(void)
149{
150 /* create remoteproc device class for sysfs */
151 int err = class_register(&rproc_class);
152
153 if (err)
154 pr_err("remoteproc: unable to register class\n");
155 return err;
156}
157
158void __exit rproc_exit_sysfs(void)
159{
160 class_unregister(&rproc_class);
161}