Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 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> |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 32 | #include <linux/slab.h> |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 33 | #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 | |
| 41 | static 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 | |
| 53 | static 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 Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 70 | struct cros_ec_command *msg; |
| 71 | struct ec_params_reboot_ec *param; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 72 | int got_cmd = 0, offset = 0; |
| 73 | int i; |
| 74 | int ret; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 75 | struct cros_ec_dev *ec = container_of(dev, |
| 76 | struct cros_ec_dev, class_dev); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 77 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 78 | msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL); |
| 79 | if (!msg) |
| 80 | return -ENOMEM; |
| 81 | |
| 82 | param = (struct ec_params_reboot_ec *)msg->data; |
| 83 | |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 84 | param->flags = 0; |
| 85 | while (1) { |
| 86 | /* Find word to start scanning */ |
| 87 | while (buf[offset] && isspace(buf[offset])) |
| 88 | offset++; |
| 89 | if (!buf[offset]) |
| 90 | break; |
| 91 | |
| 92 | for (i = 0; i < ARRAY_SIZE(words); i++) { |
| 93 | if (!strncasecmp(words[i].str, buf+offset, |
| 94 | strlen(words[i].str))) { |
| 95 | if (words[i].flags) { |
| 96 | param->flags |= words[i].flags; |
| 97 | } else { |
| 98 | param->cmd = words[i].cmd; |
| 99 | got_cmd = 1; |
| 100 | } |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* On to the next word, if any */ |
| 106 | while (buf[offset] && !isspace(buf[offset])) |
| 107 | offset++; |
| 108 | } |
| 109 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 110 | if (!got_cmd) { |
| 111 | count = -EINVAL; |
| 112 | goto exit; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 115 | msg->version = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 116 | msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 117 | msg->outsize = sizeof(*param); |
| 118 | msg->insize = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 119 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 120 | if (ret < 0) { |
| 121 | count = ret; |
| 122 | goto exit; |
| 123 | } |
| 124 | if (msg->result != EC_RES_SUCCESS) { |
| 125 | dev_dbg(ec->dev, "EC result %d\n", msg->result); |
| 126 | count = -EINVAL; |
| 127 | } |
| 128 | exit: |
| 129 | kfree(msg); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 130 | return count; |
| 131 | } |
| 132 | |
| 133 | static ssize_t show_ec_version(struct device *dev, |
| 134 | struct device_attribute *attr, char *buf) |
| 135 | { |
| 136 | static const char * const image_names[] = {"unknown", "RO", "RW"}; |
| 137 | struct ec_response_get_version *r_ver; |
| 138 | struct ec_response_get_chip_info *r_chip; |
| 139 | struct ec_response_board_version *r_board; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 140 | struct cros_ec_command *msg; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 141 | int ret; |
| 142 | int count = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 143 | struct cros_ec_dev *ec = container_of(dev, |
| 144 | struct cros_ec_dev, class_dev); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 145 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 146 | msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL); |
| 147 | if (!msg) |
| 148 | return -ENOMEM; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 149 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 150 | /* Get versions. RW may change. */ |
| 151 | msg->version = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 152 | msg->command = EC_CMD_GET_VERSION + ec->cmd_offset; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 153 | msg->insize = sizeof(*r_ver); |
| 154 | msg->outsize = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 155 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 156 | if (ret < 0) { |
| 157 | count = ret; |
| 158 | goto exit; |
| 159 | } |
| 160 | if (msg->result != EC_RES_SUCCESS) { |
| 161 | count = scnprintf(buf, PAGE_SIZE, |
| 162 | "ERROR: EC returned %d\n", msg->result); |
| 163 | goto exit; |
| 164 | } |
| 165 | |
| 166 | r_ver = (struct ec_response_get_version *)msg->data; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 167 | /* Strings should be null-terminated, but let's be sure. */ |
| 168 | r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0'; |
| 169 | r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0'; |
| 170 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 171 | "RO version: %s\n", r_ver->version_string_ro); |
| 172 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 173 | "RW version: %s\n", r_ver->version_string_rw); |
| 174 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 175 | "Firmware copy: %s\n", |
| 176 | (r_ver->current_image < ARRAY_SIZE(image_names) ? |
| 177 | image_names[r_ver->current_image] : "?")); |
| 178 | |
| 179 | /* Get build info. */ |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 180 | msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 181 | msg->insize = EC_HOST_PARAM_SIZE; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 182 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 183 | if (ret < 0) |
| 184 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 185 | "Build info: XFER ERROR %d\n", ret); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 186 | else if (msg->result != EC_RES_SUCCESS) |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 187 | count += scnprintf(buf + count, PAGE_SIZE - count, |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 188 | "Build info: EC error %d\n", msg->result); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 189 | else { |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 190 | msg->data[sizeof(msg->data) - 1] = '\0'; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 191 | count += scnprintf(buf + count, PAGE_SIZE - count, |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 192 | "Build info: %s\n", msg->data); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* Get chip info. */ |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 196 | msg->command = EC_CMD_GET_CHIP_INFO + ec->cmd_offset; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 197 | msg->insize = sizeof(*r_chip); |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 198 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 199 | if (ret < 0) |
| 200 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 201 | "Chip info: XFER ERROR %d\n", ret); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 202 | else if (msg->result != EC_RES_SUCCESS) |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 203 | count += scnprintf(buf + count, PAGE_SIZE - count, |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 204 | "Chip info: EC error %d\n", msg->result); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 205 | else { |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 206 | r_chip = (struct ec_response_get_chip_info *)msg->data; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 207 | |
| 208 | r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0'; |
| 209 | r_chip->name[sizeof(r_chip->name) - 1] = '\0'; |
| 210 | r_chip->revision[sizeof(r_chip->revision) - 1] = '\0'; |
| 211 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 212 | "Chip vendor: %s\n", r_chip->vendor); |
| 213 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 214 | "Chip name: %s\n", r_chip->name); |
| 215 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 216 | "Chip revision: %s\n", r_chip->revision); |
| 217 | } |
| 218 | |
| 219 | /* Get board version */ |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 220 | msg->command = EC_CMD_GET_BOARD_VERSION + ec->cmd_offset; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 221 | msg->insize = sizeof(*r_board); |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 222 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 223 | if (ret < 0) |
| 224 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 225 | "Board version: XFER ERROR %d\n", ret); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 226 | else if (msg->result != EC_RES_SUCCESS) |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 227 | count += scnprintf(buf + count, PAGE_SIZE - count, |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 228 | "Board version: EC error %d\n", msg->result); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 229 | else { |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 230 | r_board = (struct ec_response_board_version *)msg->data; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 231 | |
| 232 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 233 | "Board version: %d\n", |
| 234 | r_board->board_version); |
| 235 | } |
| 236 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 237 | exit: |
| 238 | kfree(msg); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 239 | return count; |
| 240 | } |
| 241 | |
| 242 | static ssize_t show_ec_flashinfo(struct device *dev, |
| 243 | struct device_attribute *attr, char *buf) |
| 244 | { |
| 245 | struct ec_response_flash_info *resp; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 246 | struct cros_ec_command *msg; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 247 | int ret; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 248 | struct cros_ec_dev *ec = container_of(dev, |
| 249 | struct cros_ec_dev, class_dev); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 250 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 251 | msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL); |
| 252 | if (!msg) |
| 253 | return -ENOMEM; |
| 254 | |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 255 | /* The flash info shouldn't ever change, but ask each time anyway. */ |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 256 | msg->version = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 257 | msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 258 | msg->insize = sizeof(*resp); |
| 259 | msg->outsize = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 260 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 261 | if (ret < 0) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 262 | goto exit; |
| 263 | if (msg->result != EC_RES_SUCCESS) { |
| 264 | ret = scnprintf(buf, PAGE_SIZE, |
| 265 | "ERROR: EC returned %d\n", msg->result); |
| 266 | goto exit; |
| 267 | } |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 268 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 269 | resp = (struct ec_response_flash_info *)msg->data; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 270 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 271 | ret = scnprintf(buf, PAGE_SIZE, |
| 272 | "FlashSize %d\nWriteSize %d\n" |
| 273 | "EraseSize %d\nProtectSize %d\n", |
| 274 | resp->flash_size, resp->write_block_size, |
| 275 | resp->erase_block_size, resp->protect_block_size); |
| 276 | exit: |
| 277 | kfree(msg); |
| 278 | return ret; |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* Module initialization */ |
| 282 | |
| 283 | static DEVICE_ATTR(reboot, S_IWUSR | S_IRUGO, show_ec_reboot, store_ec_reboot); |
| 284 | static DEVICE_ATTR(version, S_IRUGO, show_ec_version, NULL); |
| 285 | static DEVICE_ATTR(flashinfo, S_IRUGO, show_ec_flashinfo, NULL); |
| 286 | |
| 287 | static struct attribute *__ec_attrs[] = { |
| 288 | &dev_attr_reboot.attr, |
| 289 | &dev_attr_version.attr, |
| 290 | &dev_attr_flashinfo.attr, |
| 291 | NULL, |
| 292 | }; |
| 293 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame^] | 294 | struct attribute_group cros_ec_attr_group = { |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 295 | .attrs = __ec_attrs, |
| 296 | }; |
| 297 | |