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.
27 * Set enviroment defines for rt2x00.h
29 #define DRV_NAME "rt2x00lib"
31 #include <linux/debugfs.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/uaccess.h>
37 #include "rt2x00lib.h"
39 #define PRINT_LINE_LEN_MAX 32
41 struct rt2x00debug_intf {
43 * Pointer to driver structure where
44 * this debugfs entry belongs to.
46 struct rt2x00_dev *rt2x00dev;
49 * Reference to the rt2x00debug structure
50 * which can be used to communicate with
53 const struct rt2x00debug *debug;
56 * Debugfs entries for:
60 * - register offset/value files
61 * - eeprom offset/value files
62 * - bbp offset/value files
63 * - rf offset/value files
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;
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.
82 struct debugfs_blob_wrapper driver_blob;
83 struct debugfs_blob_wrapper chipset_blob;
86 * Requested offset for each register type.
88 unsigned int offset_csr;
89 unsigned int offset_eeprom;
90 unsigned int offset_bbp;
91 unsigned int offset_rf;
94 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
96 struct rt2x00debug_intf *intf = inode->i_private;
98 file->private_data = inode->i_private;
100 if (!try_module_get(intf->debug->owner))
106 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
108 struct rt2x00debug_intf *intf = file->private_data;
110 module_put(intf->debug->owner);
115 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
116 static ssize_t rt2x00debug_read_##__name(struct file *file, \
121 struct rt2x00debug_intf *intf = file->private_data; \
122 const struct rt2x00debug *debug = intf->debug; \
130 if (intf->offset_##__name >= debug->__name.word_count) \
133 debug->__name.read(intf->rt2x00dev, \
134 intf->offset_##__name, &value); \
136 size = sprintf(line, __format, value); \
138 if (copy_to_user(buf, line, size)) \
145 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
146 static ssize_t rt2x00debug_write_##__name(struct file *file, \
147 const char __user *buf,\
151 struct rt2x00debug_intf *intf = file->private_data; \
152 const struct rt2x00debug *debug = intf->debug; \
160 if (!capable(CAP_NET_ADMIN)) \
163 if (intf->offset_##__name >= debug->__name.word_count) \
166 if (copy_from_user(line, buf, length)) \
169 size = strlen(line); \
170 value = simple_strtoul(line, NULL, 0); \
172 debug->__name.write(intf->rt2x00dev, \
173 intf->offset_##__name, value); \
179 #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
180 RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
181 RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
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, \
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);
196 static struct dentry *rt2x00debug_create_file_driver(const char *name,
197 struct rt2x00debug_intf
199 struct debugfs_blob_wrapper
204 data = kzalloc(3 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
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);
214 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
217 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
218 struct rt2x00debug_intf
224 const struct rt2x00debug *debug = intf->debug;
227 data = kzalloc(4 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
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);
238 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
241 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
243 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
244 struct rt2x00debug_intf *intf;
246 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
248 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
253 intf->rt2x00dev = rt2x00dev;
254 rt2x00dev->debugfs_intf = intf;
256 intf->driver_folder =
257 debugfs_create_dir(intf->rt2x00dev->ops->name,
258 rt2x00dev->hw->wiphy->debugfsdir);
259 if (IS_ERR(intf->driver_folder))
263 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
264 if (IS_ERR(intf->driver_entry))
267 intf->chipset_entry =
268 rt2x00debug_create_file_chipset("chipset",
269 intf, &intf->chipset_blob);
270 if (IS_ERR(intf->chipset_entry))
273 #define RT2X00DEBUGFS_CREATE_ENTRY(__intf, __name) \
275 (__intf)->__name##_off_entry = \
276 debugfs_create_u32(__stringify(__name) "_offset", \
278 (__intf)->driver_folder, \
279 &(__intf)->offset_##__name); \
280 if (IS_ERR((__intf)->__name##_off_entry)) \
283 (__intf)->__name##_val_entry = \
284 debugfs_create_file(__stringify(__name) "_value", \
286 (__intf)->driver_folder, \
287 (__intf), &rt2x00debug_fop_##__name);\
288 if (IS_ERR((__intf)->__name##_val_entry)) \
292 RT2X00DEBUGFS_CREATE_ENTRY(intf, csr);
293 RT2X00DEBUGFS_CREATE_ENTRY(intf, eeprom);
294 RT2X00DEBUGFS_CREATE_ENTRY(intf, bbp);
295 RT2X00DEBUGFS_CREATE_ENTRY(intf, rf);
297 #undef RT2X00DEBUGFS_CREATE_ENTRY
302 rt2x00debug_deregister(rt2x00dev);
303 ERROR(rt2x00dev, "Failed to register debug handler.\n");
308 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
310 const struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
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);
330 rt2x00dev->debugfs_intf = NULL;