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/module.h>
9 #include <linux/platform_data/cros_ec_commands.h>
10 #include <linux/platform_data/cros_ec_proto.h>
11 #include <linux/slab.h>
12 #include <asm/unaligned.h>
14 #include "cros_ec_trace.h"
16 #define EC_COMMAND_RETRIES 50
18 static const int cros_ec_error_map[] = {
19 [EC_RES_INVALID_COMMAND] = -EOPNOTSUPP,
20 [EC_RES_ERROR] = -EIO,
21 [EC_RES_INVALID_PARAM] = -EINVAL,
22 [EC_RES_ACCESS_DENIED] = -EACCES,
23 [EC_RES_INVALID_RESPONSE] = -EPROTO,
24 [EC_RES_INVALID_VERSION] = -ENOPROTOOPT,
25 [EC_RES_INVALID_CHECKSUM] = -EBADMSG,
26 [EC_RES_IN_PROGRESS] = -EINPROGRESS,
27 [EC_RES_UNAVAILABLE] = -ENODATA,
28 [EC_RES_TIMEOUT] = -ETIMEDOUT,
29 [EC_RES_OVERFLOW] = -EOVERFLOW,
30 [EC_RES_INVALID_HEADER] = -EBADR,
31 [EC_RES_REQUEST_TRUNCATED] = -EBADR,
32 [EC_RES_RESPONSE_TOO_BIG] = -EFBIG,
33 [EC_RES_BUS_ERROR] = -EFAULT,
34 [EC_RES_BUSY] = -EBUSY,
35 [EC_RES_INVALID_HEADER_VERSION] = -EBADMSG,
36 [EC_RES_INVALID_HEADER_CRC] = -EBADMSG,
37 [EC_RES_INVALID_DATA_CRC] = -EBADMSG,
38 [EC_RES_DUP_UNAVAILABLE] = -ENODATA,
41 static int cros_ec_map_error(uint32_t result)
45 if (result != EC_RES_SUCCESS) {
46 if (result < ARRAY_SIZE(cros_ec_error_map) && cros_ec_error_map[result])
47 ret = cros_ec_error_map[result];
55 static int prepare_packet(struct cros_ec_device *ec_dev,
56 struct cros_ec_command *msg)
58 struct ec_host_request *request;
63 BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
66 request = (struct ec_host_request *)out;
67 request->struct_version = EC_HOST_REQUEST_VERSION;
68 request->checksum = 0;
69 request->command = msg->command;
70 request->command_version = msg->version;
71 request->reserved = 0;
72 request->data_len = msg->outsize;
74 for (i = 0; i < sizeof(*request); i++)
77 /* Copy data and update checksum */
78 memcpy(out + sizeof(*request), msg->data, msg->outsize);
79 for (i = 0; i < msg->outsize; i++)
82 request->checksum = -csum;
84 return sizeof(*request) + msg->outsize;
87 static int send_command(struct cros_ec_device *ec_dev,
88 struct cros_ec_command *msg)
91 int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
93 if (ec_dev->proto_version > 2)
94 xfer_fxn = ec_dev->pkt_xfer;
96 xfer_fxn = ec_dev->cmd_xfer;
100 * This error can happen if a communication error happened and
101 * the EC is trying to use protocol v2, on an underlying
102 * communication mechanism that does not support v2.
104 dev_err_once(ec_dev->dev,
105 "missing EC transfer API, cannot send command\n");
109 trace_cros_ec_request_start(msg);
110 ret = (*xfer_fxn)(ec_dev, msg);
111 trace_cros_ec_request_done(msg, ret);
112 if (msg->result == EC_RES_IN_PROGRESS) {
114 struct cros_ec_command *status_msg;
115 struct ec_response_get_comms_status *status;
117 status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
122 status_msg->version = 0;
123 status_msg->command = EC_CMD_GET_COMMS_STATUS;
124 status_msg->insize = sizeof(*status);
125 status_msg->outsize = 0;
128 * Query the EC's status until it's no longer busy or
129 * we encounter an error.
131 for (i = 0; i < EC_COMMAND_RETRIES; i++) {
132 usleep_range(10000, 11000);
134 trace_cros_ec_request_start(status_msg);
135 ret = (*xfer_fxn)(ec_dev, status_msg);
136 trace_cros_ec_request_done(status_msg, ret);
142 msg->result = status_msg->result;
143 if (status_msg->result != EC_RES_SUCCESS)
146 status = (struct ec_response_get_comms_status *)
148 if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
159 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
160 * @ec_dev: Device to register.
161 * @msg: Message to write.
163 * This is intended to be used by all ChromeOS EC drivers, but at present
164 * only SPI uses it. Once LPC uses the same protocol it can start using it.
165 * I2C could use it now, with a refactor of the existing code.
167 * Return: 0 on success or negative error code.
169 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
170 struct cros_ec_command *msg)
176 if (ec_dev->proto_version > 2)
177 return prepare_packet(ec_dev, msg);
179 BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
181 out[0] = EC_CMD_VERSION0 + msg->version;
182 out[1] = msg->command;
183 out[2] = msg->outsize;
184 csum = out[0] + out[1] + out[2];
185 for (i = 0; i < msg->outsize; i++)
186 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
187 out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
189 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
191 EXPORT_SYMBOL(cros_ec_prepare_tx);
194 * cros_ec_check_result() - Check ec_msg->result.
195 * @ec_dev: EC device.
196 * @msg: Message to check.
198 * This is used by ChromeOS EC drivers to check the ec_msg->result for
199 * errors and to warn about them.
201 * Return: 0 on success or negative error code.
203 int cros_ec_check_result(struct cros_ec_device *ec_dev,
204 struct cros_ec_command *msg)
206 switch (msg->result) {
209 case EC_RES_IN_PROGRESS:
210 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
214 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
215 msg->command, msg->result);
219 EXPORT_SYMBOL(cros_ec_check_result);
222 * cros_ec_get_host_event_wake_mask
224 * Get the mask of host events that cause wake from suspend.
226 * @ec_dev: EC device to call
227 * @msg: message structure to use
228 * @mask: result when function returns >=0.
231 * the caller has ec_dev->lock mutex, or the caller knows there is
232 * no other command in progress.
234 static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
235 struct cros_ec_command *msg,
238 struct ec_response_host_event_mask *r;
241 msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
244 msg->insize = sizeof(*r);
246 ret = send_command(ec_dev, msg);
248 if (msg->result == EC_RES_INVALID_COMMAND)
250 if (msg->result != EC_RES_SUCCESS)
254 r = (struct ec_response_host_event_mask *)msg->data;
261 static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
263 struct cros_ec_command *msg)
266 * Try using v3+ to query for supported protocols. If this
267 * command fails, fall back to v2. Returns the highest protocol
268 * supported by the EC.
269 * Also sets the max request/response/passthru size.
273 if (!ec_dev->pkt_xfer)
274 return -EPROTONOSUPPORT;
276 memset(msg, 0, sizeof(*msg));
277 msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
278 msg->insize = sizeof(struct ec_response_get_protocol_info);
280 ret = send_command(ec_dev, msg);
282 * Send command once again when timeout occurred.
283 * Fingerprint MCU (FPMCU) is restarted during system boot which
284 * introduces small window in which FPMCU won't respond for any
285 * messages sent by kernel. There is no need to wait before next
286 * attempt because we waited at least EC_MSG_DEADLINE_MS.
288 if (ret == -ETIMEDOUT)
289 ret = send_command(ec_dev, msg);
293 "failed to check for EC[%d] protocol version: %d\n",
298 if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
300 else if (msg->result != EC_RES_SUCCESS)
306 static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
308 struct cros_ec_command *msg;
309 struct ec_params_hello *hello_params;
310 struct ec_response_hello *hello_response;
312 int len = max(sizeof(*hello_params), sizeof(*hello_response));
314 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
319 msg->command = EC_CMD_HELLO;
320 hello_params = (struct ec_params_hello *)msg->data;
321 msg->outsize = sizeof(*hello_params);
322 hello_response = (struct ec_response_hello *)msg->data;
323 msg->insize = sizeof(*hello_response);
325 hello_params->in_data = 0xa0b0c0d0;
327 ret = send_command(ec_dev, msg);
331 "EC failed to respond to v2 hello: %d\n",
334 } else if (msg->result != EC_RES_SUCCESS) {
336 "EC responded to v2 hello with error: %d\n",
340 } else if (hello_response->out_data != 0xa1b2c3d4) {
342 "EC responded to v2 hello with bad result: %u\n",
343 hello_response->out_data);
356 * cros_ec_get_host_command_version_mask
358 * Get the version mask of a given command.
360 * @ec_dev: EC device to call
361 * @msg: message structure to use
362 * @cmd: command to get the version of.
363 * @mask: result when function returns 0.
365 * @return 0 on success, error code otherwise
368 * the caller has ec_dev->lock mutex or the caller knows there is
369 * no other command in progress.
371 static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
374 struct ec_params_get_cmd_versions *pver;
375 struct ec_response_get_cmd_versions *rver;
376 struct cros_ec_command *msg;
379 msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
385 msg->command = EC_CMD_GET_CMD_VERSIONS;
386 msg->insize = sizeof(*rver);
387 msg->outsize = sizeof(*pver);
389 pver = (struct ec_params_get_cmd_versions *)msg->data;
392 ret = send_command(ec_dev, msg);
394 rver = (struct ec_response_get_cmd_versions *)msg->data;
395 *mask = rver->version_mask;
404 * cros_ec_query_all() - Query the protocol version supported by the
406 * @ec_dev: Device to register.
408 * Return: 0 on success or negative error code.
410 int cros_ec_query_all(struct cros_ec_device *ec_dev)
412 struct device *dev = ec_dev->dev;
413 struct cros_ec_command *proto_msg;
414 struct ec_response_get_protocol_info *proto_info;
418 proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
423 /* First try sending with proto v3. */
424 ec_dev->proto_version = 3;
425 ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
428 proto_info = (struct ec_response_get_protocol_info *)
430 ec_dev->max_request = proto_info->max_request_packet_size -
431 sizeof(struct ec_host_request);
432 ec_dev->max_response = proto_info->max_response_packet_size -
433 sizeof(struct ec_host_response);
434 ec_dev->proto_version =
435 min(EC_HOST_REQUEST_VERSION,
436 fls(proto_info->protocol_versions) - 1);
439 ec_dev->proto_version);
441 ec_dev->din_size = ec_dev->max_response +
442 sizeof(struct ec_host_response) +
443 EC_MAX_RESPONSE_OVERHEAD;
444 ec_dev->dout_size = ec_dev->max_request +
445 sizeof(struct ec_host_request) +
446 EC_MAX_REQUEST_OVERHEAD;
451 ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
454 dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
455 ec_dev->max_passthru = 0;
457 dev_dbg(ec_dev->dev, "found PD chip\n");
458 ec_dev->max_passthru =
459 proto_info->max_request_packet_size -
460 sizeof(struct ec_host_request);
463 /* Try querying with a v2 hello message. */
464 ec_dev->proto_version = 2;
465 ret = cros_ec_host_command_proto_query_v2(ec_dev);
468 /* V2 hello succeeded. */
469 dev_dbg(ec_dev->dev, "falling back to proto v2\n");
471 ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
472 ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
473 ec_dev->max_passthru = 0;
474 ec_dev->pkt_xfer = NULL;
475 ec_dev->din_size = EC_PROTO2_MSG_BYTES;
476 ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
479 * It's possible for a test to occur too early when
480 * the EC isn't listening. If this happens, we'll
481 * test later when the first command is run.
483 ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
484 dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
489 devm_kfree(dev, ec_dev->din);
490 devm_kfree(dev, ec_dev->dout);
492 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
498 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
500 devm_kfree(dev, ec_dev->din);
505 /* Probe if MKBP event is supported */
506 ret = cros_ec_get_host_command_version_mask(ec_dev,
507 EC_CMD_GET_NEXT_EVENT,
509 if (ret < 0 || ver_mask == 0)
510 ec_dev->mkbp_event_supported = 0;
512 ec_dev->mkbp_event_supported = fls(ver_mask);
514 dev_dbg(ec_dev->dev, "MKBP support version %u\n",
515 ec_dev->mkbp_event_supported - 1);
517 /* Probe if host sleep v1 is supported for S0ix failure detection. */
518 ret = cros_ec_get_host_command_version_mask(ec_dev,
519 EC_CMD_HOST_SLEEP_EVENT,
521 ec_dev->host_sleep_v1 = (ret >= 0 && (ver_mask & EC_VER_MASK(1)));
523 /* Get host event wake mask. */
524 ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
525 &ec_dev->host_event_wake_mask);
528 * If the EC doesn't support EC_CMD_HOST_EVENT_GET_WAKE_MASK,
529 * use a reasonable default. Note that we ignore various
530 * battery, AC status, and power-state events, because (a)
531 * those can be quite common (e.g., when sitting at full
532 * charge, on AC) and (b) these are not actionable wake events;
533 * if anything, we'd like to continue suspending (to save
534 * power), not wake up.
536 ec_dev->host_event_wake_mask = U32_MAX &
537 ~(EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED) |
538 EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED) |
539 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW) |
540 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL) |
541 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY) |
542 EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
543 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS));
545 * Old ECs may not support this command. Complain about all
548 if (ret != -EOPNOTSUPP)
550 "failed to retrieve wake mask: %d\n", ret);
559 EXPORT_SYMBOL(cros_ec_query_all);
562 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
563 * @ec_dev: EC device.
564 * @msg: Message to write.
566 * Call this to send a command to the ChromeOS EC. This should be used instead
567 * of calling the EC's cmd_xfer() callback directly. This function does not
568 * convert EC command execution error codes to Linux error codes. Most
569 * in-kernel users will want to use cros_ec_cmd_xfer_status() instead since
570 * that function implements the conversion.
573 * >0 - EC command was executed successfully. The return value is the number
574 * of bytes returned by the EC (excluding the header).
575 * =0 - EC communication was successful. EC command execution results are
576 * reported in msg->result. The result will be EC_RES_SUCCESS if the
577 * command was executed successfully or report an EC command execution
579 * <0 - EC communication error. Return value is the Linux error code.
581 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
585 mutex_lock(&ec_dev->lock);
586 if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
587 ret = cros_ec_query_all(ec_dev);
590 "EC version unknown and query failed; aborting command\n");
591 mutex_unlock(&ec_dev->lock);
596 if (msg->insize > ec_dev->max_response) {
597 dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
598 msg->insize = ec_dev->max_response;
601 if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
602 if (msg->outsize > ec_dev->max_request) {
604 "request of size %u is too big (max: %u)\n",
606 ec_dev->max_request);
607 mutex_unlock(&ec_dev->lock);
611 if (msg->outsize > ec_dev->max_passthru) {
613 "passthru rq of size %u is too big (max: %u)\n",
615 ec_dev->max_passthru);
616 mutex_unlock(&ec_dev->lock);
621 ret = send_command(ec_dev, msg);
622 mutex_unlock(&ec_dev->lock);
626 EXPORT_SYMBOL(cros_ec_cmd_xfer);
629 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
630 * @ec_dev: EC device.
631 * @msg: Message to write.
633 * Call this to send a command to the ChromeOS EC. This should be used instead of calling the EC's
634 * cmd_xfer() callback directly. It returns success status only if both the command was transmitted
635 * successfully and the EC replied with success status.
638 * >=0 - The number of bytes transferred.
639 * <0 - Linux error code
641 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
642 struct cros_ec_command *msg)
646 ret = cros_ec_cmd_xfer(ec_dev, msg);
650 mapped = cros_ec_map_error(msg->result);
652 dev_dbg(ec_dev->dev, "Command result (err: %d [%d])\n",
653 msg->result, mapped);
659 EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
661 static int get_next_event_xfer(struct cros_ec_device *ec_dev,
662 struct cros_ec_command *msg,
663 struct ec_response_get_next_event_v1 *event,
664 int version, uint32_t size)
668 msg->version = version;
669 msg->command = EC_CMD_GET_NEXT_EVENT;
673 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
675 ec_dev->event_size = ret - 1;
676 ec_dev->event_data = *event;
682 static int get_next_event(struct cros_ec_device *ec_dev)
685 struct cros_ec_command msg;
686 struct ec_response_get_next_event_v1 event;
688 struct cros_ec_command *msg = &buf.msg;
689 struct ec_response_get_next_event_v1 *event = &buf.event;
690 const int cmd_version = ec_dev->mkbp_event_supported - 1;
692 memset(msg, 0, sizeof(*msg));
693 if (ec_dev->suspended) {
694 dev_dbg(ec_dev->dev, "Device suspended.\n");
698 if (cmd_version == 0)
699 return get_next_event_xfer(ec_dev, msg, event, 0,
700 sizeof(struct ec_response_get_next_event));
702 return get_next_event_xfer(ec_dev, msg, event, cmd_version,
703 sizeof(struct ec_response_get_next_event_v1));
706 static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
708 u8 buffer[sizeof(struct cros_ec_command) +
709 sizeof(ec_dev->event_data.data)];
710 struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
713 msg->command = EC_CMD_MKBP_STATE;
714 msg->insize = sizeof(ec_dev->event_data.data);
717 ec_dev->event_size = cros_ec_cmd_xfer_status(ec_dev, msg);
718 ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
719 memcpy(&ec_dev->event_data.data, msg->data,
720 sizeof(ec_dev->event_data.data));
722 return ec_dev->event_size;
726 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
727 * @ec_dev: Device to fetch event from.
728 * @wake_event: Pointer to a bool set to true upon return if the event might be
729 * treated as a wake event. Ignored if null.
730 * @has_more_events: Pointer to bool set to true if more than one event is
732 * Some EC will set this flag to indicate cros_ec_get_next_event()
733 * can be called multiple times in a row.
734 * It is an optimization to prevent issuing a EC command for
735 * nothing or wait for another interrupt from the EC to process
739 * Return: negative error code on errors; 0 for no data; or else number of
740 * bytes received (i.e., an event was retrieved successfully). Event types are
741 * written out to @ec_dev->event_data.event_type on success.
743 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
745 bool *has_more_events)
752 * Default value for wake_event.
753 * Wake up on keyboard event, wake up for spurious interrupt or link
760 * Default value for has_more_events.
761 * EC will raise another interrupt if AP does not process all events
765 *has_more_events = false;
767 if (!ec_dev->mkbp_event_supported)
768 return get_keyboard_state_event(ec_dev);
770 ret = get_next_event(ec_dev);
775 *has_more_events = ec_dev->event_data.event_type &
776 EC_MKBP_HAS_MORE_EVENTS;
777 ec_dev->event_data.event_type &= EC_MKBP_EVENT_TYPE_MASK;
780 event_type = ec_dev->event_data.event_type;
781 host_event = cros_ec_get_host_event(ec_dev);
784 * Sensor events need to be parsed by the sensor sub-device.
785 * Defer them, and don't report the wakeup here.
787 if (event_type == EC_MKBP_EVENT_SENSOR_FIFO) {
789 } else if (host_event) {
790 /* rtc_update_irq() already handles wakeup events. */
791 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC))
793 /* Masked host-events should not count as wake events. */
794 if (!(host_event & ec_dev->host_event_wake_mask))
801 EXPORT_SYMBOL(cros_ec_get_next_event);
804 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
805 * @ec_dev: Device to fetch event from.
807 * When MKBP is supported, when the EC raises an interrupt, we collect the
808 * events raised and call the functions in the ec notifier. This function
809 * is a helper to know which events are raised.
811 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
813 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
817 BUG_ON(!ec_dev->mkbp_event_supported);
819 if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
822 if (ec_dev->event_size != sizeof(host_event)) {
823 dev_warn(ec_dev->dev, "Invalid host event size\n");
827 host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
831 EXPORT_SYMBOL(cros_ec_get_host_event);
834 * cros_ec_check_features() - Test for the presence of EC features
836 * @ec: EC device, does not have to be connected directly to the AP,
837 * can be daisy chained through another device.
838 * @feature: One of ec_feature_code bit.
840 * Call this function to test whether the ChromeOS EC supports a feature.
842 * Return: true if supported, false if not (or if an error was encountered).
844 bool cros_ec_check_features(struct cros_ec_dev *ec, int feature)
846 struct ec_response_get_features *features = &ec->features;
849 if (features->flags[0] == -1U && features->flags[1] == -1U) {
850 /* features bitmap not read yet */
851 ret = cros_ec_command(ec->ec_dev, 0, EC_CMD_GET_FEATURES + ec->cmd_offset,
852 NULL, 0, features, sizeof(*features));
854 dev_warn(ec->dev, "cannot get EC features: %d\n", ret);
855 memset(features, 0, sizeof(*features));
858 dev_dbg(ec->dev, "EC features %08x %08x\n",
859 features->flags[0], features->flags[1]);
862 return !!(features->flags[feature / 32] & EC_FEATURE_MASK_0(feature));
864 EXPORT_SYMBOL_GPL(cros_ec_check_features);
867 * cros_ec_get_sensor_count() - Return the number of MEMS sensors supported.
869 * @ec: EC device, does not have to be connected directly to the AP,
870 * can be daisy chained through another device.
871 * Return: < 0 in case of error.
873 int cros_ec_get_sensor_count(struct cros_ec_dev *ec)
876 * Issue a command to get the number of sensor reported.
877 * If not supported, check for legacy mode.
879 int ret, sensor_count;
880 struct ec_params_motion_sense *params;
881 struct ec_response_motion_sense *resp;
882 struct cros_ec_command *msg;
883 struct cros_ec_device *ec_dev = ec->ec_dev;
886 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*resp)),
892 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
893 msg->outsize = sizeof(*params);
894 msg->insize = sizeof(*resp);
896 params = (struct ec_params_motion_sense *)msg->data;
897 params->cmd = MOTIONSENSE_CMD_DUMP;
899 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
903 resp = (struct ec_response_motion_sense *)msg->data;
904 sensor_count = resp->dump.sensor_count;
909 * Check legacy mode: Let's find out if sensors are accessible
912 if (sensor_count < 0 && ec->cmd_offset == 0 && ec_dev->cmd_readmem) {
913 ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS,
916 (status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
918 * We have 2 sensors, one in the lid, one in the base.
923 * EC uses LPC interface and no sensors are presented.
930 EXPORT_SYMBOL_GPL(cros_ec_get_sensor_count);
933 * cros_ec_command - Send a command to the EC.
936 * @version: EC command version
937 * @command: EC command
938 * @outdata: EC command output data
939 * @outsize: Size of outdata
940 * @indata: EC command input data
941 * @insize: Size of indata
943 * Return: >= 0 on success, negative error number on failure.
945 int cros_ec_command(struct cros_ec_device *ec_dev,
946 unsigned int version,
953 struct cros_ec_command *msg;
956 msg = kzalloc(sizeof(*msg) + max(insize, outsize), GFP_KERNEL);
960 msg->version = version;
961 msg->command = command;
962 msg->outsize = outsize;
963 msg->insize = insize;
966 memcpy(msg->data, outdata, outsize);
968 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
973 memcpy(indata, msg->data, insize);
978 EXPORT_SYMBOL_GPL(cros_ec_command);