usb: gadget: lpc32xx_udc: Remove usb_endpoint_descriptor
[linux-2.6.git] / net / batman-adv / debugfs.c
blob34fbb1667bcd5194ae391abf8ca3a03944246c8d
1 /* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
3 * Marek Lindner
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
20 #include "main.h"
22 #include <linux/debugfs.h>
24 #include "debugfs.h"
25 #include "translation-table.h"
26 #include "originator.h"
27 #include "hard-interface.h"
28 #include "gateway_common.h"
29 #include "gateway_client.h"
30 #include "soft-interface.h"
31 #include "vis.h"
32 #include "icmp_socket.h"
33 #include "bridge_loop_avoidance.h"
35 static struct dentry *batadv_debugfs;
37 #ifdef CONFIG_BATMAN_ADV_DEBUG
38 #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
40 static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
42 static char *batadv_log_char_addr(struct batadv_debug_log *debug_log,
43 size_t idx)
45 return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
48 static void batadv_emit_log_char(struct batadv_debug_log *debug_log, char c)
50 char *char_addr;
52 char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
53 *char_addr = c;
54 debug_log->log_end++;
56 if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
57 debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
60 __printf(2, 3)
61 static int batadv_fdebug_log(struct batadv_debug_log *debug_log,
62 const char *fmt, ...)
64 va_list args;
65 static char debug_log_buf[256];
66 char *p;
68 if (!debug_log)
69 return 0;
71 spin_lock_bh(&debug_log->lock);
72 va_start(args, fmt);
73 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
74 va_end(args);
76 for (p = debug_log_buf; *p != 0; p++)
77 batadv_emit_log_char(debug_log, *p);
79 spin_unlock_bh(&debug_log->lock);
81 wake_up(&debug_log->queue_wait);
83 return 0;
86 int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
88 va_list args;
89 char tmp_log_buf[256];
91 va_start(args, fmt);
92 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
93 batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
94 jiffies_to_msecs(jiffies), tmp_log_buf);
95 va_end(args);
97 return 0;
100 static int batadv_log_open(struct inode *inode, struct file *file)
102 nonseekable_open(inode, file);
103 file->private_data = inode->i_private;
104 batadv_inc_module_count();
105 return 0;
108 static int batadv_log_release(struct inode *inode, struct file *file)
110 batadv_dec_module_count();
111 return 0;
114 static int batadv_log_empty(struct batadv_debug_log *debug_log)
116 return !(debug_log->log_start - debug_log->log_end);
119 static ssize_t batadv_log_read(struct file *file, char __user *buf,
120 size_t count, loff_t *ppos)
122 struct batadv_priv *bat_priv = file->private_data;
123 struct batadv_debug_log *debug_log = bat_priv->debug_log;
124 int error, i = 0;
125 char *char_addr;
126 char c;
128 if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
129 return -EAGAIN;
131 if (!buf)
132 return -EINVAL;
134 if (count == 0)
135 return 0;
137 if (!access_ok(VERIFY_WRITE, buf, count))
138 return -EFAULT;
140 error = wait_event_interruptible(debug_log->queue_wait,
141 (!batadv_log_empty(debug_log)));
143 if (error)
144 return error;
146 spin_lock_bh(&debug_log->lock);
148 while ((!error) && (i < count) &&
149 (debug_log->log_start != debug_log->log_end)) {
150 char_addr = batadv_log_char_addr(debug_log,
151 debug_log->log_start);
152 c = *char_addr;
154 debug_log->log_start++;
156 spin_unlock_bh(&debug_log->lock);
158 error = __put_user(c, buf);
160 spin_lock_bh(&debug_log->lock);
162 buf++;
163 i++;
167 spin_unlock_bh(&debug_log->lock);
169 if (!error)
170 return i;
172 return error;
175 static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
177 struct batadv_priv *bat_priv = file->private_data;
178 struct batadv_debug_log *debug_log = bat_priv->debug_log;
180 poll_wait(file, &debug_log->queue_wait, wait);
182 if (!batadv_log_empty(debug_log))
183 return POLLIN | POLLRDNORM;
185 return 0;
188 static const struct file_operations batadv_log_fops = {
189 .open = batadv_log_open,
190 .release = batadv_log_release,
191 .read = batadv_log_read,
192 .poll = batadv_log_poll,
193 .llseek = no_llseek,
196 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
198 struct dentry *d;
200 if (!bat_priv->debug_dir)
201 goto err;
203 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
204 if (!bat_priv->debug_log)
205 goto err;
207 spin_lock_init(&bat_priv->debug_log->lock);
208 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
210 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
211 bat_priv->debug_dir, bat_priv,
212 &batadv_log_fops);
213 if (!d)
214 goto err;
216 return 0;
218 err:
219 return -ENOMEM;
222 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
224 kfree(bat_priv->debug_log);
225 bat_priv->debug_log = NULL;
227 #else /* CONFIG_BATMAN_ADV_DEBUG */
228 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
230 bat_priv->debug_log = NULL;
231 return 0;
234 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
236 return;
238 #endif
240 static int batadv_algorithms_open(struct inode *inode, struct file *file)
242 return single_open(file, batadv_algo_seq_print_text, NULL);
245 static int batadv_originators_open(struct inode *inode, struct file *file)
247 struct net_device *net_dev = (struct net_device *)inode->i_private;
248 return single_open(file, batadv_orig_seq_print_text, net_dev);
251 static int batadv_gateways_open(struct inode *inode, struct file *file)
253 struct net_device *net_dev = (struct net_device *)inode->i_private;
254 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
257 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
259 struct net_device *net_dev = (struct net_device *)inode->i_private;
260 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
263 #ifdef CONFIG_BATMAN_ADV_BLA
264 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
266 struct net_device *net_dev = (struct net_device *)inode->i_private;
267 return single_open(file, batadv_bla_claim_table_seq_print_text,
268 net_dev);
270 #endif
272 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
274 struct net_device *net_dev = (struct net_device *)inode->i_private;
275 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
278 static int batadv_vis_data_open(struct inode *inode, struct file *file)
280 struct net_device *net_dev = (struct net_device *)inode->i_private;
281 return single_open(file, batadv_vis_seq_print_text, net_dev);
284 struct batadv_debuginfo {
285 struct attribute attr;
286 const struct file_operations fops;
289 #define BATADV_DEBUGINFO(_name, _mode, _open) \
290 struct batadv_debuginfo batadv_debuginfo_##_name = { \
291 .attr = { .name = __stringify(_name), \
292 .mode = _mode, }, \
293 .fops = { .owner = THIS_MODULE, \
294 .open = _open, \
295 .read = seq_read, \
296 .llseek = seq_lseek, \
297 .release = single_release, \
301 static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
302 static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
303 static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
304 static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
305 batadv_transtable_global_open);
306 #ifdef CONFIG_BATMAN_ADV_BLA
307 static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
308 #endif
309 static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
310 batadv_transtable_local_open);
311 static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
313 static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
314 &batadv_debuginfo_originators,
315 &batadv_debuginfo_gateways,
316 &batadv_debuginfo_transtable_global,
317 #ifdef CONFIG_BATMAN_ADV_BLA
318 &batadv_debuginfo_bla_claim_table,
319 #endif
320 &batadv_debuginfo_transtable_local,
321 &batadv_debuginfo_vis_data,
322 NULL,
325 void batadv_debugfs_init(void)
327 struct batadv_debuginfo *bat_debug;
328 struct dentry *file;
330 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
331 if (batadv_debugfs == ERR_PTR(-ENODEV))
332 batadv_debugfs = NULL;
334 if (!batadv_debugfs)
335 goto out;
337 bat_debug = &batadv_debuginfo_routing_algos;
338 file = debugfs_create_file(bat_debug->attr.name,
339 S_IFREG | bat_debug->attr.mode,
340 batadv_debugfs, NULL, &bat_debug->fops);
341 if (!file)
342 pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
344 out:
345 return;
348 void batadv_debugfs_destroy(void)
350 if (batadv_debugfs) {
351 debugfs_remove_recursive(batadv_debugfs);
352 batadv_debugfs = NULL;
356 int batadv_debugfs_add_meshif(struct net_device *dev)
358 struct batadv_priv *bat_priv = netdev_priv(dev);
359 struct batadv_debuginfo **bat_debug;
360 struct dentry *file;
362 if (!batadv_debugfs)
363 goto out;
365 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
366 if (!bat_priv->debug_dir)
367 goto out;
369 if (batadv_socket_setup(bat_priv) < 0)
370 goto rem_attr;
372 if (batadv_debug_log_setup(bat_priv) < 0)
373 goto rem_attr;
375 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
376 file = debugfs_create_file(((*bat_debug)->attr).name,
377 S_IFREG | ((*bat_debug)->attr).mode,
378 bat_priv->debug_dir,
379 dev, &(*bat_debug)->fops);
380 if (!file) {
381 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
382 dev->name, ((*bat_debug)->attr).name);
383 goto rem_attr;
387 return 0;
388 rem_attr:
389 debugfs_remove_recursive(bat_priv->debug_dir);
390 bat_priv->debug_dir = NULL;
391 out:
392 #ifdef CONFIG_DEBUG_FS
393 return -ENOMEM;
394 #else
395 return 0;
396 #endif /* CONFIG_DEBUG_FS */
399 void batadv_debugfs_del_meshif(struct net_device *dev)
401 struct batadv_priv *bat_priv = netdev_priv(dev);
403 batadv_debug_log_cleanup(bat_priv);
405 if (batadv_debugfs) {
406 debugfs_remove_recursive(bat_priv->debug_dir);
407 bat_priv->debug_dir = NULL;