hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/platform/chrome/cros_ec_debugfs.c
....@@ -1,33 +1,23 @@
1
-/*
2
- * cros_ec_debugfs - debug logs for Chrome OS EC
3
- *
4
- * Copyright 2015 Google, Inc.
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version.
10
- *
11
- * This program is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
- */
1
+// SPDX-License-Identifier: GPL-2.0+
2
+// Debug logs for the ChromeOS EC
3
+//
4
+// Copyright (C) 2015 Google, Inc.
195
206 #include <linux/circ_buf.h>
217 #include <linux/debugfs.h>
228 #include <linux/delay.h>
239 #include <linux/fs.h>
24
-#include <linux/mfd/cros_ec.h>
25
-#include <linux/mfd/cros_ec_commands.h>
10
+#include <linux/module.h>
2611 #include <linux/mutex.h>
12
+#include <linux/platform_data/cros_ec_commands.h>
13
+#include <linux/platform_data/cros_ec_proto.h>
14
+#include <linux/platform_device.h>
2715 #include <linux/poll.h>
2816 #include <linux/sched.h>
2917 #include <linux/slab.h>
3018 #include <linux/wait.h>
19
+
20
+#define DRV_NAME "cros-ec-debugfs"
3121
3222 #define LOG_SHIFT 14
3323 #define LOG_SIZE (1 << LOG_SHIFT)
....@@ -35,14 +25,17 @@
3525
3626 #define CIRC_ADD(idx, size, value) (((idx) + (value)) & ((size) - 1))
3727
38
-/* struct cros_ec_debugfs - ChromeOS EC debugging information
28
+/* waitqueue for log readers */
29
+static DECLARE_WAIT_QUEUE_HEAD(cros_ec_debugfs_log_wq);
30
+
31
+/**
32
+ * struct cros_ec_debugfs - EC debugging information.
3933 *
4034 * @ec: EC device this debugfs information belongs to
4135 * @dir: dentry for debugfs files
4236 * @log_buffer: circular buffer for console log information
4337 * @read_msg: preallocated EC command and buffer to read console log
4438 * @log_mutex: mutex to protect circular buffer
45
- * @log_wq: waitqueue for log readers
4639 * @log_poll_work: recurring task to poll EC for new console log data
4740 * @panicinfo_blob: panicinfo debugfs blob
4841 */
....@@ -53,7 +46,6 @@
5346 struct circ_buf log_buffer;
5447 struct cros_ec_command *read_msg;
5548 struct mutex log_mutex;
56
- wait_queue_head_t log_wq;
5749 struct delayed_work log_poll_work;
5850 /* EC panicinfo */
5951 struct debugfs_blob_wrapper panicinfo_blob;
....@@ -82,15 +74,9 @@
8274 int buf_space;
8375 int ret;
8476
85
- ret = cros_ec_cmd_xfer(ec->ec_dev, &snapshot_msg);
86
- if (ret < 0) {
87
- dev_err(ec->dev, "EC communication failed\n");
77
+ ret = cros_ec_cmd_xfer_status(ec->ec_dev, &snapshot_msg);
78
+ if (ret < 0)
8879 goto resched;
89
- }
90
- if (snapshot_msg.result != EC_RES_SUCCESS) {
91
- dev_err(ec->dev, "EC failed to snapshot the console log\n");
92
- goto resched;
93
- }
9480
9581 /* Loop until we have read everything, or there's an error. */
9682 mutex_lock(&debug_info->log_mutex);
....@@ -105,16 +91,10 @@
10591
10692 memset(read_params, '\0', sizeof(*read_params));
10793 read_params->subcmd = CONSOLE_READ_RECENT;
108
- ret = cros_ec_cmd_xfer(ec->ec_dev, debug_info->read_msg);
109
- if (ret < 0) {
110
- dev_err(ec->dev, "EC communication failed\n");
94
+ ret = cros_ec_cmd_xfer_status(ec->ec_dev,
95
+ debug_info->read_msg);
96
+ if (ret < 0)
11197 break;
112
- }
113
- if (debug_info->read_msg->result != EC_RES_SUCCESS) {
114
- dev_err(ec->dev,
115
- "EC failed to read the console log\n");
116
- break;
117
- }
11898
11999 /* If the buffer is empty, we're done here. */
120100 if (ret == 0 || ec_buffer[0] == '\0')
....@@ -128,7 +108,7 @@
128108 buf_space--;
129109 }
130110
131
- wake_up(&debug_info->log_wq);
111
+ wake_up(&cros_ec_debugfs_log_wq);
132112 }
133113
134114 mutex_unlock(&debug_info->log_mutex);
....@@ -142,7 +122,7 @@
142122 {
143123 file->private_data = inode->i_private;
144124
145
- return nonseekable_open(inode, file);
125
+ return stream_open(inode, file);
146126 }
147127
148128 static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
....@@ -162,7 +142,7 @@
162142
163143 mutex_unlock(&debug_info->log_mutex);
164144
165
- ret = wait_event_interruptible(debug_info->log_wq,
145
+ ret = wait_event_interruptible(cros_ec_debugfs_log_wq,
166146 CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
167147 if (ret < 0)
168148 return ret;
....@@ -194,7 +174,7 @@
194174 struct cros_ec_debugfs *debug_info = file->private_data;
195175 __poll_t mask = 0;
196176
197
- poll_wait(file, &debug_info->log_wq, wait);
177
+ poll_wait(file, &cros_ec_debugfs_log_wq, wait);
198178
199179 mutex_lock(&debug_info->log_mutex);
200180 if (CIRC_CNT(debug_info->log_buffer.head,
....@@ -263,7 +243,54 @@
263243 read_buf, p - read_buf);
264244 }
265245
266
-const struct file_operations cros_ec_console_log_fops = {
246
+static bool cros_ec_uptime_is_supported(struct cros_ec_device *ec_dev)
247
+{
248
+ struct {
249
+ struct cros_ec_command cmd;
250
+ struct ec_response_uptime_info resp;
251
+ } __packed msg = {};
252
+ int ret;
253
+
254
+ msg.cmd.command = EC_CMD_GET_UPTIME_INFO;
255
+ msg.cmd.insize = sizeof(msg.resp);
256
+
257
+ ret = cros_ec_cmd_xfer_status(ec_dev, &msg.cmd);
258
+ if (ret == -EPROTO && msg.cmd.result == EC_RES_INVALID_COMMAND)
259
+ return false;
260
+
261
+ /* Other errors maybe a transient error, do not rule about support. */
262
+ return true;
263
+}
264
+
265
+static ssize_t cros_ec_uptime_read(struct file *file, char __user *user_buf,
266
+ size_t count, loff_t *ppos)
267
+{
268
+ struct cros_ec_debugfs *debug_info = file->private_data;
269
+ struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
270
+ struct {
271
+ struct cros_ec_command cmd;
272
+ struct ec_response_uptime_info resp;
273
+ } __packed msg = {};
274
+ struct ec_response_uptime_info *resp;
275
+ char read_buf[32];
276
+ int ret;
277
+
278
+ resp = (struct ec_response_uptime_info *)&msg.resp;
279
+
280
+ msg.cmd.command = EC_CMD_GET_UPTIME_INFO;
281
+ msg.cmd.insize = sizeof(*resp);
282
+
283
+ ret = cros_ec_cmd_xfer_status(ec_dev, &msg.cmd);
284
+ if (ret < 0)
285
+ return ret;
286
+
287
+ ret = scnprintf(read_buf, sizeof(read_buf), "%u\n",
288
+ resp->time_since_ec_boot_ms);
289
+
290
+ return simple_read_from_buffer(user_buf, count, ppos, read_buf, ret);
291
+}
292
+
293
+static const struct file_operations cros_ec_console_log_fops = {
267294 .owner = THIS_MODULE,
268295 .open = cros_ec_console_log_open,
269296 .read = cros_ec_console_log_read,
....@@ -272,10 +299,17 @@
272299 .release = cros_ec_console_log_release,
273300 };
274301
275
-const struct file_operations cros_ec_pdinfo_fops = {
302
+static const struct file_operations cros_ec_pdinfo_fops = {
276303 .owner = THIS_MODULE,
277304 .open = simple_open,
278305 .read = cros_ec_pdinfo_read,
306
+ .llseek = default_llseek,
307
+};
308
+
309
+static const struct file_operations cros_ec_uptime_fops = {
310
+ .owner = THIS_MODULE,
311
+ .open = simple_open,
312
+ .read = cros_ec_uptime_read,
279313 .llseek = default_llseek,
280314 };
281315
....@@ -300,9 +334,8 @@
300334 params->cmd = EC_CMD_CONSOLE_READ;
301335 response = (struct ec_response_get_cmd_versions *)msg->data;
302336
303
- ret = cros_ec_cmd_xfer(ec->ec_dev, msg) >= 0 &&
304
- msg->result == EC_RES_SUCCESS &&
305
- (response->version_mask & EC_VER_MASK(1));
337
+ ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg) >= 0 &&
338
+ response->version_mask & EC_VER_MASK(1);
306339
307340 kfree(msg);
308341
....@@ -316,11 +349,12 @@
316349 int read_params_size;
317350 int read_response_size;
318351
319
- if (!ec_read_version_supported(ec)) {
320
- dev_warn(ec->dev,
321
- "device does not support reading the console log\n");
352
+ /*
353
+ * If the console log feature is not supported return silently and
354
+ * don't create the console_log entry.
355
+ */
356
+ if (!ec_read_version_supported(ec))
322357 return 0;
323
- }
324358
325359 buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
326360 if (!buf)
....@@ -344,14 +378,9 @@
344378 debug_info->log_buffer.tail = 0;
345379
346380 mutex_init(&debug_info->log_mutex);
347
- init_waitqueue_head(&debug_info->log_wq);
348381
349
- if (!debugfs_create_file("console_log",
350
- S_IFREG | 0444,
351
- debug_info->dir,
352
- debug_info,
353
- &cros_ec_console_log_fops))
354
- return -ENOMEM;
382
+ debugfs_create_file("console_log", S_IFREG | 0444, debug_info->dir,
383
+ debug_info, &cros_ec_console_log_fops);
355384
356385 INIT_DELAYED_WORK(&debug_info->log_poll_work,
357386 cros_ec_console_log_work);
....@@ -385,9 +414,8 @@
385414 msg->command = EC_CMD_GET_PANIC_INFO;
386415 msg->insize = insize;
387416
388
- ret = cros_ec_cmd_xfer(ec_dev, msg);
417
+ ret = cros_ec_cmd_xfer_status(ec_dev, msg);
389418 if (ret < 0) {
390
- dev_warn(debug_info->ec->dev, "Cannot read panicinfo.\n");
391419 ret = 0;
392420 goto free;
393421 }
....@@ -399,13 +427,8 @@
399427 debug_info->panicinfo_blob.data = msg->data;
400428 debug_info->panicinfo_blob.size = ret;
401429
402
- if (!debugfs_create_blob("panicinfo",
403
- S_IFREG | 0444,
404
- debug_info->dir,
405
- &debug_info->panicinfo_blob)) {
406
- ret = -ENOMEM;
407
- goto free;
408
- }
430
+ debugfs_create_blob("panicinfo", S_IFREG | 0444, debug_info->dir,
431
+ &debug_info->panicinfo_blob);
409432
410433 return 0;
411434
....@@ -414,17 +437,9 @@
414437 return ret;
415438 }
416439
417
-static int cros_ec_create_pdinfo(struct cros_ec_debugfs *debug_info)
440
+static int cros_ec_debugfs_probe(struct platform_device *pd)
418441 {
419
- if (!debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
420
- &cros_ec_pdinfo_fops))
421
- return -ENOMEM;
422
-
423
- return 0;
424
-}
425
-
426
-int cros_ec_debugfs_init(struct cros_ec_dev *ec)
427
-{
442
+ struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
428443 struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
429444 const char *name = ec_platform->ec_name;
430445 struct cros_ec_debugfs *debug_info;
....@@ -436,8 +451,6 @@
436451
437452 debug_info->ec = ec;
438453 debug_info->dir = debugfs_create_dir(name, NULL);
439
- if (!debug_info->dir)
440
- return -ENOMEM;
441454
442455 ret = cros_ec_create_panicinfo(debug_info);
443456 if (ret)
....@@ -447,11 +460,19 @@
447460 if (ret)
448461 goto remove_debugfs;
449462
450
- ret = cros_ec_create_pdinfo(debug_info);
451
- if (ret)
452
- goto remove_debugfs;
463
+ debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
464
+ &cros_ec_pdinfo_fops);
465
+
466
+ if (cros_ec_uptime_is_supported(ec->ec_dev))
467
+ debugfs_create_file("uptime", 0444, debug_info->dir, debug_info,
468
+ &cros_ec_uptime_fops);
469
+
470
+ debugfs_create_x32("last_resume_result", 0444, debug_info->dir,
471
+ &ec->ec_dev->last_resume_result);
453472
454473 ec->debug_info = debug_info;
474
+
475
+ dev_set_drvdata(&pd->dev, ec);
455476
456477 return 0;
457478
....@@ -459,34 +480,51 @@
459480 debugfs_remove_recursive(debug_info->dir);
460481 return ret;
461482 }
462
-EXPORT_SYMBOL(cros_ec_debugfs_init);
463483
464
-void cros_ec_debugfs_remove(struct cros_ec_dev *ec)
484
+static int cros_ec_debugfs_remove(struct platform_device *pd)
465485 {
466
- if (!ec->debug_info)
467
- return;
486
+ struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
468487
469488 debugfs_remove_recursive(ec->debug_info->dir);
470489 cros_ec_cleanup_console_log(ec->debug_info);
471
-}
472
-EXPORT_SYMBOL(cros_ec_debugfs_remove);
473490
474
-void cros_ec_debugfs_suspend(struct cros_ec_dev *ec)
491
+ return 0;
492
+}
493
+
494
+static int __maybe_unused cros_ec_debugfs_suspend(struct device *dev)
475495 {
476
- /*
477
- * cros_ec_debugfs_init() failures are non-fatal; it's also possible
478
- * that we initted things but decided that console log wasn't supported.
479
- * We'll use the same set of checks that cros_ec_debugfs_remove() +
480
- * cros_ec_cleanup_console_log() end up using to handle those cases.
481
- */
482
- if (ec->debug_info && ec->debug_info->log_buffer.buf)
496
+ struct cros_ec_dev *ec = dev_get_drvdata(dev);
497
+
498
+ if (ec->debug_info->log_buffer.buf)
483499 cancel_delayed_work_sync(&ec->debug_info->log_poll_work);
484
-}
485
-EXPORT_SYMBOL(cros_ec_debugfs_suspend);
486500
487
-void cros_ec_debugfs_resume(struct cros_ec_dev *ec)
488
-{
489
- if (ec->debug_info && ec->debug_info->log_buffer.buf)
490
- schedule_delayed_work(&ec->debug_info->log_poll_work, 0);
501
+ return 0;
491502 }
492
-EXPORT_SYMBOL(cros_ec_debugfs_resume);
503
+
504
+static int __maybe_unused cros_ec_debugfs_resume(struct device *dev)
505
+{
506
+ struct cros_ec_dev *ec = dev_get_drvdata(dev);
507
+
508
+ if (ec->debug_info->log_buffer.buf)
509
+ schedule_delayed_work(&ec->debug_info->log_poll_work, 0);
510
+
511
+ return 0;
512
+}
513
+
514
+static SIMPLE_DEV_PM_OPS(cros_ec_debugfs_pm_ops,
515
+ cros_ec_debugfs_suspend, cros_ec_debugfs_resume);
516
+
517
+static struct platform_driver cros_ec_debugfs_driver = {
518
+ .driver = {
519
+ .name = DRV_NAME,
520
+ .pm = &cros_ec_debugfs_pm_ops,
521
+ },
522
+ .probe = cros_ec_debugfs_probe,
523
+ .remove = cros_ec_debugfs_remove,
524
+};
525
+
526
+module_platform_driver(cros_ec_debugfs_driver);
527
+
528
+MODULE_LICENSE("GPL");
529
+MODULE_DESCRIPTION("Debug logs for ChromeOS EC");
530
+MODULE_ALIAS("platform:" DRV_NAME);