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