.. | .. |
---|
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. |
---|
19 | 5 | |
---|
20 | 6 | #include <linux/circ_buf.h> |
---|
21 | 7 | #include <linux/debugfs.h> |
---|
22 | 8 | #include <linux/delay.h> |
---|
23 | 9 | #include <linux/fs.h> |
---|
24 | | -#include <linux/mfd/cros_ec.h> |
---|
25 | | -#include <linux/mfd/cros_ec_commands.h> |
---|
| 10 | +#include <linux/module.h> |
---|
26 | 11 | #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> |
---|
27 | 15 | #include <linux/poll.h> |
---|
28 | 16 | #include <linux/sched.h> |
---|
29 | 17 | #include <linux/slab.h> |
---|
30 | 18 | #include <linux/wait.h> |
---|
| 19 | + |
---|
| 20 | +#define DRV_NAME "cros-ec-debugfs" |
---|
31 | 21 | |
---|
32 | 22 | #define LOG_SHIFT 14 |
---|
33 | 23 | #define LOG_SIZE (1 << LOG_SHIFT) |
---|
.. | .. |
---|
35 | 25 | |
---|
36 | 26 | #define CIRC_ADD(idx, size, value) (((idx) + (value)) & ((size) - 1)) |
---|
37 | 27 | |
---|
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. |
---|
39 | 33 | * |
---|
40 | 34 | * @ec: EC device this debugfs information belongs to |
---|
41 | 35 | * @dir: dentry for debugfs files |
---|
42 | 36 | * @log_buffer: circular buffer for console log information |
---|
43 | 37 | * @read_msg: preallocated EC command and buffer to read console log |
---|
44 | 38 | * @log_mutex: mutex to protect circular buffer |
---|
45 | | - * @log_wq: waitqueue for log readers |
---|
46 | 39 | * @log_poll_work: recurring task to poll EC for new console log data |
---|
47 | 40 | * @panicinfo_blob: panicinfo debugfs blob |
---|
48 | 41 | */ |
---|
.. | .. |
---|
53 | 46 | struct circ_buf log_buffer; |
---|
54 | 47 | struct cros_ec_command *read_msg; |
---|
55 | 48 | struct mutex log_mutex; |
---|
56 | | - wait_queue_head_t log_wq; |
---|
57 | 49 | struct delayed_work log_poll_work; |
---|
58 | 50 | /* EC panicinfo */ |
---|
59 | 51 | struct debugfs_blob_wrapper panicinfo_blob; |
---|
.. | .. |
---|
82 | 74 | int buf_space; |
---|
83 | 75 | int ret; |
---|
84 | 76 | |
---|
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) |
---|
88 | 79 | 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 | | - } |
---|
94 | 80 | |
---|
95 | 81 | /* Loop until we have read everything, or there's an error. */ |
---|
96 | 82 | mutex_lock(&debug_info->log_mutex); |
---|
.. | .. |
---|
105 | 91 | |
---|
106 | 92 | memset(read_params, '\0', sizeof(*read_params)); |
---|
107 | 93 | 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) |
---|
111 | 97 | 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 | | - } |
---|
118 | 98 | |
---|
119 | 99 | /* If the buffer is empty, we're done here. */ |
---|
120 | 100 | if (ret == 0 || ec_buffer[0] == '\0') |
---|
.. | .. |
---|
128 | 108 | buf_space--; |
---|
129 | 109 | } |
---|
130 | 110 | |
---|
131 | | - wake_up(&debug_info->log_wq); |
---|
| 111 | + wake_up(&cros_ec_debugfs_log_wq); |
---|
132 | 112 | } |
---|
133 | 113 | |
---|
134 | 114 | mutex_unlock(&debug_info->log_mutex); |
---|
.. | .. |
---|
142 | 122 | { |
---|
143 | 123 | file->private_data = inode->i_private; |
---|
144 | 124 | |
---|
145 | | - return nonseekable_open(inode, file); |
---|
| 125 | + return stream_open(inode, file); |
---|
146 | 126 | } |
---|
147 | 127 | |
---|
148 | 128 | static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf, |
---|
.. | .. |
---|
162 | 142 | |
---|
163 | 143 | mutex_unlock(&debug_info->log_mutex); |
---|
164 | 144 | |
---|
165 | | - ret = wait_event_interruptible(debug_info->log_wq, |
---|
| 145 | + ret = wait_event_interruptible(cros_ec_debugfs_log_wq, |
---|
166 | 146 | CIRC_CNT(cb->head, cb->tail, LOG_SIZE)); |
---|
167 | 147 | if (ret < 0) |
---|
168 | 148 | return ret; |
---|
.. | .. |
---|
194 | 174 | struct cros_ec_debugfs *debug_info = file->private_data; |
---|
195 | 175 | __poll_t mask = 0; |
---|
196 | 176 | |
---|
197 | | - poll_wait(file, &debug_info->log_wq, wait); |
---|
| 177 | + poll_wait(file, &cros_ec_debugfs_log_wq, wait); |
---|
198 | 178 | |
---|
199 | 179 | mutex_lock(&debug_info->log_mutex); |
---|
200 | 180 | if (CIRC_CNT(debug_info->log_buffer.head, |
---|
.. | .. |
---|
263 | 243 | read_buf, p - read_buf); |
---|
264 | 244 | } |
---|
265 | 245 | |
---|
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 = { |
---|
267 | 294 | .owner = THIS_MODULE, |
---|
268 | 295 | .open = cros_ec_console_log_open, |
---|
269 | 296 | .read = cros_ec_console_log_read, |
---|
.. | .. |
---|
272 | 299 | .release = cros_ec_console_log_release, |
---|
273 | 300 | }; |
---|
274 | 301 | |
---|
275 | | -const struct file_operations cros_ec_pdinfo_fops = { |
---|
| 302 | +static const struct file_operations cros_ec_pdinfo_fops = { |
---|
276 | 303 | .owner = THIS_MODULE, |
---|
277 | 304 | .open = simple_open, |
---|
278 | 305 | .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, |
---|
279 | 313 | .llseek = default_llseek, |
---|
280 | 314 | }; |
---|
281 | 315 | |
---|
.. | .. |
---|
300 | 334 | params->cmd = EC_CMD_CONSOLE_READ; |
---|
301 | 335 | response = (struct ec_response_get_cmd_versions *)msg->data; |
---|
302 | 336 | |
---|
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); |
---|
306 | 339 | |
---|
307 | 340 | kfree(msg); |
---|
308 | 341 | |
---|
.. | .. |
---|
316 | 349 | int read_params_size; |
---|
317 | 350 | int read_response_size; |
---|
318 | 351 | |
---|
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)) |
---|
322 | 357 | return 0; |
---|
323 | | - } |
---|
324 | 358 | |
---|
325 | 359 | buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL); |
---|
326 | 360 | if (!buf) |
---|
.. | .. |
---|
344 | 378 | debug_info->log_buffer.tail = 0; |
---|
345 | 379 | |
---|
346 | 380 | mutex_init(&debug_info->log_mutex); |
---|
347 | | - init_waitqueue_head(&debug_info->log_wq); |
---|
348 | 381 | |
---|
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); |
---|
355 | 384 | |
---|
356 | 385 | INIT_DELAYED_WORK(&debug_info->log_poll_work, |
---|
357 | 386 | cros_ec_console_log_work); |
---|
.. | .. |
---|
385 | 414 | msg->command = EC_CMD_GET_PANIC_INFO; |
---|
386 | 415 | msg->insize = insize; |
---|
387 | 416 | |
---|
388 | | - ret = cros_ec_cmd_xfer(ec_dev, msg); |
---|
| 417 | + ret = cros_ec_cmd_xfer_status(ec_dev, msg); |
---|
389 | 418 | if (ret < 0) { |
---|
390 | | - dev_warn(debug_info->ec->dev, "Cannot read panicinfo.\n"); |
---|
391 | 419 | ret = 0; |
---|
392 | 420 | goto free; |
---|
393 | 421 | } |
---|
.. | .. |
---|
399 | 427 | debug_info->panicinfo_blob.data = msg->data; |
---|
400 | 428 | debug_info->panicinfo_blob.size = ret; |
---|
401 | 429 | |
---|
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); |
---|
409 | 432 | |
---|
410 | 433 | return 0; |
---|
411 | 434 | |
---|
.. | .. |
---|
414 | 437 | return ret; |
---|
415 | 438 | } |
---|
416 | 439 | |
---|
417 | | -static int cros_ec_create_pdinfo(struct cros_ec_debugfs *debug_info) |
---|
| 440 | +static int cros_ec_debugfs_probe(struct platform_device *pd) |
---|
418 | 441 | { |
---|
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); |
---|
428 | 443 | struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev); |
---|
429 | 444 | const char *name = ec_platform->ec_name; |
---|
430 | 445 | struct cros_ec_debugfs *debug_info; |
---|
.. | .. |
---|
436 | 451 | |
---|
437 | 452 | debug_info->ec = ec; |
---|
438 | 453 | debug_info->dir = debugfs_create_dir(name, NULL); |
---|
439 | | - if (!debug_info->dir) |
---|
440 | | - return -ENOMEM; |
---|
441 | 454 | |
---|
442 | 455 | ret = cros_ec_create_panicinfo(debug_info); |
---|
443 | 456 | if (ret) |
---|
.. | .. |
---|
447 | 460 | if (ret) |
---|
448 | 461 | goto remove_debugfs; |
---|
449 | 462 | |
---|
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); |
---|
453 | 472 | |
---|
454 | 473 | ec->debug_info = debug_info; |
---|
| 474 | + |
---|
| 475 | + dev_set_drvdata(&pd->dev, ec); |
---|
455 | 476 | |
---|
456 | 477 | return 0; |
---|
457 | 478 | |
---|
.. | .. |
---|
459 | 480 | debugfs_remove_recursive(debug_info->dir); |
---|
460 | 481 | return ret; |
---|
461 | 482 | } |
---|
462 | | -EXPORT_SYMBOL(cros_ec_debugfs_init); |
---|
463 | 483 | |
---|
464 | | -void cros_ec_debugfs_remove(struct cros_ec_dev *ec) |
---|
| 484 | +static int cros_ec_debugfs_remove(struct platform_device *pd) |
---|
465 | 485 | { |
---|
466 | | - if (!ec->debug_info) |
---|
467 | | - return; |
---|
| 486 | + struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent); |
---|
468 | 487 | |
---|
469 | 488 | debugfs_remove_recursive(ec->debug_info->dir); |
---|
470 | 489 | cros_ec_cleanup_console_log(ec->debug_info); |
---|
471 | | -} |
---|
472 | | -EXPORT_SYMBOL(cros_ec_debugfs_remove); |
---|
473 | 490 | |
---|
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) |
---|
475 | 495 | { |
---|
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) |
---|
483 | 499 | cancel_delayed_work_sync(&ec->debug_info->log_poll_work); |
---|
484 | | -} |
---|
485 | | -EXPORT_SYMBOL(cros_ec_debugfs_suspend); |
---|
486 | 500 | |
---|
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; |
---|
491 | 502 | } |
---|
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); |
---|