diff options
author | Bill Richardson <wfrichar@chromium.org> | 2015-02-02 06:26:27 -0500 |
---|---|---|
committer | Olof Johansson <olof@lixom.net> | 2015-02-26 18:45:12 -0500 |
commit | 71af4b52cc22a8d0f7b66a51427a804741a045b6 (patch) | |
tree | ff3821ad460186ba3fb2e1904d7181fa21add2c5 | |
parent | 8a4be850b6a1eb8c7b910d98d3683a803ff5ddb4 (diff) |
platform/chrome: Create sysfs attributes for the ChromeOS EC
This adds the first few sysfs attributes for the Chrome OS EC. These
controls are made available under /sys/devices/virtual/chromeos/cros_ec
flashinfo - display current flash info
reboot - tell the EC to reboot in various ways
version - information about the EC software and hardware
Future changes will build on this to add additional controls.
From a root shell, you should be able to do things like this:
cd /sys/devices/virtual/chromeos/cros_ec
cat flashinfo
cat version
echo rw > reboot
cat version
echo ro > reboot
cat version
echo rw > reboot
cat version
echo cold > reboot
That last command will reboot the AP too.
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-by: Olof Johansson <olofj@chromium.org>
Signed-off-by: Javier Martinez Canillas <javier.martinez@collabora.co.uk>
Tested-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
Signed-off-by: Olof Johansson <olof@lixom.net>
-rw-r--r-- | drivers/platform/chrome/Makefile | 3 | ||||
-rw-r--r-- | drivers/platform/chrome/cros_ec_dev.c | 4 | ||||
-rw-r--r-- | drivers/platform/chrome/cros_ec_dev.h | 3 | ||||
-rw-r--r-- | drivers/platform/chrome/cros_ec_sysfs.c | 271 |
4 files changed, 280 insertions, 1 deletions
diff --git a/drivers/platform/chrome/Makefile b/drivers/platform/chrome/Makefile index ec682900f889..34b631ceff67 100644 --- a/drivers/platform/chrome/Makefile +++ b/drivers/platform/chrome/Makefile | |||
@@ -1,5 +1,6 @@ | |||
1 | 1 | ||
2 | obj-$(CONFIG_CHROMEOS_LAPTOP) += chromeos_laptop.o | 2 | obj-$(CONFIG_CHROMEOS_LAPTOP) += chromeos_laptop.o |
3 | obj-$(CONFIG_CHROMEOS_PSTORE) += chromeos_pstore.o | 3 | obj-$(CONFIG_CHROMEOS_PSTORE) += chromeos_pstore.o |
4 | obj-$(CONFIG_CROS_EC_CHARDEV) += cros_ec_dev.o | 4 | cros_ec_devs-objs := cros_ec_dev.o cros_ec_sysfs.o |
5 | obj-$(CONFIG_CROS_EC_CHARDEV) += cros_ec_devs.o | ||
5 | obj-$(CONFIG_CROS_EC_LPC) += cros_ec_lpc.o | 6 | obj-$(CONFIG_CROS_EC_LPC) += cros_ec_lpc.o |
diff --git a/drivers/platform/chrome/cros_ec_dev.c b/drivers/platform/chrome/cros_ec_dev.c index 94c1442d5104..33f37ad36892 100644 --- a/drivers/platform/chrome/cros_ec_dev.c +++ b/drivers/platform/chrome/cros_ec_dev.c | |||
@@ -198,6 +198,9 @@ static int ec_device_probe(struct platform_device *pdev) | |||
198 | return retval; | 198 | return retval; |
199 | } | 199 | } |
200 | 200 | ||
201 | /* Initialize extra interfaces */ | ||
202 | ec_dev_sysfs_init(ec); | ||
203 | |||
201 | return 0; | 204 | return 0; |
202 | } | 205 | } |
203 | 206 | ||
@@ -205,6 +208,7 @@ static int ec_device_remove(struct platform_device *pdev) | |||
205 | { | 208 | { |
206 | struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent); | 209 | struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent); |
207 | 210 | ||
211 | ec_dev_sysfs_remove(ec); | ||
208 | device_destroy(cros_class, MKDEV(ec_major, 0)); | 212 | device_destroy(cros_class, MKDEV(ec_major, 0)); |
209 | cdev_del(&ec->cdev); | 213 | cdev_del(&ec->cdev); |
210 | return 0; | 214 | return 0; |
diff --git a/drivers/platform/chrome/cros_ec_dev.h b/drivers/platform/chrome/cros_ec_dev.h index 15c54c4c5531..f03613290ecf 100644 --- a/drivers/platform/chrome/cros_ec_dev.h +++ b/drivers/platform/chrome/cros_ec_dev.h | |||
@@ -44,4 +44,7 @@ struct cros_ec_readmem { | |||
44 | #define CROS_EC_DEV_IOCXCMD _IOWR(CROS_EC_DEV_IOC, 0, struct cros_ec_command) | 44 | #define CROS_EC_DEV_IOCXCMD _IOWR(CROS_EC_DEV_IOC, 0, struct cros_ec_command) |
45 | #define CROS_EC_DEV_IOCRDMEM _IOWR(CROS_EC_DEV_IOC, 1, struct cros_ec_readmem) | 45 | #define CROS_EC_DEV_IOCRDMEM _IOWR(CROS_EC_DEV_IOC, 1, struct cros_ec_readmem) |
46 | 46 | ||
47 | void ec_dev_sysfs_init(struct cros_ec_device *); | ||
48 | void ec_dev_sysfs_remove(struct cros_ec_device *); | ||
49 | |||
47 | #endif /* _CROS_EC_DEV_H_ */ | 50 | #endif /* _CROS_EC_DEV_H_ */ |
diff --git a/drivers/platform/chrome/cros_ec_sysfs.c b/drivers/platform/chrome/cros_ec_sysfs.c new file mode 100644 index 000000000000..fb62ab6cc659 --- /dev/null +++ b/drivers/platform/chrome/cros_ec_sysfs.c | |||
@@ -0,0 +1,271 @@ | |||
1 | /* | ||
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> | ||
32 | #include <linux/stat.h> | ||
33 | #include <linux/types.h> | ||
34 | #include <linux/uaccess.h> | ||
35 | |||
36 | #include "cros_ec_dev.h" | ||
37 | |||
38 | /* Accessor functions */ | ||
39 | |||
40 | static ssize_t show_ec_reboot(struct device *dev, | ||
41 | struct device_attribute *attr, char *buf) | ||
42 | { | ||
43 | int count = 0; | ||
44 | |||
45 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
46 | "ro|rw|cancel|cold|disable-jump|hibernate"); | ||
47 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
48 | " [at-shutdown]\n"); | ||
49 | return count; | ||
50 | } | ||
51 | |||
52 | static ssize_t store_ec_reboot(struct device *dev, | ||
53 | struct device_attribute *attr, | ||
54 | const char *buf, size_t count) | ||
55 | { | ||
56 | static const struct { | ||
57 | const char * const str; | ||
58 | uint8_t cmd; | ||
59 | uint8_t flags; | ||
60 | } words[] = { | ||
61 | {"cancel", EC_REBOOT_CANCEL, 0}, | ||
62 | {"ro", EC_REBOOT_JUMP_RO, 0}, | ||
63 | {"rw", EC_REBOOT_JUMP_RW, 0}, | ||
64 | {"cold", EC_REBOOT_COLD, 0}, | ||
65 | {"disable-jump", EC_REBOOT_DISABLE_JUMP, 0}, | ||
66 | {"hibernate", EC_REBOOT_HIBERNATE, 0}, | ||
67 | {"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN}, | ||
68 | }; | ||
69 | struct cros_ec_command msg = { 0 }; | ||
70 | struct ec_params_reboot_ec *param = | ||
71 | (struct ec_params_reboot_ec *)msg.outdata; | ||
72 | int got_cmd = 0, offset = 0; | ||
73 | int i; | ||
74 | int ret; | ||
75 | struct cros_ec_device *ec = dev_get_drvdata(dev); | ||
76 | |||
77 | param->flags = 0; | ||
78 | while (1) { | ||
79 | /* Find word to start scanning */ | ||
80 | while (buf[offset] && isspace(buf[offset])) | ||
81 | offset++; | ||
82 | if (!buf[offset]) | ||
83 | break; | ||
84 | |||
85 | for (i = 0; i < ARRAY_SIZE(words); i++) { | ||
86 | if (!strncasecmp(words[i].str, buf+offset, | ||
87 | strlen(words[i].str))) { | ||
88 | if (words[i].flags) { | ||
89 | param->flags |= words[i].flags; | ||
90 | } else { | ||
91 | param->cmd = words[i].cmd; | ||
92 | got_cmd = 1; | ||
93 | } | ||
94 | break; | ||
95 | } | ||
96 | } | ||
97 | |||
98 | /* On to the next word, if any */ | ||
99 | while (buf[offset] && !isspace(buf[offset])) | ||
100 | offset++; | ||
101 | } | ||
102 | |||
103 | if (!got_cmd) | ||
104 | return -EINVAL; | ||
105 | |||
106 | msg.command = EC_CMD_REBOOT_EC; | ||
107 | msg.outsize = sizeof(param); | ||
108 | ret = cros_ec_cmd_xfer(ec, &msg); | ||
109 | if (ret < 0) | ||
110 | return ret; | ||
111 | if (msg.result != EC_RES_SUCCESS) { | ||
112 | dev_dbg(ec->dev, "EC result %d\n", msg.result); | ||
113 | return -EINVAL; | ||
114 | } | ||
115 | |||
116 | return count; | ||
117 | } | ||
118 | |||
119 | static ssize_t show_ec_version(struct device *dev, | ||
120 | struct device_attribute *attr, char *buf) | ||
121 | { | ||
122 | static const char * const image_names[] = {"unknown", "RO", "RW"}; | ||
123 | struct ec_response_get_version *r_ver; | ||
124 | struct ec_response_get_chip_info *r_chip; | ||
125 | struct ec_response_board_version *r_board; | ||
126 | struct cros_ec_command msg = { 0 }; | ||
127 | int ret; | ||
128 | int count = 0; | ||
129 | struct cros_ec_device *ec = dev_get_drvdata(dev); | ||
130 | |||
131 | /* Get versions. RW may change. */ | ||
132 | msg.command = EC_CMD_GET_VERSION; | ||
133 | msg.insize = sizeof(*r_ver); | ||
134 | ret = cros_ec_cmd_xfer(ec, &msg); | ||
135 | if (ret < 0) | ||
136 | return ret; | ||
137 | if (msg.result != EC_RES_SUCCESS) | ||
138 | return scnprintf(buf, PAGE_SIZE, | ||
139 | "ERROR: EC returned %d\n", msg.result); | ||
140 | |||
141 | r_ver = (struct ec_response_get_version *)msg.indata; | ||
142 | /* Strings should be null-terminated, but let's be sure. */ | ||
143 | r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0'; | ||
144 | r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0'; | ||
145 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
146 | "RO version: %s\n", r_ver->version_string_ro); | ||
147 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
148 | "RW version: %s\n", r_ver->version_string_rw); | ||
149 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
150 | "Firmware copy: %s\n", | ||
151 | (r_ver->current_image < ARRAY_SIZE(image_names) ? | ||
152 | image_names[r_ver->current_image] : "?")); | ||
153 | |||
154 | /* Get build info. */ | ||
155 | msg.command = EC_CMD_GET_BUILD_INFO; | ||
156 | msg.insize = sizeof(msg.indata); | ||
157 | ret = cros_ec_cmd_xfer(ec, &msg); | ||
158 | if (ret < 0) | ||
159 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
160 | "Build info: XFER ERROR %d\n", ret); | ||
161 | else if (msg.result != EC_RES_SUCCESS) | ||
162 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
163 | "Build info: EC error %d\n", msg.result); | ||
164 | else { | ||
165 | msg.indata[sizeof(msg.indata) - 1] = '\0'; | ||
166 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
167 | "Build info: %s\n", msg.indata); | ||
168 | } | ||
169 | |||
170 | /* Get chip info. */ | ||
171 | msg.command = EC_CMD_GET_CHIP_INFO; | ||
172 | msg.insize = sizeof(*r_chip); | ||
173 | ret = cros_ec_cmd_xfer(ec, &msg); | ||
174 | if (ret < 0) | ||
175 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
176 | "Chip info: XFER ERROR %d\n", ret); | ||
177 | else if (msg.result != EC_RES_SUCCESS) | ||
178 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
179 | "Chip info: EC error %d\n", msg.result); | ||
180 | else { | ||
181 | r_chip = (struct ec_response_get_chip_info *)msg.indata; | ||
182 | |||
183 | r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0'; | ||
184 | r_chip->name[sizeof(r_chip->name) - 1] = '\0'; | ||
185 | r_chip->revision[sizeof(r_chip->revision) - 1] = '\0'; | ||
186 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
187 | "Chip vendor: %s\n", r_chip->vendor); | ||
188 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
189 | "Chip name: %s\n", r_chip->name); | ||
190 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
191 | "Chip revision: %s\n", r_chip->revision); | ||
192 | } | ||
193 | |||
194 | /* Get board version */ | ||
195 | msg.command = EC_CMD_GET_BOARD_VERSION; | ||
196 | msg.insize = sizeof(*r_board); | ||
197 | ret = cros_ec_cmd_xfer(ec, &msg); | ||
198 | if (ret < 0) | ||
199 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
200 | "Board version: XFER ERROR %d\n", ret); | ||
201 | else if (msg.result != EC_RES_SUCCESS) | ||
202 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
203 | "Board version: EC error %d\n", msg.result); | ||
204 | else { | ||
205 | r_board = (struct ec_response_board_version *)msg.indata; | ||
206 | |||
207 | count += scnprintf(buf + count, PAGE_SIZE - count, | ||
208 | "Board version: %d\n", | ||
209 | r_board->board_version); | ||
210 | } | ||
211 | |||
212 | return count; | ||
213 | } | ||
214 | |||
215 | static ssize_t show_ec_flashinfo(struct device *dev, | ||
216 | struct device_attribute *attr, char *buf) | ||
217 | { | ||
218 | struct ec_response_flash_info *resp; | ||
219 | struct cros_ec_command msg = { 0 }; | ||
220 | int ret; | ||
221 | struct cros_ec_device *ec = dev_get_drvdata(dev); | ||
222 | |||
223 | /* The flash info shouldn't ever change, but ask each time anyway. */ | ||
224 | msg.command = EC_CMD_FLASH_INFO; | ||
225 | msg.insize = sizeof(*resp); | ||
226 | ret = cros_ec_cmd_xfer(ec, &msg); | ||
227 | if (ret < 0) | ||
228 | return ret; | ||
229 | if (msg.result != EC_RES_SUCCESS) | ||
230 | return scnprintf(buf, PAGE_SIZE, | ||
231 | "ERROR: EC returned %d\n", msg.result); | ||
232 | |||
233 | resp = (struct ec_response_flash_info *)msg.indata; | ||
234 | |||
235 | return scnprintf(buf, PAGE_SIZE, | ||
236 | "FlashSize %d\nWriteSize %d\n" | ||
237 | "EraseSize %d\nProtectSize %d\n", | ||
238 | resp->flash_size, resp->write_block_size, | ||
239 | resp->erase_block_size, resp->protect_block_size); | ||
240 | } | ||
241 | |||
242 | /* Module initialization */ | ||
243 | |||
244 | static DEVICE_ATTR(reboot, S_IWUSR | S_IRUGO, show_ec_reboot, store_ec_reboot); | ||
245 | static DEVICE_ATTR(version, S_IRUGO, show_ec_version, NULL); | ||
246 | static DEVICE_ATTR(flashinfo, S_IRUGO, show_ec_flashinfo, NULL); | ||
247 | |||
248 | static struct attribute *__ec_attrs[] = { | ||
249 | &dev_attr_reboot.attr, | ||
250 | &dev_attr_version.attr, | ||
251 | &dev_attr_flashinfo.attr, | ||
252 | NULL, | ||
253 | }; | ||
254 | |||
255 | static struct attribute_group ec_attr_group = { | ||
256 | .attrs = __ec_attrs, | ||
257 | }; | ||
258 | |||
259 | void ec_dev_sysfs_init(struct cros_ec_device *ec) | ||
260 | { | ||
261 | int error; | ||
262 | |||
263 | error = sysfs_create_group(&ec->vdev->kobj, &ec_attr_group); | ||
264 | if (error) | ||
265 | pr_warn("failed to create group: %d\n", error); | ||
266 | } | ||
267 | |||
268 | void ec_dev_sysfs_remove(struct cros_ec_device *ec) | ||
269 | { | ||
270 | sysfs_remove_group(&ec->vdev->kobj, &ec_attr_group); | ||
271 | } | ||