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
error code gets lost. Convert all EC errors to Linux error codes to report
a more meaningful error to the caller to aid debugging.

Cc: Yu-Hsuan Hsu <[email protected]>
Cc: Prashant Malani <[email protected]>
Signed-off-by: Guenter Roeck <[email protected]>
---
 drivers/platform/chrome/cros_ec_proto.c | 37 +++++++++++++++++++------
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_proto.c 
b/drivers/platform/chrome/cros_ec_proto.c
index 3e745e0fe092..10aa9e483d35 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -543,6 +543,29 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
 }
 EXPORT_SYMBOL(cros_ec_cmd_xfer);
 
+static const int cros_ec_error_map[] = {
+       [EC_RES_INVALID_COMMAND] = -EOPNOTSUPP,
+       [EC_RES_ERROR] = -EIO,
+       [EC_RES_INVALID_PARAM] = -EINVAL,
+       [EC_RES_ACCESS_DENIED] = -EACCES,
+       [EC_RES_INVALID_RESPONSE] = -EPROTO,
+       [EC_RES_INVALID_VERSION] = -ENOTSUPP,
+       [EC_RES_INVALID_CHECKSUM] = -EBADMSG,
+       [EC_RES_IN_PROGRESS] = -EINPROGRESS,
+       [EC_RES_UNAVAILABLE] = -ENODATA,
+       [EC_RES_TIMEOUT] = -ETIMEDOUT,
+       [EC_RES_OVERFLOW] = -EOVERFLOW,
+       [EC_RES_INVALID_HEADER] = -EBADR,
+       [EC_RES_REQUEST_TRUNCATED] = -EBADR,
+       [EC_RES_RESPONSE_TOO_BIG] = -EFBIG,
+       [EC_RES_BUS_ERROR] = -EFAULT,
+       [EC_RES_BUSY] = -EBUSY,
+       [EC_RES_INVALID_HEADER_VERSION] = -EBADMSG,
+       [EC_RES_INVALID_HEADER_CRC] = -EBADMSG,
+       [EC_RES_INVALID_DATA_CRC] = -EBADMSG,
+       [EC_RES_DUP_UNAVAILABLE] = -ENODATA,
+};
+
 /**
  * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
  * @ec_dev: EC device.
@@ -555,8 +578,7 @@ EXPORT_SYMBOL(cros_ec_cmd_xfer);
  *
  * Return:
  * >=0 - The number of bytes transferred
- * -ENOTSUPP - Operation not supported
- * -EPROTO - Protocol error
+ * <0 - Linux error code
  */
 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
                            struct cros_ec_command *msg)
@@ -566,13 +588,12 @@ int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
        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_INVALID_VERSION) {
-               dev_dbg(ec_dev->dev, "Command invalid version (err:%d)\n",
-                       msg->result);
-               return -ENOTSUPP;
        } else if (msg->result != EC_RES_SUCCESS) {
-               dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
-               return -EPROTO;
+               if (msg->result < ARRAY_SIZE(cros_ec_error_map) && 
cros_ec_error_map[msg->result])
+                       ret = cros_ec_error_map[msg->result];
+               else
+                       ret = -EPROTO;
+               dev_dbg(ec_dev->dev, "Command result (err: %d [%d])\n", 
msg->result, ret);
        }
 
        return ret;
-- 
2.17.1

Reply via email to