1 // SPDX-License-Identifier: GPL-2.0
2 // ChromeOS EC communication protocol helper functions
4 // Copyright (C) 2015 Google, Inc
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 <asm/unaligned.h>
15 #include "cros_ec_trace.h"
17 #define EC_COMMAND_RETRIES 50
19 static const int cros_ec_error_map[] = {
20 [EC_RES_INVALID_COMMAND] = -EOPNOTSUPP,
21 [EC_RES_ERROR] = -EIO,
22 [EC_RES_INVALID_PARAM] = -EINVAL,
23 [EC_RES_ACCESS_DENIED] = -EACCES,
24 [EC_RES_INVALID_RESPONSE] = -EPROTO,
25 [EC_RES_INVALID_VERSION] = -ENOPROTOOPT,
26 [EC_RES_INVALID_CHECKSUM] = -EBADMSG,
27 [EC_RES_IN_PROGRESS] = -EINPROGRESS,
28 [EC_RES_UNAVAILABLE] = -ENODATA,
29 [EC_RES_TIMEOUT] = -ETIMEDOUT,
30 [EC_RES_OVERFLOW] = -EOVERFLOW,
31 [EC_RES_INVALID_HEADER] = -EBADR,
32 [EC_RES_REQUEST_TRUNCATED] = -EBADR,
33 [EC_RES_RESPONSE_TOO_BIG] = -EFBIG,
34 [EC_RES_BUS_ERROR] = -EFAULT,
35 [EC_RES_BUSY] = -EBUSY,
36 [EC_RES_INVALID_HEADER_VERSION] = -EBADMSG,
37 [EC_RES_INVALID_HEADER_CRC] = -EBADMSG,
38 [EC_RES_INVALID_DATA_CRC] = -EBADMSG,
39 [EC_RES_DUP_UNAVAILABLE] = -ENODATA,
42 static int cros_ec_map_error(uint32_t result)
46 if (result != EC_RES_SUCCESS) {
47 if (result < ARRAY_SIZE(cros_ec_error_map) && cros_ec_error_map[result])
48 ret = cros_ec_error_map[result];
56 static int prepare_tx(struct cros_ec_device *ec_dev,
57 struct cros_ec_command *msg)
59 struct ec_host_request *request;
64 if (msg->outsize + sizeof(*request) > ec_dev->dout_size)
68 request = (struct ec_host_request *)out;
69 request->struct_version = EC_HOST_REQUEST_VERSION;
70 request->checksum = 0;
71 request->command = msg->command;
72 request->command_version = msg->version;
73 request->reserved = 0;
74 request->data_len = msg->outsize;
76 for (i = 0; i < sizeof(*request); i++)
79 /* Copy data and update checksum */
80 memcpy(out + sizeof(*request), msg->data, msg->outsize);
81 for (i = 0; i < msg->outsize; i++)
84 request->checksum = -csum;
86 return sizeof(*request) + msg->outsize;
89 static int prepare_tx_legacy(struct cros_ec_device *ec_dev,
90 struct cros_ec_command *msg)
96 if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE)
100 out[0] = EC_CMD_VERSION0 + msg->version;
101 out[1] = msg->command;
102 out[2] = msg->outsize;
103 csum = out[0] + out[1] + out[2];
104 for (i = 0; i < msg->outsize; i++)
105 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
106 out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
108 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
111 static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
114 int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
116 if (ec_dev->proto_version > 2)
117 xfer_fxn = ec_dev->pkt_xfer;
119 xfer_fxn = ec_dev->cmd_xfer;
123 * This error can happen if a communication error happened and
124 * the EC is trying to use protocol v2, on an underlying
125 * communication mechanism that does not support v2.
127 dev_err_once(ec_dev->dev, "missing EC transfer API, cannot send command\n");
131 trace_cros_ec_request_start(msg);
132 ret = (*xfer_fxn)(ec_dev, msg);
133 trace_cros_ec_request_done(msg, ret);
138 static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *result)
141 struct cros_ec_command msg;
142 struct ec_response_get_comms_status status;
144 struct cros_ec_command *msg = &buf.msg;
145 struct ec_response_get_comms_status *status = &buf.status;
149 msg->command = EC_CMD_GET_COMMS_STATUS;
150 msg->insize = sizeof(*status);
153 /* Query the EC's status until it's no longer busy or we encounter an error. */
154 for (i = 0; i < EC_COMMAND_RETRIES; ++i) {
155 usleep_range(10000, 11000);
157 ret = cros_ec_xfer_command(ec_dev, msg);
163 *result = msg->result;
164 if (msg->result != EC_RES_SUCCESS)
172 if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
176 if (i >= EC_COMMAND_RETRIES)
182 static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
184 int ret = cros_ec_xfer_command(ec_dev, msg);
186 if (msg->result == EC_RES_IN_PROGRESS)
187 ret = cros_ec_wait_until_complete(ec_dev, &msg->result);
193 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
194 * @ec_dev: Device to register.
195 * @msg: Message to write.
197 * This is used by all ChromeOS EC drivers to prepare the outgoing message
198 * according to different protocol versions.
200 * Return: number of prepared bytes on success or negative error code.
202 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
203 struct cros_ec_command *msg)
205 if (ec_dev->proto_version > 2)
206 return prepare_tx(ec_dev, msg);
208 return prepare_tx_legacy(ec_dev, msg);
210 EXPORT_SYMBOL(cros_ec_prepare_tx);
213 * cros_ec_check_result() - Check ec_msg->result.
214 * @ec_dev: EC device.
215 * @msg: Message to check.
217 * This is used by ChromeOS EC drivers to check the ec_msg->result for
218 * EC_RES_IN_PROGRESS and to warn about them.
220 * The function should not check for furthermore error codes. Otherwise,
221 * it would break the ABI.
223 * Return: -EAGAIN if ec_msg->result == EC_RES_IN_PROGRESS. Otherwise, 0.
225 int cros_ec_check_result(struct cros_ec_device *ec_dev,
226 struct cros_ec_command *msg)
228 switch (msg->result) {
231 case EC_RES_IN_PROGRESS:
232 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
236 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
237 msg->command, msg->result);
241 EXPORT_SYMBOL(cros_ec_check_result);
244 * cros_ec_get_host_event_wake_mask
246 * Get the mask of host events that cause wake from suspend.
248 * @ec_dev: EC device to call
249 * @mask: result when function returns 0.
252 * the caller has ec_dev->lock mutex, or the caller knows there is
253 * no other command in progress.
255 static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev, uint32_t *mask)
257 struct cros_ec_command *msg;
258 struct ec_response_host_event_mask *r;
261 msg = kzalloc(sizeof(*msg) + sizeof(*r), GFP_KERNEL);
265 msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
266 msg->insize = sizeof(*r);
268 ret = cros_ec_send_command(ec_dev, msg);
272 mapped = cros_ec_map_error(msg->result);
283 r = (struct ec_response_host_event_mask *)msg->data;
291 static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx)
293 struct cros_ec_command *msg;
294 struct ec_response_get_protocol_info *info;
297 ec_dev->proto_version = 3;
299 ec_dev->max_passthru = 0;
301 msg = kzalloc(sizeof(*msg) + sizeof(*info), GFP_KERNEL);
305 msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
306 msg->insize = sizeof(*info);
308 ret = cros_ec_send_command(ec_dev, msg);
310 * Send command once again when timeout occurred.
311 * Fingerprint MCU (FPMCU) is restarted during system boot which
312 * introduces small window in which FPMCU won't respond for any
313 * messages sent by kernel. There is no need to wait before next
314 * attempt because we waited at least EC_MSG_DEADLINE_MS.
316 if (ret == -ETIMEDOUT)
317 ret = cros_ec_send_command(ec_dev, msg);
321 "failed to check for EC[%d] protocol version: %d\n",
326 mapped = cros_ec_map_error(msg->result);
337 info = (struct ec_response_get_protocol_info *)msg->data;
340 case CROS_EC_DEV_EC_INDEX:
341 ec_dev->max_request = info->max_request_packet_size -
342 sizeof(struct ec_host_request);
343 ec_dev->max_response = info->max_response_packet_size -
344 sizeof(struct ec_host_response);
345 ec_dev->proto_version = min(EC_HOST_REQUEST_VERSION,
346 fls(info->protocol_versions) - 1);
347 ec_dev->din_size = info->max_response_packet_size + EC_MAX_RESPONSE_OVERHEAD;
348 ec_dev->dout_size = info->max_request_packet_size + EC_MAX_REQUEST_OVERHEAD;
350 dev_dbg(ec_dev->dev, "using proto v%u\n", ec_dev->proto_version);
352 case CROS_EC_DEV_PD_INDEX:
353 ec_dev->max_passthru = info->max_request_packet_size -
354 sizeof(struct ec_host_request);
356 dev_dbg(ec_dev->dev, "found PD chip\n");
359 dev_dbg(ec_dev->dev, "unknown passthru index: %d\n", devidx);
369 static int cros_ec_get_proto_info_legacy(struct cros_ec_device *ec_dev)
371 struct cros_ec_command *msg;
372 struct ec_params_hello *params;
373 struct ec_response_hello *response;
376 ec_dev->proto_version = 2;
378 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)), GFP_KERNEL);
382 msg->command = EC_CMD_HELLO;
383 msg->insize = sizeof(*response);
384 msg->outsize = sizeof(*params);
386 params = (struct ec_params_hello *)msg->data;
387 params->in_data = 0xa0b0c0d0;
389 ret = cros_ec_send_command(ec_dev, msg);
391 dev_dbg(ec_dev->dev, "EC failed to respond to v2 hello: %d\n", ret);
395 mapped = cros_ec_map_error(msg->result);
398 dev_err(ec_dev->dev, "EC responded to v2 hello with error: %d\n", msg->result);
407 response = (struct ec_response_hello *)msg->data;
408 if (response->out_data != 0xa1b2c3d4) {
410 "EC responded to v2 hello with bad result: %u\n",
416 ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
417 ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
418 ec_dev->max_passthru = 0;
419 ec_dev->pkt_xfer = NULL;
420 ec_dev->din_size = EC_PROTO2_MSG_BYTES;
421 ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
423 dev_dbg(ec_dev->dev, "falling back to proto v2\n");
431 * cros_ec_get_host_command_version_mask
433 * Get the version mask of a given command.
435 * @ec_dev: EC device to call
436 * @cmd: command to get the version of.
437 * @mask: result when function returns 0.
439 * @return 0 on success, error code otherwise
442 * the caller has ec_dev->lock mutex or the caller knows there is
443 * no other command in progress.
445 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev, u16 cmd, u32 *mask)
447 struct ec_params_get_cmd_versions *pver;
448 struct ec_response_get_cmd_versions *rver;
449 struct cros_ec_command *msg;
452 msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
458 msg->command = EC_CMD_GET_CMD_VERSIONS;
459 msg->insize = sizeof(*rver);
460 msg->outsize = sizeof(*pver);
462 pver = (struct ec_params_get_cmd_versions *)msg->data;
465 ret = cros_ec_send_command(ec_dev, msg);
469 mapped = cros_ec_map_error(msg->result);
480 rver = (struct ec_response_get_cmd_versions *)msg->data;
481 *mask = rver->version_mask;
489 * cros_ec_query_all() - Query the protocol version supported by the
491 * @ec_dev: Device to register.
493 * Return: 0 on success or negative error code.
495 int cros_ec_query_all(struct cros_ec_device *ec_dev)
497 struct device *dev = ec_dev->dev;
501 /* First try sending with proto v3. */
502 if (!cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX)) {
504 cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_PD_INDEX);
506 /* Try querying with a v2 hello message. */
507 ret = cros_ec_get_proto_info_legacy(ec_dev);
510 * It's possible for a test to occur too early when
511 * the EC isn't listening. If this happens, we'll
512 * test later when the first command is run.
514 ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
515 dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
520 devm_kfree(dev, ec_dev->din);
521 devm_kfree(dev, ec_dev->dout);
523 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
529 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
531 devm_kfree(dev, ec_dev->din);
536 /* Probe if MKBP event is supported */
537 ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_GET_NEXT_EVENT, &ver_mask);
538 if (ret < 0 || ver_mask == 0) {
539 ec_dev->mkbp_event_supported = 0;
541 ec_dev->mkbp_event_supported = fls(ver_mask);
543 dev_dbg(ec_dev->dev, "MKBP support version %u\n", ec_dev->mkbp_event_supported - 1);
546 /* Probe if host sleep v1 is supported for S0ix failure detection. */
547 ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_HOST_SLEEP_EVENT, &ver_mask);
548 ec_dev->host_sleep_v1 = (ret == 0 && (ver_mask & EC_VER_MASK(1)));
550 /* Get host event wake mask. */
551 ret = cros_ec_get_host_event_wake_mask(ec_dev, &ec_dev->host_event_wake_mask);
554 * If the EC doesn't support EC_CMD_HOST_EVENT_GET_WAKE_MASK,
555 * use a reasonable default. Note that we ignore various
556 * battery, AC status, and power-state events, because (a)
557 * those can be quite common (e.g., when sitting at full
558 * charge, on AC) and (b) these are not actionable wake events;
559 * if anything, we'd like to continue suspending (to save
560 * power), not wake up.
562 ec_dev->host_event_wake_mask = U32_MAX &
563 ~(EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED) |
564 EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED) |
565 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW) |
566 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL) |
567 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY) |
568 EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
569 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS));
571 * Old ECs may not support this command. Complain about all
574 if (ret != -EOPNOTSUPP)
576 "failed to retrieve wake mask: %d\n", ret);
584 EXPORT_SYMBOL(cros_ec_query_all);
587 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
588 * @ec_dev: EC device.
589 * @msg: Message to write.
591 * Call this to send a command to the ChromeOS EC. This should be used instead
592 * of calling the EC's cmd_xfer() callback directly. This function does not
593 * convert EC command execution error codes to Linux error codes. Most
594 * in-kernel users will want to use cros_ec_cmd_xfer_status() instead since
595 * that function implements the conversion.
598 * >0 - EC command was executed successfully. The return value is the number
599 * of bytes returned by the EC (excluding the header).
600 * =0 - EC communication was successful. EC command execution results are
601 * reported in msg->result. The result will be EC_RES_SUCCESS if the
602 * command was executed successfully or report an EC command execution
604 * <0 - EC communication error. Return value is the Linux error code.
606 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
610 mutex_lock(&ec_dev->lock);
611 if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
612 ret = cros_ec_query_all(ec_dev);
615 "EC version unknown and query failed; aborting command\n");
616 mutex_unlock(&ec_dev->lock);
621 if (msg->insize > ec_dev->max_response) {
622 dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
623 msg->insize = ec_dev->max_response;
626 if (msg->command < EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX)) {
627 if (msg->outsize > ec_dev->max_request) {
629 "request of size %u is too big (max: %u)\n",
631 ec_dev->max_request);
632 mutex_unlock(&ec_dev->lock);
636 if (msg->outsize > ec_dev->max_passthru) {
638 "passthru rq of size %u is too big (max: %u)\n",
640 ec_dev->max_passthru);
641 mutex_unlock(&ec_dev->lock);
646 ret = cros_ec_send_command(ec_dev, msg);
647 mutex_unlock(&ec_dev->lock);
651 EXPORT_SYMBOL(cros_ec_cmd_xfer);
654 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
655 * @ec_dev: EC device.
656 * @msg: Message to write.
658 * Call this to send a command to the ChromeOS EC. This should be used instead of calling the EC's
659 * cmd_xfer() callback directly. It returns success status only if both the command was transmitted
660 * successfully and the EC replied with success status.
663 * >=0 - The number of bytes transferred.
664 * <0 - Linux error code
666 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
667 struct cros_ec_command *msg)
671 ret = cros_ec_cmd_xfer(ec_dev, msg);
675 mapped = cros_ec_map_error(msg->result);
677 dev_dbg(ec_dev->dev, "Command result (err: %d [%d])\n",
678 msg->result, mapped);
684 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
686 static int get_next_event_xfer(struct cros_ec_device *ec_dev,
687 struct cros_ec_command *msg,
688 struct ec_response_get_next_event_v3 *event,
689 int version, uint32_t size)
693 msg->version = version;
694 msg->command = EC_CMD_GET_NEXT_EVENT;
698 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
700 ec_dev->event_size = ret - 1;
701 ec_dev->event_data = *event;
707 static int get_next_event(struct cros_ec_device *ec_dev)
710 struct cros_ec_command msg;
711 struct ec_response_get_next_event_v3 event;
713 struct cros_ec_command *msg = &buf.msg;
714 struct ec_response_get_next_event_v3 *event = &buf.event;
715 int cmd_version = ec_dev->mkbp_event_supported - 1;
718 memset(msg, 0, sizeof(*msg));
719 if (ec_dev->suspended) {
720 dev_dbg(ec_dev->dev, "Device suspended.\n");
724 if (cmd_version == 0) {
725 size = sizeof(struct ec_response_get_next_event);
726 } else if (cmd_version < 3) {
727 size = sizeof(struct ec_response_get_next_event_v1);
730 * The max version we support is v3. So, we speak v3 even if the
731 * EC says it supports v4+.
734 size = sizeof(struct ec_response_get_next_event_v3);
737 return get_next_event_xfer(ec_dev, msg, event, cmd_version, size);
740 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
742 u8 buffer[sizeof(struct cros_ec_command) +
743 sizeof(ec_dev->event_data.data)];
744 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
747 msg->command = EC_CMD_MKBP_STATE;
748 msg->insize = sizeof(ec_dev->event_data.data);
751 ec_dev->event_size = cros_ec_cmd_xfer_status(ec_dev, msg);
752 ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
753 memcpy(&ec_dev->event_data.data, msg->data,
754 sizeof(ec_dev->event_data.data));
756 return ec_dev->event_size;
760 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
761 * @ec_dev: Device to fetch event from.
762 * @wake_event: Pointer to a bool set to true upon return if the event might be
763 * treated as a wake event. Ignored if null.
764 * @has_more_events: Pointer to bool set to true if more than one event is
766 * Some EC will set this flag to indicate cros_ec_get_next_event()
767 * can be called multiple times in a row.
768 * It is an optimization to prevent issuing a EC command for
769 * nothing or wait for another interrupt from the EC to process
773 * Return: negative error code on errors; 0 for no data; or else number of
774 * bytes received (i.e., an event was retrieved successfully). Event types are
775 * written out to @ec_dev->event_data.event_type on success.
777 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
779 bool *has_more_events)
787 * Default value for wake_event.
788 * Wake up on keyboard event, wake up for spurious interrupt or link
795 * Default value for has_more_events.
796 * EC will raise another interrupt if AP does not process all events
800 *has_more_events = false;
802 if (!ec_dev->mkbp_event_supported)
803 return get_keyboard_state_event(ec_dev);
805 ret = get_next_event(ec_dev);
807 * -ENOPROTOOPT is returned when EC returns EC_RES_INVALID_VERSION.
808 * This can occur when EC based device (e.g. Fingerprint MCU) jumps to
809 * the RO image which doesn't support newer version of the command. In
810 * this case we will attempt to update maximum supported version of the
811 * EC_CMD_GET_NEXT_EVENT.
813 if (ret == -ENOPROTOOPT) {
815 "GET_NEXT_EVENT returned invalid version error.\n");
816 ret = cros_ec_get_host_command_version_mask(ec_dev,
817 EC_CMD_GET_NEXT_EVENT,
819 if (ret < 0 || ver_mask == 0)
821 * Do not change the MKBP supported version if we can't
822 * obtain supported version correctly. Please note that
823 * calling EC_CMD_GET_NEXT_EVENT returned
824 * EC_RES_INVALID_VERSION which means that the command
829 ec_dev->mkbp_event_supported = fls(ver_mask);
830 dev_dbg(ec_dev->dev, "MKBP support version changed to %u\n",
831 ec_dev->mkbp_event_supported - 1);
833 /* Try to get next event with new MKBP support version set. */
834 ret = get_next_event(ec_dev);
841 *has_more_events = ec_dev->event_data.event_type &
842 EC_MKBP_HAS_MORE_EVENTS;
843 ec_dev->event_data.event_type &= EC_MKBP_EVENT_TYPE_MASK;
846 event_type = ec_dev->event_data.event_type;
847 host_event = cros_ec_get_host_event(ec_dev);
850 * Sensor events need to be parsed by the sensor sub-device.
851 * Defer them, and don't report the wakeup here.
853 if (event_type == EC_MKBP_EVENT_SENSOR_FIFO) {
855 } else if (host_event) {
856 /* rtc_update_irq() already handles wakeup events. */
857 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC))
859 /* Masked host-events should not count as wake events. */
860 if (!(host_event & ec_dev->host_event_wake_mask))
867 EXPORT_SYMBOL(cros_ec_get_next_event);
870 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
871 * @ec_dev: Device to fetch event from.
873 * When MKBP is supported, when the EC raises an interrupt, we collect the
874 * events raised and call the functions in the ec notifier. This function
875 * is a helper to know which events are raised.
877 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
879 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
883 if (!ec_dev->mkbp_event_supported)
886 if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
889 if (ec_dev->event_size != sizeof(host_event)) {
890 dev_warn(ec_dev->dev, "Invalid host event size\n");
894 host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
898 EXPORT_SYMBOL(cros_ec_get_host_event);
901 * cros_ec_check_features() - Test for the presence of EC features
903 * @ec: EC device, does not have to be connected directly to the AP,
904 * can be daisy chained through another device.
905 * @feature: One of ec_feature_code bit.
907 * Call this function to test whether the ChromeOS EC supports a feature.
909 * Return: true if supported, false if not (or if an error was encountered).
911 bool cros_ec_check_features(struct cros_ec_dev *ec, int feature)
913 struct ec_response_get_features *features = &ec->features;
916 if (features->flags[0] == -1U && features->flags[1] == -1U) {
917 /* features bitmap not read yet */
918 ret = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_GET_FEATURES + ec->cmd_offset,
919 NULL, 0, features, sizeof(*features));
921 dev_warn(ec->dev, "cannot get EC features: %d\n", ret);
922 memset(features, 0, sizeof(*features));
925 dev_dbg(ec->dev, "EC features %08x %08x\n",
926 features->flags[0], features->flags[1]);
929 return !!(features->flags[feature / 32] & EC_FEATURE_MASK_0(feature));
931 EXPORT_SYMBOL_GPL(cros_ec_check_features);
934 * cros_ec_get_sensor_count() - Return the number of MEMS sensors supported.
936 * @ec: EC device, does not have to be connected directly to the AP,
937 * can be daisy chained through another device.
938 * Return: < 0 in case of error.
940 int cros_ec_get_sensor_count(struct cros_ec_dev *ec)
943 * Issue a command to get the number of sensor reported.
944 * If not supported, check for legacy mode.
946 int ret, sensor_count;
947 struct ec_params_motion_sense *params;
948 struct ec_response_motion_sense *resp;
949 struct cros_ec_command *msg;
950 struct cros_ec_device *ec_dev = ec->ec_dev;
953 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*resp)),
959 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
960 msg->outsize = sizeof(*params);
961 msg->insize = sizeof(*resp);
963 params = (struct ec_params_motion_sense *)msg->data;
964 params->cmd = MOTIONSENSE_CMD_DUMP;
966 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
970 resp = (struct ec_response_motion_sense *)msg->data;
971 sensor_count = resp->dump.sensor_count;
976 * Check legacy mode: Let's find out if sensors are accessible
979 if (sensor_count < 0 && ec->cmd_offset == 0 && ec_dev->cmd_readmem) {
980 ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS,
983 (status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
985 * We have 2 sensors, one in the lid, one in the base.
990 * EC uses LPC interface and no sensors are presented.
997 EXPORT_SYMBOL_GPL(cros_ec_get_sensor_count);
1000 * cros_ec_cmd - Send a command to the EC.
1002 * @ec_dev: EC device
1003 * @version: EC command version
1004 * @command: EC command
1005 * @outdata: EC command output data
1006 * @outsize: Size of outdata
1007 * @indata: EC command input data
1008 * @insize: Size of indata
1010 * Return: >= 0 on success, negative error number on failure.
1012 int cros_ec_cmd(struct cros_ec_device *ec_dev,
1013 unsigned int version,
1015 const void *outdata,
1020 struct cros_ec_command *msg;
1023 msg = kzalloc(sizeof(*msg) + max(insize, outsize), GFP_KERNEL);
1027 msg->version = version;
1028 msg->command = command;
1029 msg->outsize = outsize;
1030 msg->insize = insize;
1033 memcpy(msg->data, outdata, outsize);
1035 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
1040 memcpy(indata, msg->data, insize);
1045 EXPORT_SYMBOL_GPL(cros_ec_cmd);
1048 * cros_ec_cmd_readmem - Read from EC memory.
1050 * @ec_dev: EC device
1051 * @offset: Is within EC_LPC_ADDR_MEMMAP region.
1052 * @size: Number of bytes to read.
1053 * @dest: EC command output data
1055 * Return: >= 0 on success, negative error number on failure.
1057 int cros_ec_cmd_readmem(struct cros_ec_device *ec_dev, u8 offset, u8 size, void *dest)
1059 struct ec_params_read_memmap params = {};
1064 if (ec_dev->cmd_readmem)
1065 return ec_dev->cmd_readmem(ec_dev, offset, size, dest);
1067 params.offset = offset;
1069 return cros_ec_cmd(ec_dev, 0, EC_CMD_READ_MEMMAP,
1070 ¶ms, sizeof(params), dest, size);
1072 EXPORT_SYMBOL_GPL(cros_ec_cmd_readmem);
1075 * cros_ec_get_cmd_versions - Get supported version mask.
1077 * @ec_dev: EC device
1078 * @cmd: Command to test
1080 * Return: version mask on success, negative error number on failure.
1082 int cros_ec_get_cmd_versions(struct cros_ec_device *ec_dev, u16 cmd)
1084 struct ec_params_get_cmd_versions req_v0;
1085 struct ec_params_get_cmd_versions_v1 req_v1;
1086 struct ec_response_get_cmd_versions resp;
1089 if (cmd <= U8_MAX) {
1091 ret = cros_ec_cmd(ec_dev, 0, EC_CMD_GET_CMD_VERSIONS,
1092 &req_v0, sizeof(req_v0), &resp, sizeof(resp));
1095 ret = cros_ec_cmd(ec_dev, 1, EC_CMD_GET_CMD_VERSIONS,
1096 &req_v1, sizeof(req_v1), &resp, sizeof(resp));
1100 return 0; /* Command not implemented */
1104 return resp.version_mask;
1106 EXPORT_SYMBOL_GPL(cros_ec_get_cmd_versions);