blob: 78ba82d670cbd9e10c8dc6d1f5d1d558bb6ccb47 [file] [log] [blame]
Bill Richardson71af4b52015-02-02 12:26:27 +01001/*
2 * cros_ec_sysfs - expose the Chrome OS EC through sysfs
3 *
4 * Copyright (C) 2014 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#define pr_fmt(fmt) "cros_ec_sysfs: " fmt
21
22#include <linux/ctype.h>
23#include <linux/delay.h>
24#include <linux/device.h>
25#include <linux/fs.h>
26#include <linux/kobject.h>
27#include <linux/mfd/cros_ec.h>
28#include <linux/mfd/cros_ec_commands.h>
29#include <linux/module.h>
30#include <linux/platform_device.h>
31#include <linux/printk.h>
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020032#include <linux/slab.h>
Bill Richardson71af4b52015-02-02 12:26:27 +010033#include <linux/stat.h>
34#include <linux/types.h>
35#include <linux/uaccess.h>
36
37#include "cros_ec_dev.h"
38
39/* Accessor functions */
40
41static ssize_t show_ec_reboot(struct device *dev,
42 struct device_attribute *attr, char *buf)
43{
44 int count = 0;
45
46 count += scnprintf(buf + count, PAGE_SIZE - count,
47 "ro|rw|cancel|cold|disable-jump|hibernate");
48 count += scnprintf(buf + count, PAGE_SIZE - count,
49 " [at-shutdown]\n");
50 return count;
51}
52
53static ssize_t store_ec_reboot(struct device *dev,
54 struct device_attribute *attr,
55 const char *buf, size_t count)
56{
57 static const struct {
58 const char * const str;
59 uint8_t cmd;
60 uint8_t flags;
61 } words[] = {
62 {"cancel", EC_REBOOT_CANCEL, 0},
63 {"ro", EC_REBOOT_JUMP_RO, 0},
64 {"rw", EC_REBOOT_JUMP_RW, 0},
65 {"cold", EC_REBOOT_COLD, 0},
66 {"disable-jump", EC_REBOOT_DISABLE_JUMP, 0},
67 {"hibernate", EC_REBOOT_HIBERNATE, 0},
68 {"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
69 };
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020070 struct cros_ec_command *msg;
71 struct ec_params_reboot_ec *param;
Bill Richardson71af4b52015-02-02 12:26:27 +010072 int got_cmd = 0, offset = 0;
73 int i;
74 int ret;
75 struct cros_ec_device *ec = dev_get_drvdata(dev);
76
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020077 msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
78 if (!msg)
79 return -ENOMEM;
80
81 param = (struct ec_params_reboot_ec *)msg->data;
82
Bill Richardson71af4b52015-02-02 12:26:27 +010083 param->flags = 0;
84 while (1) {
85 /* Find word to start scanning */
86 while (buf[offset] && isspace(buf[offset]))
87 offset++;
88 if (!buf[offset])
89 break;
90
91 for (i = 0; i < ARRAY_SIZE(words); i++) {
92 if (!strncasecmp(words[i].str, buf+offset,
93 strlen(words[i].str))) {
94 if (words[i].flags) {
95 param->flags |= words[i].flags;
96 } else {
97 param->cmd = words[i].cmd;
98 got_cmd = 1;
99 }
100 break;
101 }
102 }
103
104 /* On to the next word, if any */
105 while (buf[offset] && !isspace(buf[offset]))
106 offset++;
107 }
108
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200109 if (!got_cmd) {
110 count = -EINVAL;
111 goto exit;
Bill Richardson71af4b52015-02-02 12:26:27 +0100112 }
113
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200114 msg->version = 0;
115 msg->command = EC_CMD_REBOOT_EC;
116 msg->outsize = sizeof(*param);
117 msg->insize = 0;
118 ret = cros_ec_cmd_xfer(ec, msg);
119 if (ret < 0) {
120 count = ret;
121 goto exit;
122 }
123 if (msg->result != EC_RES_SUCCESS) {
124 dev_dbg(ec->dev, "EC result %d\n", msg->result);
125 count = -EINVAL;
126 }
127exit:
128 kfree(msg);
Bill Richardson71af4b52015-02-02 12:26:27 +0100129 return count;
130}
131
132static ssize_t show_ec_version(struct device *dev,
133 struct device_attribute *attr, char *buf)
134{
135 static const char * const image_names[] = {"unknown", "RO", "RW"};
136 struct ec_response_get_version *r_ver;
137 struct ec_response_get_chip_info *r_chip;
138 struct ec_response_board_version *r_board;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200139 struct cros_ec_command *msg;
Bill Richardson71af4b52015-02-02 12:26:27 +0100140 int ret;
141 int count = 0;
142 struct cros_ec_device *ec = dev_get_drvdata(dev);
143
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200144 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
145 if (!msg)
146 return -ENOMEM;
Bill Richardson71af4b52015-02-02 12:26:27 +0100147
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200148 /* Get versions. RW may change. */
149 msg->version = 0;
150 msg->command = EC_CMD_GET_VERSION;
151 msg->insize = sizeof(*r_ver);
152 msg->outsize = 0;
153 ret = cros_ec_cmd_xfer(ec, msg);
154 if (ret < 0) {
155 count = ret;
156 goto exit;
157 }
158 if (msg->result != EC_RES_SUCCESS) {
159 count = scnprintf(buf, PAGE_SIZE,
160 "ERROR: EC returned %d\n", msg->result);
161 goto exit;
162 }
163
164 r_ver = (struct ec_response_get_version *)msg->data;
Bill Richardson71af4b52015-02-02 12:26:27 +0100165 /* Strings should be null-terminated, but let's be sure. */
166 r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
167 r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
168 count += scnprintf(buf + count, PAGE_SIZE - count,
169 "RO version: %s\n", r_ver->version_string_ro);
170 count += scnprintf(buf + count, PAGE_SIZE - count,
171 "RW version: %s\n", r_ver->version_string_rw);
172 count += scnprintf(buf + count, PAGE_SIZE - count,
173 "Firmware copy: %s\n",
174 (r_ver->current_image < ARRAY_SIZE(image_names) ?
175 image_names[r_ver->current_image] : "?"));
176
177 /* Get build info. */
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200178 msg->command = EC_CMD_GET_BUILD_INFO;
179 msg->insize = EC_HOST_PARAM_SIZE;
180 ret = cros_ec_cmd_xfer(ec, msg);
Bill Richardson71af4b52015-02-02 12:26:27 +0100181 if (ret < 0)
182 count += scnprintf(buf + count, PAGE_SIZE - count,
183 "Build info: XFER ERROR %d\n", ret);
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200184 else if (msg->result != EC_RES_SUCCESS)
Bill Richardson71af4b52015-02-02 12:26:27 +0100185 count += scnprintf(buf + count, PAGE_SIZE - count,
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200186 "Build info: EC error %d\n", msg->result);
Bill Richardson71af4b52015-02-02 12:26:27 +0100187 else {
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200188 msg->data[sizeof(msg->data) - 1] = '\0';
Bill Richardson71af4b52015-02-02 12:26:27 +0100189 count += scnprintf(buf + count, PAGE_SIZE - count,
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200190 "Build info: %s\n", msg->data);
Bill Richardson71af4b52015-02-02 12:26:27 +0100191 }
192
193 /* Get chip info. */
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200194 msg->command = EC_CMD_GET_CHIP_INFO;
195 msg->insize = sizeof(*r_chip);
196 ret = cros_ec_cmd_xfer(ec, msg);
Bill Richardson71af4b52015-02-02 12:26:27 +0100197 if (ret < 0)
198 count += scnprintf(buf + count, PAGE_SIZE - count,
199 "Chip info: XFER ERROR %d\n", ret);
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200200 else if (msg->result != EC_RES_SUCCESS)
Bill Richardson71af4b52015-02-02 12:26:27 +0100201 count += scnprintf(buf + count, PAGE_SIZE - count,
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200202 "Chip info: EC error %d\n", msg->result);
Bill Richardson71af4b52015-02-02 12:26:27 +0100203 else {
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200204 r_chip = (struct ec_response_get_chip_info *)msg->data;
Bill Richardson71af4b52015-02-02 12:26:27 +0100205
206 r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
207 r_chip->name[sizeof(r_chip->name) - 1] = '\0';
208 r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
209 count += scnprintf(buf + count, PAGE_SIZE - count,
210 "Chip vendor: %s\n", r_chip->vendor);
211 count += scnprintf(buf + count, PAGE_SIZE - count,
212 "Chip name: %s\n", r_chip->name);
213 count += scnprintf(buf + count, PAGE_SIZE - count,
214 "Chip revision: %s\n", r_chip->revision);
215 }
216
217 /* Get board version */
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200218 msg->command = EC_CMD_GET_BOARD_VERSION;
219 msg->insize = sizeof(*r_board);
220 ret = cros_ec_cmd_xfer(ec, msg);
Bill Richardson71af4b52015-02-02 12:26:27 +0100221 if (ret < 0)
222 count += scnprintf(buf + count, PAGE_SIZE - count,
223 "Board version: XFER ERROR %d\n", ret);
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200224 else if (msg->result != EC_RES_SUCCESS)
Bill Richardson71af4b52015-02-02 12:26:27 +0100225 count += scnprintf(buf + count, PAGE_SIZE - count,
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200226 "Board version: EC error %d\n", msg->result);
Bill Richardson71af4b52015-02-02 12:26:27 +0100227 else {
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200228 r_board = (struct ec_response_board_version *)msg->data;
Bill Richardson71af4b52015-02-02 12:26:27 +0100229
230 count += scnprintf(buf + count, PAGE_SIZE - count,
231 "Board version: %d\n",
232 r_board->board_version);
233 }
234
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200235exit:
236 kfree(msg);
Bill Richardson71af4b52015-02-02 12:26:27 +0100237 return count;
238}
239
240static ssize_t show_ec_flashinfo(struct device *dev,
241 struct device_attribute *attr, char *buf)
242{
243 struct ec_response_flash_info *resp;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200244 struct cros_ec_command *msg;
Bill Richardson71af4b52015-02-02 12:26:27 +0100245 int ret;
246 struct cros_ec_device *ec = dev_get_drvdata(dev);
247
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200248 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
249 if (!msg)
250 return -ENOMEM;
251
Bill Richardson71af4b52015-02-02 12:26:27 +0100252 /* The flash info shouldn't ever change, but ask each time anyway. */
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200253 msg->version = 0;
254 msg->command = EC_CMD_FLASH_INFO;
255 msg->insize = sizeof(*resp);
256 msg->outsize = 0;
257 ret = cros_ec_cmd_xfer(ec, msg);
Bill Richardson71af4b52015-02-02 12:26:27 +0100258 if (ret < 0)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200259 goto exit;
260 if (msg->result != EC_RES_SUCCESS) {
261 ret = scnprintf(buf, PAGE_SIZE,
262 "ERROR: EC returned %d\n", msg->result);
263 goto exit;
264 }
Bill Richardson71af4b52015-02-02 12:26:27 +0100265
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200266 resp = (struct ec_response_flash_info *)msg->data;
Bill Richardson71af4b52015-02-02 12:26:27 +0100267
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200268 ret = scnprintf(buf, PAGE_SIZE,
269 "FlashSize %d\nWriteSize %d\n"
270 "EraseSize %d\nProtectSize %d\n",
271 resp->flash_size, resp->write_block_size,
272 resp->erase_block_size, resp->protect_block_size);
273exit:
274 kfree(msg);
275 return ret;
Bill Richardson71af4b52015-02-02 12:26:27 +0100276}
277
278/* Module initialization */
279
280static DEVICE_ATTR(reboot, S_IWUSR | S_IRUGO, show_ec_reboot, store_ec_reboot);
281static DEVICE_ATTR(version, S_IRUGO, show_ec_version, NULL);
282static DEVICE_ATTR(flashinfo, S_IRUGO, show_ec_flashinfo, NULL);
283
284static struct attribute *__ec_attrs[] = {
285 &dev_attr_reboot.attr,
286 &dev_attr_version.attr,
287 &dev_attr_flashinfo.attr,
288 NULL,
289};
290
291static struct attribute_group ec_attr_group = {
292 .attrs = __ec_attrs,
293};
294
295void ec_dev_sysfs_init(struct cros_ec_device *ec)
296{
297 int error;
298
299 error = sysfs_create_group(&ec->vdev->kobj, &ec_attr_group);
300 if (error)
301 pr_warn("failed to create group: %d\n", error);
302}
303
304void ec_dev_sysfs_remove(struct cros_ec_device *ec)
305{
306 sysfs_remove_group(&ec->vdev->kobj, &ec_attr_group);
307}