GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / batman-adv / bat_debugfs.c
blob507da68054c76ae63b1f56c46881c755259a1b66
1 /*
2 * Copyright (C) 2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
22 #include "main.h"
24 #include <linux/debugfs.h>
26 #include "bat_debugfs.h"
27 #include "translation-table.h"
28 #include "originator.h"
29 #include "hard-interface.h"
30 #include "vis.h"
31 #include "icmp_socket.h"
33 static struct dentry *bat_debugfs;
35 #ifdef CONFIG_BATMAN_ADV_DEBUG
36 #define LOG_BUFF_MASK (log_buff_len-1)
37 #define LOG_BUFF(idx) (debug_log->log_buff[(idx) & LOG_BUFF_MASK])
39 static int log_buff_len = LOG_BUF_LEN;
41 static void emit_log_char(struct debug_log *debug_log, char c)
43 LOG_BUFF(debug_log->log_end) = c;
44 debug_log->log_end++;
46 if (debug_log->log_end - debug_log->log_start > log_buff_len)
47 debug_log->log_start = debug_log->log_end - log_buff_len;
50 static int fdebug_log(struct debug_log *debug_log, char *fmt, ...)
52 int printed_len;
53 va_list args;
54 static char debug_log_buf[256];
55 char *p;
56 unsigned long flags;
58 if (!debug_log)
59 return 0;
61 spin_lock_irqsave(&debug_log->lock, flags);
62 va_start(args, fmt);
63 printed_len = vscnprintf(debug_log_buf, sizeof(debug_log_buf),
64 fmt, args);
65 va_end(args);
67 for (p = debug_log_buf; *p != 0; p++)
68 emit_log_char(debug_log, *p);
70 spin_unlock_irqrestore(&debug_log->lock, flags);
72 wake_up(&debug_log->queue_wait);
74 return 0;
77 int debug_log(struct bat_priv *bat_priv, char *fmt, ...)
79 va_list args;
80 char tmp_log_buf[256];
82 va_start(args, fmt);
83 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
84 fdebug_log(bat_priv->debug_log, "[%10u] %s",
85 (jiffies / HZ), tmp_log_buf);
86 va_end(args);
88 return 0;
91 static int log_open(struct inode *inode, struct file *file)
93 file->private_data = inode->i_private;
94 inc_module_count();
95 return 0;
98 static int log_release(struct inode *inode, struct file *file)
100 dec_module_count();
101 return 0;
104 static ssize_t log_read(struct file *file, char __user *buf,
105 size_t count, loff_t *ppos)
107 struct bat_priv *bat_priv = file->private_data;
108 struct debug_log *debug_log = bat_priv->debug_log;
109 int error, i = 0;
110 char c;
111 unsigned long flags;
113 if ((file->f_flags & O_NONBLOCK) &&
114 !(debug_log->log_end - debug_log->log_start))
115 return -EAGAIN;
117 if ((!buf) || (count < 0))
118 return -EINVAL;
120 if (count == 0)
121 return 0;
123 if (!access_ok(VERIFY_WRITE, buf, count))
124 return -EFAULT;
126 error = wait_event_interruptible(debug_log->queue_wait,
127 (debug_log->log_start - debug_log->log_end));
129 if (error)
130 return error;
132 spin_lock_irqsave(&debug_log->lock, flags);
134 while ((!error) && (i < count) &&
135 (debug_log->log_start != debug_log->log_end)) {
136 c = LOG_BUFF(debug_log->log_start);
138 debug_log->log_start++;
140 spin_unlock_irqrestore(&debug_log->lock, flags);
142 error = __put_user(c, buf);
144 spin_lock_irqsave(&debug_log->lock, flags);
146 buf++;
147 i++;
151 spin_unlock_irqrestore(&debug_log->lock, flags);
153 if (!error)
154 return i;
156 return error;
159 static unsigned int log_poll(struct file *file, poll_table *wait)
161 struct bat_priv *bat_priv = file->private_data;
162 struct debug_log *debug_log = bat_priv->debug_log;
164 poll_wait(file, &debug_log->queue_wait, wait);
166 if (debug_log->log_end - debug_log->log_start)
167 return POLLIN | POLLRDNORM;
169 return 0;
172 static const struct file_operations log_fops = {
173 .open = log_open,
174 .release = log_release,
175 .read = log_read,
176 .poll = log_poll,
179 static int debug_log_setup(struct bat_priv *bat_priv)
181 struct dentry *d;
183 if (!bat_priv->debug_dir)
184 goto err;
186 bat_priv->debug_log = kzalloc(sizeof(struct debug_log), GFP_ATOMIC);
187 if (!bat_priv->debug_log)
188 goto err;
190 spin_lock_init(&bat_priv->debug_log->lock);
191 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
193 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
194 bat_priv->debug_dir, bat_priv, &log_fops);
195 if (d)
196 goto err;
198 return 0;
200 err:
201 return 1;
204 static void debug_log_cleanup(struct bat_priv *bat_priv)
206 kfree(bat_priv->debug_log);
207 bat_priv->debug_log = NULL;
209 #else /* CONFIG_BATMAN_ADV_DEBUG */
210 static int debug_log_setup(struct bat_priv *bat_priv)
212 bat_priv->debug_log = NULL;
213 return 0;
216 static void debug_log_cleanup(struct bat_priv *bat_priv)
218 return;
220 #endif
222 static int originators_open(struct inode *inode, struct file *file)
224 struct net_device *net_dev = (struct net_device *)inode->i_private;
225 return single_open(file, orig_seq_print_text, net_dev);
228 static int transtable_global_open(struct inode *inode, struct file *file)
230 struct net_device *net_dev = (struct net_device *)inode->i_private;
231 return single_open(file, hna_global_seq_print_text, net_dev);
234 static int transtable_local_open(struct inode *inode, struct file *file)
236 struct net_device *net_dev = (struct net_device *)inode->i_private;
237 return single_open(file, hna_local_seq_print_text, net_dev);
240 static int vis_data_open(struct inode *inode, struct file *file)
242 struct net_device *net_dev = (struct net_device *)inode->i_private;
243 return single_open(file, vis_seq_print_text, net_dev);
246 struct bat_debuginfo {
247 struct attribute attr;
248 const struct file_operations fops;
251 #define BAT_DEBUGINFO(_name, _mode, _open) \
252 struct bat_debuginfo bat_debuginfo_##_name = { \
253 .attr = { .name = __stringify(_name), \
254 .mode = _mode, }, \
255 .fops = { .owner = THIS_MODULE, \
256 .open = _open, \
257 .read = seq_read, \
258 .llseek = seq_lseek, \
259 .release = single_release, \
263 static BAT_DEBUGINFO(originators, S_IRUGO, originators_open);
264 static BAT_DEBUGINFO(transtable_global, S_IRUGO, transtable_global_open);
265 static BAT_DEBUGINFO(transtable_local, S_IRUGO, transtable_local_open);
266 static BAT_DEBUGINFO(vis_data, S_IRUGO, vis_data_open);
268 static struct bat_debuginfo *mesh_debuginfos[] = {
269 &bat_debuginfo_originators,
270 &bat_debuginfo_transtable_global,
271 &bat_debuginfo_transtable_local,
272 &bat_debuginfo_vis_data,
273 NULL,
276 void debugfs_init(void)
278 bat_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
279 if (bat_debugfs == ERR_PTR(-ENODEV))
280 bat_debugfs = NULL;
283 void debugfs_destroy(void)
285 if (bat_debugfs) {
286 debugfs_remove_recursive(bat_debugfs);
287 bat_debugfs = NULL;
291 int debugfs_add_meshif(struct net_device *dev)
293 struct bat_priv *bat_priv = netdev_priv(dev);
294 struct bat_debuginfo **bat_debug;
295 struct dentry *file;
297 if (!bat_debugfs)
298 goto out;
300 bat_priv->debug_dir = debugfs_create_dir(dev->name, bat_debugfs);
301 if (!bat_priv->debug_dir)
302 goto out;
304 bat_socket_setup(bat_priv);
305 debug_log_setup(bat_priv);
307 for (bat_debug = mesh_debuginfos; *bat_debug; ++bat_debug) {
308 file = debugfs_create_file(((*bat_debug)->attr).name,
309 S_IFREG | ((*bat_debug)->attr).mode,
310 bat_priv->debug_dir,
311 dev, &(*bat_debug)->fops);
312 if (!file) {
313 bat_err(dev, "Can't add debugfs file: %s/%s\n",
314 dev->name, ((*bat_debug)->attr).name);
315 goto rem_attr;
319 return 0;
320 rem_attr:
321 debugfs_remove_recursive(bat_priv->debug_dir);
322 bat_priv->debug_dir = NULL;
323 out:
324 #ifdef CONFIG_DEBUG_FS
325 return -ENOMEM;
326 #else
327 return 0;
328 #endif /* CONFIG_DEBUG_FS */
331 void debugfs_del_meshif(struct net_device *dev)
333 struct bat_priv *bat_priv = netdev_priv(dev);
335 debug_log_cleanup(bat_priv);
337 if (bat_debugfs) {
338 debugfs_remove_recursive(bat_priv->debug_dir);
339 bat_priv->debug_dir = NULL;