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