Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[sfrench/cifs-2.6.git] / drivers / staging / wilc1000 / wilc_debugfs.c
1 /*
2  * NewportMedia WiFi chipset driver test tools - wilc-debug
3  * Copyright (c) 2012 NewportMedia Inc.
4  * Author: SSW <sswd@wilcsemic.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11
12 #if defined(WILC_DEBUGFS)
13 #include <linux/module.h>
14 #include <linux/debugfs.h>
15 #include <linux/poll.h>
16 #include <linux/sched.h>
17
18 #include "wilc_wlan_if.h"
19
20 static struct dentry *wilc_dir;
21
22 /*
23  * --------------------------------------------------------------------------------
24  */
25 #define DEBUG           BIT(0)
26 #define INFO            BIT(1)
27 #define WRN             BIT(2)
28 #define ERR             BIT(3)
29
30 #define DBG_LEVEL_ALL   (DEBUG | INFO | WRN | ERR)
31 static atomic_t WILC_DEBUG_LEVEL = ATOMIC_INIT(ERR);
32 EXPORT_SYMBOL_GPL(WILC_DEBUG_LEVEL);
33
34 /*
35  * --------------------------------------------------------------------------------
36  */
37
38 static ssize_t wilc_debug_level_read(struct file *file, char __user *userbuf, size_t count, loff_t *ppos)
39 {
40         char buf[128];
41         int res = 0;
42
43         /* only allow read from start */
44         if (*ppos > 0)
45                 return 0;
46
47         res = scnprintf(buf, sizeof(buf), "Debug Level: %x\n", atomic_read(&WILC_DEBUG_LEVEL));
48
49         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
50 }
51
52 static ssize_t wilc_debug_level_write(struct file *filp, const char __user *buf,
53                                       size_t count, loff_t *ppos)
54 {
55         int flag = 0;
56         int ret;
57
58         ret = kstrtouint_from_user(buf, count, 16, &flag);
59         if (ret)
60                 return ret;
61
62         if (flag > DBG_LEVEL_ALL) {
63                 pr_info("%s, value (0x%08x) is out of range, stay previous flag (0x%08x)\n", __func__, flag, atomic_read(&WILC_DEBUG_LEVEL));
64                 return -EINVAL;
65         }
66
67         atomic_set(&WILC_DEBUG_LEVEL, (int)flag);
68
69         if (flag == 0)
70                 pr_info("Debug-level disabled\n");
71         else
72                 pr_info("Debug-level enabled\n");
73
74         return count;
75 }
76
77 /*
78  * --------------------------------------------------------------------------------
79  */
80
81 #define FOPS(_open, _read, _write, _poll) { \
82                 .owner  = THIS_MODULE, \
83                 .open   = (_open), \
84                 .read   = (_read), \
85                 .write  = (_write), \
86                 .poll           = (_poll), \
87 }
88
89 struct wilc_debugfs_info_t {
90         const char *name;
91         int perm;
92         unsigned int data;
93         const struct file_operations fops;
94 };
95
96 static struct wilc_debugfs_info_t debugfs_info[] = {
97         { "wilc_debug_level",   0666,   (DEBUG | ERR), FOPS(NULL, wilc_debug_level_read, wilc_debug_level_write, NULL), },
98 };
99
100 static int __init wilc_debugfs_init(void)
101 {
102         int i;
103         struct wilc_debugfs_info_t *info;
104
105         wilc_dir = debugfs_create_dir("wilc_wifi", NULL);
106         for (i = 0; i < ARRAY_SIZE(debugfs_info); i++) {
107                 info = &debugfs_info[i];
108                 debugfs_create_file(info->name,
109                                     info->perm,
110                                     wilc_dir,
111                                     &info->data,
112                                     &info->fops);
113         }
114         return 0;
115 }
116 module_init(wilc_debugfs_init);
117
118 static void __exit wilc_debugfs_remove(void)
119 {
120         debugfs_remove_recursive(wilc_dir);
121 }
122 module_exit(wilc_debugfs_remove);
123
124 #endif
125