1 /**
2 * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
3 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
17 *
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.
21 *
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.
33 */
34
35 #include <linux/debugfs.h>
36 #include "vchiq_core.h"
37 #include "vchiq_arm.h"
38 #include "vchiq_debugfs.h"
39
40 #ifdef CONFIG_DEBUG_FS
41
42 /****************************************************************************
43 *
44 * log category entries
45 *
46 ***************************************************************************/
47 #define DEBUGFS_WRITE_BUF_SIZE 256
48
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"
53
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;
57
58 /* Log category debugfs entries */
59 struct vchiq_debugfs_log_entry {
60 const char *name;
61 void *plevel;
62 };
63
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 },
70 };
71 static int n_log_entries = ARRAY_SIZE(vchiq_debugfs_log_entries);
72
debugfs_log_show(struct seq_file * f,void * offset)73 static int debugfs_log_show(struct seq_file *f, void *offset)
74 {
75 int *levp = f->private;
76 char *log_value = NULL;
77
78 switch (*levp) {
79 case VCHIQ_LOG_ERROR:
80 log_value = VCHIQ_LOG_ERROR_STR;
81 break;
82 case VCHIQ_LOG_WARNING:
83 log_value = VCHIQ_LOG_WARNING_STR;
84 break;
85 case VCHIQ_LOG_INFO:
86 log_value = VCHIQ_LOG_INFO_STR;
87 break;
88 case VCHIQ_LOG_TRACE:
89 log_value = VCHIQ_LOG_TRACE_STR;
90 break;
91 default:
92 break;
93 }
94
95 seq_printf(f, "%s\n", log_value ? log_value : "(null)");
96
97 return 0;
98 }
99
debugfs_log_open(struct inode * inode,struct file * file)100 static int debugfs_log_open(struct inode *inode, struct file *file)
101 {
102 return single_open(file, debugfs_log_show, inode->i_private);
103 }
104
debugfs_log_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)105 static ssize_t debugfs_log_write(struct file *file,
106 const char __user *buffer,
107 size_t count, loff_t *ppos)
108 {
109 struct seq_file *f = (struct seq_file *)file->private_data;
110 int *levp = f->private;
111 char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
112
113 memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
114 if (count >= DEBUGFS_WRITE_BUF_SIZE)
115 count = DEBUGFS_WRITE_BUF_SIZE;
116
117 if (copy_from_user(kbuf, buffer, count) != 0)
118 return -EFAULT;
119 kbuf[count - 1] = 0;
120
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;
129 else
130 *levp = VCHIQ_LOG_DEFAULT;
131
132 *ppos += count;
133
134 return count;
135 }
136
137 static const struct file_operations debugfs_log_fops = {
138 .owner = THIS_MODULE,
139 .open = debugfs_log_open,
140 .write = debugfs_log_write,
141 .read = seq_read,
142 .llseek = seq_lseek,
143 .release = single_release,
144 };
145
debugfs_usecount_show(struct seq_file * f,void * offset)146 static int debugfs_usecount_show(struct seq_file *f, void *offset)
147 {
148 VCHIQ_INSTANCE_T instance = f->private;
149 int use_count;
150
151 use_count = vchiq_instance_get_use_count(instance);
152 seq_printf(f, "%d\n", use_count);
153
154 return 0;
155 }
156
debugfs_usecount_open(struct inode * inode,struct file * file)157 static int debugfs_usecount_open(struct inode *inode, struct file *file)
158 {
159 return single_open(file, debugfs_usecount_show, inode->i_private);
160 }
161
162 static const struct file_operations debugfs_usecount_fops = {
163 .owner = THIS_MODULE,
164 .open = debugfs_usecount_open,
165 .read = seq_read,
166 .llseek = seq_lseek,
167 .release = single_release,
168 };
169
debugfs_trace_show(struct seq_file * f,void * offset)170 static int debugfs_trace_show(struct seq_file *f, void *offset)
171 {
172 VCHIQ_INSTANCE_T instance = f->private;
173 int trace;
174
175 trace = vchiq_instance_get_trace(instance);
176 seq_printf(f, "%s\n", trace ? "Y" : "N");
177
178 return 0;
179 }
180
debugfs_trace_open(struct inode * inode,struct file * file)181 static int debugfs_trace_open(struct inode *inode, struct file *file)
182 {
183 return single_open(file, debugfs_trace_show, inode->i_private);
184 }
185
debugfs_trace_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)186 static ssize_t debugfs_trace_write(struct file *file,
187 const char __user *buffer,
188 size_t count, loff_t *ppos)
189 {
190 struct seq_file *f = (struct seq_file *)file->private_data;
191 VCHIQ_INSTANCE_T instance = f->private;
192 char firstchar;
193
194 if (copy_from_user(&firstchar, buffer, 1) != 0)
195 return -EFAULT;
196
197 switch (firstchar) {
198 case 'Y':
199 case 'y':
200 case '1':
201 vchiq_instance_set_trace(instance, 1);
202 break;
203 case 'N':
204 case 'n':
205 case '0':
206 vchiq_instance_set_trace(instance, 0);
207 break;
208 default:
209 break;
210 }
211
212 *ppos += count;
213
214 return count;
215 }
216
217 static const struct file_operations debugfs_trace_fops = {
218 .owner = THIS_MODULE,
219 .open = debugfs_trace_open,
220 .write = debugfs_trace_write,
221 .read = seq_read,
222 .llseek = seq_lseek,
223 .release = single_release,
224 };
225
226 /* add an instance (process) to the debugfs entries */
vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)227 void vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
228 {
229 char pidstr[16];
230 struct dentry *top;
231
232 snprintf(pidstr, sizeof(pidstr), "%d",
233 vchiq_instance_get_pid(instance));
234
235 top = debugfs_create_dir(pidstr, vchiq_dbg_clients);
236
237 debugfs_create_file("use_count", 0444, top, instance,
238 &debugfs_usecount_fops);
239 debugfs_create_file("trace", 0644, top, instance, &debugfs_trace_fops);
240
241 vchiq_instance_get_debugfs_node(instance)->dentry = top;
242 }
243
vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)244 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
245 {
246 VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
247
248 debugfs_remove_recursive(node->dentry);
249 }
250
vchiq_debugfs_init(void)251 void vchiq_debugfs_init(void)
252 {
253 struct dentry *dir;
254 int i;
255
256 vchiq_dbg_dir = debugfs_create_dir("vchiq", NULL);
257 vchiq_dbg_clients = debugfs_create_dir("clients", vchiq_dbg_dir);
258
259 /* create an entry under <debugfs>/vchiq/log for each log category */
260 dir = debugfs_create_dir("log", vchiq_dbg_dir);
261
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,
265 &debugfs_log_fops);
266 }
267
268 /* remove all the debugfs entries */
vchiq_debugfs_deinit(void)269 void vchiq_debugfs_deinit(void)
270 {
271 debugfs_remove_recursive(vchiq_dbg_dir);
272 }
273
274 #else /* CONFIG_DEBUG_FS */
275
vchiq_debugfs_init(void)276 void vchiq_debugfs_init(void)
277 {
278 }
279
vchiq_debugfs_deinit(void)280 void vchiq_debugfs_deinit(void)
281 {
282 }
283
vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)284 void vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
285 {
286 }
287
vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)288 void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
289 {
290 }
291
292 #endif /* CONFIG_DEBUG_FS */
293