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