2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
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.
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.
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.
23 Abstract: rt2x00 debugfs specific routines.
26 #include <linux/debugfs.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/uaccess.h>
32 #include "rt2x00lib.h"
34 #define PRINT_LINE_LEN_MAX 32
36 struct rt2x00debug_intf {
38 * Pointer to driver structure where
39 * this debugfs entry belongs to.
41 struct rt2x00_dev *rt2x00dev;
44 * Reference to the rt2x00debug structure
45 * which can be used to communicate with
48 const struct rt2x00debug *debug;
51 * Debugfs entries for:
57 * - csr offset/value files
58 * - eeprom offset/value files
59 * - bbp offset/value files
60 * - rf offset/value files
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;
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.
81 struct debugfs_blob_wrapper driver_blob;
82 struct debugfs_blob_wrapper chipset_blob;
85 * Requested offset for each register type.
87 unsigned int offset_csr;
88 unsigned int offset_eeprom;
89 unsigned int offset_bbp;
90 unsigned int offset_rf;
93 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
95 struct rt2x00debug_intf *intf = inode->i_private;
97 file->private_data = inode->i_private;
99 if (!try_module_get(intf->debug->owner))
105 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
107 struct rt2x00debug_intf *intf = file->private_data;
109 module_put(intf->debug->owner);
114 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
115 static ssize_t rt2x00debug_read_##__name(struct file *file, \
120 struct rt2x00debug_intf *intf = file->private_data; \
121 const struct rt2x00debug *debug = intf->debug; \
129 if (intf->offset_##__name >= debug->__name.word_count) \
132 debug->__name.read(intf->rt2x00dev, \
133 intf->offset_##__name, &value); \
135 size = sprintf(line, __format, value); \
137 if (copy_to_user(buf, line, size)) \
144 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
145 static ssize_t rt2x00debug_write_##__name(struct file *file, \
146 const char __user *buf,\
150 struct rt2x00debug_intf *intf = file->private_data; \
151 const struct rt2x00debug *debug = intf->debug; \
159 if (!capable(CAP_NET_ADMIN)) \
162 if (intf->offset_##__name >= debug->__name.word_count) \
165 if (copy_from_user(line, buf, length)) \
168 size = strlen(line); \
169 value = simple_strtoul(line, NULL, 0); \
171 debug->__name.write(intf->rt2x00dev, \
172 intf->offset_##__name, value); \
178 #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
179 RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
180 RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
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, \
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);
195 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
200 struct rt2x00debug_intf *intf = file->private_data;
207 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
209 if (copy_to_user(buf, line, size))
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,
223 static struct dentry *rt2x00debug_create_file_driver(const char *name,
224 struct rt2x00debug_intf
226 struct debugfs_blob_wrapper
231 data = kzalloc(3 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
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);
241 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
244 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
245 struct rt2x00debug_intf
251 const struct rt2x00debug *debug = intf->debug;
254 data = kzalloc(4 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
259 data += sprintf(data, "csr length: %d\n", debug->csr.word_count);
260 data += sprintf(data, "eeprom length: %d\n", debug->eeprom.word_count);
261 data += sprintf(data, "bbp length: %d\n", debug->bbp.word_count);
262 data += sprintf(data, "rf length: %d\n", debug->rf.word_count);
263 blob->size = strlen(blob->data);
265 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
268 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
270 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
271 struct rt2x00debug_intf *intf;
273 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
275 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
280 intf->rt2x00dev = rt2x00dev;
281 rt2x00dev->debugfs_intf = intf;
283 intf->driver_folder =
284 debugfs_create_dir(intf->rt2x00dev->ops->name,
285 rt2x00dev->hw->wiphy->debugfsdir);
286 if (IS_ERR(intf->driver_folder))
290 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
291 if (IS_ERR(intf->driver_entry))
294 intf->chipset_entry =
295 rt2x00debug_create_file_chipset("chipset",
296 intf, &intf->chipset_blob);
297 if (IS_ERR(intf->chipset_entry))
300 intf->dev_flags = debugfs_create_file("dev_flags", S_IRUGO,
301 intf->driver_folder, intf,
302 &rt2x00debug_fop_dev_flags);
303 if (IS_ERR(intf->dev_flags))
306 intf->register_folder =
307 debugfs_create_dir("register", intf->driver_folder);
308 if (IS_ERR(intf->register_folder))
311 #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
313 (__intf)->__name##_off_entry = \
314 debugfs_create_u32(__stringify(__name) "_offset", \
316 (__intf)->register_folder, \
317 &(__intf)->offset_##__name); \
318 if (IS_ERR((__intf)->__name##_off_entry)) \
321 (__intf)->__name##_val_entry = \
322 debugfs_create_file(__stringify(__name) "_value", \
324 (__intf)->register_folder, \
325 (__intf), &rt2x00debug_fop_##__name);\
326 if (IS_ERR((__intf)->__name##_val_entry)) \
330 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
331 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
332 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
333 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
335 #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
340 rt2x00debug_deregister(rt2x00dev);
341 ERROR(rt2x00dev, "Failed to register debug handler.\n");
346 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
348 const struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
353 debugfs_remove(intf->rf_val_entry);
354 debugfs_remove(intf->rf_off_entry);
355 debugfs_remove(intf->bbp_val_entry);
356 debugfs_remove(intf->bbp_off_entry);
357 debugfs_remove(intf->eeprom_val_entry);
358 debugfs_remove(intf->eeprom_off_entry);
359 debugfs_remove(intf->csr_val_entry);
360 debugfs_remove(intf->csr_off_entry);
361 debugfs_remove(intf->register_folder);
362 debugfs_remove(intf->dev_flags);
363 debugfs_remove(intf->chipset_entry);
364 debugfs_remove(intf->driver_entry);
365 debugfs_remove(intf->driver_folder);
366 kfree(intf->chipset_blob.data);
367 kfree(intf->driver_blob.data);
370 rt2x00dev->debugfs_intf = NULL;