Loading...
1// SPDX-License-Identifier: GPL-2.0+
2// Debug logs for the ChromeOS EC
3//
4// Copyright (C) 2015 Google, Inc.
5
6#include <linux/circ_buf.h>
7#include <linux/debugfs.h>
8#include <linux/delay.h>
9#include <linux/fs.h>
10#include <linux/mod_devicetable.h>
11#include <linux/module.h>
12#include <linux/mutex.h>
13#include <linux/platform_data/cros_ec_commands.h>
14#include <linux/platform_data/cros_ec_proto.h>
15#include <linux/platform_device.h>
16#include <linux/poll.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/wait.h>
20
21#define DRV_NAME "cros-ec-debugfs"
22
23#define LOG_SHIFT 14
24#define LOG_SIZE (1 << LOG_SHIFT)
25#define LOG_POLL_SEC 10
26
27#define CIRC_ADD(idx, size, value) (((idx) + (value)) & ((size) - 1))
28
29static unsigned int log_poll_period_ms = LOG_POLL_SEC * MSEC_PER_SEC;
30module_param(log_poll_period_ms, uint, 0644);
31MODULE_PARM_DESC(log_poll_period_ms, "EC log polling period(ms)");
32
33/* waitqueue for log readers */
34static DECLARE_WAIT_QUEUE_HEAD(cros_ec_debugfs_log_wq);
35
36/**
37 * struct cros_ec_debugfs - EC debugging information.
38 *
39 * @ec: EC device this debugfs information belongs to
40 * @dir: dentry for debugfs files
41 * @log_buffer: circular buffer for console log information
42 * @read_msg: preallocated EC command and buffer to read console log
43 * @log_mutex: mutex to protect circular buffer
44 * @log_poll_work: recurring task to poll EC for new console log data
45 * @panicinfo_blob: panicinfo debugfs blob
46 * @notifier_panic: notifier_block to let kernel to flush buffered log
47 * when EC panic
48 */
49struct cros_ec_debugfs {
50 struct cros_ec_dev *ec;
51 struct dentry *dir;
52 /* EC log */
53 struct circ_buf log_buffer;
54 struct cros_ec_command *read_msg;
55 struct mutex log_mutex;
56 struct delayed_work log_poll_work;
57 /* EC panicinfo */
58 struct debugfs_blob_wrapper panicinfo_blob;
59 struct notifier_block notifier_panic;
60};
61
62/*
63 * We need to make sure that the EC log buffer on the UART is large enough,
64 * so that it is unlikely enough to overlow within log_poll_period_ms.
65 */
66static void cros_ec_console_log_work(struct work_struct *__work)
67{
68 struct cros_ec_debugfs *debug_info =
69 container_of(to_delayed_work(__work),
70 struct cros_ec_debugfs,
71 log_poll_work);
72 struct cros_ec_dev *ec = debug_info->ec;
73 struct circ_buf *cb = &debug_info->log_buffer;
74 struct cros_ec_command snapshot_msg = {
75 .command = EC_CMD_CONSOLE_SNAPSHOT + ec->cmd_offset,
76 };
77
78 struct ec_params_console_read_v1 *read_params =
79 (struct ec_params_console_read_v1 *)debug_info->read_msg->data;
80 uint8_t *ec_buffer = (uint8_t *)debug_info->read_msg->data;
81 int idx;
82 int buf_space;
83 int ret;
84
85 ret = cros_ec_cmd_xfer_status(ec->ec_dev, &snapshot_msg);
86 if (ret < 0)
87 goto resched;
88
89 /* Loop until we have read everything, or there's an error. */
90 mutex_lock(&debug_info->log_mutex);
91 buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
92
93 while (1) {
94 if (!buf_space) {
95 dev_info_once(ec->dev,
96 "Some logs may have been dropped...\n");
97 break;
98 }
99
100 memset(read_params, '\0', sizeof(*read_params));
101 read_params->subcmd = CONSOLE_READ_RECENT;
102 ret = cros_ec_cmd_xfer_status(ec->ec_dev,
103 debug_info->read_msg);
104 if (ret < 0)
105 break;
106
107 /* If the buffer is empty, we're done here. */
108 if (ret == 0 || ec_buffer[0] == '\0')
109 break;
110
111 idx = 0;
112 while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
113 cb->buf[cb->head] = ec_buffer[idx];
114 cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
115 idx++;
116 buf_space--;
117 }
118
119 wake_up(&cros_ec_debugfs_log_wq);
120 }
121
122 mutex_unlock(&debug_info->log_mutex);
123
124resched:
125 schedule_delayed_work(&debug_info->log_poll_work,
126 msecs_to_jiffies(log_poll_period_ms));
127}
128
129static int cros_ec_console_log_open(struct inode *inode, struct file *file)
130{
131 file->private_data = inode->i_private;
132
133 return stream_open(inode, file);
134}
135
136static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
137 size_t count, loff_t *ppos)
138{
139 struct cros_ec_debugfs *debug_info = file->private_data;
140 struct circ_buf *cb = &debug_info->log_buffer;
141 ssize_t ret;
142
143 mutex_lock(&debug_info->log_mutex);
144
145 while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
146 if (file->f_flags & O_NONBLOCK) {
147 ret = -EAGAIN;
148 goto error;
149 }
150
151 mutex_unlock(&debug_info->log_mutex);
152
153 ret = wait_event_interruptible(cros_ec_debugfs_log_wq,
154 CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
155 if (ret < 0)
156 return ret;
157
158 mutex_lock(&debug_info->log_mutex);
159 }
160
161 /* Only copy until the end of the circular buffer, and let userspace
162 * retry to get the rest of the data.
163 */
164 ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
165 count);
166
167 if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
168 ret = -EFAULT;
169 goto error;
170 }
171
172 cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
173
174error:
175 mutex_unlock(&debug_info->log_mutex);
176 return ret;
177}
178
179static __poll_t cros_ec_console_log_poll(struct file *file,
180 poll_table *wait)
181{
182 struct cros_ec_debugfs *debug_info = file->private_data;
183 __poll_t mask = 0;
184
185 poll_wait(file, &cros_ec_debugfs_log_wq, wait);
186
187 mutex_lock(&debug_info->log_mutex);
188 if (CIRC_CNT(debug_info->log_buffer.head,
189 debug_info->log_buffer.tail,
190 LOG_SIZE))
191 mask |= EPOLLIN | EPOLLRDNORM;
192 mutex_unlock(&debug_info->log_mutex);
193
194 return mask;
195}
196
197static int cros_ec_console_log_release(struct inode *inode, struct file *file)
198{
199 return 0;
200}
201
202static ssize_t cros_ec_pdinfo_read(struct file *file,
203 char __user *user_buf,
204 size_t count,
205 loff_t *ppos)
206{
207 char read_buf[EC_USB_PD_MAX_PORTS * 40], *p = read_buf;
208 struct cros_ec_debugfs *debug_info = file->private_data;
209 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
210 struct {
211 struct cros_ec_command msg;
212 union {
213 struct ec_response_usb_pd_control_v1 resp;
214 struct ec_params_usb_pd_control params;
215 };
216 } __packed ec_buf;
217 struct cros_ec_command *msg;
218 struct ec_response_usb_pd_control_v1 *resp;
219 struct ec_params_usb_pd_control *params;
220 int i;
221
222 msg = &ec_buf.msg;
223 params = (struct ec_params_usb_pd_control *)msg->data;
224 resp = (struct ec_response_usb_pd_control_v1 *)msg->data;
225
226 msg->command = EC_CMD_USB_PD_CONTROL;
227 msg->version = 1;
228 msg->insize = sizeof(*resp);
229 msg->outsize = sizeof(*params);
230
231 /*
232 * Read status from all PD ports until failure, typically caused
233 * by attempting to read status on a port that doesn't exist.
234 */
235 for (i = 0; i < EC_USB_PD_MAX_PORTS; ++i) {
236 params->port = i;
237 params->role = 0;
238 params->mux = 0;
239 params->swap = 0;
240
241 if (cros_ec_cmd_xfer_status(ec_dev, msg) < 0)
242 break;
243
244 p += scnprintf(p, sizeof(read_buf) + read_buf - p,
245 "p%d: %s en:%.2x role:%.2x pol:%.2x\n", i,
246 resp->state, resp->enabled, resp->role,
247 resp->polarity);
248 }
249
250 return simple_read_from_buffer(user_buf, count, ppos,
251 read_buf, p - read_buf);
252}
253
254static bool cros_ec_uptime_is_supported(struct cros_ec_device *ec_dev)
255{
256 struct {
257 struct cros_ec_command cmd;
258 struct ec_response_uptime_info resp;
259 } __packed msg = {};
260 int ret;
261
262 msg.cmd.command = EC_CMD_GET_UPTIME_INFO;
263 msg.cmd.insize = sizeof(msg.resp);
264
265 ret = cros_ec_cmd_xfer_status(ec_dev, &msg.cmd);
266 if (ret == -EPROTO && msg.cmd.result == EC_RES_INVALID_COMMAND)
267 return false;
268
269 /* Other errors maybe a transient error, do not rule about support. */
270 return true;
271}
272
273static ssize_t cros_ec_uptime_read(struct file *file, char __user *user_buf,
274 size_t count, loff_t *ppos)
275{
276 struct cros_ec_debugfs *debug_info = file->private_data;
277 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
278 struct {
279 struct cros_ec_command cmd;
280 struct ec_response_uptime_info resp;
281 } __packed msg = {};
282 struct ec_response_uptime_info *resp;
283 char read_buf[32];
284 int ret;
285
286 resp = (struct ec_response_uptime_info *)&msg.resp;
287
288 msg.cmd.command = EC_CMD_GET_UPTIME_INFO;
289 msg.cmd.insize = sizeof(*resp);
290
291 ret = cros_ec_cmd_xfer_status(ec_dev, &msg.cmd);
292 if (ret < 0)
293 return ret;
294
295 ret = scnprintf(read_buf, sizeof(read_buf), "%u\n",
296 resp->time_since_ec_boot_ms);
297
298 return simple_read_from_buffer(user_buf, count, ppos, read_buf, ret);
299}
300
301static const struct file_operations cros_ec_console_log_fops = {
302 .owner = THIS_MODULE,
303 .open = cros_ec_console_log_open,
304 .read = cros_ec_console_log_read,
305 .poll = cros_ec_console_log_poll,
306 .release = cros_ec_console_log_release,
307};
308
309static const struct file_operations cros_ec_pdinfo_fops = {
310 .owner = THIS_MODULE,
311 .open = simple_open,
312 .read = cros_ec_pdinfo_read,
313 .llseek = default_llseek,
314};
315
316static const struct file_operations cros_ec_uptime_fops = {
317 .owner = THIS_MODULE,
318 .open = simple_open,
319 .read = cros_ec_uptime_read,
320 .llseek = default_llseek,
321};
322
323static int ec_read_version_supported(struct cros_ec_dev *ec)
324{
325 struct ec_params_get_cmd_versions_v1 *params;
326 struct ec_response_get_cmd_versions *response;
327 int ret;
328
329 struct cros_ec_command *msg;
330
331 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)),
332 GFP_KERNEL);
333 if (!msg)
334 return 0;
335
336 msg->version = 1;
337 msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
338 msg->outsize = sizeof(*params);
339 msg->insize = sizeof(*response);
340
341 params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
342 params->cmd = EC_CMD_CONSOLE_READ;
343 response = (struct ec_response_get_cmd_versions *)msg->data;
344
345 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg) >= 0 &&
346 response->version_mask & EC_VER_MASK(1);
347
348 kfree(msg);
349
350 return ret;
351}
352
353static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
354{
355 struct cros_ec_dev *ec = debug_info->ec;
356 char *buf;
357 int read_params_size;
358 int read_response_size;
359
360 /*
361 * If the console log feature is not supported return silently and
362 * don't create the console_log entry.
363 */
364 if (!ec_read_version_supported(ec))
365 return 0;
366
367 buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
368 if (!buf)
369 return -ENOMEM;
370
371 read_params_size = sizeof(struct ec_params_console_read_v1);
372 read_response_size = ec->ec_dev->max_response;
373 debug_info->read_msg = devm_kzalloc(ec->dev,
374 sizeof(*debug_info->read_msg) +
375 max(read_params_size, read_response_size), GFP_KERNEL);
376 if (!debug_info->read_msg)
377 return -ENOMEM;
378
379 debug_info->read_msg->version = 1;
380 debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
381 debug_info->read_msg->outsize = read_params_size;
382 debug_info->read_msg->insize = read_response_size;
383
384 debug_info->log_buffer.buf = buf;
385 debug_info->log_buffer.head = 0;
386 debug_info->log_buffer.tail = 0;
387
388 mutex_init(&debug_info->log_mutex);
389
390 debugfs_create_file("console_log", S_IFREG | 0444, debug_info->dir,
391 debug_info, &cros_ec_console_log_fops);
392
393 INIT_DELAYED_WORK(&debug_info->log_poll_work,
394 cros_ec_console_log_work);
395 schedule_delayed_work(&debug_info->log_poll_work, 0);
396
397 return 0;
398}
399
400static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
401{
402 if (debug_info->log_buffer.buf) {
403 cancel_delayed_work_sync(&debug_info->log_poll_work);
404 mutex_destroy(&debug_info->log_mutex);
405 }
406}
407
408/*
409 * Returns the size of the panicinfo data fetched from the EC
410 */
411static int cros_ec_get_panicinfo(struct cros_ec_device *ec_dev, uint8_t *data,
412 int data_size)
413{
414 int ret;
415 struct cros_ec_command *msg;
416
417 if (!data || data_size <= 0 || data_size > ec_dev->max_response)
418 return -EINVAL;
419
420 msg = kzalloc(sizeof(*msg) + data_size, GFP_KERNEL);
421 if (!msg)
422 return -ENOMEM;
423
424 msg->command = EC_CMD_GET_PANIC_INFO;
425 msg->insize = data_size;
426
427 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
428 if (ret < 0)
429 goto free;
430
431 memcpy(data, msg->data, data_size);
432
433free:
434 kfree(msg);
435 return ret;
436}
437
438static int cros_ec_create_panicinfo(struct cros_ec_debugfs *debug_info)
439{
440 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
441 int ret;
442 void *data;
443
444 data = devm_kzalloc(debug_info->ec->dev, ec_dev->max_response,
445 GFP_KERNEL);
446 if (!data)
447 return -ENOMEM;
448
449 ret = cros_ec_get_panicinfo(ec_dev, data, ec_dev->max_response);
450 if (ret < 0) {
451 ret = 0;
452 goto free;
453 }
454
455 /* No panic data */
456 if (ret == 0)
457 goto free;
458
459 debug_info->panicinfo_blob.data = data;
460 debug_info->panicinfo_blob.size = ret;
461
462 debugfs_create_blob("panicinfo", 0444, debug_info->dir,
463 &debug_info->panicinfo_blob);
464
465 return 0;
466
467free:
468 devm_kfree(debug_info->ec->dev, data);
469 return ret;
470}
471
472static int cros_ec_debugfs_panic_event(struct notifier_block *nb,
473 unsigned long queued_during_suspend, void *_notify)
474{
475 struct cros_ec_debugfs *debug_info =
476 container_of(nb, struct cros_ec_debugfs, notifier_panic);
477
478 if (debug_info->log_buffer.buf) {
479 /* Force log poll work to run immediately */
480 mod_delayed_work(debug_info->log_poll_work.wq, &debug_info->log_poll_work, 0);
481 /* Block until log poll work finishes */
482 flush_delayed_work(&debug_info->log_poll_work);
483 }
484
485 return NOTIFY_DONE;
486}
487
488static int cros_ec_debugfs_probe(struct platform_device *pd)
489{
490 struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
491 struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
492 const char *name = ec_platform->ec_name;
493 struct cros_ec_debugfs *debug_info;
494 int ret;
495
496 debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
497 if (!debug_info)
498 return -ENOMEM;
499
500 debug_info->ec = ec;
501 debug_info->dir = debugfs_create_dir(name, NULL);
502
503 ret = cros_ec_create_panicinfo(debug_info);
504 if (ret)
505 goto remove_debugfs;
506
507 ret = cros_ec_create_console_log(debug_info);
508 if (ret)
509 goto remove_debugfs;
510
511 debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
512 &cros_ec_pdinfo_fops);
513
514 if (cros_ec_uptime_is_supported(ec->ec_dev))
515 debugfs_create_file("uptime", 0444, debug_info->dir, debug_info,
516 &cros_ec_uptime_fops);
517
518 debugfs_create_x32("last_resume_result", 0444, debug_info->dir,
519 &ec->ec_dev->last_resume_result);
520
521 debugfs_create_u16("suspend_timeout_ms", 0664, debug_info->dir,
522 &ec->ec_dev->suspend_timeout_ms);
523
524 debug_info->notifier_panic.notifier_call = cros_ec_debugfs_panic_event;
525 ret = blocking_notifier_chain_register(&ec->ec_dev->panic_notifier,
526 &debug_info->notifier_panic);
527 if (ret)
528 goto remove_debugfs;
529
530 ec->debug_info = debug_info;
531
532 dev_set_drvdata(&pd->dev, ec);
533
534 return 0;
535
536remove_debugfs:
537 debugfs_remove_recursive(debug_info->dir);
538 return ret;
539}
540
541static void cros_ec_debugfs_remove(struct platform_device *pd)
542{
543 struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
544
545 debugfs_remove_recursive(ec->debug_info->dir);
546 cros_ec_cleanup_console_log(ec->debug_info);
547}
548
549static int __maybe_unused cros_ec_debugfs_suspend(struct device *dev)
550{
551 struct cros_ec_dev *ec = dev_get_drvdata(dev);
552
553 if (ec->debug_info->log_buffer.buf)
554 cancel_delayed_work_sync(&ec->debug_info->log_poll_work);
555
556 return 0;
557}
558
559static int __maybe_unused cros_ec_debugfs_resume(struct device *dev)
560{
561 struct cros_ec_dev *ec = dev_get_drvdata(dev);
562
563 if (ec->debug_info->log_buffer.buf)
564 schedule_delayed_work(&ec->debug_info->log_poll_work, 0);
565
566 return 0;
567}
568
569static SIMPLE_DEV_PM_OPS(cros_ec_debugfs_pm_ops,
570 cros_ec_debugfs_suspend, cros_ec_debugfs_resume);
571
572static const struct platform_device_id cros_ec_debugfs_id[] = {
573 { DRV_NAME, 0 },
574 {}
575};
576MODULE_DEVICE_TABLE(platform, cros_ec_debugfs_id);
577
578static struct platform_driver cros_ec_debugfs_driver = {
579 .driver = {
580 .name = DRV_NAME,
581 .pm = &cros_ec_debugfs_pm_ops,
582 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
583 },
584 .probe = cros_ec_debugfs_probe,
585 .remove = cros_ec_debugfs_remove,
586 .id_table = cros_ec_debugfs_id,
587};
588
589module_platform_driver(cros_ec_debugfs_driver);
590
591MODULE_LICENSE("GPL");
592MODULE_DESCRIPTION("Debug logs for ChromeOS EC");
1// SPDX-License-Identifier: GPL-2.0+
2// Debug logs for the ChromeOS EC
3//
4// Copyright (C) 2015 Google, Inc.
5
6#include <linux/circ_buf.h>
7#include <linux/debugfs.h>
8#include <linux/delay.h>
9#include <linux/fs.h>
10#include <linux/mfd/cros_ec.h>
11#include <linux/module.h>
12#include <linux/mutex.h>
13#include <linux/platform_data/cros_ec_commands.h>
14#include <linux/platform_data/cros_ec_proto.h>
15#include <linux/platform_device.h>
16#include <linux/poll.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/wait.h>
20
21#define DRV_NAME "cros-ec-debugfs"
22
23#define LOG_SHIFT 14
24#define LOG_SIZE (1 << LOG_SHIFT)
25#define LOG_POLL_SEC 10
26
27#define CIRC_ADD(idx, size, value) (((idx) + (value)) & ((size) - 1))
28
29/**
30 * struct cros_ec_debugfs - EC debugging information.
31 *
32 * @ec: EC device this debugfs information belongs to
33 * @dir: dentry for debugfs files
34 * @log_buffer: circular buffer for console log information
35 * @read_msg: preallocated EC command and buffer to read console log
36 * @log_mutex: mutex to protect circular buffer
37 * @log_wq: waitqueue for log readers
38 * @log_poll_work: recurring task to poll EC for new console log data
39 * @panicinfo_blob: panicinfo debugfs blob
40 */
41struct cros_ec_debugfs {
42 struct cros_ec_dev *ec;
43 struct dentry *dir;
44 /* EC log */
45 struct circ_buf log_buffer;
46 struct cros_ec_command *read_msg;
47 struct mutex log_mutex;
48 wait_queue_head_t log_wq;
49 struct delayed_work log_poll_work;
50 /* EC panicinfo */
51 struct debugfs_blob_wrapper panicinfo_blob;
52};
53
54/*
55 * We need to make sure that the EC log buffer on the UART is large enough,
56 * so that it is unlikely enough to overlow within LOG_POLL_SEC.
57 */
58static void cros_ec_console_log_work(struct work_struct *__work)
59{
60 struct cros_ec_debugfs *debug_info =
61 container_of(to_delayed_work(__work),
62 struct cros_ec_debugfs,
63 log_poll_work);
64 struct cros_ec_dev *ec = debug_info->ec;
65 struct circ_buf *cb = &debug_info->log_buffer;
66 struct cros_ec_command snapshot_msg = {
67 .command = EC_CMD_CONSOLE_SNAPSHOT + ec->cmd_offset,
68 };
69
70 struct ec_params_console_read_v1 *read_params =
71 (struct ec_params_console_read_v1 *)debug_info->read_msg->data;
72 uint8_t *ec_buffer = (uint8_t *)debug_info->read_msg->data;
73 int idx;
74 int buf_space;
75 int ret;
76
77 ret = cros_ec_cmd_xfer_status(ec->ec_dev, &snapshot_msg);
78 if (ret < 0)
79 goto resched;
80
81 /* Loop until we have read everything, or there's an error. */
82 mutex_lock(&debug_info->log_mutex);
83 buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
84
85 while (1) {
86 if (!buf_space) {
87 dev_info_once(ec->dev,
88 "Some logs may have been dropped...\n");
89 break;
90 }
91
92 memset(read_params, '\0', sizeof(*read_params));
93 read_params->subcmd = CONSOLE_READ_RECENT;
94 ret = cros_ec_cmd_xfer_status(ec->ec_dev,
95 debug_info->read_msg);
96 if (ret < 0)
97 break;
98
99 /* If the buffer is empty, we're done here. */
100 if (ret == 0 || ec_buffer[0] == '\0')
101 break;
102
103 idx = 0;
104 while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
105 cb->buf[cb->head] = ec_buffer[idx];
106 cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
107 idx++;
108 buf_space--;
109 }
110
111 wake_up(&debug_info->log_wq);
112 }
113
114 mutex_unlock(&debug_info->log_mutex);
115
116resched:
117 schedule_delayed_work(&debug_info->log_poll_work,
118 msecs_to_jiffies(LOG_POLL_SEC * 1000));
119}
120
121static int cros_ec_console_log_open(struct inode *inode, struct file *file)
122{
123 file->private_data = inode->i_private;
124
125 return stream_open(inode, file);
126}
127
128static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
129 size_t count, loff_t *ppos)
130{
131 struct cros_ec_debugfs *debug_info = file->private_data;
132 struct circ_buf *cb = &debug_info->log_buffer;
133 ssize_t ret;
134
135 mutex_lock(&debug_info->log_mutex);
136
137 while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
138 if (file->f_flags & O_NONBLOCK) {
139 ret = -EAGAIN;
140 goto error;
141 }
142
143 mutex_unlock(&debug_info->log_mutex);
144
145 ret = wait_event_interruptible(debug_info->log_wq,
146 CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
147 if (ret < 0)
148 return ret;
149
150 mutex_lock(&debug_info->log_mutex);
151 }
152
153 /* Only copy until the end of the circular buffer, and let userspace
154 * retry to get the rest of the data.
155 */
156 ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
157 count);
158
159 if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
160 ret = -EFAULT;
161 goto error;
162 }
163
164 cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
165
166error:
167 mutex_unlock(&debug_info->log_mutex);
168 return ret;
169}
170
171static __poll_t cros_ec_console_log_poll(struct file *file,
172 poll_table *wait)
173{
174 struct cros_ec_debugfs *debug_info = file->private_data;
175 __poll_t mask = 0;
176
177 poll_wait(file, &debug_info->log_wq, wait);
178
179 mutex_lock(&debug_info->log_mutex);
180 if (CIRC_CNT(debug_info->log_buffer.head,
181 debug_info->log_buffer.tail,
182 LOG_SIZE))
183 mask |= EPOLLIN | EPOLLRDNORM;
184 mutex_unlock(&debug_info->log_mutex);
185
186 return mask;
187}
188
189static int cros_ec_console_log_release(struct inode *inode, struct file *file)
190{
191 return 0;
192}
193
194static ssize_t cros_ec_pdinfo_read(struct file *file,
195 char __user *user_buf,
196 size_t count,
197 loff_t *ppos)
198{
199 char read_buf[EC_USB_PD_MAX_PORTS * 40], *p = read_buf;
200 struct cros_ec_debugfs *debug_info = file->private_data;
201 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
202 struct {
203 struct cros_ec_command msg;
204 union {
205 struct ec_response_usb_pd_control_v1 resp;
206 struct ec_params_usb_pd_control params;
207 };
208 } __packed ec_buf;
209 struct cros_ec_command *msg;
210 struct ec_response_usb_pd_control_v1 *resp;
211 struct ec_params_usb_pd_control *params;
212 int i;
213
214 msg = &ec_buf.msg;
215 params = (struct ec_params_usb_pd_control *)msg->data;
216 resp = (struct ec_response_usb_pd_control_v1 *)msg->data;
217
218 msg->command = EC_CMD_USB_PD_CONTROL;
219 msg->version = 1;
220 msg->insize = sizeof(*resp);
221 msg->outsize = sizeof(*params);
222
223 /*
224 * Read status from all PD ports until failure, typically caused
225 * by attempting to read status on a port that doesn't exist.
226 */
227 for (i = 0; i < EC_USB_PD_MAX_PORTS; ++i) {
228 params->port = i;
229 params->role = 0;
230 params->mux = 0;
231 params->swap = 0;
232
233 if (cros_ec_cmd_xfer_status(ec_dev, msg) < 0)
234 break;
235
236 p += scnprintf(p, sizeof(read_buf) + read_buf - p,
237 "p%d: %s en:%.2x role:%.2x pol:%.2x\n", i,
238 resp->state, resp->enabled, resp->role,
239 resp->polarity);
240 }
241
242 return simple_read_from_buffer(user_buf, count, ppos,
243 read_buf, p - read_buf);
244}
245
246static ssize_t cros_ec_uptime_read(struct file *file, char __user *user_buf,
247 size_t count, loff_t *ppos)
248{
249 struct cros_ec_debugfs *debug_info = file->private_data;
250 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
251 struct {
252 struct cros_ec_command cmd;
253 struct ec_response_uptime_info resp;
254 } __packed msg = {};
255 struct ec_response_uptime_info *resp;
256 char read_buf[32];
257 int ret;
258
259 resp = (struct ec_response_uptime_info *)&msg.resp;
260
261 msg.cmd.command = EC_CMD_GET_UPTIME_INFO;
262 msg.cmd.insize = sizeof(*resp);
263
264 ret = cros_ec_cmd_xfer_status(ec_dev, &msg.cmd);
265 if (ret < 0)
266 return ret;
267
268 ret = scnprintf(read_buf, sizeof(read_buf), "%u\n",
269 resp->time_since_ec_boot_ms);
270
271 return simple_read_from_buffer(user_buf, count, ppos, read_buf, ret);
272}
273
274static const struct file_operations cros_ec_console_log_fops = {
275 .owner = THIS_MODULE,
276 .open = cros_ec_console_log_open,
277 .read = cros_ec_console_log_read,
278 .llseek = no_llseek,
279 .poll = cros_ec_console_log_poll,
280 .release = cros_ec_console_log_release,
281};
282
283static const struct file_operations cros_ec_pdinfo_fops = {
284 .owner = THIS_MODULE,
285 .open = simple_open,
286 .read = cros_ec_pdinfo_read,
287 .llseek = default_llseek,
288};
289
290static const struct file_operations cros_ec_uptime_fops = {
291 .owner = THIS_MODULE,
292 .open = simple_open,
293 .read = cros_ec_uptime_read,
294 .llseek = default_llseek,
295};
296
297static int ec_read_version_supported(struct cros_ec_dev *ec)
298{
299 struct ec_params_get_cmd_versions_v1 *params;
300 struct ec_response_get_cmd_versions *response;
301 int ret;
302
303 struct cros_ec_command *msg;
304
305 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)),
306 GFP_KERNEL);
307 if (!msg)
308 return 0;
309
310 msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
311 msg->outsize = sizeof(*params);
312 msg->insize = sizeof(*response);
313
314 params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
315 params->cmd = EC_CMD_CONSOLE_READ;
316 response = (struct ec_response_get_cmd_versions *)msg->data;
317
318 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg) >= 0 &&
319 response->version_mask & EC_VER_MASK(1);
320
321 kfree(msg);
322
323 return ret;
324}
325
326static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
327{
328 struct cros_ec_dev *ec = debug_info->ec;
329 char *buf;
330 int read_params_size;
331 int read_response_size;
332
333 /*
334 * If the console log feature is not supported return silently and
335 * don't create the console_log entry.
336 */
337 if (!ec_read_version_supported(ec))
338 return 0;
339
340 buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
341 if (!buf)
342 return -ENOMEM;
343
344 read_params_size = sizeof(struct ec_params_console_read_v1);
345 read_response_size = ec->ec_dev->max_response;
346 debug_info->read_msg = devm_kzalloc(ec->dev,
347 sizeof(*debug_info->read_msg) +
348 max(read_params_size, read_response_size), GFP_KERNEL);
349 if (!debug_info->read_msg)
350 return -ENOMEM;
351
352 debug_info->read_msg->version = 1;
353 debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
354 debug_info->read_msg->outsize = read_params_size;
355 debug_info->read_msg->insize = read_response_size;
356
357 debug_info->log_buffer.buf = buf;
358 debug_info->log_buffer.head = 0;
359 debug_info->log_buffer.tail = 0;
360
361 mutex_init(&debug_info->log_mutex);
362 init_waitqueue_head(&debug_info->log_wq);
363
364 debugfs_create_file("console_log", S_IFREG | 0444, debug_info->dir,
365 debug_info, &cros_ec_console_log_fops);
366
367 INIT_DELAYED_WORK(&debug_info->log_poll_work,
368 cros_ec_console_log_work);
369 schedule_delayed_work(&debug_info->log_poll_work, 0);
370
371 return 0;
372}
373
374static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
375{
376 if (debug_info->log_buffer.buf) {
377 cancel_delayed_work_sync(&debug_info->log_poll_work);
378 mutex_destroy(&debug_info->log_mutex);
379 }
380}
381
382static int cros_ec_create_panicinfo(struct cros_ec_debugfs *debug_info)
383{
384 struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
385 int ret;
386 struct cros_ec_command *msg;
387 int insize;
388
389 insize = ec_dev->max_response;
390
391 msg = devm_kzalloc(debug_info->ec->dev,
392 sizeof(*msg) + insize, GFP_KERNEL);
393 if (!msg)
394 return -ENOMEM;
395
396 msg->command = EC_CMD_GET_PANIC_INFO;
397 msg->insize = insize;
398
399 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
400 if (ret < 0) {
401 ret = 0;
402 goto free;
403 }
404
405 /* No panic data */
406 if (ret == 0)
407 goto free;
408
409 debug_info->panicinfo_blob.data = msg->data;
410 debug_info->panicinfo_blob.size = ret;
411
412 debugfs_create_blob("panicinfo", S_IFREG | 0444, debug_info->dir,
413 &debug_info->panicinfo_blob);
414
415 return 0;
416
417free:
418 devm_kfree(debug_info->ec->dev, msg);
419 return ret;
420}
421
422static int cros_ec_debugfs_probe(struct platform_device *pd)
423{
424 struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
425 struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
426 const char *name = ec_platform->ec_name;
427 struct cros_ec_debugfs *debug_info;
428 int ret;
429
430 debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
431 if (!debug_info)
432 return -ENOMEM;
433
434 debug_info->ec = ec;
435 debug_info->dir = debugfs_create_dir(name, NULL);
436
437 ret = cros_ec_create_panicinfo(debug_info);
438 if (ret)
439 goto remove_debugfs;
440
441 ret = cros_ec_create_console_log(debug_info);
442 if (ret)
443 goto remove_debugfs;
444
445 debugfs_create_file("pdinfo", 0444, debug_info->dir, debug_info,
446 &cros_ec_pdinfo_fops);
447
448 debugfs_create_file("uptime", 0444, debug_info->dir, debug_info,
449 &cros_ec_uptime_fops);
450
451 debugfs_create_x32("last_resume_result", 0444, debug_info->dir,
452 &ec->ec_dev->last_resume_result);
453
454 ec->debug_info = debug_info;
455
456 dev_set_drvdata(&pd->dev, ec);
457
458 return 0;
459
460remove_debugfs:
461 debugfs_remove_recursive(debug_info->dir);
462 return ret;
463}
464
465static int cros_ec_debugfs_remove(struct platform_device *pd)
466{
467 struct cros_ec_dev *ec = dev_get_drvdata(pd->dev.parent);
468
469 debugfs_remove_recursive(ec->debug_info->dir);
470 cros_ec_cleanup_console_log(ec->debug_info);
471
472 return 0;
473}
474
475static int __maybe_unused cros_ec_debugfs_suspend(struct device *dev)
476{
477 struct cros_ec_dev *ec = dev_get_drvdata(dev);
478
479 if (ec->debug_info->log_buffer.buf)
480 cancel_delayed_work_sync(&ec->debug_info->log_poll_work);
481
482 return 0;
483}
484
485static int __maybe_unused cros_ec_debugfs_resume(struct device *dev)
486{
487 struct cros_ec_dev *ec = dev_get_drvdata(dev);
488
489 if (ec->debug_info->log_buffer.buf)
490 schedule_delayed_work(&ec->debug_info->log_poll_work, 0);
491
492 return 0;
493}
494
495static SIMPLE_DEV_PM_OPS(cros_ec_debugfs_pm_ops,
496 cros_ec_debugfs_suspend, cros_ec_debugfs_resume);
497
498static struct platform_driver cros_ec_debugfs_driver = {
499 .driver = {
500 .name = DRV_NAME,
501 .pm = &cros_ec_debugfs_pm_ops,
502 },
503 .probe = cros_ec_debugfs_probe,
504 .remove = cros_ec_debugfs_remove,
505};
506
507module_platform_driver(cros_ec_debugfs_driver);
508
509MODULE_LICENSE("GPL");
510MODULE_DESCRIPTION("Debug logs for ChromeOS EC");
511MODULE_ALIAS("platform:" DRV_NAME);