[PATCH v6] platform/chrome: mfd/cros_ec_debugfs: Add debugfs entry to retrieve EC uptime.

From: twawrzynczak
Date: Thu Jun 13 2019 - 14:02:40 EST


From: Tim Wawrzynczak <twawrzynczak@xxxxxxxxxxxx>

The new debugfs entry 'uptime' is being made available to userspace so that
a userspace daemon can synchronize EC logs with host time.

Signed-off-by: Tim Wawrzynczak <twawrzynczak@xxxxxxxxxxxx>
---
Sorry about that Enric, I was confused that you meant Lee's tree. Also I
got myself confused with all the patchsets :)

Changelist from v5:
1) Rebase on lee's for-mfd-next
Changelist from v4:
1) Add EC_CMD_GET_UPTIME_INFO
2) Rebase on top of 5.3
3) Get rid of cros_ec_create_uptime
Changelist from v3:
1) Don't check return values of debugfs_* functions.
2) Only expose 'uptime' file if EC supports it.
---
Documentation/ABI/testing/debugfs-cros-ec | 10 ++++
drivers/platform/chrome/cros_ec_debugfs.c | 56 +++++++++++++++++++++++
2 files changed, 66 insertions(+)
create mode 100644 Documentation/ABI/testing/debugfs-cros-ec

diff --git a/Documentation/ABI/testing/debugfs-cros-ec b/Documentation/ABI/testing/debugfs-cros-ec
new file mode 100644
index 000000000000..ca6f4838ee0d
--- /dev/null
+++ b/Documentation/ABI/testing/debugfs-cros-ec
@@ -0,0 +1,10 @@
+What: /sys/kernel/debug/<cros-ec-device>/uptime
+Date: June 2019
+KernelVersion: 5.3
+Description:
+ Read-only.
+ Reads the EC's current uptime information
+ (using EC_CMD_GET_UPTIME_INFO) and prints
+ time_since_ec_boot_ms into the file.
+ This is used for synchronizing AP host time
+ with the cros_ec log.
diff --git a/drivers/platform/chrome/cros_ec_debugfs.c b/drivers/platform/chrome/cros_ec_debugfs.c
index 4c2a27f6a6d0..dbba007c576d 100644
--- a/drivers/platform/chrome/cros_ec_debugfs.c
+++ b/drivers/platform/chrome/cros_ec_debugfs.c
@@ -241,6 +241,52 @@ static ssize_t cros_ec_pdinfo_read(struct file *file,
read_buf, p - read_buf);
}

+static int cros_ec_get_uptime(struct cros_ec_device *ec_dev, u32 *uptime)
+{
+ struct {
+ struct cros_ec_command msg;
+ struct ec_response_uptime_info resp;
+ } __packed ec_buf;
+ struct ec_response_uptime_info *resp;
+ struct cros_ec_command *msg;
+ int ret;
+
+ msg = &ec_buf.msg;
+ resp = (struct ec_response_uptime_info *)msg->data;
+
+ msg->command = EC_CMD_GET_UPTIME_INFO;
+ msg->version = 0;
+ msg->insize = sizeof(*resp);
+ msg->outsize = 0;
+
+ ret = cros_ec_cmd_xfer_status(ec_dev, msg);
+ if (ret < 0)
+ return ret;
+
+ *uptime = resp->time_since_ec_boot_ms;
+ return 0;
+}
+
+static ssize_t cros_ec_uptime_read(struct file *file,
+ char __user *user_buf,
+ size_t count,
+ loff_t *ppos)
+{
+ struct cros_ec_debugfs *debug_info = file->private_data;
+ struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
+ char read_buf[32];
+ u32 uptime;
+ int ret;
+
+ ret = cros_ec_get_uptime(ec_dev, &uptime);
+ if (ret < 0)
+ return ret;
+
+ ret = scnprintf(read_buf, sizeof(read_buf), "%u\n", uptime);
+
+ return simple_read_from_buffer(user_buf, count, ppos, read_buf, ret);
+}
+
const struct file_operations cros_ec_console_log_fops = {
.owner = THIS_MODULE,
.open = cros_ec_console_log_open,
@@ -257,6 +303,13 @@ const struct file_operations cros_ec_pdinfo_fops = {
.llseek = default_llseek,
};

+const struct file_operations cros_ec_uptime_fops = {
+ .owner = THIS_MODULE,
+ .open = simple_open,
+ .read = cros_ec_uptime_read,
+ .llseek = default_llseek,
+};
+
static int ec_read_version_supported(struct cros_ec_dev *ec)
{
struct ec_params_get_cmd_versions_v1 *params;
@@ -408,6 +461,9 @@ static int cros_ec_debugfs_probe(struct platform_device *pd)
debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
&cros_ec_pdinfo_fops);

+ debugfs_create_file("uptime", 0444, debug_info->dir, debug_info,
+ &cros_ec_uptime_fops);
+
ec->debug_info = debug_info;

dev_set_drvdata(&pd->dev, ec);
--
2.22.0.rc2.383.gf4fbbf30c2-goog