1 // SPDX-License-Identifier: GPL-2.0+
2 // Expose the ChromeOS EC through sysfs
3 //
4 // Copyright (C) 2014 Google, Inc.
5
6 #include <linux/ctype.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/fs.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>
20
21 #define DRV_NAME "cros-ec-sysfs"
22
23 /* Accessor functions */
24
reboot_show(struct device * dev,struct device_attribute * attr,char * buf)25 static ssize_t reboot_show(struct device *dev,
26 struct device_attribute *attr, char *buf)
27 {
28 int count = 0;
29
30 count += sysfs_emit_at(buf, count,
31 "ro|rw|cancel|cold|disable-jump|hibernate|cold-ap-off");
32 count += sysfs_emit_at(buf, count, " [at-shutdown]\n");
33 return count;
34 }
35
reboot_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)36 static ssize_t reboot_store(struct device *dev,
37 struct device_attribute *attr,
38 const char *buf, size_t count)
39 {
40 static const struct {
41 const char * const str;
42 uint8_t cmd;
43 uint8_t flags;
44 } words[] = {
45 {"cancel", EC_REBOOT_CANCEL, 0},
46 {"ro", EC_REBOOT_JUMP_RO, 0},
47 {"rw", EC_REBOOT_JUMP_RW, 0},
48 {"cold-ap-off", EC_REBOOT_COLD_AP_OFF, 0},
49 {"cold", EC_REBOOT_COLD, 0},
50 {"disable-jump", EC_REBOOT_DISABLE_JUMP, 0},
51 {"hibernate", EC_REBOOT_HIBERNATE, 0},
52 {"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
53 };
54 struct cros_ec_command *msg;
55 struct ec_params_reboot_ec *param;
56 int got_cmd = 0, offset = 0;
57 int i;
58 int ret;
59 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
60
61 msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
62 if (!msg)
63 return -ENOMEM;
64
65 param = (struct ec_params_reboot_ec *)msg->data;
66
67 param->flags = 0;
68 while (1) {
69 /* Find word to start scanning */
70 while (buf[offset] && isspace(buf[offset]))
71 offset++;
72 if (!buf[offset])
73 break;
74
75 for (i = 0; i < ARRAY_SIZE(words); i++) {
76 if (!strncasecmp(words[i].str, buf+offset,
77 strlen(words[i].str))) {
78 if (words[i].flags) {
79 param->flags |= words[i].flags;
80 } else {
81 param->cmd = words[i].cmd;
82 got_cmd = 1;
83 }
84 break;
85 }
86 }
87
88 /* On to the next word, if any */
89 while (buf[offset] && !isspace(buf[offset]))
90 offset++;
91 }
92
93 if (!got_cmd) {
94 count = -EINVAL;
95 goto exit;
96 }
97
98 msg->version = 0;
99 msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset;
100 msg->outsize = sizeof(*param);
101 msg->insize = 0;
102 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
103 if (ret < 0)
104 count = ret;
105 exit:
106 kfree(msg);
107 return count;
108 }
109
version_show(struct device * dev,struct device_attribute * attr,char * buf)110 static ssize_t version_show(struct device *dev,
111 struct device_attribute *attr, char *buf)
112 {
113 static const char * const image_names[] = {"unknown", "RO", "RW"};
114 struct ec_response_get_version *r_ver;
115 struct ec_response_get_chip_info *r_chip;
116 struct ec_response_board_version *r_board;
117 struct cros_ec_command *msg;
118 int ret;
119 int count = 0;
120 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
121
122 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
123 if (!msg)
124 return -ENOMEM;
125
126 /* Get versions. RW may change. */
127 msg->version = 0;
128 msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
129 msg->insize = sizeof(*r_ver);
130 msg->outsize = 0;
131 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
132 if (ret < 0) {
133 count = ret;
134 goto exit;
135 }
136 r_ver = (struct ec_response_get_version *)msg->data;
137 /* Strings should be null-terminated, but let's be sure. */
138 r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
139 r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
140 count += sysfs_emit_at(buf, count, "RO version: %s\n", r_ver->version_string_ro);
141 count += sysfs_emit_at(buf, count, "RW version: %s\n", r_ver->version_string_rw);
142 count += sysfs_emit_at(buf, count, "Firmware copy: %s\n",
143 (r_ver->current_image < ARRAY_SIZE(image_names) ?
144 image_names[r_ver->current_image] : "?"));
145
146 /* Get build info. */
147 msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset;
148 msg->insize = EC_HOST_PARAM_SIZE;
149 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
150 if (ret < 0) {
151 count += sysfs_emit_at(buf, count,
152 "Build info: XFER / EC ERROR %d / %d\n",
153 ret, msg->result);
154 } else {
155 msg->data[EC_HOST_PARAM_SIZE - 1] = '\0';
156 count += sysfs_emit_at(buf, count, "Build info: %s\n", msg->data);
157 }
158
159 /* Get chip info. */
160 msg->command = EC_CMD_GET_CHIP_INFO + ec->cmd_offset;
161 msg->insize = sizeof(*r_chip);
162 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
163 if (ret < 0) {
164 count += sysfs_emit_at(buf, count,
165 "Chip info: XFER / EC ERROR %d / %d\n",
166 ret, msg->result);
167 } else {
168 r_chip = (struct ec_response_get_chip_info *)msg->data;
169
170 r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
171 r_chip->name[sizeof(r_chip->name) - 1] = '\0';
172 r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
173 count += sysfs_emit_at(buf, count, "Chip vendor: %s\n", r_chip->vendor);
174 count += sysfs_emit_at(buf, count, "Chip name: %s\n", r_chip->name);
175 count += sysfs_emit_at(buf, count, "Chip revision: %s\n", r_chip->revision);
176 }
177
178 /* Get board version */
179 msg->command = EC_CMD_GET_BOARD_VERSION + ec->cmd_offset;
180 msg->insize = sizeof(*r_board);
181 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
182 if (ret < 0) {
183 count += sysfs_emit_at(buf, count,
184 "Board version: XFER / EC ERROR %d / %d\n",
185 ret, msg->result);
186 } else {
187 r_board = (struct ec_response_board_version *)msg->data;
188
189 count += sysfs_emit_at(buf, count,
190 "Board version: %d\n",
191 r_board->board_version);
192 }
193
194 exit:
195 kfree(msg);
196 return count;
197 }
198
flashinfo_show(struct device * dev,struct device_attribute * attr,char * buf)199 static ssize_t flashinfo_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
201 {
202 struct ec_response_flash_info *resp;
203 struct cros_ec_command *msg;
204 int ret;
205 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
206
207 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
208 if (!msg)
209 return -ENOMEM;
210
211 /* The flash info shouldn't ever change, but ask each time anyway. */
212 msg->version = 0;
213 msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset;
214 msg->insize = sizeof(*resp);
215 msg->outsize = 0;
216 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
217 if (ret < 0)
218 goto exit;
219
220 resp = (struct ec_response_flash_info *)msg->data;
221
222 ret = sysfs_emit(buf,
223 "FlashSize %d\nWriteSize %d\n"
224 "EraseSize %d\nProtectSize %d\n",
225 resp->flash_size, resp->write_block_size,
226 resp->erase_block_size, resp->protect_block_size);
227 exit:
228 kfree(msg);
229 return ret;
230 }
231
232 /* Keyboard wake angle control */
kb_wake_angle_show(struct device * dev,struct device_attribute * attr,char * buf)233 static ssize_t kb_wake_angle_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
235 {
236 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
237 struct ec_response_motion_sense *resp;
238 struct ec_params_motion_sense *param;
239 struct cros_ec_command *msg;
240 int ret;
241
242 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
243 if (!msg)
244 return -ENOMEM;
245
246 param = (struct ec_params_motion_sense *)msg->data;
247 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
248 msg->version = 2;
249 param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
250 param->kb_wake_angle.data = EC_MOTION_SENSE_NO_VALUE;
251 msg->outsize = sizeof(*param);
252 msg->insize = sizeof(*resp);
253
254 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
255 if (ret < 0)
256 goto exit;
257
258 resp = (struct ec_response_motion_sense *)msg->data;
259 ret = sysfs_emit(buf, "%d\n", resp->kb_wake_angle.ret);
260 exit:
261 kfree(msg);
262 return ret;
263 }
264
kb_wake_angle_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)265 static ssize_t kb_wake_angle_store(struct device *dev,
266 struct device_attribute *attr,
267 const char *buf, size_t count)
268 {
269 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
270 struct ec_params_motion_sense *param;
271 struct cros_ec_command *msg;
272 u16 angle;
273 int ret;
274
275 ret = kstrtou16(buf, 0, &angle);
276 if (ret)
277 return ret;
278
279 msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
280 if (!msg)
281 return -ENOMEM;
282
283 param = (struct ec_params_motion_sense *)msg->data;
284 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
285 msg->version = 2;
286 param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
287 param->kb_wake_angle.data = angle;
288 msg->outsize = sizeof(*param);
289 msg->insize = sizeof(struct ec_response_motion_sense);
290
291 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
292 kfree(msg);
293 if (ret < 0)
294 return ret;
295 return count;
296 }
297
usbpdmuxinfo_show(struct device * dev,struct device_attribute * attr,char * buf)298 static ssize_t usbpdmuxinfo_show(struct device *dev,
299 struct device_attribute *attr,
300 char *buf)
301 {
302 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
303 ssize_t count = 0;
304 struct ec_response_usb_pd_ports resp_pd_ports;
305 int ret;
306 int i;
307
308 ret = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
309 &resp_pd_ports, sizeof(resp_pd_ports));
310 if (ret < 0)
311 return -EIO;
312
313 for (i = 0; i < resp_pd_ports.num_ports; i++) {
314 struct ec_response_usb_pd_mux_info resp_mux;
315 struct ec_params_usb_pd_mux_info req = {
316 .port = i,
317 };
318
319 ret = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_USB_PD_MUX_INFO,
320 &req, sizeof(req), &resp_mux, sizeof(resp_mux));
321
322 if (ret >= 0) {
323 count += sysfs_emit_at(buf, count, "Port %d:", i);
324 count += sysfs_emit_at(buf, count, " USB=%d",
325 !!(resp_mux.flags & USB_PD_MUX_USB_ENABLED));
326 count += sysfs_emit_at(buf, count, " DP=%d",
327 !!(resp_mux.flags & USB_PD_MUX_DP_ENABLED));
328 count += sysfs_emit_at(buf, count, " POLARITY=%s",
329 (resp_mux.flags & USB_PD_MUX_POLARITY_INVERTED) ?
330 "INVERTED" : "NORMAL");
331 count += sysfs_emit_at(buf, count, " HPD_IRQ=%d",
332 !!(resp_mux.flags & USB_PD_MUX_HPD_IRQ));
333 count += sysfs_emit_at(buf, count, " HPD_LVL=%d",
334 !!(resp_mux.flags & USB_PD_MUX_HPD_LVL));
335 count += sysfs_emit_at(buf, count, " SAFE=%d",
336 !!(resp_mux.flags & USB_PD_MUX_SAFE_MODE));
337 count += sysfs_emit_at(buf, count, " TBT=%d",
338 !!(resp_mux.flags & USB_PD_MUX_TBT_COMPAT_ENABLED));
339 count += sysfs_emit_at(buf, count, " USB4=%d\n",
340 !!(resp_mux.flags & USB_PD_MUX_USB4_ENABLED));
341 }
342 }
343
344 return count ? : -EIO;
345 }
346
ap_mode_entry_show(struct device * dev,struct device_attribute * attr,char * buf)347 static ssize_t ap_mode_entry_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
349 {
350 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
351 const bool ap_driven_altmode = cros_ec_check_features(
352 ec, EC_FEATURE_TYPEC_REQUIRE_AP_MODE_ENTRY);
353
354 return sysfs_emit(buf, "%s\n", ap_driven_altmode ? "yes" : "no");
355 }
356
357 /* Module initialization */
358
359 static DEVICE_ATTR_RW(reboot);
360 static DEVICE_ATTR_RO(version);
361 static DEVICE_ATTR_RO(flashinfo);
362 static DEVICE_ATTR_RW(kb_wake_angle);
363 static DEVICE_ATTR_RO(usbpdmuxinfo);
364 static DEVICE_ATTR_RO(ap_mode_entry);
365
366 static struct attribute *__ec_attrs[] = {
367 &dev_attr_kb_wake_angle.attr,
368 &dev_attr_reboot.attr,
369 &dev_attr_version.attr,
370 &dev_attr_flashinfo.attr,
371 &dev_attr_usbpdmuxinfo.attr,
372 &dev_attr_ap_mode_entry.attr,
373 NULL,
374 };
375
cros_ec_ctrl_visible(struct kobject * kobj,struct attribute * a,int n)376 static umode_t cros_ec_ctrl_visible(struct kobject *kobj,
377 struct attribute *a, int n)
378 {
379 struct device *dev = kobj_to_dev(kobj);
380 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
381
382 if (a == &dev_attr_kb_wake_angle.attr && !ec->has_kb_wake_angle)
383 return 0;
384
385 if (a == &dev_attr_usbpdmuxinfo.attr ||
386 a == &dev_attr_ap_mode_entry.attr) {
387 struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
388
389 if (strcmp(ec_platform->ec_name, CROS_EC_DEV_NAME))
390 return 0;
391 }
392
393 return a->mode;
394 }
395
396 static const struct attribute_group cros_ec_attr_group = {
397 .attrs = __ec_attrs,
398 .is_visible = cros_ec_ctrl_visible,
399 };
400
cros_ec_sysfs_probe(struct platform_device * pd)401 static int cros_ec_sysfs_probe(struct platform_device *pd)
402 {
403 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
404 struct device *dev = &pd->dev;
405 int ret;
406
407 ec_dev->group = &cros_ec_attr_group;
408 ret = sysfs_create_group(&ec_dev->class_dev.kobj, ec_dev->group);
409 if (ret < 0)
410 dev_err(dev, "failed to create attributes. err=%d\n", ret);
411
412 return ret;
413 }
414
cros_ec_sysfs_remove(struct platform_device * pd)415 static void cros_ec_sysfs_remove(struct platform_device *pd)
416 {
417 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
418
419 sysfs_remove_group(&ec_dev->class_dev.kobj, ec_dev->group);
420 }
421
422 static const struct platform_device_id cros_ec_sysfs_id[] = {
423 { .name = DRV_NAME },
424 { }
425 };
426 MODULE_DEVICE_TABLE(platform, cros_ec_sysfs_id);
427
428 static struct platform_driver cros_ec_sysfs_driver = {
429 .driver = {
430 .name = DRV_NAME,
431 },
432 .probe = cros_ec_sysfs_probe,
433 .remove = cros_ec_sysfs_remove,
434 .id_table = cros_ec_sysfs_id,
435 };
436
437 module_platform_driver(cros_ec_sysfs_driver);
438
439 MODULE_LICENSE("GPL");
440 MODULE_DESCRIPTION("Expose the ChromeOS EC through sysfs");
441