2 * cros_ec_sysfs - expose the Chrome OS EC through sysfs
4 * Copyright (C) 2014 Google, Inc.
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.
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.
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/>.
20 #define pr_fmt(fmt) "cros_ec_sysfs: " fmt
22 #include <linux/ctype.h>
23 #include <linux/delay.h>
24 #include <linux/device.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/slab.h>
33 #include <linux/stat.h>
34 #include <linux/types.h>
35 #include <linux/uaccess.h>
37 /* Accessor functions */
39 static ssize_t reboot_show(struct device *dev,
40 struct device_attribute *attr, char *buf)
44 count += scnprintf(buf + count, PAGE_SIZE - count,
45 "ro|rw|cancel|cold|disable-jump|hibernate");
46 count += scnprintf(buf + count, PAGE_SIZE - count,
51 static ssize_t reboot_store(struct device *dev,
52 struct device_attribute *attr,
53 const char *buf, size_t count)
56 const char * const str;
60 {"cancel", EC_REBOOT_CANCEL, 0},
61 {"ro", EC_REBOOT_JUMP_RO, 0},
62 {"rw", EC_REBOOT_JUMP_RW, 0},
63 {"cold", EC_REBOOT_COLD, 0},
64 {"disable-jump", EC_REBOOT_DISABLE_JUMP, 0},
65 {"hibernate", EC_REBOOT_HIBERNATE, 0},
66 {"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
68 struct cros_ec_command *msg;
69 struct ec_params_reboot_ec *param;
70 int got_cmd = 0, offset = 0;
73 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
75 msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
79 param = (struct ec_params_reboot_ec *)msg->data;
83 /* Find word to start scanning */
84 while (buf[offset] && isspace(buf[offset]))
89 for (i = 0; i < ARRAY_SIZE(words); i++) {
90 if (!strncasecmp(words[i].str, buf+offset,
91 strlen(words[i].str))) {
93 param->flags |= words[i].flags;
95 param->cmd = words[i].cmd;
102 /* On to the next word, if any */
103 while (buf[offset] && !isspace(buf[offset]))
113 msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset;
114 msg->outsize = sizeof(*param);
116 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
124 static ssize_t version_show(struct device *dev,
125 struct device_attribute *attr, char *buf)
127 static const char * const image_names[] = {"unknown", "RO", "RW"};
128 struct ec_response_get_version *r_ver;
129 struct ec_response_get_chip_info *r_chip;
130 struct ec_response_board_version *r_board;
131 struct cros_ec_command *msg;
134 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
136 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
140 /* Get versions. RW may change. */
142 msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
143 msg->insize = sizeof(*r_ver);
145 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
150 r_ver = (struct ec_response_get_version *)msg->data;
151 /* Strings should be null-terminated, but let's be sure. */
152 r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
153 r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
154 count += scnprintf(buf + count, PAGE_SIZE - count,
155 "RO version: %s\n", r_ver->version_string_ro);
156 count += scnprintf(buf + count, PAGE_SIZE - count,
157 "RW version: %s\n", r_ver->version_string_rw);
158 count += scnprintf(buf + count, PAGE_SIZE - count,
159 "Firmware copy: %s\n",
160 (r_ver->current_image < ARRAY_SIZE(image_names) ?
161 image_names[r_ver->current_image] : "?"));
163 /* Get build info. */
164 msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset;
165 msg->insize = EC_HOST_PARAM_SIZE;
166 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
168 count += scnprintf(buf + count, PAGE_SIZE - count,
169 "Build info: XFER ERROR %d\n", ret);
170 else if (msg->result != EC_RES_SUCCESS)
171 count += scnprintf(buf + count, PAGE_SIZE - count,
172 "Build info: EC error %d\n", msg->result);
174 msg->data[EC_HOST_PARAM_SIZE - 1] = '\0';
175 count += scnprintf(buf + count, PAGE_SIZE - count,
176 "Build info: %s\n", msg->data);
180 msg->command = EC_CMD_GET_CHIP_INFO + ec->cmd_offset;
181 msg->insize = sizeof(*r_chip);
182 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
184 count += scnprintf(buf + count, PAGE_SIZE - count,
185 "Chip info: XFER ERROR %d\n", ret);
186 else if (msg->result != EC_RES_SUCCESS)
187 count += scnprintf(buf + count, PAGE_SIZE - count,
188 "Chip info: EC error %d\n", msg->result);
190 r_chip = (struct ec_response_get_chip_info *)msg->data;
192 r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
193 r_chip->name[sizeof(r_chip->name) - 1] = '\0';
194 r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
195 count += scnprintf(buf + count, PAGE_SIZE - count,
196 "Chip vendor: %s\n", r_chip->vendor);
197 count += scnprintf(buf + count, PAGE_SIZE - count,
198 "Chip name: %s\n", r_chip->name);
199 count += scnprintf(buf + count, PAGE_SIZE - count,
200 "Chip revision: %s\n", r_chip->revision);
203 /* Get board version */
204 msg->command = EC_CMD_GET_BOARD_VERSION + ec->cmd_offset;
205 msg->insize = sizeof(*r_board);
206 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
208 count += scnprintf(buf + count, PAGE_SIZE - count,
209 "Board version: XFER ERROR %d\n", ret);
210 else if (msg->result != EC_RES_SUCCESS)
211 count += scnprintf(buf + count, PAGE_SIZE - count,
212 "Board version: EC error %d\n", msg->result);
214 r_board = (struct ec_response_board_version *)msg->data;
216 count += scnprintf(buf + count, PAGE_SIZE - count,
217 "Board version: %d\n",
218 r_board->board_version);
226 static ssize_t flashinfo_show(struct device *dev,
227 struct device_attribute *attr, char *buf)
229 struct ec_response_flash_info *resp;
230 struct cros_ec_command *msg;
232 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
234 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
238 /* The flash info shouldn't ever change, but ask each time anyway. */
240 msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset;
241 msg->insize = sizeof(*resp);
243 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
247 resp = (struct ec_response_flash_info *)msg->data;
249 ret = scnprintf(buf, PAGE_SIZE,
250 "FlashSize %d\nWriteSize %d\n"
251 "EraseSize %d\nProtectSize %d\n",
252 resp->flash_size, resp->write_block_size,
253 resp->erase_block_size, resp->protect_block_size);
259 /* Keyboard wake angle control */
260 static ssize_t kb_wake_angle_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
263 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
264 struct ec_response_motion_sense *resp;
265 struct ec_params_motion_sense *param;
266 struct cros_ec_command *msg;
269 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
273 param = (struct ec_params_motion_sense *)msg->data;
274 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
276 param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
277 param->kb_wake_angle.data = EC_MOTION_SENSE_NO_VALUE;
278 msg->outsize = sizeof(*param);
279 msg->insize = sizeof(*resp);
281 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
285 resp = (struct ec_response_motion_sense *)msg->data;
286 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->kb_wake_angle.ret);
292 static ssize_t kb_wake_angle_store(struct device *dev,
293 struct device_attribute *attr,
294 const char *buf, size_t count)
296 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
297 struct ec_params_motion_sense *param;
298 struct cros_ec_command *msg;
302 ret = kstrtou16(buf, 0, &angle);
306 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
310 param = (struct ec_params_motion_sense *)msg->data;
311 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
313 param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
314 param->kb_wake_angle.data = angle;
315 msg->outsize = sizeof(*param);
316 msg->insize = sizeof(struct ec_response_motion_sense);
318 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
325 /* Module initialization */
327 static DEVICE_ATTR_RW(reboot);
328 static DEVICE_ATTR_RO(version);
329 static DEVICE_ATTR_RO(flashinfo);
330 static DEVICE_ATTR_RW(kb_wake_angle);
332 static struct attribute *__ec_attrs[] = {
333 &dev_attr_kb_wake_angle.attr,
334 &dev_attr_reboot.attr,
335 &dev_attr_version.attr,
336 &dev_attr_flashinfo.attr,
340 static umode_t cros_ec_ctrl_visible(struct kobject *kobj,
341 struct attribute *a, int n)
343 struct device *dev = container_of(kobj, struct device, kobj);
344 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
346 if (a == &dev_attr_kb_wake_angle.attr && !ec->has_kb_wake_angle)
352 struct attribute_group cros_ec_attr_group = {
354 .is_visible = cros_ec_ctrl_visible,
356 EXPORT_SYMBOL(cros_ec_attr_group);
358 MODULE_LICENSE("GPL");
359 MODULE_DESCRIPTION("ChromeOS EC control driver");