simple_open: automatically convert to simple_open()
[linux-flexiantxendom0-3.2.10.git] / drivers / net / wimax / i2400m / debugfs.c
1 /*
2  * Intel Wireless WiMAX Connection 2400m
3  * Debugfs interfaces to manipulate driver and device information
4  *
5  *
6  * Copyright (C) 2007 Intel Corporation <linux-wimax@intel.com>
7  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version
11  * 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301, USA.
22  */
23
24 #include <linux/debugfs.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/spinlock.h>
28 #include <linux/device.h>
29 #include <linux/export.h>
30 #include "i2400m.h"
31
32
33 #define D_SUBMODULE debugfs
34 #include "debug-levels.h"
35
36 static
37 int debugfs_netdev_queue_stopped_get(void *data, u64 *val)
38 {
39         struct i2400m *i2400m = data;
40         *val = netif_queue_stopped(i2400m->wimax_dev.net_dev);
41         return 0;
42 }
43 DEFINE_SIMPLE_ATTRIBUTE(fops_netdev_queue_stopped,
44                         debugfs_netdev_queue_stopped_get,
45                         NULL, "%llu\n");
46
47
48 static
49 struct dentry *debugfs_create_netdev_queue_stopped(
50         const char *name, struct dentry *parent, struct i2400m *i2400m)
51 {
52         return debugfs_create_file(name, 0400, parent, i2400m,
53                                    &fops_netdev_queue_stopped);
54 }
55
56 /*
57  * We don't allow partial reads of this file, as then the reader would
58  * get weirdly confused data as it is updated.
59  *
60  * So or you read it all or nothing; if you try to read with an offset
61  * != 0, we consider you are done reading.
62  */
63 static
64 ssize_t i2400m_rx_stats_read(struct file *filp, char __user *buffer,
65                              size_t count, loff_t *ppos)
66 {
67         struct i2400m *i2400m = filp->private_data;
68         char buf[128];
69         unsigned long flags;
70
71         if (*ppos != 0)
72                 return 0;
73         if (count < sizeof(buf))
74                 return -ENOSPC;
75         spin_lock_irqsave(&i2400m->rx_lock, flags);
76         snprintf(buf, sizeof(buf), "%u %u %u %u %u %u %u\n",
77                  i2400m->rx_pl_num, i2400m->rx_pl_min,
78                  i2400m->rx_pl_max, i2400m->rx_num,
79                  i2400m->rx_size_acc,
80                  i2400m->rx_size_min, i2400m->rx_size_max);
81         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
82         return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
83 }
84
85
86 /* Any write clears the stats */
87 static
88 ssize_t i2400m_rx_stats_write(struct file *filp, const char __user *buffer,
89                               size_t count, loff_t *ppos)
90 {
91         struct i2400m *i2400m = filp->private_data;
92         unsigned long flags;
93
94         spin_lock_irqsave(&i2400m->rx_lock, flags);
95         i2400m->rx_pl_num = 0;
96         i2400m->rx_pl_max = 0;
97         i2400m->rx_pl_min = UINT_MAX;
98         i2400m->rx_num = 0;
99         i2400m->rx_size_acc = 0;
100         i2400m->rx_size_min = UINT_MAX;
101         i2400m->rx_size_max = 0;
102         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
103         return count;
104 }
105
106 static
107 const struct file_operations i2400m_rx_stats_fops = {
108         .owner =        THIS_MODULE,
109         .open =         simple_open,
110         .read =         i2400m_rx_stats_read,
111         .write =        i2400m_rx_stats_write,
112         .llseek =       default_llseek,
113 };
114
115
116 /* See i2400m_rx_stats_read() */
117 static
118 ssize_t i2400m_tx_stats_read(struct file *filp, char __user *buffer,
119                              size_t count, loff_t *ppos)
120 {
121         struct i2400m *i2400m = filp->private_data;
122         char buf[128];
123         unsigned long flags;
124
125         if (*ppos != 0)
126                 return 0;
127         if (count < sizeof(buf))
128                 return -ENOSPC;
129         spin_lock_irqsave(&i2400m->tx_lock, flags);
130         snprintf(buf, sizeof(buf), "%u %u %u %u %u %u %u\n",
131                  i2400m->tx_pl_num, i2400m->tx_pl_min,
132                  i2400m->tx_pl_max, i2400m->tx_num,
133                  i2400m->tx_size_acc,
134                  i2400m->tx_size_min, i2400m->tx_size_max);
135         spin_unlock_irqrestore(&i2400m->tx_lock, flags);
136         return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
137 }
138
139 /* Any write clears the stats */
140 static
141 ssize_t i2400m_tx_stats_write(struct file *filp, const char __user *buffer,
142                               size_t count, loff_t *ppos)
143 {
144         struct i2400m *i2400m = filp->private_data;
145         unsigned long flags;
146
147         spin_lock_irqsave(&i2400m->tx_lock, flags);
148         i2400m->tx_pl_num = 0;
149         i2400m->tx_pl_max = 0;
150         i2400m->tx_pl_min = UINT_MAX;
151         i2400m->tx_num = 0;
152         i2400m->tx_size_acc = 0;
153         i2400m->tx_size_min = UINT_MAX;
154         i2400m->tx_size_max = 0;
155         spin_unlock_irqrestore(&i2400m->tx_lock, flags);
156         return count;
157 }
158
159 static
160 const struct file_operations i2400m_tx_stats_fops = {
161         .owner =        THIS_MODULE,
162         .open =         simple_open,
163         .read =         i2400m_tx_stats_read,
164         .write =        i2400m_tx_stats_write,
165         .llseek =       default_llseek,
166 };
167
168
169 /* Write 1 to ask the device to go into suspend */
170 static
171 int debugfs_i2400m_suspend_set(void *data, u64 val)
172 {
173         int result;
174         struct i2400m *i2400m = data;
175         result = i2400m_cmd_enter_powersave(i2400m);
176         if (result >= 0)
177                 result = 0;
178         return result;
179 }
180 DEFINE_SIMPLE_ATTRIBUTE(fops_i2400m_suspend,
181                         NULL, debugfs_i2400m_suspend_set,
182                         "%llu\n");
183
184 static
185 struct dentry *debugfs_create_i2400m_suspend(
186         const char *name, struct dentry *parent, struct i2400m *i2400m)
187 {
188         return debugfs_create_file(name, 0200, parent, i2400m,
189                                    &fops_i2400m_suspend);
190 }
191
192
193 /*
194  * Reset the device
195  *
196  * Write 0 to ask the device to soft reset, 1 to cold reset, 2 to bus
197  * reset (as defined by enum i2400m_reset_type).
198  */
199 static
200 int debugfs_i2400m_reset_set(void *data, u64 val)
201 {
202         int result;
203         struct i2400m *i2400m = data;
204         enum i2400m_reset_type rt = val;
205         switch(rt) {
206         case I2400M_RT_WARM:
207         case I2400M_RT_COLD:
208         case I2400M_RT_BUS:
209                 result = i2400m_reset(i2400m, rt);
210                 if (result >= 0)
211                         result = 0;
212         default:
213                 result = -EINVAL;
214         }
215         return result;
216 }
217 DEFINE_SIMPLE_ATTRIBUTE(fops_i2400m_reset,
218                         NULL, debugfs_i2400m_reset_set,
219                         "%llu\n");
220
221 static
222 struct dentry *debugfs_create_i2400m_reset(
223         const char *name, struct dentry *parent, struct i2400m *i2400m)
224 {
225         return debugfs_create_file(name, 0200, parent, i2400m,
226                                    &fops_i2400m_reset);
227 }
228
229
230 #define __debugfs_register(prefix, name, parent)                        \
231 do {                                                                    \
232         result = d_level_register_debugfs(prefix, name, parent);        \
233         if (result < 0)                                                 \
234                 goto error;                                             \
235 } while (0)
236
237
238 int i2400m_debugfs_add(struct i2400m *i2400m)
239 {
240         int result;
241         struct device *dev = i2400m_dev(i2400m);
242         struct dentry *dentry = i2400m->wimax_dev.debugfs_dentry;
243         struct dentry *fd;
244
245         dentry = debugfs_create_dir("i2400m", dentry);
246         result = PTR_ERR(dentry);
247         if (IS_ERR(dentry)) {
248                 if (result == -ENODEV)
249                         result = 0;     /* No debugfs support */
250                 goto error;
251         }
252         i2400m->debugfs_dentry = dentry;
253         __debugfs_register("dl_", control, dentry);
254         __debugfs_register("dl_", driver, dentry);
255         __debugfs_register("dl_", debugfs, dentry);
256         __debugfs_register("dl_", fw, dentry);
257         __debugfs_register("dl_", netdev, dentry);
258         __debugfs_register("dl_", rfkill, dentry);
259         __debugfs_register("dl_", rx, dentry);
260         __debugfs_register("dl_", tx, dentry);
261
262         fd = debugfs_create_size_t("tx_in", 0400, dentry,
263                                    &i2400m->tx_in);
264         result = PTR_ERR(fd);
265         if (IS_ERR(fd) && result != -ENODEV) {
266                 dev_err(dev, "Can't create debugfs entry "
267                         "tx_in: %d\n", result);
268                 goto error;
269         }
270
271         fd = debugfs_create_size_t("tx_out", 0400, dentry,
272                                    &i2400m->tx_out);
273         result = PTR_ERR(fd);
274         if (IS_ERR(fd) && result != -ENODEV) {
275                 dev_err(dev, "Can't create debugfs entry "
276                         "tx_out: %d\n", result);
277                 goto error;
278         }
279
280         fd = debugfs_create_u32("state", 0600, dentry,
281                                 &i2400m->state);
282         result = PTR_ERR(fd);
283         if (IS_ERR(fd) && result != -ENODEV) {
284                 dev_err(dev, "Can't create debugfs entry "
285                         "state: %d\n", result);
286                 goto error;
287         }
288
289         /*
290          * Trace received messages from user space
291          *
292          * In order to tap the bidirectional message stream in the
293          * 'msg' pipe, user space can read from the 'msg' pipe;
294          * however, due to limitations in libnl, we can't know what
295          * the different applications are sending down to the kernel.
296          *
297          * So we have this hack where the driver will echo any message
298          * received on the msg pipe from user space [through a call to
299          * wimax_dev->op_msg_from_user() into
300          * i2400m_op_msg_from_user()] into the 'trace' pipe that this
301          * driver creates.
302          *
303          * So then, reading from both the 'trace' and 'msg' pipes in
304          * user space will provide a full dump of the traffic.
305          *
306          * Write 1 to activate, 0 to clear.
307          *
308          * It is not really very atomic, but it is also not too
309          * critical.
310          */
311         fd = debugfs_create_u8("trace_msg_from_user", 0600, dentry,
312                                &i2400m->trace_msg_from_user);
313         result = PTR_ERR(fd);
314         if (IS_ERR(fd) && result != -ENODEV) {
315                 dev_err(dev, "Can't create debugfs entry "
316                         "trace_msg_from_user: %d\n", result);
317                 goto error;
318         }
319
320         fd = debugfs_create_netdev_queue_stopped("netdev_queue_stopped",
321                                                  dentry, i2400m);
322         result = PTR_ERR(fd);
323         if (IS_ERR(fd) && result != -ENODEV) {
324                 dev_err(dev, "Can't create debugfs entry "
325                         "netdev_queue_stopped: %d\n", result);
326                 goto error;
327         }
328
329         fd = debugfs_create_file("rx_stats", 0600, dentry, i2400m,
330                                  &i2400m_rx_stats_fops);
331         result = PTR_ERR(fd);
332         if (IS_ERR(fd) && result != -ENODEV) {
333                 dev_err(dev, "Can't create debugfs entry "
334                         "rx_stats: %d\n", result);
335                 goto error;
336         }
337
338         fd = debugfs_create_file("tx_stats", 0600, dentry, i2400m,
339                                  &i2400m_tx_stats_fops);
340         result = PTR_ERR(fd);
341         if (IS_ERR(fd) && result != -ENODEV) {
342                 dev_err(dev, "Can't create debugfs entry "
343                         "tx_stats: %d\n", result);
344                 goto error;
345         }
346
347         fd = debugfs_create_i2400m_suspend("suspend", dentry, i2400m);
348         result = PTR_ERR(fd);
349         if (IS_ERR(fd) && result != -ENODEV) {
350                 dev_err(dev, "Can't create debugfs entry suspend: %d\n",
351                         result);
352                 goto error;
353         }
354
355         fd = debugfs_create_i2400m_reset("reset", dentry, i2400m);
356         result = PTR_ERR(fd);
357         if (IS_ERR(fd) && result != -ENODEV) {
358                 dev_err(dev, "Can't create debugfs entry reset: %d\n", result);
359                 goto error;
360         }
361
362         result = 0;
363 error:
364         return result;
365 }
366
367 void i2400m_debugfs_rm(struct i2400m *i2400m)
368 {
369         debugfs_remove_recursive(i2400m->debugfs_dentry);
370 }