net/mlx5: fix a -Wstringop-truncation warning
[linux-2.6-block.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>
7
8#include "remoteproc_internal.h"
9
10#define to_rproc(d) container_of(d, struct rproc, dev)
11
12/* Expose the loaded / running firmware name via sysfs */
13static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
14 char *buf)
15{
16 struct rproc *rproc = to_rproc(dev);
17
18 return sprintf(buf, "%s\n", rproc->firmware);
19}
20
21/* Change firmware name via sysfs */
22static ssize_t firmware_store(struct device *dev,
23 struct device_attribute *attr,
24 const char *buf, size_t count)
25{
26 struct rproc *rproc = to_rproc(dev);
27 char *p;
28 int err, len = count;
29
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;
34 }
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
42 len = strcspn(buf, "\n");
faeadbb6
SA
43 if (!len) {
44 dev_err(dev, "can't provide a NULL firmware\n");
45 err = -EINVAL;
46 goto out;
47 }
2aefbef0
MR
48
49 p = kstrndup(buf, len, GFP_KERNEL);
50 if (!p) {
51 err = -ENOMEM;
52 goto out;
53 }
54
55 kfree(rproc->firmware);
56 rproc->firmware = p;
57out:
58 mutex_unlock(&rproc->lock);
59
60 return err ? err : count;
61}
62static DEVICE_ATTR_RW(firmware);
63
64/*
65 * A state-to-string lookup table, for exposing a human readable state
66 * via sysfs. Always keep in sync with enum rproc_state
67 */
68static const char * const rproc_state_string[] = {
69 [RPROC_OFFLINE] = "offline",
70 [RPROC_SUSPENDED] = "suspended",
71 [RPROC_RUNNING] = "running",
72 [RPROC_CRASHED] = "crashed",
608d7921 73 [RPROC_DELETED] = "deleted",
2aefbef0
MR
74 [RPROC_LAST] = "invalid",
75};
76
77/* Expose the state of the remote processor via sysfs */
78static ssize_t state_show(struct device *dev, struct device_attribute *attr,
79 char *buf)
80{
81 struct rproc *rproc = to_rproc(dev);
82 unsigned int state;
83
84 state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
85 return sprintf(buf, "%s\n", rproc_state_string[state]);
86}
87
88/* Change remote processor state via sysfs */
89static ssize_t state_store(struct device *dev,
90 struct device_attribute *attr,
91 const char *buf, size_t count)
92{
93 struct rproc *rproc = to_rproc(dev);
94 int ret = 0;
95
96 if (sysfs_streq(buf, "start")) {
97 if (rproc->state == RPROC_RUNNING)
98 return -EBUSY;
99
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;
106
107 rproc_shutdown(rproc);
108 } else {
109 dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
110 ret = -EINVAL;
111 }
112 return ret ? ret : count;
113}
114static DEVICE_ATTR_RW(state);
115
116static struct attribute *rproc_attrs[] = {
117 &dev_attr_firmware.attr,
118 &dev_attr_state.attr,
119 NULL
120};
121
122static const struct attribute_group rproc_devgroup = {
123 .attrs = rproc_attrs
124};
125
126static const struct attribute_group *rproc_devgroups[] = {
127 &rproc_devgroup,
128 NULL
129};
130
131struct class rproc_class = {
132 .name = "remoteproc",
133 .dev_groups = rproc_devgroups,
134};
135
136int __init rproc_init_sysfs(void)
137{
138 /* create remoteproc device class for sysfs */
139 int err = class_register(&rproc_class);
140
141 if (err)
142 pr_err("remoteproc: unable to register class\n");
143 return err;
144}
145
146void __exit rproc_exit_sysfs(void)
147{
148 class_unregister(&rproc_class);
149}