Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / drivers / staging / batman-adv / bat_debugfs.c
1 /*
2  * Copyright (C) 2010 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner
5  *
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.
9  *
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.
14  *
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
19  *
20  */
21
22 #include "main.h"
23
24 #include <linux/debugfs.h>
25
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"
32
33 static struct dentry *bat_debugfs;
34
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])
38
39 static int log_buff_len = LOG_BUF_LEN;
40
41 static void emit_log_char(struct debug_log *debug_log, char c)
42 {
43         LOG_BUFF(debug_log->log_end) = c;
44         debug_log->log_end++;
45
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;
48 }
49
50 static int fdebug_log(struct debug_log *debug_log, char *fmt, ...)
51 {
52         int printed_len;
53         va_list args;
54         static char debug_log_buf[256];
55         char *p;
56         unsigned long flags;
57
58         if (!debug_log)
59                 return 0;
60
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);
66
67         for (p = debug_log_buf; *p != 0; p++)
68                 emit_log_char(debug_log, *p);
69
70         spin_unlock_irqrestore(&debug_log->lock, flags);
71
72         wake_up(&debug_log->queue_wait);
73
74         return 0;
75 }
76
77 int debug_log(struct bat_priv *bat_priv, char *fmt, ...)
78 {
79         va_list args;
80         char tmp_log_buf[256];
81
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);
87
88         return 0;
89 }
90
91 static int log_open(struct inode *inode, struct file *file)
92 {
93         nonseekable_open(inode, file);
94         file->private_data = inode->i_private;
95         inc_module_count();
96         return 0;
97 }
98
99 static int log_release(struct inode *inode, struct file *file)
100 {
101         dec_module_count();
102         return 0;
103 }
104
105 static ssize_t log_read(struct file *file, char __user *buf,
106                         size_t count, loff_t *ppos)
107 {
108         struct bat_priv *bat_priv = file->private_data;
109         struct debug_log *debug_log = bat_priv->debug_log;
110         int error, i = 0;
111         char c;
112         unsigned long flags;
113
114         if ((file->f_flags & O_NONBLOCK) &&
115             !(debug_log->log_end - debug_log->log_start))
116                 return -EAGAIN;
117
118         if ((!buf) || (count < 0))
119                 return -EINVAL;
120
121         if (count == 0)
122                 return 0;
123
124         if (!access_ok(VERIFY_WRITE, buf, count))
125                 return -EFAULT;
126
127         error = wait_event_interruptible(debug_log->queue_wait,
128                                 (debug_log->log_start - debug_log->log_end));
129
130         if (error)
131                 return error;
132
133         spin_lock_irqsave(&debug_log->lock, flags);
134
135         while ((!error) && (i < count) &&
136                (debug_log->log_start != debug_log->log_end)) {
137                 c = LOG_BUFF(debug_log->log_start);
138
139                 debug_log->log_start++;
140
141                 spin_unlock_irqrestore(&debug_log->lock, flags);
142
143                 error = __put_user(c, buf);
144
145                 spin_lock_irqsave(&debug_log->lock, flags);
146
147                 buf++;
148                 i++;
149
150         }
151
152         spin_unlock_irqrestore(&debug_log->lock, flags);
153
154         if (!error)
155                 return i;
156
157         return error;
158 }
159
160 static unsigned int log_poll(struct file *file, poll_table *wait)
161 {
162         struct bat_priv *bat_priv = file->private_data;
163         struct debug_log *debug_log = bat_priv->debug_log;
164
165         poll_wait(file, &debug_log->queue_wait, wait);
166
167         if (debug_log->log_end - debug_log->log_start)
168                 return POLLIN | POLLRDNORM;
169
170         return 0;
171 }
172
173 static const struct file_operations log_fops = {
174         .open           = log_open,
175         .release        = log_release,
176         .read           = log_read,
177         .poll           = log_poll,
178         .llseek         = no_llseek,
179 };
180
181 static int debug_log_setup(struct bat_priv *bat_priv)
182 {
183         struct dentry *d;
184
185         if (!bat_priv->debug_dir)
186                 goto err;
187
188         bat_priv->debug_log = kzalloc(sizeof(struct debug_log), GFP_ATOMIC);
189         if (!bat_priv->debug_log)
190                 goto err;
191
192         spin_lock_init(&bat_priv->debug_log->lock);
193         init_waitqueue_head(&bat_priv->debug_log->queue_wait);
194
195         d = debugfs_create_file("log", S_IFREG | S_IRUSR,
196                                 bat_priv->debug_dir, bat_priv, &log_fops);
197         if (d)
198                 goto err;
199
200         return 0;
201
202 err:
203         return 1;
204 }
205
206 static void debug_log_cleanup(struct bat_priv *bat_priv)
207 {
208         kfree(bat_priv->debug_log);
209         bat_priv->debug_log = NULL;
210 }
211 #else /* CONFIG_BATMAN_ADV_DEBUG */
212 static int debug_log_setup(struct bat_priv *bat_priv)
213 {
214         bat_priv->debug_log = NULL;
215         return 0;
216 }
217
218 static void debug_log_cleanup(struct bat_priv *bat_priv)
219 {
220         return;
221 }
222 #endif
223
224 static int originators_open(struct inode *inode, struct file *file)
225 {
226         struct net_device *net_dev = (struct net_device *)inode->i_private;
227         return single_open(file, orig_seq_print_text, net_dev);
228 }
229
230 static int transtable_global_open(struct inode *inode, struct file *file)
231 {
232         struct net_device *net_dev = (struct net_device *)inode->i_private;
233         return single_open(file, hna_global_seq_print_text, net_dev);
234 }
235
236 static int transtable_local_open(struct inode *inode, struct file *file)
237 {
238         struct net_device *net_dev = (struct net_device *)inode->i_private;
239         return single_open(file, hna_local_seq_print_text, net_dev);
240 }
241
242 static int vis_data_open(struct inode *inode, struct file *file)
243 {
244         struct net_device *net_dev = (struct net_device *)inode->i_private;
245         return single_open(file, vis_seq_print_text, net_dev);
246 }
247
248 struct bat_debuginfo {
249         struct attribute attr;
250         const struct file_operations fops;
251 };
252
253 #define BAT_DEBUGINFO(_name, _mode, _open)      \
254 struct bat_debuginfo bat_debuginfo_##_name = {  \
255         .attr = { .name = __stringify(_name),   \
256                   .mode = _mode, },             \
257         .fops = { .owner = THIS_MODULE,         \
258                   .open = _open,                \
259                   .read = seq_read,             \
260                   .llseek = seq_lseek,          \
261                   .release = single_release,    \
262                 }                               \
263 };
264
265 static BAT_DEBUGINFO(originators, S_IRUGO, originators_open);
266 static BAT_DEBUGINFO(transtable_global, S_IRUGO, transtable_global_open);
267 static BAT_DEBUGINFO(transtable_local, S_IRUGO, transtable_local_open);
268 static BAT_DEBUGINFO(vis_data, S_IRUGO, vis_data_open);
269
270 static struct bat_debuginfo *mesh_debuginfos[] = {
271         &bat_debuginfo_originators,
272         &bat_debuginfo_transtable_global,
273         &bat_debuginfo_transtable_local,
274         &bat_debuginfo_vis_data,
275         NULL,
276 };
277
278 void debugfs_init(void)
279 {
280         bat_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
281         if (bat_debugfs == ERR_PTR(-ENODEV))
282                 bat_debugfs = NULL;
283 }
284
285 void debugfs_destroy(void)
286 {
287         if (bat_debugfs) {
288                 debugfs_remove_recursive(bat_debugfs);
289                 bat_debugfs = NULL;
290         }
291 }
292
293 int debugfs_add_meshif(struct net_device *dev)
294 {
295         struct bat_priv *bat_priv = netdev_priv(dev);
296         struct bat_debuginfo **bat_debug;
297         struct dentry *file;
298
299         if (!bat_debugfs)
300                 goto out;
301
302         bat_priv->debug_dir = debugfs_create_dir(dev->name, bat_debugfs);
303         if (!bat_priv->debug_dir)
304                 goto out;
305
306         bat_socket_setup(bat_priv);
307         debug_log_setup(bat_priv);
308
309         for (bat_debug = mesh_debuginfos; *bat_debug; ++bat_debug) {
310                 file = debugfs_create_file(((*bat_debug)->attr).name,
311                                           S_IFREG | ((*bat_debug)->attr).mode,
312                                           bat_priv->debug_dir,
313                                           dev, &(*bat_debug)->fops);
314                 if (!file) {
315                         bat_err(dev, "Can't add debugfs file: %s/%s\n",
316                                 dev->name, ((*bat_debug)->attr).name);
317                         goto rem_attr;
318                 }
319         }
320
321         return 0;
322 rem_attr:
323         debugfs_remove_recursive(bat_priv->debug_dir);
324         bat_priv->debug_dir = NULL;
325 out:
326 #ifdef CONFIG_DEBUG_FS
327         return -ENOMEM;
328 #else
329         return 0;
330 #endif /* CONFIG_DEBUG_FS */
331 }
332
333 void debugfs_del_meshif(struct net_device *dev)
334 {
335         struct bat_priv *bat_priv = netdev_priv(dev);
336
337         debug_log_cleanup(bat_priv);
338
339         if (bat_debugfs) {
340                 debugfs_remove_recursive(bat_priv->debug_dir);
341                 bat_priv->debug_dir = NULL;
342         }
343 }