2 * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
3 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The names of the above-listed copyright holders may not be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
18 * ALTERNATIVELY, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2, as published by the Free
20 * Software Foundation.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <linux/debugfs.h>
37 #include "vchiq_core.h"
38 #include "vchiq_arm.h"
39 #include "vchiq_debugfs.h"
41 #ifdef CONFIG_DEBUG_FS
43 /****************************************************************************
45 * log category entries
47 ***************************************************************************/
48 #define DEBUGFS_WRITE_BUF_SIZE 256
50 #define VCHIQ_LOG_ERROR_STR "error"
51 #define VCHIQ_LOG_WARNING_STR "warning"
52 #define VCHIQ_LOG_INFO_STR "info"
53 #define VCHIQ_LOG_TRACE_STR "trace"
56 /* Top-level debug info */
57 struct vchiq_debugfs_info {
58 /* Global 'vchiq' debugfs entry used by all instances */
59 struct dentry *vchiq_cfg_dir;
61 /* one entry per client process */
62 struct dentry *clients;
65 struct dentry *log_categories;
68 static struct vchiq_debugfs_info debugfs_info;
70 /* Log category debugfs entries */
71 struct vchiq_debugfs_log_entry {
77 static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
78 { "core", &vchiq_core_log_level },
79 { "msg", &vchiq_core_msg_log_level },
80 { "sync", &vchiq_sync_log_level },
81 { "susp", &vchiq_susp_log_level },
82 { "arm", &vchiq_arm_log_level },
84 static int n_log_entries =
85 sizeof(vchiq_debugfs_log_entries)/sizeof(vchiq_debugfs_log_entries[0]);
88 static struct dentry *vchiq_clients_top(void);
89 static struct dentry *vchiq_debugfs_top(void);
91 static int debugfs_log_show(struct seq_file *f, void *offset)
93 int *levp = f->private;
94 char *log_value = NULL;
98 log_value = VCHIQ_LOG_ERROR_STR;
100 case VCHIQ_LOG_WARNING:
101 log_value = VCHIQ_LOG_WARNING_STR;
104 log_value = VCHIQ_LOG_INFO_STR;
106 case VCHIQ_LOG_TRACE:
107 log_value = VCHIQ_LOG_TRACE_STR;
113 seq_printf(f, "%s\n", log_value ? log_value : "(null)");
118 static int debugfs_log_open(struct inode *inode, struct file *file)
120 return single_open(file, debugfs_log_show, inode->i_private);
123 static int debugfs_log_write(struct file *file,
124 const char __user *buffer,
125 size_t count, loff_t *ppos)
127 struct seq_file *f = (struct seq_file *)file->private_data;
128 int *levp = f->private;
129 char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
131 memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
132 if (count >= DEBUGFS_WRITE_BUF_SIZE)
133 count = DEBUGFS_WRITE_BUF_SIZE;
135 if (copy_from_user(kbuf, buffer, count) != 0)
139 if (strncmp("error", kbuf, strlen("error")) == 0)
140 *levp = VCHIQ_LOG_ERROR;
141 else if (strncmp("warning", kbuf, strlen("warning")) == 0)
142 *levp = VCHIQ_LOG_WARNING;
143 else if (strncmp("info", kbuf, strlen("info")) == 0)
144 *levp = VCHIQ_LOG_INFO;
145 else if (strncmp("trace", kbuf, strlen("trace")) == 0)
146 *levp = VCHIQ_LOG_TRACE;
148 *levp = VCHIQ_LOG_DEFAULT;
155 static const struct file_operations debugfs_log_fops = {
156 .owner = THIS_MODULE,
157 .open = debugfs_log_open,
158 .write = debugfs_log_write,
161 .release = single_release,
164 /* create an entry under <debugfs>/vchiq/log for each log category */
165 static int vchiq_debugfs_create_log_entries(struct dentry *top)
170 dir = debugfs_create_dir("log", vchiq_debugfs_top());
173 debugfs_info.log_categories = dir;
175 for (i = 0; i < n_log_entries; i++) {
176 void *levp = (void *)vchiq_debugfs_log_entries[i].plevel;
177 dir = debugfs_create_file(vchiq_debugfs_log_entries[i].name,
179 debugfs_info.log_categories,
187 vchiq_debugfs_log_entries[i].dir = dir;
192 static int debugfs_usecount_show(struct seq_file *f, void *offset)
194 VCHIQ_INSTANCE_T instance = f->private;
197 use_count = vchiq_instance_get_use_count(instance);
198 seq_printf(f, "%d\n", use_count);
203 static int debugfs_usecount_open(struct inode *inode, struct file *file)
205 return single_open(file, debugfs_usecount_show, inode->i_private);
208 static const struct file_operations debugfs_usecount_fops = {
209 .owner = THIS_MODULE,
210 .open = debugfs_usecount_open,
213 .release = single_release,
216 static int debugfs_trace_show(struct seq_file *f, void *offset)
218 VCHIQ_INSTANCE_T instance = f->private;
221 trace = vchiq_instance_get_trace(instance);
222 seq_printf(f, "%s\n", trace ? "Y" : "N");
227 static int debugfs_trace_open(struct inode *inode, struct file *file)
229 return single_open(file, debugfs_trace_show, inode->i_private);
232 static int debugfs_trace_write(struct file *file,
233 const char __user *buffer,
234 size_t count, loff_t *ppos)
236 struct seq_file *f = (struct seq_file *)file->private_data;
237 VCHIQ_INSTANCE_T instance = f->private;
240 if (copy_from_user(&firstchar, buffer, 1) != 0)
247 vchiq_instance_set_trace(instance, 1);
252 vchiq_instance_set_trace(instance, 0);
263 static const struct file_operations debugfs_trace_fops = {
264 .owner = THIS_MODULE,
265 .open = debugfs_trace_open,
266 .write = debugfs_trace_write,
269 .release = single_release,
272 /* add an instance (process) to the debugfs entries */
273 int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
276 struct dentry *top, *use_count, *trace;
277 struct dentry *clients = vchiq_clients_top();
279 snprintf(pidstr, sizeof(pidstr), "%d",
280 vchiq_instance_get_pid(instance));
282 top = debugfs_create_dir(pidstr, clients);
286 use_count = debugfs_create_file("use_count",
289 &debugfs_usecount_fops);
293 trace = debugfs_create_file("trace",
296 &debugfs_trace_fops);
300 vchiq_instance_get_debugfs_node(instance)->dentry = top;
305 debugfs_remove(use_count);
312 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
314 VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
315 debugfs_remove_recursive(node->dentry);
319 int vchiq_debugfs_init(void)
321 BUG_ON(debugfs_info.vchiq_cfg_dir != NULL);
323 debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL);
324 if (debugfs_info.vchiq_cfg_dir == NULL)
327 debugfs_info.clients = debugfs_create_dir("clients",
328 vchiq_debugfs_top());
329 if (!debugfs_info.clients)
332 if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0)
338 vchiq_debugfs_deinit();
339 vchiq_log_error(vchiq_arm_log_level,
340 "%s: failed to create debugfs directory",
346 /* remove all the debugfs entries */
347 void vchiq_debugfs_deinit(void)
349 debugfs_remove_recursive(vchiq_debugfs_top());
352 static struct dentry *vchiq_clients_top(void)
354 return debugfs_info.clients;
357 static struct dentry *vchiq_debugfs_top(void)
359 BUG_ON(debugfs_info.vchiq_cfg_dir == NULL);
360 return debugfs_info.vchiq_cfg_dir;
363 #else /* CONFIG_DEBUG_FS */
365 int vchiq_debugfs_init(void)
370 void vchiq_debugfs_deinit(void)
374 int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
379 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
383 #endif /* CONFIG_DEBUG_FS */