2019-01-29 17:38:19 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
// ChromeOS EC communication protocol helper functions
|
|
|
|
//
|
|
|
|
// Copyright (C) 2015 Google, Inc
|
2015-06-09 19:04:44 +08:00
|
|
|
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/module.h>
|
2019-09-02 17:53:05 +08:00
|
|
|
#include <linux/platform_data/cros_ec_commands.h>
|
|
|
|
#include <linux/platform_data/cros_ec_proto.h>
|
2015-06-09 19:04:44 +08:00
|
|
|
#include <linux/slab.h>
|
2016-08-11 01:05:24 +08:00
|
|
|
#include <asm/unaligned.h>
|
2015-06-09 19:04:44 +08:00
|
|
|
|
2019-04-17 05:03:28 +08:00
|
|
|
#include "cros_ec_trace.h"
|
|
|
|
|
2015-06-09 19:04:44 +08:00
|
|
|
#define EC_COMMAND_RETRIES 50
|
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
static int prepare_packet(struct cros_ec_device *ec_dev,
|
|
|
|
struct cros_ec_command *msg)
|
|
|
|
{
|
|
|
|
struct ec_host_request *request;
|
|
|
|
u8 *out;
|
|
|
|
int i;
|
|
|
|
u8 csum = 0;
|
|
|
|
|
|
|
|
BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
|
|
|
|
BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
|
|
|
|
|
|
|
|
out = ec_dev->dout;
|
|
|
|
request = (struct ec_host_request *)out;
|
|
|
|
request->struct_version = EC_HOST_REQUEST_VERSION;
|
|
|
|
request->checksum = 0;
|
|
|
|
request->command = msg->command;
|
|
|
|
request->command_version = msg->version;
|
|
|
|
request->reserved = 0;
|
|
|
|
request->data_len = msg->outsize;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(*request); i++)
|
|
|
|
csum += out[i];
|
|
|
|
|
|
|
|
/* Copy data and update checksum */
|
|
|
|
memcpy(out + sizeof(*request), msg->data, msg->outsize);
|
|
|
|
for (i = 0; i < msg->outsize; i++)
|
|
|
|
csum += msg->data[i];
|
|
|
|
|
|
|
|
request->checksum = -csum;
|
|
|
|
|
|
|
|
return sizeof(*request) + msg->outsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int send_command(struct cros_ec_device *ec_dev,
|
|
|
|
struct cros_ec_command *msg)
|
|
|
|
{
|
|
|
|
int ret;
|
2017-09-09 04:50:11 +08:00
|
|
|
int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
|
2015-06-09 19:04:45 +08:00
|
|
|
|
2019-04-17 05:03:28 +08:00
|
|
|
trace_cros_ec_cmd(msg);
|
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
if (ec_dev->proto_version > 2)
|
2017-09-09 04:50:11 +08:00
|
|
|
xfer_fxn = ec_dev->pkt_xfer;
|
2015-06-09 19:04:45 +08:00
|
|
|
else
|
2017-09-09 04:50:11 +08:00
|
|
|
xfer_fxn = ec_dev->cmd_xfer;
|
2015-06-09 19:04:45 +08:00
|
|
|
|
2019-04-04 06:40:36 +08:00
|
|
|
if (!xfer_fxn) {
|
|
|
|
/*
|
|
|
|
* This error can happen if a communication error happened and
|
|
|
|
* the EC is trying to use protocol v2, on an underlying
|
|
|
|
* communication mechanism that does not support v2.
|
|
|
|
*/
|
|
|
|
dev_err_once(ec_dev->dev,
|
|
|
|
"missing EC transfer API, cannot send command\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2017-09-09 04:50:11 +08:00
|
|
|
ret = (*xfer_fxn)(ec_dev, msg);
|
2015-06-09 19:04:45 +08:00
|
|
|
if (msg->result == EC_RES_IN_PROGRESS) {
|
|
|
|
int i;
|
|
|
|
struct cros_ec_command *status_msg;
|
|
|
|
struct ec_response_get_comms_status *status;
|
|
|
|
|
|
|
|
status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!status_msg)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
status_msg->version = 0;
|
|
|
|
status_msg->command = EC_CMD_GET_COMMS_STATUS;
|
|
|
|
status_msg->insize = sizeof(*status);
|
|
|
|
status_msg->outsize = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Query the EC's status until it's no longer busy or
|
|
|
|
* we encounter an error.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < EC_COMMAND_RETRIES; i++) {
|
|
|
|
usleep_range(10000, 11000);
|
|
|
|
|
2017-09-09 04:50:11 +08:00
|
|
|
ret = (*xfer_fxn)(ec_dev, status_msg);
|
mfd: cros_ec: Retry commands when EC is known to be busy
Commit 001dde9400d5 ("mfd: cros ec: spi: Fix "in progress" error
signaling") pointed out some bad code, but its analysis and conclusion
was not 100% correct.
It *is* correct that we should not propagate result==EC_RES_IN_PROGRESS
for transport errors, because this has a special meaning -- that we
should follow up with EC_CMD_GET_COMMS_STATUS until the EC is no longer
busy. This is definitely the wrong thing for many commands, because
among other problems, EC_CMD_GET_COMMS_STATUS doesn't actually retrieve
any RX data from the EC, so commands that expected some data back will
instead start processing junk.
For such commands, the right answer is to either propagate the error
(and return that error to the caller) or resend the original command
(*not* EC_CMD_GET_COMMS_STATUS).
Unfortunately, commit 001dde9400d5 forgets a crucial point: that for
some long-running operations, the EC physically cannot respond to
commands any more. For example, with EC_CMD_FLASH_ERASE, the EC may be
re-flashing its own code regions, so it can't respond to SPI interrupts.
Instead, the EC prepares us ahead of time for being busy for a "long"
time, and fills its hardware buffer with EC_SPI_PAST_END. Thus, we
expect to see several "transport" errors (or, messages filled with
EC_SPI_PAST_END). So we should really translate that to a retryable
error (-EAGAIN) and continue sending EC_CMD_GET_COMMS_STATUS until we
get a ready status.
IOW, it is actually important to treat some of these "junk" values as
retryable errors.
Together with commit 001dde9400d5, this resolves bugs like the
following:
1. EC_CMD_FLASH_ERASE now works again (with commit 001dde9400d5, we
would abort the first time we saw EC_SPI_PAST_END)
2. Before commit 001dde9400d5, transport errors (e.g.,
EC_SPI_RX_BAD_DATA) seen in other commands (e.g.,
EC_CMD_RTC_GET_VALUE) used to yield junk data in the RX buffer; they
will now yield -EAGAIN return values, and tools like 'hwclock' will
simply fail instead of retrieving and re-programming undefined time
values
Fixes: 001dde9400d5 ("mfd: cros ec: spi: Fix "in progress" error signaling")
Signed-off-by: Brian Norris <briannorris@chromium.org>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
2018-05-23 08:23:10 +08:00
|
|
|
if (ret == -EAGAIN)
|
|
|
|
continue;
|
2015-06-09 19:04:45 +08:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
msg->result = status_msg->result;
|
|
|
|
if (status_msg->result != EC_RES_SUCCESS)
|
|
|
|
break;
|
|
|
|
|
|
|
|
status = (struct ec_response_get_comms_status *)
|
|
|
|
status_msg->data;
|
|
|
|
if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(status_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:04:44 +08:00
|
|
|
int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
|
|
|
|
struct cros_ec_command *msg)
|
|
|
|
{
|
2015-06-09 19:04:45 +08:00
|
|
|
u8 *out;
|
|
|
|
u8 csum;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (ec_dev->proto_version > 2)
|
|
|
|
return prepare_packet(ec_dev, msg);
|
2015-06-09 19:04:44 +08:00
|
|
|
|
|
|
|
BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
|
|
|
|
out = ec_dev->dout;
|
|
|
|
out[0] = EC_CMD_VERSION0 + msg->version;
|
|
|
|
out[1] = msg->command;
|
|
|
|
out[2] = msg->outsize;
|
|
|
|
csum = out[0] + out[1] + out[2];
|
|
|
|
for (i = 0; i < msg->outsize; i++)
|
|
|
|
csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
|
2015-06-09 19:04:45 +08:00
|
|
|
out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
|
2015-06-09 19:04:44 +08:00
|
|
|
|
|
|
|
return EC_MSG_TX_PROTO_BYTES + msg->outsize;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(cros_ec_prepare_tx);
|
|
|
|
|
|
|
|
int cros_ec_check_result(struct cros_ec_device *ec_dev,
|
|
|
|
struct cros_ec_command *msg)
|
|
|
|
{
|
|
|
|
switch (msg->result) {
|
|
|
|
case EC_RES_SUCCESS:
|
|
|
|
return 0;
|
|
|
|
case EC_RES_IN_PROGRESS:
|
|
|
|
dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
|
|
|
|
msg->command);
|
|
|
|
return -EAGAIN;
|
|
|
|
default:
|
|
|
|
dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
|
|
|
|
msg->command, msg->result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(cros_ec_check_result);
|
|
|
|
|
2017-02-15 03:58:02 +08:00
|
|
|
/*
|
|
|
|
* cros_ec_get_host_event_wake_mask
|
|
|
|
*
|
|
|
|
* Get the mask of host events that cause wake from suspend.
|
|
|
|
*
|
|
|
|
* @ec_dev: EC device to call
|
|
|
|
* @msg: message structure to use
|
|
|
|
* @mask: result when function returns >=0.
|
|
|
|
*
|
|
|
|
* LOCKING:
|
|
|
|
* the caller has ec_dev->lock mutex, or the caller knows there is
|
|
|
|
* no other command in progress.
|
|
|
|
*/
|
|
|
|
static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
|
|
|
|
struct cros_ec_command *msg,
|
|
|
|
uint32_t *mask)
|
|
|
|
{
|
|
|
|
struct ec_response_host_event_mask *r;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
|
|
|
|
msg->version = 0;
|
|
|
|
msg->outsize = 0;
|
|
|
|
msg->insize = sizeof(*r);
|
|
|
|
|
|
|
|
ret = send_command(ec_dev, msg);
|
|
|
|
if (ret > 0) {
|
|
|
|
r = (struct ec_response_host_event_mask *)msg->data;
|
|
|
|
*mask = r->mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
|
|
|
|
int devidx,
|
|
|
|
struct cros_ec_command *msg)
|
2015-06-09 19:04:44 +08:00
|
|
|
{
|
2015-06-09 19:04:45 +08:00
|
|
|
/*
|
|
|
|
* Try using v3+ to query for supported protocols. If this
|
|
|
|
* command fails, fall back to v2. Returns the highest protocol
|
|
|
|
* supported by the EC.
|
|
|
|
* Also sets the max request/response/passthru size.
|
|
|
|
*/
|
2015-06-09 19:04:44 +08:00
|
|
|
int ret;
|
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
if (!ec_dev->pkt_xfer)
|
|
|
|
return -EPROTONOSUPPORT;
|
2015-06-09 19:04:44 +08:00
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
memset(msg, 0, sizeof(*msg));
|
|
|
|
msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
|
|
|
|
msg->insize = sizeof(struct ec_response_get_protocol_info);
|
2015-06-09 19:04:44 +08:00
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
ret = send_command(ec_dev, msg);
|
2024-06-12 13:13:20 +08:00
|
|
|
/*
|
|
|
|
* Send command once again when timeout occurred.
|
|
|
|
* Fingerprint MCU (FPMCU) is restarted during system boot which
|
|
|
|
* introduces small window in which FPMCU won't respond for any
|
|
|
|
* messages sent by kernel. There is no need to wait before next
|
|
|
|
* attempt because we waited at least EC_MSG_DEADLINE_MS.
|
|
|
|
*/
|
|
|
|
if (ret == -ETIMEDOUT)
|
|
|
|
ret = send_command(ec_dev, msg);
|
2015-06-09 19:04:45 +08:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_dbg(ec_dev->dev,
|
|
|
|
"failed to check for EC[%d] protocol version: %d\n",
|
|
|
|
devidx, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
|
|
|
|
return -ENODEV;
|
|
|
|
else if (msg->result != EC_RES_SUCCESS)
|
|
|
|
return msg->result;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
|
|
|
|
{
|
|
|
|
struct cros_ec_command *msg;
|
|
|
|
struct ec_params_hello *hello_params;
|
|
|
|
struct ec_response_hello *hello_response;
|
|
|
|
int ret;
|
|
|
|
int len = max(sizeof(*hello_params), sizeof(*hello_response));
|
|
|
|
|
|
|
|
msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
|
|
|
|
if (!msg)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
msg->version = 0;
|
|
|
|
msg->command = EC_CMD_HELLO;
|
|
|
|
hello_params = (struct ec_params_hello *)msg->data;
|
|
|
|
msg->outsize = sizeof(*hello_params);
|
|
|
|
hello_response = (struct ec_response_hello *)msg->data;
|
|
|
|
msg->insize = sizeof(*hello_response);
|
|
|
|
|
|
|
|
hello_params->in_data = 0xa0b0c0d0;
|
|
|
|
|
|
|
|
ret = send_command(ec_dev, msg);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_dbg(ec_dev->dev,
|
|
|
|
"EC failed to respond to v2 hello: %d\n",
|
|
|
|
ret);
|
|
|
|
goto exit;
|
|
|
|
} else if (msg->result != EC_RES_SUCCESS) {
|
|
|
|
dev_err(ec_dev->dev,
|
|
|
|
"EC responded to v2 hello with error: %d\n",
|
|
|
|
msg->result);
|
|
|
|
ret = msg->result;
|
|
|
|
goto exit;
|
|
|
|
} else if (hello_response->out_data != 0xa1b2c3d4) {
|
|
|
|
dev_err(ec_dev->dev,
|
|
|
|
"EC responded to v2 hello with bad result: %u\n",
|
|
|
|
hello_response->out_data);
|
|
|
|
ret = -EBADMSG;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
kfree(msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-15 03:58:01 +08:00
|
|
|
/*
|
|
|
|
* cros_ec_get_host_command_version_mask
|
|
|
|
*
|
|
|
|
* Get the version mask of a given command.
|
|
|
|
*
|
|
|
|
* @ec_dev: EC device to call
|
|
|
|
* @msg: message structure to use
|
|
|
|
* @cmd: command to get the version of.
|
|
|
|
* @mask: result when function returns 0.
|
|
|
|
*
|
|
|
|
* @return 0 on success, error code otherwise
|
|
|
|
*
|
|
|
|
* LOCKING:
|
|
|
|
* the caller has ec_dev->lock mutex or the caller knows there is
|
|
|
|
* no other command in progress.
|
|
|
|
*/
|
2016-08-11 01:05:24 +08:00
|
|
|
static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
|
|
|
|
u16 cmd, u32 *mask)
|
|
|
|
{
|
|
|
|
struct ec_params_get_cmd_versions *pver;
|
|
|
|
struct ec_response_get_cmd_versions *rver;
|
|
|
|
struct cros_ec_command *msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!msg)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
msg->version = 0;
|
|
|
|
msg->command = EC_CMD_GET_CMD_VERSIONS;
|
|
|
|
msg->insize = sizeof(*rver);
|
|
|
|
msg->outsize = sizeof(*pver);
|
|
|
|
|
|
|
|
pver = (struct ec_params_get_cmd_versions *)msg->data;
|
|
|
|
pver->cmd = cmd;
|
|
|
|
|
2017-02-15 03:58:01 +08:00
|
|
|
ret = send_command(ec_dev, msg);
|
2016-08-11 01:05:24 +08:00
|
|
|
if (ret > 0) {
|
|
|
|
rver = (struct ec_response_get_cmd_versions *)msg->data;
|
|
|
|
*mask = rver->version_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(msg);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
int cros_ec_query_all(struct cros_ec_device *ec_dev)
|
|
|
|
{
|
|
|
|
struct device *dev = ec_dev->dev;
|
|
|
|
struct cros_ec_command *proto_msg;
|
|
|
|
struct ec_response_get_protocol_info *proto_info;
|
2016-08-11 01:05:24 +08:00
|
|
|
u32 ver_mask = 0;
|
2015-06-09 19:04:45 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!proto_msg)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* First try sending with proto v3. */
|
|
|
|
ec_dev->proto_version = 3;
|
|
|
|
ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
proto_info = (struct ec_response_get_protocol_info *)
|
|
|
|
proto_msg->data;
|
|
|
|
ec_dev->max_request = proto_info->max_request_packet_size -
|
|
|
|
sizeof(struct ec_host_request);
|
|
|
|
ec_dev->max_response = proto_info->max_response_packet_size -
|
|
|
|
sizeof(struct ec_host_response);
|
|
|
|
ec_dev->proto_version =
|
|
|
|
min(EC_HOST_REQUEST_VERSION,
|
|
|
|
fls(proto_info->protocol_versions) - 1);
|
|
|
|
dev_dbg(ec_dev->dev,
|
|
|
|
"using proto v%u\n",
|
|
|
|
ec_dev->proto_version);
|
|
|
|
|
|
|
|
ec_dev->din_size = ec_dev->max_response +
|
|
|
|
sizeof(struct ec_host_response) +
|
|
|
|
EC_MAX_RESPONSE_OVERHEAD;
|
|
|
|
ec_dev->dout_size = ec_dev->max_request +
|
|
|
|
sizeof(struct ec_host_request) +
|
|
|
|
EC_MAX_REQUEST_OVERHEAD;
|
2015-06-09 19:04:44 +08:00
|
|
|
|
|
|
|
/*
|
2015-06-09 19:04:45 +08:00
|
|
|
* Check for PD
|
2015-06-09 19:04:44 +08:00
|
|
|
*/
|
2015-06-09 19:04:45 +08:00
|
|
|
ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
|
2015-06-09 19:04:44 +08:00
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
|
|
|
|
ec_dev->max_passthru = 0;
|
|
|
|
} else {
|
|
|
|
dev_dbg(ec_dev->dev, "found PD chip\n");
|
|
|
|
ec_dev->max_passthru =
|
|
|
|
proto_info->max_request_packet_size -
|
|
|
|
sizeof(struct ec_host_request);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Try querying with a v2 hello message. */
|
|
|
|
ec_dev->proto_version = 2;
|
|
|
|
ret = cros_ec_host_command_proto_query_v2(ec_dev);
|
2015-06-09 19:04:44 +08:00
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
if (ret == 0) {
|
|
|
|
/* V2 hello succeeded. */
|
|
|
|
dev_dbg(ec_dev->dev, "falling back to proto v2\n");
|
2015-06-09 19:04:44 +08:00
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
|
|
|
|
ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
|
|
|
|
ec_dev->max_passthru = 0;
|
|
|
|
ec_dev->pkt_xfer = NULL;
|
2016-03-09 01:13:52 +08:00
|
|
|
ec_dev->din_size = EC_PROTO2_MSG_BYTES;
|
|
|
|
ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
|
2015-06-09 19:04:45 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* It's possible for a test to occur too early when
|
|
|
|
* the EC isn't listening. If this happens, we'll
|
|
|
|
* test later when the first command is run.
|
|
|
|
*/
|
|
|
|
ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
|
|
|
|
dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
|
|
|
|
goto exit;
|
2015-06-09 19:04:44 +08:00
|
|
|
}
|
2015-06-09 19:04:45 +08:00
|
|
|
}
|
2015-06-09 19:04:44 +08:00
|
|
|
|
2015-06-09 19:04:45 +08:00
|
|
|
devm_kfree(dev, ec_dev->din);
|
|
|
|
devm_kfree(dev, ec_dev->dout);
|
|
|
|
|
|
|
|
ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
|
|
|
|
if (!ec_dev->din) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto exit;
|
2015-06-09 19:04:44 +08:00
|
|
|
}
|
2015-06-09 19:04:45 +08:00
|
|
|
|
|
|
|
ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
|
|
|
|
if (!ec_dev->dout) {
|
|
|
|
devm_kfree(dev, ec_dev->din);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2016-08-11 01:05:24 +08:00
|
|
|
/* Probe if MKBP event is supported */
|
|
|
|
ret = cros_ec_get_host_command_version_mask(ec_dev,
|
|
|
|
EC_CMD_GET_NEXT_EVENT,
|
|
|
|
&ver_mask);
|
|
|
|
if (ret < 0 || ver_mask == 0)
|
|
|
|
ec_dev->mkbp_event_supported = 0;
|
|
|
|
else
|
|
|
|
ec_dev->mkbp_event_supported = 1;
|
|
|
|
|
2019-04-04 05:34:28 +08:00
|
|
|
/* Probe if host sleep v1 is supported for S0ix failure detection. */
|
|
|
|
ret = cros_ec_get_host_command_version_mask(ec_dev,
|
|
|
|
EC_CMD_HOST_SLEEP_EVENT,
|
|
|
|
&ver_mask);
|
|
|
|
ec_dev->host_sleep_v1 = (ret >= 0 && (ver_mask & EC_VER_MASK(1)));
|
|
|
|
|
2017-02-15 03:58:02 +08:00
|
|
|
/*
|
|
|
|
* Get host event wake mask, assume all events are wake events
|
|
|
|
* if unavailable.
|
|
|
|
*/
|
|
|
|
ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
|
|
|
|
&ec_dev->host_event_wake_mask);
|
|
|
|
if (ret < 0)
|
|
|
|
ec_dev->host_event_wake_mask = U32_MAX;
|
|
|
|
|
2017-02-15 03:58:00 +08:00
|
|
|
ret = 0;
|
|
|
|
|
2015-06-09 19:04:44 +08:00
|
|
|
exit:
|
2015-06-09 19:04:45 +08:00
|
|
|
kfree(proto_msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(cros_ec_query_all);
|
|
|
|
|
|
|
|
int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
|
|
|
|
struct cros_ec_command *msg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&ec_dev->lock);
|
|
|
|
if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
|
|
|
|
ret = cros_ec_query_all(ec_dev);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(ec_dev->dev,
|
|
|
|
"EC version unknown and query failed; aborting command\n");
|
|
|
|
mutex_unlock(&ec_dev->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->insize > ec_dev->max_response) {
|
|
|
|
dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
|
|
|
|
msg->insize = ec_dev->max_response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
|
|
|
|
if (msg->outsize > ec_dev->max_request) {
|
|
|
|
dev_err(ec_dev->dev,
|
|
|
|
"request of size %u is too big (max: %u)\n",
|
|
|
|
msg->outsize,
|
|
|
|
ec_dev->max_request);
|
|
|
|
mutex_unlock(&ec_dev->lock);
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (msg->outsize > ec_dev->max_passthru) {
|
|
|
|
dev_err(ec_dev->dev,
|
|
|
|
"passthru rq of size %u is too big (max: %u)\n",
|
|
|
|
msg->outsize,
|
|
|
|
ec_dev->max_passthru);
|
|
|
|
mutex_unlock(&ec_dev->lock);
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = send_command(ec_dev, msg);
|
2015-06-09 19:04:44 +08:00
|
|
|
mutex_unlock(&ec_dev->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(cros_ec_cmd_xfer);
|
2016-07-16 07:28:41 +08:00
|
|
|
|
|
|
|
int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
|
|
|
|
struct cros_ec_command *msg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = cros_ec_cmd_xfer(ec_dev, msg);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
|
|
|
|
} else if (msg->result != EC_RES_SUCCESS) {
|
|
|
|
dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
|
|
|
|
return -EPROTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
|
2016-08-11 01:05:24 +08:00
|
|
|
|
2018-07-04 23:08:18 +08:00
|
|
|
static int get_next_event_xfer(struct cros_ec_device *ec_dev,
|
|
|
|
struct cros_ec_command *msg,
|
|
|
|
int version, uint32_t size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
msg->version = version;
|
|
|
|
msg->command = EC_CMD_GET_NEXT_EVENT;
|
|
|
|
msg->insize = size;
|
|
|
|
msg->outsize = 0;
|
|
|
|
|
|
|
|
ret = cros_ec_cmd_xfer(ec_dev, msg);
|
|
|
|
if (ret > 0) {
|
|
|
|
ec_dev->event_size = ret - 1;
|
2018-10-04 02:43:18 +08:00
|
|
|
memcpy(&ec_dev->event_data, msg->data, ret);
|
2018-07-04 23:08:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-11 01:05:24 +08:00
|
|
|
static int get_next_event(struct cros_ec_device *ec_dev)
|
|
|
|
{
|
|
|
|
u8 buffer[sizeof(struct cros_ec_command) + sizeof(ec_dev->event_data)];
|
|
|
|
struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
|
2018-07-04 23:08:18 +08:00
|
|
|
static int cmd_version = 1;
|
2016-08-11 01:05:24 +08:00
|
|
|
int ret;
|
|
|
|
|
2016-12-17 01:57:36 +08:00
|
|
|
if (ec_dev->suspended) {
|
|
|
|
dev_dbg(ec_dev->dev, "Device suspended.\n");
|
|
|
|
return -EHOSTDOWN;
|
|
|
|
}
|
|
|
|
|
2018-07-04 23:08:18 +08:00
|
|
|
if (cmd_version == 1) {
|
|
|
|
ret = get_next_event_xfer(ec_dev, msg, cmd_version,
|
|
|
|
sizeof(struct ec_response_get_next_event_v1));
|
|
|
|
if (ret < 0 || msg->result != EC_RES_INVALID_VERSION)
|
|
|
|
return ret;
|
2016-08-11 01:05:24 +08:00
|
|
|
|
2018-07-04 23:08:18 +08:00
|
|
|
/* Fallback to version 0 for future send attempts */
|
|
|
|
cmd_version = 0;
|
2016-08-11 01:05:24 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 23:08:18 +08:00
|
|
|
ret = get_next_event_xfer(ec_dev, msg, cmd_version,
|
|
|
|
sizeof(struct ec_response_get_next_event));
|
|
|
|
|
2016-08-11 01:05:24 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
|
|
|
|
{
|
|
|
|
u8 buffer[sizeof(struct cros_ec_command) +
|
|
|
|
sizeof(ec_dev->event_data.data)];
|
|
|
|
struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
|
|
|
|
|
|
|
|
msg->version = 0;
|
|
|
|
msg->command = EC_CMD_MKBP_STATE;
|
|
|
|
msg->insize = sizeof(ec_dev->event_data.data);
|
|
|
|
msg->outsize = 0;
|
|
|
|
|
|
|
|
ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
|
|
|
|
ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
|
|
|
|
memcpy(&ec_dev->event_data.data, msg->data,
|
|
|
|
sizeof(ec_dev->event_data.data));
|
|
|
|
|
|
|
|
return ec_dev->event_size;
|
|
|
|
}
|
|
|
|
|
2017-02-15 03:58:02 +08:00
|
|
|
int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event)
|
2016-08-11 01:05:24 +08:00
|
|
|
{
|
2018-11-08 10:49:39 +08:00
|
|
|
u8 event_type;
|
2017-02-15 03:58:02 +08:00
|
|
|
u32 host_event;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!ec_dev->mkbp_event_supported) {
|
|
|
|
ret = get_keyboard_state_event(ec_dev);
|
2018-11-08 10:49:38 +08:00
|
|
|
if (ret <= 0)
|
2017-02-15 03:58:02 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (wake_event)
|
|
|
|
*wake_event = true;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = get_next_event(ec_dev);
|
2018-11-08 10:49:38 +08:00
|
|
|
if (ret <= 0)
|
2017-02-15 03:58:02 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (wake_event) {
|
2018-11-08 10:49:39 +08:00
|
|
|
event_type = ec_dev->event_data.event_type;
|
2017-02-15 03:58:02 +08:00
|
|
|
host_event = cros_ec_get_host_event(ec_dev);
|
|
|
|
|
2018-11-08 10:49:39 +08:00
|
|
|
/*
|
|
|
|
* Sensor events need to be parsed by the sensor sub-device.
|
|
|
|
* Defer them, and don't report the wakeup here.
|
|
|
|
*/
|
|
|
|
if (event_type == EC_MKBP_EVENT_SENSOR_FIFO)
|
|
|
|
*wake_event = false;
|
|
|
|
/* Masked host-events should not count as wake events. */
|
|
|
|
else if (host_event &&
|
|
|
|
!(host_event & ec_dev->host_event_wake_mask))
|
|
|
|
*wake_event = false;
|
|
|
|
/* Consider all other events as wake events. */
|
|
|
|
else
|
|
|
|
*wake_event = true;
|
2017-02-15 03:58:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-08-11 01:05:24 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(cros_ec_get_next_event);
|
2017-05-16 23:46:48 +08:00
|
|
|
|
|
|
|
u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
|
|
|
|
{
|
|
|
|
u32 host_event;
|
|
|
|
|
|
|
|
BUG_ON(!ec_dev->mkbp_event_supported);
|
|
|
|
|
|
|
|
if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ec_dev->event_size != sizeof(host_event)) {
|
|
|
|
dev_warn(ec_dev->dev, "Invalid host event size\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
|
|
|
|
|
|
|
|
return host_event;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(cros_ec_get_host_event);
|