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