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.
35 #include <linux/debugfs.h>
36 #include "vchiq_core.h"
37 #include "vchiq_arm.h"
38 #include "vchiq_debugfs.h"
40 #ifdef CONFIG_DEBUG_FS
42 /****************************************************************************
44 * log category entries
46 ***************************************************************************/
47 #define DEBUGFS_WRITE_BUF_SIZE 256
49 #define VCHIQ_LOG_ERROR_STR "error"
50 #define VCHIQ_LOG_WARNING_STR "warning"
51 #define VCHIQ_LOG_INFO_STR "info"
52 #define VCHIQ_LOG_TRACE_STR "trace"
54 /* Global 'vchiq' debugfs and clients entry used by all instances */
55 static struct dentry *vchiq_dbg_dir;
56 static struct dentry *vchiq_dbg_clients;
58 /* Log category debugfs entries */
59 struct vchiq_debugfs_log_entry {
64 static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
65 { "core", &vchiq_core_log_level },
66 { "msg", &vchiq_core_msg_log_level },
67 { "sync", &vchiq_sync_log_level },
68 { "susp", &vchiq_susp_log_level },
69 { "arm", &vchiq_arm_log_level },
71 static int n_log_entries = ARRAY_SIZE(vchiq_debugfs_log_entries);
73 static int debugfs_log_show(struct seq_file *f, void *offset)
75 int *levp = f->private;
76 char *log_value = NULL;
80 log_value = VCHIQ_LOG_ERROR_STR;
82 case VCHIQ_LOG_WARNING:
83 log_value = VCHIQ_LOG_WARNING_STR;
86 log_value = VCHIQ_LOG_INFO_STR;
89 log_value = VCHIQ_LOG_TRACE_STR;
95 seq_printf(f, "%s\n", log_value ? log_value : "(null)");
100 static int debugfs_log_open(struct inode *inode, struct file *file)
102 return single_open(file, debugfs_log_show, inode->i_private);
105 static ssize_t debugfs_log_write(struct file *file,
106 const char __user *buffer,
107 size_t count, loff_t *ppos)
109 struct seq_file *f = (struct seq_file *)file->private_data;
110 int *levp = f->private;
111 char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
113 memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
114 if (count >= DEBUGFS_WRITE_BUF_SIZE)
115 count = DEBUGFS_WRITE_BUF_SIZE;
117 if (copy_from_user(kbuf, buffer, count) != 0)
121 if (strncmp("error", kbuf, strlen("error")) == 0)
122 *levp = VCHIQ_LOG_ERROR;
123 else if (strncmp("warning", kbuf, strlen("warning")) == 0)
124 *levp = VCHIQ_LOG_WARNING;
125 else if (strncmp("info", kbuf, strlen("info")) == 0)
126 *levp = VCHIQ_LOG_INFO;
127 else if (strncmp("trace", kbuf, strlen("trace")) == 0)
128 *levp = VCHIQ_LOG_TRACE;
130 *levp = VCHIQ_LOG_DEFAULT;
137 static const struct file_operations debugfs_log_fops = {
138 .owner = THIS_MODULE,
139 .open = debugfs_log_open,
140 .write = debugfs_log_write,
143 .release = single_release,
146 static int debugfs_usecount_show(struct seq_file *f, void *offset)
148 VCHIQ_INSTANCE_T instance = f->private;
151 use_count = vchiq_instance_get_use_count(instance);
152 seq_printf(f, "%d\n", use_count);
157 static int debugfs_usecount_open(struct inode *inode, struct file *file)
159 return single_open(file, debugfs_usecount_show, inode->i_private);
162 static const struct file_operations debugfs_usecount_fops = {
163 .owner = THIS_MODULE,
164 .open = debugfs_usecount_open,
167 .release = single_release,
170 static int debugfs_trace_show(struct seq_file *f, void *offset)
172 VCHIQ_INSTANCE_T instance = f->private;
175 trace = vchiq_instance_get_trace(instance);
176 seq_printf(f, "%s\n", trace ? "Y" : "N");
181 static int debugfs_trace_open(struct inode *inode, struct file *file)
183 return single_open(file, debugfs_trace_show, inode->i_private);
186 static ssize_t debugfs_trace_write(struct file *file,
187 const char __user *buffer,
188 size_t count, loff_t *ppos)
190 struct seq_file *f = (struct seq_file *)file->private_data;
191 VCHIQ_INSTANCE_T instance = f->private;
194 if (copy_from_user(&firstchar, buffer, 1) != 0)
201 vchiq_instance_set_trace(instance, 1);
206 vchiq_instance_set_trace(instance, 0);
217 static const struct file_operations debugfs_trace_fops = {
218 .owner = THIS_MODULE,
219 .open = debugfs_trace_open,
220 .write = debugfs_trace_write,
223 .release = single_release,
226 /* add an instance (process) to the debugfs entries */
227 void vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
232 snprintf(pidstr, sizeof(pidstr), "%d",
233 vchiq_instance_get_pid(instance));
235 top = debugfs_create_dir(pidstr, vchiq_dbg_clients);
237 debugfs_create_file("use_count", 0444, top, instance,
238 &debugfs_usecount_fops);
239 debugfs_create_file("trace", 0644, top, instance, &debugfs_trace_fops);
241 vchiq_instance_get_debugfs_node(instance)->dentry = top;
244 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
246 VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
248 debugfs_remove_recursive(node->dentry);
251 void vchiq_debugfs_init(void)
256 vchiq_dbg_dir = debugfs_create_dir("vchiq", NULL);
257 vchiq_dbg_clients = debugfs_create_dir("clients", vchiq_dbg_dir);
259 /* create an entry under <debugfs>/vchiq/log for each log category */
260 dir = debugfs_create_dir("log", vchiq_dbg_dir);
262 for (i = 0; i < n_log_entries; i++)
263 debugfs_create_file(vchiq_debugfs_log_entries[i].name, 0644,
264 dir, vchiq_debugfs_log_entries[i].plevel,
268 /* remove all the debugfs entries */
269 void vchiq_debugfs_deinit(void)
271 debugfs_remove_recursive(vchiq_dbg_dir);
274 #else /* CONFIG_DEBUG_FS */
276 void vchiq_debugfs_init(void)
280 void vchiq_debugfs_deinit(void)
284 void vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
288 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
292 #endif /* CONFIG_DEBUG_FS */