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