1 // SPDX-License-Identifier: GPL-2.0+
2 // Expose the ChromeOS EC through sysfs
4 // Copyright (C) 2014 Google, Inc.
6 #include <linux/ctype.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
10 #include <linux/kobject.h>
11 #include <linux/module.h>
12 #include <linux/platform_data/cros_ec_commands.h>
13 #include <linux/platform_data/cros_ec_proto.h>
14 #include <linux/platform_device.h>
15 #include <linux/printk.h>
16 #include <linux/slab.h>
17 #include <linux/stat.h>
18 #include <linux/types.h>
19 #include <linux/uaccess.h>
21 #define DRV_NAME "cros-ec-sysfs"
23 /* Accessor functions */
25 static ssize_t reboot_show(struct device *dev,
26 struct device_attribute *attr, char *buf)
30 count += scnprintf(buf + count, PAGE_SIZE - count,
31 "ro|rw|cancel|cold|disable-jump|hibernate|cold-ap-off");
32 count += scnprintf(buf + count, PAGE_SIZE - count,
37 static ssize_t reboot_store(struct device *dev,
38 struct device_attribute *attr,
39 const char *buf, size_t count)
42 const char * const str;
46 {"cancel", EC_REBOOT_CANCEL, 0},
47 {"ro", EC_REBOOT_JUMP_RO, 0},
48 {"rw", EC_REBOOT_JUMP_RW, 0},
49 {"cold-ap-off", EC_REBOOT_COLD_AP_OFF, 0},
50 {"cold", EC_REBOOT_COLD, 0},
51 {"disable-jump", EC_REBOOT_DISABLE_JUMP, 0},
52 {"hibernate", EC_REBOOT_HIBERNATE, 0},
53 {"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
55 struct cros_ec_command *msg;
56 struct ec_params_reboot_ec *param;
57 int got_cmd = 0, offset = 0;
60 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
62 msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
66 param = (struct ec_params_reboot_ec *)msg->data;
70 /* Find word to start scanning */
71 while (buf[offset] && isspace(buf[offset]))
76 for (i = 0; i < ARRAY_SIZE(words); i++) {
77 if (!strncasecmp(words[i].str, buf+offset,
78 strlen(words[i].str))) {
80 param->flags |= words[i].flags;
82 param->cmd = words[i].cmd;
89 /* On to the next word, if any */
90 while (buf[offset] && !isspace(buf[offset]))
100 msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset;
101 msg->outsize = sizeof(*param);
103 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
111 static ssize_t version_show(struct device *dev,
112 struct device_attribute *attr, char *buf)
114 static const char * const image_names[] = {"unknown", "RO", "RW"};
115 struct ec_response_get_version *r_ver;
116 struct ec_response_get_chip_info *r_chip;
117 struct ec_response_board_version *r_board;
118 struct cros_ec_command *msg;
121 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
123 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
127 /* Get versions. RW may change. */
129 msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
130 msg->insize = sizeof(*r_ver);
132 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
137 r_ver = (struct ec_response_get_version *)msg->data;
138 /* Strings should be null-terminated, but let's be sure. */
139 r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
140 r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
141 count += scnprintf(buf + count, PAGE_SIZE - count,
142 "RO version: %s\n", r_ver->version_string_ro);
143 count += scnprintf(buf + count, PAGE_SIZE - count,
144 "RW version: %s\n", r_ver->version_string_rw);
145 count += scnprintf(buf + count, PAGE_SIZE - count,
146 "Firmware copy: %s\n",
147 (r_ver->current_image < ARRAY_SIZE(image_names) ?
148 image_names[r_ver->current_image] : "?"));
150 /* Get build info. */
151 msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset;
152 msg->insize = EC_HOST_PARAM_SIZE;
153 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
155 count += scnprintf(buf + count, PAGE_SIZE - count,
156 "Build info: XFER / EC ERROR %d / %d\n",
159 msg->data[EC_HOST_PARAM_SIZE - 1] = '\0';
160 count += scnprintf(buf + count, PAGE_SIZE - count,
161 "Build info: %s\n", msg->data);
165 msg->command = EC_CMD_GET_CHIP_INFO + ec->cmd_offset;
166 msg->insize = sizeof(*r_chip);
167 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
169 count += scnprintf(buf + count, PAGE_SIZE - count,
170 "Chip info: XFER / EC ERROR %d / %d\n",
173 r_chip = (struct ec_response_get_chip_info *)msg->data;
175 r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
176 r_chip->name[sizeof(r_chip->name) - 1] = '\0';
177 r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
178 count += scnprintf(buf + count, PAGE_SIZE - count,
179 "Chip vendor: %s\n", r_chip->vendor);
180 count += scnprintf(buf + count, PAGE_SIZE - count,
181 "Chip name: %s\n", r_chip->name);
182 count += scnprintf(buf + count, PAGE_SIZE - count,
183 "Chip revision: %s\n", r_chip->revision);
186 /* Get board version */
187 msg->command = EC_CMD_GET_BOARD_VERSION + ec->cmd_offset;
188 msg->insize = sizeof(*r_board);
189 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
191 count += scnprintf(buf + count, PAGE_SIZE - count,
192 "Board version: XFER / EC ERROR %d / %d\n",
195 r_board = (struct ec_response_board_version *)msg->data;
197 count += scnprintf(buf + count, PAGE_SIZE - count,
198 "Board version: %d\n",
199 r_board->board_version);
207 static ssize_t flashinfo_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
210 struct ec_response_flash_info *resp;
211 struct cros_ec_command *msg;
213 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
215 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
219 /* The flash info shouldn't ever change, but ask each time anyway. */
221 msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset;
222 msg->insize = sizeof(*resp);
224 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
228 resp = (struct ec_response_flash_info *)msg->data;
230 ret = scnprintf(buf, PAGE_SIZE,
231 "FlashSize %d\nWriteSize %d\n"
232 "EraseSize %d\nProtectSize %d\n",
233 resp->flash_size, resp->write_block_size,
234 resp->erase_block_size, resp->protect_block_size);
240 /* Keyboard wake angle control */
241 static ssize_t kb_wake_angle_show(struct device *dev,
242 struct device_attribute *attr, char *buf)
244 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
245 struct ec_response_motion_sense *resp;
246 struct ec_params_motion_sense *param;
247 struct cros_ec_command *msg;
250 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
254 param = (struct ec_params_motion_sense *)msg->data;
255 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
257 param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
258 param->kb_wake_angle.data = EC_MOTION_SENSE_NO_VALUE;
259 msg->outsize = sizeof(*param);
260 msg->insize = sizeof(*resp);
262 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
266 resp = (struct ec_response_motion_sense *)msg->data;
267 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->kb_wake_angle.ret);
273 static ssize_t kb_wake_angle_store(struct device *dev,
274 struct device_attribute *attr,
275 const char *buf, size_t count)
277 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
278 struct ec_params_motion_sense *param;
279 struct cros_ec_command *msg;
283 ret = kstrtou16(buf, 0, &angle);
287 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
291 param = (struct ec_params_motion_sense *)msg->data;
292 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
294 param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
295 param->kb_wake_angle.data = angle;
296 msg->outsize = sizeof(*param);
297 msg->insize = sizeof(struct ec_response_motion_sense);
299 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
306 /* Module initialization */
308 static DEVICE_ATTR_RW(reboot);
309 static DEVICE_ATTR_RO(version);
310 static DEVICE_ATTR_RO(flashinfo);
311 static DEVICE_ATTR_RW(kb_wake_angle);
313 static struct attribute *__ec_attrs[] = {
314 &dev_attr_kb_wake_angle.attr,
315 &dev_attr_reboot.attr,
316 &dev_attr_version.attr,
317 &dev_attr_flashinfo.attr,
321 static umode_t cros_ec_ctrl_visible(struct kobject *kobj,
322 struct attribute *a, int n)
324 struct device *dev = kobj_to_dev(kobj);
325 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
327 if (a == &dev_attr_kb_wake_angle.attr && !ec->has_kb_wake_angle)
333 static const struct attribute_group cros_ec_attr_group = {
335 .is_visible = cros_ec_ctrl_visible,
338 static int cros_ec_sysfs_probe(struct platform_device *pd)
340 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
341 struct device *dev = &pd->dev;
344 ret = sysfs_create_group(&ec_dev->class_dev.kobj, &cros_ec_attr_group);
346 dev_err(dev, "failed to create attributes. err=%d\n", ret);
351 static int cros_ec_sysfs_remove(struct platform_device *pd)
353 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
355 sysfs_remove_group(&ec_dev->class_dev.kobj, &cros_ec_attr_group);
360 static struct platform_driver cros_ec_sysfs_driver = {
364 .probe = cros_ec_sysfs_probe,
365 .remove = cros_ec_sysfs_remove,
368 module_platform_driver(cros_ec_sysfs_driver);
370 MODULE_LICENSE("GPL");
371 MODULE_DESCRIPTION("Expose the ChromeOS EC through sysfs");
372 MODULE_ALIAS("platform:" DRV_NAME);