[RT2x00]: add driver for Ralink wireless hardware
[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 /*
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          * - register offset/value files
61          * - eeprom offset/value files
62          * - bbp offset/value files
63          * - rf offset/value files
64          */
65         struct dentry *driver_folder;
66         struct dentry *driver_entry;
67         struct dentry *chipset_entry;
68         struct dentry *csr_off_entry;
69         struct dentry *csr_val_entry;
70         struct dentry *eeprom_off_entry;
71         struct dentry *eeprom_val_entry;
72         struct dentry *bbp_off_entry;
73         struct dentry *bbp_val_entry;
74         struct dentry *rf_off_entry;
75         struct dentry *rf_val_entry;
76
77         /*
78          * Driver and chipset files will use a data buffer
79          * that has been created in advance. This will simplify
80          * the code since we can use the debugfs functions.
81          */
82         struct debugfs_blob_wrapper driver_blob;
83         struct debugfs_blob_wrapper chipset_blob;
84
85         /*
86          * Requested offset for each register type.
87          */
88         unsigned int offset_csr;
89         unsigned int offset_eeprom;
90         unsigned int offset_bbp;
91         unsigned int offset_rf;
92 };
93
94 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
95 {
96         struct rt2x00debug_intf *intf = inode->i_private;
97
98         file->private_data = inode->i_private;
99
100         if (!try_module_get(intf->debug->owner))
101                 return -EBUSY;
102
103         return 0;
104 }
105
106 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
107 {
108         struct rt2x00debug_intf *intf = file->private_data;
109
110         module_put(intf->debug->owner);
111
112         return 0;
113 }
114
115 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type)        \
116 static ssize_t rt2x00debug_read_##__name(struct file *file,     \
117                                          char __user *buf,      \
118                                          size_t length,         \
119                                          loff_t *offset)        \
120 {                                                               \
121         struct rt2x00debug_intf *intf = file->private_data;     \
122         const struct rt2x00debug *debug = intf->debug;          \
123         char line[16];                                          \
124         size_t size;                                            \
125         __type value;                                           \
126                                                                 \
127         if (*offset)                                            \
128                 return 0;                                       \
129                                                                 \
130         if (intf->offset_##__name >= debug->__name.word_count)  \
131                 return -EINVAL;                                 \
132                                                                 \
133         debug->__name.read(intf->rt2x00dev,                     \
134                            intf->offset_##__name, &value);      \
135                                                                 \
136         size = sprintf(line, __format, value);                  \
137                                                                 \
138         if (copy_to_user(buf, line, size))                      \
139                 return -EFAULT;                                 \
140                                                                 \
141         *offset += size;                                        \
142         return size;                                            \
143 }
144
145 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type)                 \
146 static ssize_t rt2x00debug_write_##__name(struct file *file,    \
147                                           const char __user *buf,\
148                                           size_t length,        \
149                                           loff_t *offset)       \
150 {                                                               \
151         struct rt2x00debug_intf *intf = file->private_data;     \
152         const struct rt2x00debug *debug = intf->debug;          \
153         char line[16];                                          \
154         size_t size;                                            \
155         __type value;                                           \
156                                                                 \
157         if (*offset)                                            \
158                 return 0;                                       \
159                                                                 \
160         if (!capable(CAP_NET_ADMIN))                            \
161                 return -EPERM;                                  \
162                                                                 \
163         if (intf->offset_##__name >= debug->__name.word_count)  \
164                 return -EINVAL;                                 \
165                                                                 \
166         if (copy_from_user(line, buf, length))                  \
167                 return -EFAULT;                                 \
168                                                                 \
169         size = strlen(line);                                    \
170         value = simple_strtoul(line, NULL, 0);                  \
171                                                                 \
172         debug->__name.write(intf->rt2x00dev,                    \
173                             intf->offset_##__name, value);      \
174                                                                 \
175         *offset += size;                                        \
176         return size;                                            \
177 }
178
179 #define RT2X00DEBUGFS_OPS(__name, __format, __type)             \
180 RT2X00DEBUGFS_OPS_READ(__name, __format, __type);               \
181 RT2X00DEBUGFS_OPS_WRITE(__name, __type);                        \
182                                                                 \
183 static const struct file_operations rt2x00debug_fop_##__name = {\
184         .owner          = THIS_MODULE,                          \
185         .read           = rt2x00debug_read_##__name,            \
186         .write          = rt2x00debug_write_##__name,           \
187         .open           = rt2x00debug_file_open,                \
188         .release        = rt2x00debug_file_release,             \
189 };
190
191 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
192 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
193 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
194 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
195
196 static struct dentry *rt2x00debug_create_file_driver(const char *name,
197                                                      struct rt2x00debug_intf
198                                                      *intf,
199                                                      struct debugfs_blob_wrapper
200                                                      *blob)
201 {
202         char *data;
203
204         data = kzalloc(3 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
205         if (!data)
206                 return NULL;
207
208         blob->data = data;
209         data += sprintf(data, "driver: %s\n", intf->rt2x00dev->ops->name);
210         data += sprintf(data, "version: %s\n", DRV_VERSION);
211         data += sprintf(data, "compiled: %s %s\n", __DATE__, __TIME__);
212         blob->size = strlen(blob->data);
213
214         return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
215 }
216
217 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
218                                                       struct rt2x00debug_intf
219                                                       *intf,
220                                                       struct
221                                                       debugfs_blob_wrapper
222                                                       *blob)
223 {
224         const struct rt2x00debug *debug = intf->debug;
225         char *data;
226
227         data = kzalloc(4 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
228         if (!data)
229                 return NULL;
230
231         blob->data = data;
232         data += sprintf(data, "csr length: %d\n", debug->csr.word_count);
233         data += sprintf(data, "eeprom length: %d\n", debug->eeprom.word_count);
234         data += sprintf(data, "bbp length: %d\n", debug->bbp.word_count);
235         data += sprintf(data, "rf length: %d\n", debug->rf.word_count);
236         blob->size = strlen(blob->data);
237
238         return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
239 }
240
241 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
242 {
243         const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
244         struct rt2x00debug_intf *intf;
245
246         intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
247         if (!intf) {
248                 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
249                 return;
250         }
251
252         intf->debug = debug;
253         intf->rt2x00dev = rt2x00dev;
254         rt2x00dev->debugfs_intf = intf;
255
256         intf->driver_folder =
257             debugfs_create_dir(intf->rt2x00dev->ops->name,
258                                rt2x00dev->hw->wiphy->debugfsdir);
259         if (IS_ERR(intf->driver_folder))
260                 goto exit;
261
262         intf->driver_entry =
263             rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
264         if (IS_ERR(intf->driver_entry))
265                 goto exit;
266
267         intf->chipset_entry =
268             rt2x00debug_create_file_chipset("chipset",
269                                             intf, &intf->chipset_blob);
270         if (IS_ERR(intf->chipset_entry))
271                 goto exit;
272
273 #define RT2X00DEBUGFS_CREATE_ENTRY(__intf, __name)              \
274 ({                                                              \
275         (__intf)->__name##_off_entry =                          \
276             debugfs_create_u32(__stringify(__name) "_offset",   \
277                                S_IRUGO | S_IWUSR,               \
278                                (__intf)->driver_folder,         \
279                                &(__intf)->offset_##__name);     \
280         if (IS_ERR((__intf)->__name##_off_entry))               \
281                 goto exit;                                      \
282                                                                 \
283         (__intf)->__name##_val_entry =                          \
284             debugfs_create_file(__stringify(__name) "_value",   \
285                                 S_IRUGO | S_IWUSR,              \
286                                 (__intf)->driver_folder,        \
287                                 (__intf), &rt2x00debug_fop_##__name);\
288         if (IS_ERR((__intf)->__name##_val_entry))               \
289                 goto exit;                                      \
290 })
291
292         RT2X00DEBUGFS_CREATE_ENTRY(intf, csr);
293         RT2X00DEBUGFS_CREATE_ENTRY(intf, eeprom);
294         RT2X00DEBUGFS_CREATE_ENTRY(intf, bbp);
295         RT2X00DEBUGFS_CREATE_ENTRY(intf, rf);
296
297 #undef RT2X00DEBUGFS_CREATE_ENTRY
298
299         return;
300
301 exit:
302         rt2x00debug_deregister(rt2x00dev);
303         ERROR(rt2x00dev, "Failed to register debug handler.\n");
304
305         return;
306 }
307
308 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
309 {
310         const struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
311
312         if (unlikely(!intf))
313                 return;
314
315         debugfs_remove(intf->rf_val_entry);
316         debugfs_remove(intf->rf_off_entry);
317         debugfs_remove(intf->bbp_val_entry);
318         debugfs_remove(intf->bbp_off_entry);
319         debugfs_remove(intf->eeprom_val_entry);
320         debugfs_remove(intf->eeprom_off_entry);
321         debugfs_remove(intf->csr_val_entry);
322         debugfs_remove(intf->csr_off_entry);
323         debugfs_remove(intf->chipset_entry);
324         debugfs_remove(intf->driver_entry);
325         debugfs_remove(intf->driver_folder);
326         kfree(intf->chipset_blob.data);
327         kfree(intf->driver_blob.data);
328         kfree(intf);
329
330         rt2x00dev->debugfs_intf = NULL;
331 }