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