Skip to content

Commit 0d08045

Browse files
groeckEnric Balletbo i Serra
authored andcommitted
platform/chrome: cros_ec_proto: Convert EC error codes to Linux error codes
The EC reports a variety of error codes. Most of those, with the exception of EC_RES_INVALID_VERSION, are converted to -EPROTO. As result, the actual EC error code gets lost. Introduce cros_ec_map_error() to map EC error codes to Linux error codes, and use it in cros_ec_cmd_xfer_status() to report more meaningful errors to the caller. With this change, callers of cros_ec_cmd_xfer_status() can implement a more distinguished action without having to rely on the EC error code. At the same time, debugging is improved in situations where the Linux error code is reported to userspace and/or in the kernel log. Cc: Gwendal Grignou <gwendal@chromium.org> Cc: Yu-Hsuan Hsu <yuhsuan@chromium.org> Cc: Prashant Malani <pmalani@chromium.org> Cc: Brian Norris <briannorris@chromium.org> Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
1 parent b4e452b commit 0d08045

1 file changed

Lines changed: 46 additions & 11 deletions

File tree

‎drivers/platform/chrome/cros_ec_proto.c‎

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,43 @@
1515

1616
#define EC_COMMAND_RETRIES 50
1717

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,
39+
};
40+
41+
static int cros_ec_map_error(uint32_t result)
42+
{
43+
int ret = 0;
44+
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];
48+
else
49+
ret = -EPROTO;
50+
}
51+
52+
return ret;
53+
}
54+
1855
static int prepare_packet(struct cros_ec_device *ec_dev,
1956
struct cros_ec_command *msg)
2057
{
@@ -579,26 +616,24 @@ static int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
579616
*
580617
* Return:
581618
* >=0 - The number of bytes transferred
582-
* -ENOPROTOOPT - Operation not supported
583-
* -EPROTO - Protocol error
619+
* <0 - Linux error code
584620
*/
585621
int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
586622
struct cros_ec_command *msg)
587623
{
588-
int ret;
624+
int ret, mapped;
589625

590626
ret = cros_ec_cmd_xfer(ec_dev, msg);
591627
if (ret < 0) {
592628
dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
593-
} else if (msg->result == EC_RES_INVALID_VERSION) {
594-
dev_dbg(ec_dev->dev, "Command invalid version (err:%d)\n",
595-
msg->result);
596-
return -ENOPROTOOPT;
597-
} else if (msg->result != EC_RES_SUCCESS) {
598-
dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
599-
return -EPROTO;
629+
return ret;
630+
}
631+
mapped = cros_ec_map_error(msg->result);
632+
if (mapped) {
633+
dev_dbg(ec_dev->dev, "Command result (err: %d [%d])\n",
634+
msg->result, mapped);
635+
ret = mapped;
600636
}
601-
602637
return ret;
603638
}
604639
EXPORT_SYMBOL(cros_ec_cmd_xfer_status);

0 commit comments

Comments
 (0)