1 // SPDX-License-Identifier: GPL-2.0
2 // ChromeOS EC communication protocol helper functions
3 //
4 // Copyright (C) 2015 Google, Inc
5
6 #include <linux/delay.h>
7 #include <linux/device.h>
8 #include <linux/limits.h>
9 #include <linux/module.h>
10 #include <linux/platform_data/cros_ec_commands.h>
11 #include <linux/platform_data/cros_ec_proto.h>
12 #include <linux/slab.h>
13 #include <linux/unaligned.h>
14
15 #include "cros_ec_trace.h"
16
17 #define EC_COMMAND_RETRIES 50
18 #define RWSIG_CONTINUE_RETRIES 8
19 #define RWSIG_CONTINUE_MAX_ERRORS_IN_ROW 3
20
21 static const int cros_ec_error_map[] = {
22 [EC_RES_INVALID_COMMAND] = -EOPNOTSUPP,
23 [EC_RES_ERROR] = -EIO,
24 [EC_RES_INVALID_PARAM] = -EINVAL,
25 [EC_RES_ACCESS_DENIED] = -EACCES,
26 [EC_RES_INVALID_RESPONSE] = -EPROTO,
27 [EC_RES_INVALID_VERSION] = -ENOPROTOOPT,
28 [EC_RES_INVALID_CHECKSUM] = -EBADMSG,
29 [EC_RES_IN_PROGRESS] = -EINPROGRESS,
30 [EC_RES_UNAVAILABLE] = -ENODATA,
31 [EC_RES_TIMEOUT] = -ETIMEDOUT,
32 [EC_RES_OVERFLOW] = -EOVERFLOW,
33 [EC_RES_INVALID_HEADER] = -EBADR,
34 [EC_RES_REQUEST_TRUNCATED] = -EBADR,
35 [EC_RES_RESPONSE_TOO_BIG] = -EFBIG,
36 [EC_RES_BUS_ERROR] = -EFAULT,
37 [EC_RES_BUSY] = -EBUSY,
38 [EC_RES_INVALID_HEADER_VERSION] = -EBADMSG,
39 [EC_RES_INVALID_HEADER_CRC] = -EBADMSG,
40 [EC_RES_INVALID_DATA_CRC] = -EBADMSG,
41 [EC_RES_DUP_UNAVAILABLE] = -ENODATA,
42 };
43
cros_ec_map_error(uint32_t result)44 static int cros_ec_map_error(uint32_t result)
45 {
46 int ret = 0;
47
48 if (result != EC_RES_SUCCESS) {
49 if (result < ARRAY_SIZE(cros_ec_error_map) && cros_ec_error_map[result])
50 ret = cros_ec_error_map[result];
51 else
52 ret = -EPROTO;
53 }
54
55 return ret;
56 }
57
prepare_tx(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)58 static int prepare_tx(struct cros_ec_device *ec_dev,
59 struct cros_ec_command *msg)
60 {
61 struct ec_host_request *request;
62 u8 *out;
63 int i;
64 u8 csum = 0;
65
66 if (msg->outsize + sizeof(*request) > ec_dev->dout_size)
67 return -EINVAL;
68
69 out = ec_dev->dout;
70 request = (struct ec_host_request *)out;
71 request->struct_version = EC_HOST_REQUEST_VERSION;
72 request->checksum = 0;
73 request->command = msg->command;
74 request->command_version = msg->version;
75 request->reserved = 0;
76 request->data_len = msg->outsize;
77
78 for (i = 0; i < sizeof(*request); i++)
79 csum += out[i];
80
81 /* Copy data and update checksum */
82 memcpy(out + sizeof(*request), msg->data, msg->outsize);
83 for (i = 0; i < msg->outsize; i++)
84 csum += msg->data[i];
85
86 request->checksum = -csum;
87
88 return sizeof(*request) + msg->outsize;
89 }
90
prepare_tx_legacy(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)91 static int prepare_tx_legacy(struct cros_ec_device *ec_dev,
92 struct cros_ec_command *msg)
93 {
94 u8 *out;
95 u8 csum;
96 int i;
97
98 if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE)
99 return -EINVAL;
100
101 out = ec_dev->dout;
102 out[0] = EC_CMD_VERSION0 + msg->version;
103 out[1] = msg->command;
104 out[2] = msg->outsize;
105 csum = out[0] + out[1] + out[2];
106 for (i = 0; i < msg->outsize; i++)
107 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
108 out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
109
110 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
111 }
112
cros_ec_xfer_command(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)113 static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
114 {
115 int ret;
116 int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
117
118 if (ec_dev->proto_version > 2)
119 xfer_fxn = ec_dev->pkt_xfer;
120 else
121 xfer_fxn = ec_dev->cmd_xfer;
122
123 if (!xfer_fxn) {
124 /*
125 * This error can happen if a communication error happened and
126 * the EC is trying to use protocol v2, on an underlying
127 * communication mechanism that does not support v2.
128 */
129 dev_err_once(ec_dev->dev, "missing EC transfer API, cannot send command\n");
130 return -EIO;
131 }
132
133 trace_cros_ec_request_start(msg);
134 ret = (*xfer_fxn)(ec_dev, msg);
135 trace_cros_ec_request_done(msg, ret);
136
137 return ret;
138 }
139
cros_ec_wait_until_complete(struct cros_ec_device * ec_dev,uint32_t * result)140 static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *result)
141 {
142 struct {
143 struct cros_ec_command msg;
144 struct ec_response_get_comms_status status;
145 } __packed buf;
146 struct cros_ec_command *msg = &buf.msg;
147 struct ec_response_get_comms_status *status = &buf.status;
148 int ret = 0, i;
149
150 msg->version = 0;
151 msg->command = EC_CMD_GET_COMMS_STATUS;
152 msg->insize = sizeof(*status);
153 msg->outsize = 0;
154
155 /* Query the EC's status until it's no longer busy or we encounter an error. */
156 for (i = 0; i < EC_COMMAND_RETRIES; ++i) {
157 usleep_range(10000, 11000);
158
159 ret = cros_ec_xfer_command(ec_dev, msg);
160 if (ret == -EAGAIN)
161 continue;
162 if (ret < 0)
163 return ret;
164
165 *result = msg->result;
166 if (msg->result != EC_RES_SUCCESS)
167 return ret;
168
169 if (ret == 0) {
170 ret = -EPROTO;
171 break;
172 }
173
174 if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
175 return ret;
176 }
177
178 if (i >= EC_COMMAND_RETRIES)
179 ret = -EAGAIN;
180
181 return ret;
182 }
183
cros_ec_send_command(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)184 static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
185 {
186 int ret = cros_ec_xfer_command(ec_dev, msg);
187
188 if (msg->result == EC_RES_IN_PROGRESS)
189 ret = cros_ec_wait_until_complete(ec_dev, &msg->result);
190
191 return ret;
192 }
193
194 /**
195 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
196 * @ec_dev: Device to register.
197 * @msg: Message to write.
198 *
199 * This is used by all ChromeOS EC drivers to prepare the outgoing message
200 * according to different protocol versions.
201 *
202 * Return: number of prepared bytes on success or negative error code.
203 */
cros_ec_prepare_tx(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)204 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
205 struct cros_ec_command *msg)
206 {
207 if (ec_dev->proto_version > 2)
208 return prepare_tx(ec_dev, msg);
209
210 return prepare_tx_legacy(ec_dev, msg);
211 }
212 EXPORT_SYMBOL(cros_ec_prepare_tx);
213
214 /**
215 * cros_ec_check_result() - Check ec_msg->result.
216 * @ec_dev: EC device.
217 * @msg: Message to check.
218 *
219 * This is used by ChromeOS EC drivers to check the ec_msg->result for
220 * EC_RES_IN_PROGRESS and to warn about them.
221 *
222 * The function should not check for furthermore error codes. Otherwise,
223 * it would break the ABI.
224 *
225 * Return: -EAGAIN if ec_msg->result == EC_RES_IN_PROGRESS. Otherwise, 0.
226 */
cros_ec_check_result(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)227 int cros_ec_check_result(struct cros_ec_device *ec_dev,
228 struct cros_ec_command *msg)
229 {
230 switch (msg->result) {
231 case EC_RES_SUCCESS:
232 return 0;
233 case EC_RES_IN_PROGRESS:
234 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
235 msg->command);
236 return -EAGAIN;
237 default:
238 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
239 msg->command, msg->result);
240 return 0;
241 }
242 }
243 EXPORT_SYMBOL(cros_ec_check_result);
244
245 /**
246 * cros_ec_get_host_event_wake_mask
247 *
248 * Get the mask of host events that cause wake from suspend.
249 *
250 * @ec_dev: EC device to call
251 * @mask: result when function returns 0.
252 *
253 * LOCKING:
254 * the caller has ec_dev->lock mutex, or the caller knows there is
255 * no other command in progress.
256 */
cros_ec_get_host_event_wake_mask(struct cros_ec_device * ec_dev,uint32_t * mask)257 static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev, uint32_t *mask)
258 {
259 struct cros_ec_command *msg;
260 struct ec_response_host_event_mask *r;
261 int ret, mapped;
262
263 msg = kzalloc(sizeof(*msg) + sizeof(*r), GFP_KERNEL);
264 if (!msg)
265 return -ENOMEM;
266
267 msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
268 msg->insize = sizeof(*r);
269
270 ret = cros_ec_send_command(ec_dev, msg);
271 if (ret < 0)
272 goto exit;
273
274 mapped = cros_ec_map_error(msg->result);
275 if (mapped) {
276 ret = mapped;
277 goto exit;
278 }
279
280 if (ret == 0) {
281 ret = -EPROTO;
282 goto exit;
283 }
284
285 r = (struct ec_response_host_event_mask *)msg->data;
286 *mask = r->mask;
287 ret = 0;
288 exit:
289 kfree(msg);
290 return ret;
291 }
292
cros_ec_rwsig_continue(struct cros_ec_device * ec_dev)293 int cros_ec_rwsig_continue(struct cros_ec_device *ec_dev)
294 {
295 struct cros_ec_command *msg;
296 struct ec_params_rwsig_action *rwsig_action;
297 int ret = 0;
298 int error_count = 0;
299
300 ec_dev->proto_version = 3;
301
302 msg = kmalloc(sizeof(*msg) + sizeof(*rwsig_action), GFP_KERNEL);
303 if (!msg)
304 return -ENOMEM;
305
306 msg->version = 0;
307 msg->command = EC_CMD_RWSIG_ACTION;
308 msg->insize = 0;
309 msg->outsize = sizeof(*rwsig_action);
310
311 rwsig_action = (struct ec_params_rwsig_action *)msg->data;
312 rwsig_action->action = RWSIG_ACTION_CONTINUE;
313
314 for (int i = 0; i < RWSIG_CONTINUE_RETRIES; i++) {
315 ret = cros_ec_send_command(ec_dev, msg);
316
317 if (ret < 0) {
318 if (++error_count >= RWSIG_CONTINUE_MAX_ERRORS_IN_ROW)
319 break;
320 } else if (msg->result == EC_RES_INVALID_COMMAND) {
321 /*
322 * If EC_RES_INVALID_COMMAND is retured, it means RWSIG
323 * is not supported or EC is already in RW, so there is
324 * nothing left to do.
325 */
326 break;
327 } else if (msg->result != EC_RES_SUCCESS) {
328 /* Unexpected command error. */
329 ret = cros_ec_map_error(msg->result);
330 break;
331 } else {
332 /*
333 * The EC_CMD_RWSIG_ACTION succeed. Send the command
334 * more times, to make sure EC is in RW. A following
335 * command can timeout, because EC may need some time to
336 * initialize after jump to RW.
337 */
338 error_count = 0;
339 }
340
341 if (ret != -ETIMEDOUT)
342 usleep_range(90000, 100000);
343 }
344
345 kfree(msg);
346
347 return ret;
348 }
349 EXPORT_SYMBOL(cros_ec_rwsig_continue);
350
cros_ec_get_proto_info(struct cros_ec_device * ec_dev,int devidx)351 static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx)
352 {
353 struct cros_ec_command *msg;
354 struct ec_response_get_protocol_info *info;
355 int ret, mapped;
356
357 ec_dev->proto_version = 3;
358 if (devidx > 0)
359 ec_dev->max_passthru = 0;
360
361 msg = kzalloc(sizeof(*msg) + sizeof(*info), GFP_KERNEL);
362 if (!msg)
363 return -ENOMEM;
364
365 msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
366 msg->insize = sizeof(*info);
367
368 ret = cros_ec_send_command(ec_dev, msg);
369
370 if (ret < 0) {
371 dev_dbg(ec_dev->dev,
372 "failed to check for EC[%d] protocol version: %d\n",
373 devidx, ret);
374 goto exit;
375 }
376
377 mapped = cros_ec_map_error(msg->result);
378 if (mapped) {
379 ret = mapped;
380 goto exit;
381 }
382
383 if (ret == 0) {
384 ret = -EPROTO;
385 goto exit;
386 }
387
388 info = (struct ec_response_get_protocol_info *)msg->data;
389
390 switch (devidx) {
391 case CROS_EC_DEV_EC_INDEX:
392 ec_dev->max_request = info->max_request_packet_size -
393 sizeof(struct ec_host_request);
394 ec_dev->max_response = info->max_response_packet_size -
395 sizeof(struct ec_host_response);
396 ec_dev->proto_version = min(EC_HOST_REQUEST_VERSION,
397 fls(info->protocol_versions) - 1);
398 ec_dev->din_size = info->max_response_packet_size + EC_MAX_RESPONSE_OVERHEAD;
399 ec_dev->dout_size = info->max_request_packet_size + EC_MAX_REQUEST_OVERHEAD;
400
401 dev_dbg(ec_dev->dev, "using proto v%u\n", ec_dev->proto_version);
402 break;
403 case CROS_EC_DEV_PD_INDEX:
404 ec_dev->max_passthru = info->max_request_packet_size -
405 sizeof(struct ec_host_request);
406
407 dev_dbg(ec_dev->dev, "found PD chip\n");
408 break;
409 default:
410 dev_dbg(ec_dev->dev, "unknown passthru index: %d\n", devidx);
411 break;
412 }
413
414 ret = 0;
415 exit:
416 kfree(msg);
417 return ret;
418 }
419
cros_ec_get_proto_info_legacy(struct cros_ec_device * ec_dev)420 static int cros_ec_get_proto_info_legacy(struct cros_ec_device *ec_dev)
421 {
422 struct cros_ec_command *msg;
423 struct ec_params_hello *params;
424 struct ec_response_hello *response;
425 int ret, mapped;
426
427 ec_dev->proto_version = 2;
428
429 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)), GFP_KERNEL);
430 if (!msg)
431 return -ENOMEM;
432
433 msg->command = EC_CMD_HELLO;
434 msg->insize = sizeof(*response);
435 msg->outsize = sizeof(*params);
436
437 params = (struct ec_params_hello *)msg->data;
438 params->in_data = 0xa0b0c0d0;
439
440 ret = cros_ec_send_command(ec_dev, msg);
441 if (ret < 0) {
442 dev_dbg(ec_dev->dev, "EC failed to respond to v2 hello: %d\n", ret);
443 goto exit;
444 }
445
446 mapped = cros_ec_map_error(msg->result);
447 if (mapped) {
448 ret = mapped;
449 dev_err(ec_dev->dev, "EC responded to v2 hello with error: %d\n", msg->result);
450 goto exit;
451 }
452
453 if (ret == 0) {
454 ret = -EPROTO;
455 goto exit;
456 }
457
458 response = (struct ec_response_hello *)msg->data;
459 if (response->out_data != 0xa1b2c3d4) {
460 dev_err(ec_dev->dev,
461 "EC responded to v2 hello with bad result: %u\n",
462 response->out_data);
463 ret = -EBADMSG;
464 goto exit;
465 }
466
467 ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
468 ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
469 ec_dev->max_passthru = 0;
470 ec_dev->pkt_xfer = NULL;
471 ec_dev->din_size = EC_PROTO2_MSG_BYTES;
472 ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
473
474 dev_dbg(ec_dev->dev, "falling back to proto v2\n");
475 ret = 0;
476 exit:
477 kfree(msg);
478 return ret;
479 }
480
481 /**
482 * cros_ec_get_host_command_version_mask
483 *
484 * Get the version mask of a given command.
485 *
486 * @ec_dev: EC device to call
487 * @cmd: command to get the version of.
488 * @mask: result when function returns 0.
489 *
490 * @return 0 on success, error code otherwise
491 *
492 * LOCKING:
493 * the caller has ec_dev->lock mutex or the caller knows there is
494 * no other command in progress.
495 */
cros_ec_get_host_command_version_mask(struct cros_ec_device * ec_dev,u16 cmd,u32 * mask)496 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev, u16 cmd, u32 *mask)
497 {
498 struct ec_params_get_cmd_versions *pver;
499 struct ec_response_get_cmd_versions *rver;
500 struct cros_ec_command *msg;
501 int ret, mapped;
502
503 msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
504 GFP_KERNEL);
505 if (!msg)
506 return -ENOMEM;
507
508 msg->version = 0;
509 msg->command = EC_CMD_GET_CMD_VERSIONS;
510 msg->insize = sizeof(*rver);
511 msg->outsize = sizeof(*pver);
512
513 pver = (struct ec_params_get_cmd_versions *)msg->data;
514 pver->cmd = cmd;
515
516 ret = cros_ec_send_command(ec_dev, msg);
517 if (ret < 0)
518 goto exit;
519
520 mapped = cros_ec_map_error(msg->result);
521 if (mapped) {
522 ret = mapped;
523 goto exit;
524 }
525
526 if (ret == 0) {
527 ret = -EPROTO;
528 goto exit;
529 }
530
531 rver = (struct ec_response_get_cmd_versions *)msg->data;
532 *mask = rver->version_mask;
533 ret = 0;
534 exit:
535 kfree(msg);
536 return ret;
537 }
538
539 /**
540 * cros_ec_query_all() - Query the protocol version supported by the
541 * ChromeOS EC.
542 * @ec_dev: Device to register.
543 *
544 * Return: 0 on success or negative error code.
545 */
cros_ec_query_all(struct cros_ec_device * ec_dev)546 int cros_ec_query_all(struct cros_ec_device *ec_dev)
547 {
548 struct device *dev = ec_dev->dev;
549 u32 ver_mask;
550 int ret;
551
552 /* First try sending with proto v3. */
553 if (!cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX)) {
554 /* Check for PD. */
555 cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_PD_INDEX);
556 } else {
557 /* Try querying with a v2 hello message. */
558 ret = cros_ec_get_proto_info_legacy(ec_dev);
559 if (ret) {
560 /*
561 * It's possible for a test to occur too early when
562 * the EC isn't listening. If this happens, we'll
563 * test later when the first command is run.
564 */
565 ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
566 dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
567 return ret;
568 }
569 }
570
571 devm_kfree(dev, ec_dev->din);
572 devm_kfree(dev, ec_dev->dout);
573
574 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
575 if (!ec_dev->din) {
576 ret = -ENOMEM;
577 goto exit;
578 }
579
580 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
581 if (!ec_dev->dout) {
582 devm_kfree(dev, ec_dev->din);
583 ret = -ENOMEM;
584 goto exit;
585 }
586
587 /* Probe if MKBP event is supported */
588 ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_GET_NEXT_EVENT, &ver_mask);
589 if (ret < 0 || ver_mask == 0) {
590 ec_dev->mkbp_event_supported = 0;
591 } else {
592 ec_dev->mkbp_event_supported = fls(ver_mask);
593
594 dev_dbg(ec_dev->dev, "MKBP support version %u\n", ec_dev->mkbp_event_supported - 1);
595 }
596
597 /* Probe if host sleep v1 is supported for S0ix failure detection. */
598 ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_HOST_SLEEP_EVENT, &ver_mask);
599 ec_dev->host_sleep_v1 = (ret == 0 && (ver_mask & EC_VER_MASK(1)));
600
601 /* Get host event wake mask. */
602 ret = cros_ec_get_host_event_wake_mask(ec_dev, &ec_dev->host_event_wake_mask);
603 if (ret < 0) {
604 /*
605 * If the EC doesn't support EC_CMD_HOST_EVENT_GET_WAKE_MASK,
606 * use a reasonable default. Note that we ignore various
607 * battery, AC status, and power-state events, because (a)
608 * those can be quite common (e.g., when sitting at full
609 * charge, on AC) and (b) these are not actionable wake events;
610 * if anything, we'd like to continue suspending (to save
611 * power), not wake up.
612 */
613 ec_dev->host_event_wake_mask = U32_MAX &
614 ~(EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED) |
615 EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED) |
616 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW) |
617 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL) |
618 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY) |
619 EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
620 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS));
621 /*
622 * Old ECs may not support this command. Complain about all
623 * other errors.
624 */
625 if (ret != -EOPNOTSUPP)
626 dev_err(ec_dev->dev,
627 "failed to retrieve wake mask: %d\n", ret);
628 }
629
630 ret = 0;
631
632 exit:
633 return ret;
634 }
635 EXPORT_SYMBOL(cros_ec_query_all);
636
637 /**
638 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
639 * @ec_dev: EC device.
640 * @msg: Message to write.
641 *
642 * Call this to send a command to the ChromeOS EC. This should be used instead
643 * of calling the EC's cmd_xfer() callback directly. This function does not
644 * convert EC command execution error codes to Linux error codes. Most
645 * in-kernel users will want to use cros_ec_cmd_xfer_status() instead since
646 * that function implements the conversion.
647 *
648 * Return:
649 * >0 - EC command was executed successfully. The return value is the number
650 * of bytes returned by the EC (excluding the header).
651 * =0 - EC communication was successful. EC command execution results are
652 * reported in msg->result. The result will be EC_RES_SUCCESS if the
653 * command was executed successfully or report an EC command execution
654 * error.
655 * <0 - EC communication error. Return value is the Linux error code.
656 */
cros_ec_cmd_xfer(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)657 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
658 {
659 int ret;
660
661 mutex_lock(&ec_dev->lock);
662 if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
663 ret = cros_ec_query_all(ec_dev);
664 if (ret) {
665 dev_err(ec_dev->dev,
666 "EC version unknown and query failed; aborting command\n");
667 mutex_unlock(&ec_dev->lock);
668 return ret;
669 }
670 }
671
672 if (msg->insize > ec_dev->max_response) {
673 dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
674 msg->insize = ec_dev->max_response;
675 }
676
677 if (msg->command < EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX)) {
678 if (msg->outsize > ec_dev->max_request) {
679 dev_err(ec_dev->dev,
680 "request of size %u is too big (max: %u)\n",
681 msg->outsize,
682 ec_dev->max_request);
683 mutex_unlock(&ec_dev->lock);
684 return -EMSGSIZE;
685 }
686 } else {
687 if (msg->outsize > ec_dev->max_passthru) {
688 dev_err(ec_dev->dev,
689 "passthru rq of size %u is too big (max: %u)\n",
690 msg->outsize,
691 ec_dev->max_passthru);
692 mutex_unlock(&ec_dev->lock);
693 return -EMSGSIZE;
694 }
695 }
696
697 ret = cros_ec_send_command(ec_dev, msg);
698 mutex_unlock(&ec_dev->lock);
699
700 return ret;
701 }
702 EXPORT_SYMBOL(cros_ec_cmd_xfer);
703
704 /**
705 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
706 * @ec_dev: EC device.
707 * @msg: Message to write.
708 *
709 * Call this to send a command to the ChromeOS EC. This should be used instead of calling the EC's
710 * cmd_xfer() callback directly. It returns success status only if both the command was transmitted
711 * successfully and the EC replied with success status.
712 *
713 * Return:
714 * >=0 - The number of bytes transferred.
715 * <0 - Linux error code
716 */
cros_ec_cmd_xfer_status(struct cros_ec_device * ec_dev,struct cros_ec_command * msg)717 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
718 struct cros_ec_command *msg)
719 {
720 int ret, mapped;
721
722 ret = cros_ec_cmd_xfer(ec_dev, msg);
723 if (ret < 0)
724 return ret;
725
726 mapped = cros_ec_map_error(msg->result);
727 if (mapped) {
728 dev_dbg(ec_dev->dev, "Command result (err: %d [%d])\n",
729 msg->result, mapped);
730 ret = mapped;
731 }
732
733 return ret;
734 }
735 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
736
get_next_event_xfer(struct cros_ec_device * ec_dev,struct cros_ec_command * msg,struct ec_response_get_next_event_v3 * event,int version,uint32_t size)737 static int get_next_event_xfer(struct cros_ec_device *ec_dev,
738 struct cros_ec_command *msg,
739 struct ec_response_get_next_event_v3 *event,
740 int version, uint32_t size)
741 {
742 int ret;
743
744 msg->version = version;
745 msg->command = EC_CMD_GET_NEXT_EVENT;
746 msg->insize = size;
747 msg->outsize = 0;
748
749 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
750 if (ret > 0) {
751 ec_dev->event_size = ret - 1;
752 ec_dev->event_data = *event;
753 }
754
755 return ret;
756 }
757
get_next_event(struct cros_ec_device * ec_dev)758 static int get_next_event(struct cros_ec_device *ec_dev)
759 {
760 struct {
761 struct cros_ec_command msg;
762 struct ec_response_get_next_event_v3 event;
763 } __packed buf;
764 struct cros_ec_command *msg = &buf.msg;
765 struct ec_response_get_next_event_v3 *event = &buf.event;
766 int cmd_version = ec_dev->mkbp_event_supported - 1;
767 u32 size;
768
769 memset(msg, 0, sizeof(*msg));
770 if (ec_dev->suspended) {
771 dev_dbg(ec_dev->dev, "Device suspended.\n");
772 return -EHOSTDOWN;
773 }
774
775 if (cmd_version == 0) {
776 size = sizeof(struct ec_response_get_next_event);
777 } else if (cmd_version < 3) {
778 size = sizeof(struct ec_response_get_next_event_v1);
779 } else {
780 /*
781 * The max version we support is v3. So, we speak v3 even if the
782 * EC says it supports v4+.
783 */
784 cmd_version = 3;
785 size = sizeof(struct ec_response_get_next_event_v3);
786 }
787
788 return get_next_event_xfer(ec_dev, msg, event, cmd_version, size);
789 }
790
get_keyboard_state_event(struct cros_ec_device * ec_dev)791 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
792 {
793 u8 buffer[sizeof(struct cros_ec_command) +
794 sizeof(ec_dev->event_data.data)];
795 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
796
797 msg->version = 0;
798 msg->command = EC_CMD_MKBP_STATE;
799 msg->insize = sizeof(ec_dev->event_data.data);
800 msg->outsize = 0;
801
802 ec_dev->event_size = cros_ec_cmd_xfer_status(ec_dev, msg);
803 ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
804 memcpy(&ec_dev->event_data.data, msg->data,
805 sizeof(ec_dev->event_data.data));
806
807 return ec_dev->event_size;
808 }
809
810 /**
811 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
812 * @ec_dev: Device to fetch event from.
813 * @wake_event: Pointer to a bool set to true upon return if the event might be
814 * treated as a wake event. Ignored if null.
815 * @has_more_events: Pointer to bool set to true if more than one event is
816 * pending.
817 * Some EC will set this flag to indicate cros_ec_get_next_event()
818 * can be called multiple times in a row.
819 * It is an optimization to prevent issuing a EC command for
820 * nothing or wait for another interrupt from the EC to process
821 * the next message.
822 * Ignored if null.
823 *
824 * Return: negative error code on errors; 0 for no data; or else number of
825 * bytes received (i.e., an event was retrieved successfully). Event types are
826 * written out to @ec_dev->event_data.event_type on success.
827 */
cros_ec_get_next_event(struct cros_ec_device * ec_dev,bool * wake_event,bool * has_more_events)828 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
829 bool *wake_event,
830 bool *has_more_events)
831 {
832 u8 event_type;
833 u32 host_event;
834 int ret;
835 u32 ver_mask;
836
837 /*
838 * Default value for wake_event.
839 * Wake up on keyboard event, wake up for spurious interrupt or link
840 * error to the EC.
841 */
842 if (wake_event)
843 *wake_event = true;
844
845 /*
846 * Default value for has_more_events.
847 * EC will raise another interrupt if AP does not process all events
848 * anyway.
849 */
850 if (has_more_events)
851 *has_more_events = false;
852
853 if (!ec_dev->mkbp_event_supported)
854 return get_keyboard_state_event(ec_dev);
855
856 ret = get_next_event(ec_dev);
857 /*
858 * -ENOPROTOOPT is returned when EC returns EC_RES_INVALID_VERSION.
859 * This can occur when EC based device (e.g. Fingerprint MCU) jumps to
860 * the RO image which doesn't support newer version of the command. In
861 * this case we will attempt to update maximum supported version of the
862 * EC_CMD_GET_NEXT_EVENT.
863 */
864 if (ret == -ENOPROTOOPT) {
865 dev_dbg(ec_dev->dev,
866 "GET_NEXT_EVENT returned invalid version error.\n");
867 mutex_lock(&ec_dev->lock);
868 ret = cros_ec_get_host_command_version_mask(ec_dev,
869 EC_CMD_GET_NEXT_EVENT,
870 &ver_mask);
871 mutex_unlock(&ec_dev->lock);
872 if (ret < 0 || ver_mask == 0)
873 /*
874 * Do not change the MKBP supported version if we can't
875 * obtain supported version correctly. Please note that
876 * calling EC_CMD_GET_NEXT_EVENT returned
877 * EC_RES_INVALID_VERSION which means that the command
878 * is present.
879 */
880 return -ENOPROTOOPT;
881
882 ec_dev->mkbp_event_supported = fls(ver_mask);
883 dev_dbg(ec_dev->dev, "MKBP support version changed to %u\n",
884 ec_dev->mkbp_event_supported - 1);
885
886 /* Try to get next event with new MKBP support version set. */
887 ret = get_next_event(ec_dev);
888 }
889
890 if (ret <= 0)
891 return ret;
892
893 if (has_more_events)
894 *has_more_events = ec_dev->event_data.event_type &
895 EC_MKBP_HAS_MORE_EVENTS;
896 ec_dev->event_data.event_type &= EC_MKBP_EVENT_TYPE_MASK;
897
898 if (wake_event) {
899 event_type = ec_dev->event_data.event_type;
900 host_event = cros_ec_get_host_event(ec_dev);
901
902 /*
903 * Sensor events need to be parsed by the sensor sub-device.
904 * Defer them, and don't report the wakeup here.
905 */
906 if (event_type == EC_MKBP_EVENT_SENSOR_FIFO) {
907 *wake_event = false;
908 } else if (host_event) {
909 /* rtc_update_irq() already handles wakeup events. */
910 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC))
911 *wake_event = false;
912 /* Masked host-events should not count as wake events. */
913 if (!(host_event & ec_dev->host_event_wake_mask))
914 *wake_event = false;
915 }
916 }
917
918 return ret;
919 }
920 EXPORT_SYMBOL(cros_ec_get_next_event);
921
922 /**
923 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
924 * @ec_dev: Device to fetch event from.
925 *
926 * When MKBP is supported, when the EC raises an interrupt, we collect the
927 * events raised and call the functions in the ec notifier. This function
928 * is a helper to know which events are raised.
929 *
930 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
931 */
cros_ec_get_host_event(struct cros_ec_device * ec_dev)932 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
933 {
934 u32 host_event;
935
936 if (!ec_dev->mkbp_event_supported)
937 return 0;
938
939 if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
940 return 0;
941
942 if (ec_dev->event_size != sizeof(host_event)) {
943 dev_warn(ec_dev->dev, "Invalid host event size\n");
944 return 0;
945 }
946
947 host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
948
949 return host_event;
950 }
951 EXPORT_SYMBOL(cros_ec_get_host_event);
952
953 /**
954 * cros_ec_check_features() - Test for the presence of EC features
955 *
956 * @ec: EC device, does not have to be connected directly to the AP,
957 * can be daisy chained through another device.
958 * @feature: One of ec_feature_code bit.
959 *
960 * Call this function to test whether the ChromeOS EC supports a feature.
961 *
962 * Return: true if supported, false if not (or if an error was encountered).
963 */
cros_ec_check_features(struct cros_ec_dev * ec,int feature)964 bool cros_ec_check_features(struct cros_ec_dev *ec, int feature)
965 {
966 struct ec_response_get_features *features = &ec->features;
967 int ret;
968
969 if (features->flags[0] == -1U && features->flags[1] == -1U) {
970 /* features bitmap not read yet */
971 ret = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_GET_FEATURES + ec->cmd_offset,
972 NULL, 0, features, sizeof(*features));
973 if (ret < 0) {
974 dev_warn(ec->dev, "cannot get EC features: %d\n", ret);
975 memset(features, 0, sizeof(*features));
976 }
977
978 dev_dbg(ec->dev, "EC features %08x %08x\n",
979 features->flags[0], features->flags[1]);
980 }
981
982 return !!(features->flags[feature / 32] & EC_FEATURE_MASK_0(feature));
983 }
984 EXPORT_SYMBOL_GPL(cros_ec_check_features);
985
986 /**
987 * cros_ec_get_sensor_count() - Return the number of MEMS sensors supported.
988 *
989 * @ec: EC device, does not have to be connected directly to the AP,
990 * can be daisy chained through another device.
991 * Return: < 0 in case of error.
992 */
cros_ec_get_sensor_count(struct cros_ec_dev * ec)993 int cros_ec_get_sensor_count(struct cros_ec_dev *ec)
994 {
995 /*
996 * Issue a command to get the number of sensor reported.
997 * If not supported, check for legacy mode.
998 */
999 int ret, sensor_count;
1000 struct ec_params_motion_sense *params;
1001 struct ec_response_motion_sense *resp;
1002 struct cros_ec_command *msg;
1003 struct cros_ec_device *ec_dev = ec->ec_dev;
1004 u8 status;
1005
1006 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*resp)),
1007 GFP_KERNEL);
1008 if (!msg)
1009 return -ENOMEM;
1010
1011 msg->version = 1;
1012 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
1013 msg->outsize = sizeof(*params);
1014 msg->insize = sizeof(*resp);
1015
1016 params = (struct ec_params_motion_sense *)msg->data;
1017 params->cmd = MOTIONSENSE_CMD_DUMP;
1018
1019 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
1020 if (ret < 0) {
1021 sensor_count = ret;
1022 } else {
1023 resp = (struct ec_response_motion_sense *)msg->data;
1024 sensor_count = resp->dump.sensor_count;
1025 }
1026 kfree(msg);
1027
1028 /*
1029 * Check legacy mode: Let's find out if sensors are accessible
1030 * via LPC interface.
1031 */
1032 if (sensor_count < 0 && ec->cmd_offset == 0 && ec_dev->cmd_readmem) {
1033 ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS,
1034 1, &status);
1035 if (ret >= 0 &&
1036 (status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
1037 /*
1038 * We have 2 sensors, one in the lid, one in the base.
1039 */
1040 sensor_count = 2;
1041 } else {
1042 /*
1043 * EC uses LPC interface and no sensors are presented.
1044 */
1045 sensor_count = 0;
1046 }
1047 }
1048 return sensor_count;
1049 }
1050 EXPORT_SYMBOL_GPL(cros_ec_get_sensor_count);
1051
1052 /**
1053 * cros_ec_cmd - Send a command to the EC.
1054 *
1055 * @ec_dev: EC device
1056 * @version: EC command version
1057 * @command: EC command
1058 * @outdata: EC command output data
1059 * @outsize: Size of outdata
1060 * @indata: EC command input data
1061 * @insize: Size of indata
1062 *
1063 * Return: >= 0 on success, negative error number on failure.
1064 */
cros_ec_cmd(struct cros_ec_device * ec_dev,unsigned int version,int command,const void * outdata,size_t outsize,void * indata,size_t insize)1065 int cros_ec_cmd(struct cros_ec_device *ec_dev,
1066 unsigned int version,
1067 int command,
1068 const void *outdata,
1069 size_t outsize,
1070 void *indata,
1071 size_t insize)
1072 {
1073 struct cros_ec_command *msg;
1074 int ret;
1075
1076 msg = kzalloc(sizeof(*msg) + max(insize, outsize), GFP_KERNEL);
1077 if (!msg)
1078 return -ENOMEM;
1079
1080 msg->version = version;
1081 msg->command = command;
1082 msg->outsize = outsize;
1083 msg->insize = insize;
1084
1085 if (outsize)
1086 memcpy(msg->data, outdata, outsize);
1087
1088 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
1089 if (ret < 0)
1090 goto error;
1091
1092 if (insize)
1093 memcpy(indata, msg->data, insize);
1094 error:
1095 kfree(msg);
1096 return ret;
1097 }
1098 EXPORT_SYMBOL_GPL(cros_ec_cmd);
1099
1100 /**
1101 * cros_ec_cmd_readmem - Read from EC memory.
1102 *
1103 * @ec_dev: EC device
1104 * @offset: Is within EC_LPC_ADDR_MEMMAP region.
1105 * @size: Number of bytes to read.
1106 * @dest: EC command output data
1107 *
1108 * Return: >= 0 on success, negative error number on failure.
1109 */
cros_ec_cmd_readmem(struct cros_ec_device * ec_dev,u8 offset,u8 size,void * dest)1110 int cros_ec_cmd_readmem(struct cros_ec_device *ec_dev, u8 offset, u8 size, void *dest)
1111 {
1112 struct ec_params_read_memmap params = {};
1113
1114 if (!size)
1115 return -EINVAL;
1116
1117 if (ec_dev->cmd_readmem)
1118 return ec_dev->cmd_readmem(ec_dev, offset, size, dest);
1119
1120 params.offset = offset;
1121 params.size = size;
1122 return cros_ec_cmd(ec_dev, 0, EC_CMD_READ_MEMMAP,
1123 ¶ms, sizeof(params), dest, size);
1124 }
1125 EXPORT_SYMBOL_GPL(cros_ec_cmd_readmem);
1126
1127 /**
1128 * cros_ec_get_cmd_versions - Get supported version mask.
1129 *
1130 * @ec_dev: EC device
1131 * @cmd: Command to test
1132 *
1133 * Return: version mask on success, negative error number on failure.
1134 */
cros_ec_get_cmd_versions(struct cros_ec_device * ec_dev,u16 cmd)1135 int cros_ec_get_cmd_versions(struct cros_ec_device *ec_dev, u16 cmd)
1136 {
1137 struct ec_params_get_cmd_versions req_v0;
1138 struct ec_params_get_cmd_versions_v1 req_v1;
1139 struct ec_response_get_cmd_versions resp;
1140 int ret;
1141
1142 if (cmd <= U8_MAX) {
1143 req_v0.cmd = cmd;
1144 ret = cros_ec_cmd(ec_dev, 0, EC_CMD_GET_CMD_VERSIONS,
1145 &req_v0, sizeof(req_v0), &resp, sizeof(resp));
1146 } else {
1147 req_v1.cmd = cmd;
1148 ret = cros_ec_cmd(ec_dev, 1, EC_CMD_GET_CMD_VERSIONS,
1149 &req_v1, sizeof(req_v1), &resp, sizeof(resp));
1150 }
1151
1152 if (ret == -EINVAL)
1153 return 0; /* Command not implemented */
1154 else if (ret < 0)
1155 return ret;
1156 else
1157 return resp.version_mask;
1158 }
1159 EXPORT_SYMBOL_GPL(cros_ec_get_cmd_versions);
1160