Merge branch 'nfs-server-stable' of git://linux-nfs.org/~bfields/linux
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00debug.c
1 /*
2         Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3         <http://rt2x00.serialmonkey.com>
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13         GNU 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
17         Free Software Foundation, Inc.,
18         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 /*
22         Module: rt2x00lib
23         Abstract: rt2x00 debugfs specific routines.
24  */
25
26 /*
27  * Set enviroment defines for rt2x00.h
28  */
29 #define DRV_NAME "rt2x00lib"
30
31 #include <linux/debugfs.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/uaccess.h>
35
36 #include "rt2x00.h"
37 #include "rt2x00lib.h"
38
39 #define PRINT_LINE_LEN_MAX 32
40
41 struct rt2x00debug_intf {
42         /*
43          * Pointer to driver structure where
44          * this debugfs entry belongs to.
45          */
46         struct rt2x00_dev *rt2x00dev;
47
48         /*
49          * Reference to the rt2x00debug structure
50          * which can be used to communicate with
51          * the registers.
52          */
53         const struct rt2x00debug *debug;
54
55         /*
56          * Debugfs entries for:
57          * - driver folder
58          * - driver file
59          * - chipset file
60          * - device flags file
61          * - register offset/value files
62          * - eeprom offset/value files
63          * - bbp offset/value files
64          * - rf offset/value files
65          */
66         struct dentry *driver_folder;
67         struct dentry *driver_entry;
68         struct dentry *chipset_entry;
69         struct dentry *dev_flags;
70         struct dentry *csr_off_entry;
71         struct dentry *csr_val_entry;
72         struct dentry *eeprom_off_entry;
73         struct dentry *eeprom_val_entry;
74         struct dentry *bbp_off_entry;
75         struct dentry *bbp_val_entry;
76         struct dentry *rf_off_entry;
77         struct dentry *rf_val_entry;
78
79         /*
80          * Driver and chipset files will use a data buffer
81          * that has been created in advance. This will simplify
82          * the code since we can use the debugfs functions.
83          */
84         struct debugfs_blob_wrapper driver_blob;
85         struct debugfs_blob_wrapper chipset_blob;
86
87         /*
88          * Requested offset for each register type.
89          */
90         unsigned int offset_csr;
91         unsigned int offset_eeprom;
92         unsigned int offset_bbp;
93         unsigned int offset_rf;
94 };
95
96 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
97 {
98         struct rt2x00debug_intf *intf = inode->i_private;
99
100         file->private_data = inode->i_private;
101
102         if (!try_module_get(intf->debug->owner))
103                 return -EBUSY;
104
105         return 0;
106 }
107
108 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
109 {
110         struct rt2x00debug_intf *intf = file->private_data;
111
112         module_put(intf->debug->owner);
113
114         return 0;
115 }
116
117 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type)        \
118 static ssize_t rt2x00debug_read_##__name(struct file *file,     \
119                                          char __user *buf,      \
120                                          size_t length,         \
121                                          loff_t *offset)        \
122 {                                                               \
123         struct rt2x00debug_intf *intf = file->private_data;     \
124         const struct rt2x00debug *debug = intf->debug;          \
125         char line[16];                                          \
126         size_t size;                                            \
127         __type value;                                           \
128                                                                 \
129         if (*offset)                                            \
130                 return 0;                                       \
131                                                                 \
132         if (intf->offset_##__name >= debug->__name.word_count)  \
133                 return -EINVAL;                                 \
134                                                                 \
135         debug->__name.read(intf->rt2x00dev,                     \
136                            intf->offset_##__name, &value);      \
137                                                                 \
138         size = sprintf(line, __format, value);                  \
139                                                                 \
140         if (copy_to_user(buf, line, size))                      \
141                 return -EFAULT;                                 \
142                                                                 \
143         *offset += size;                                        \
144         return size;                                            \
145 }
146
147 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type)                 \
148 static ssize_t rt2x00debug_write_##__name(struct file *file,    \
149                                           const char __user *buf,\
150                                           size_t length,        \
151                                           loff_t *offset)       \
152 {                                                               \
153         struct rt2x00debug_intf *intf = file->private_data;     \
154         const struct rt2x00debug *debug = intf->debug;          \
155         char line[16];                                          \
156         size_t size;                                            \
157         __type value;                                           \
158                                                                 \
159         if (*offset)                                            \
160                 return 0;                                       \
161                                                                 \
162         if (!capable(CAP_NET_ADMIN))                            \
163                 return -EPERM;                                  \
164                                                                 \
165         if (intf->offset_##__name >= debug->__name.word_count)  \
166                 return -EINVAL;                                 \
167                                                                 \
168         if (copy_from_user(line, buf, length))                  \
169                 return -EFAULT;                                 \
170                                                                 \
171         size = strlen(line);                                    \
172         value = simple_strtoul(line, NULL, 0);                  \
173                                                                 \
174         debug->__name.write(intf->rt2x00dev,                    \
175                             intf->offset_##__name, value);      \
176                                                                 \
177         *offset += size;                                        \
178         return size;                                            \
179 }
180
181 #define RT2X00DEBUGFS_OPS(__name, __format, __type)             \
182 RT2X00DEBUGFS_OPS_READ(__name, __format, __type);               \
183 RT2X00DEBUGFS_OPS_WRITE(__name, __type);                        \
184                                                                 \
185 static const struct file_operations rt2x00debug_fop_##__name = {\
186         .owner          = THIS_MODULE,                          \
187         .read           = rt2x00debug_read_##__name,            \
188         .write          = rt2x00debug_write_##__name,           \
189         .open           = rt2x00debug_file_open,                \
190         .release        = rt2x00debug_file_release,             \
191 };
192
193 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
194 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
195 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
196 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
197
198 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
199                                           char __user *buf,
200                                           size_t length,
201                                           loff_t *offset)
202 {
203         struct rt2x00debug_intf *intf = file->private_data;
204         char line[16];
205         size_t size;
206
207         if (*offset)
208                 return 0;
209
210         size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
211
212         if (copy_to_user(buf, line, size))
213                 return -EFAULT;
214
215         *offset += size;
216         return size;
217 }
218
219 static const struct file_operations rt2x00debug_fop_dev_flags = {
220         .owner          = THIS_MODULE,
221         .read           = rt2x00debug_read_dev_flags,
222         .open           = rt2x00debug_file_open,
223         .release        = rt2x00debug_file_release,
224 };
225
226 static struct dentry *rt2x00debug_create_file_driver(const char *name,
227                                                      struct rt2x00debug_intf
228                                                      *intf,
229                                                      struct debugfs_blob_wrapper
230                                                      *blob)
231 {
232         char *data;
233
234         data = kzalloc(3 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
235         if (!data)
236                 return NULL;
237
238         blob->data = data;
239         data += sprintf(data, "driver: %s\n", intf->rt2x00dev->ops->name);
240         data += sprintf(data, "version: %s\n", DRV_VERSION);
241         data += sprintf(data, "compiled: %s %s\n", __DATE__, __TIME__);
242         blob->size = strlen(blob->data);
243
244         return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
245 }
246
247 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
248                                                       struct rt2x00debug_intf
249                                                       *intf,
250                                                       struct
251                                                       debugfs_blob_wrapper
252                                                       *blob)
253 {
254         const struct rt2x00debug *debug = intf->debug;
255         char *data;
256
257         data = kzalloc(4 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
258         if (!data)
259                 return NULL;
260
261         blob->data = data;
262         data += sprintf(data, "csr length: %d\n", debug->csr.word_count);
263         data += sprintf(data, "eeprom length: %d\n", debug->eeprom.word_count);
264         data += sprintf(data, "bbp length: %d\n", debug->bbp.word_count);
265         data += sprintf(data, "rf length: %d\n", debug->rf.word_count);
266         blob->size = strlen(blob->data);
267
268         return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
269 }
270
271 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
272 {
273         const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
274         struct rt2x00debug_intf *intf;
275
276         intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
277         if (!intf) {
278                 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
279                 return;
280         }
281
282         intf->debug = debug;
283         intf->rt2x00dev = rt2x00dev;
284         rt2x00dev->debugfs_intf = intf;
285
286         intf->driver_folder =
287             debugfs_create_dir(intf->rt2x00dev->ops->name,
288                                rt2x00dev->hw->wiphy->debugfsdir);
289         if (IS_ERR(intf->driver_folder))
290                 goto exit;
291
292         intf->driver_entry =
293             rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
294         if (IS_ERR(intf->driver_entry))
295                 goto exit;
296
297         intf->chipset_entry =
298             rt2x00debug_create_file_chipset("chipset",
299                                             intf, &intf->chipset_blob);
300         if (IS_ERR(intf->chipset_entry))
301                 goto exit;
302
303         intf->dev_flags = debugfs_create_file("dev_flags", S_IRUGO,
304                                               intf->driver_folder, intf,
305                                               &rt2x00debug_fop_dev_flags);
306         if (IS_ERR(intf->dev_flags))
307                 goto exit;
308
309 #define RT2X00DEBUGFS_CREATE_ENTRY(__intf, __name)              \
310 ({                                                              \
311         (__intf)->__name##_off_entry =                          \
312             debugfs_create_u32(__stringify(__name) "_offset",   \
313                                S_IRUGO | S_IWUSR,               \
314                                (__intf)->driver_folder,         \
315                                &(__intf)->offset_##__name);     \
316         if (IS_ERR((__intf)->__name##_off_entry))               \
317                 goto exit;                                      \
318                                                                 \
319         (__intf)->__name##_val_entry =                          \
320             debugfs_create_file(__stringify(__name) "_value",   \
321                                 S_IRUGO | S_IWUSR,              \
322                                 (__intf)->driver_folder,        \
323                                 (__intf), &rt2x00debug_fop_##__name);\
324         if (IS_ERR((__intf)->__name##_val_entry))               \
325                 goto exit;                                      \
326 })
327
328         RT2X00DEBUGFS_CREATE_ENTRY(intf, csr);
329         RT2X00DEBUGFS_CREATE_ENTRY(intf, eeprom);
330         RT2X00DEBUGFS_CREATE_ENTRY(intf, bbp);
331         RT2X00DEBUGFS_CREATE_ENTRY(intf, rf);
332
333 #undef RT2X00DEBUGFS_CREATE_ENTRY
334
335         return;
336
337 exit:
338         rt2x00debug_deregister(rt2x00dev);
339         ERROR(rt2x00dev, "Failed to register debug handler.\n");
340
341         return;
342 }
343
344 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
345 {
346         const struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
347
348         if (unlikely(!intf))
349                 return;
350
351         debugfs_remove(intf->rf_val_entry);
352         debugfs_remove(intf->rf_off_entry);
353         debugfs_remove(intf->bbp_val_entry);
354         debugfs_remove(intf->bbp_off_entry);
355         debugfs_remove(intf->eeprom_val_entry);
356         debugfs_remove(intf->eeprom_off_entry);
357         debugfs_remove(intf->csr_val_entry);
358         debugfs_remove(intf->csr_off_entry);
359         debugfs_remove(intf->dev_flags);
360         debugfs_remove(intf->chipset_entry);
361         debugfs_remove(intf->driver_entry);
362         debugfs_remove(intf->driver_folder);
363         kfree(intf->chipset_blob.data);
364         kfree(intf->driver_blob.data);
365         kfree(intf);
366
367         rt2x00dev->debugfs_intf = NULL;
368 }