pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / net / wireless / rt2x00 / rt2x00debug.c
blob9275d6f9517e32c714a179923f2b162c157cbd8a
1 /*
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.
22 Module: rt2x00lib
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>
36 #include "rt2x00.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
51 * the registers.
53 const struct rt2x00debug *debug;
56 * Debugfs entries for:
57 * - driver folder
58 * - driver file
59 * - chipset file
60 * - device flags file
61 * - register offset/value files
62 * - eeprom offset/value files
63 * - bbp offset/value files
64 * - rf offset/value files
66 struct dentry *driver_folder;
67 struct dentry *driver_entry;
68 struct dentry *chipset_entry;
69 struct dentry *dev_flags;
70 struct dentry *csr_off_entry;
71 struct dentry *csr_val_entry;
72 struct dentry *eeprom_off_entry;
73 struct dentry *eeprom_val_entry;
74 struct dentry *bbp_off_entry;
75 struct dentry *bbp_val_entry;
76 struct dentry *rf_off_entry;
77 struct dentry *rf_val_entry;
80 * Driver and chipset files will use a data buffer
81 * that has been created in advance. This will simplify
82 * the code since we can use the debugfs functions.
84 struct debugfs_blob_wrapper driver_blob;
85 struct debugfs_blob_wrapper chipset_blob;
88 * Requested offset for each register type.
90 unsigned int offset_csr;
91 unsigned int offset_eeprom;
92 unsigned int offset_bbp;
93 unsigned int offset_rf;
96 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
98 struct rt2x00debug_intf *intf = inode->i_private;
100 file->private_data = inode->i_private;
102 if (!try_module_get(intf->debug->owner))
103 return -EBUSY;
105 return 0;
108 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
110 struct rt2x00debug_intf *intf = file->private_data;
112 module_put(intf->debug->owner);
114 return 0;
117 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
118 static ssize_t rt2x00debug_read_##__name(struct file *file, \
119 char __user *buf, \
120 size_t length, \
121 loff_t *offset) \
123 struct rt2x00debug_intf *intf = file->private_data; \
124 const struct rt2x00debug *debug = intf->debug; \
125 char line[16]; \
126 size_t size; \
127 __type value; \
129 if (*offset) \
130 return 0; \
132 if (intf->offset_##__name >= debug->__name.word_count) \
133 return -EINVAL; \
135 debug->__name.read(intf->rt2x00dev, \
136 intf->offset_##__name, &value); \
138 size = sprintf(line, __format, value); \
140 if (copy_to_user(buf, line, size)) \
141 return -EFAULT; \
143 *offset += size; \
144 return size; \
147 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
148 static ssize_t rt2x00debug_write_##__name(struct file *file, \
149 const char __user *buf,\
150 size_t length, \
151 loff_t *offset) \
153 struct rt2x00debug_intf *intf = file->private_data; \
154 const struct rt2x00debug *debug = intf->debug; \
155 char line[16]; \
156 size_t size; \
157 __type value; \
159 if (*offset) \
160 return 0; \
162 if (!capable(CAP_NET_ADMIN)) \
163 return -EPERM; \
165 if (intf->offset_##__name >= debug->__name.word_count) \
166 return -EINVAL; \
168 if (copy_from_user(line, buf, length)) \
169 return -EFAULT; \
171 size = strlen(line); \
172 value = simple_strtoul(line, NULL, 0); \
174 debug->__name.write(intf->rt2x00dev, \
175 intf->offset_##__name, value); \
177 *offset += size; \
178 return size; \
181 #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
182 RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
183 RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
185 static const struct file_operations rt2x00debug_fop_##__name = {\
186 .owner = THIS_MODULE, \
187 .read = rt2x00debug_read_##__name, \
188 .write = rt2x00debug_write_##__name, \
189 .open = rt2x00debug_file_open, \
190 .release = rt2x00debug_file_release, \
193 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
194 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
195 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
196 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
198 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
199 char __user *buf,
200 size_t length,
201 loff_t *offset)
203 struct rt2x00debug_intf *intf = file->private_data;
204 char line[16];
205 size_t size;
207 if (*offset)
208 return 0;
210 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
212 if (copy_to_user(buf, line, size))
213 return -EFAULT;
215 *offset += size;
216 return size;
219 static const struct file_operations rt2x00debug_fop_dev_flags = {
220 .owner = THIS_MODULE,
221 .read = rt2x00debug_read_dev_flags,
222 .open = rt2x00debug_file_open,
223 .release = rt2x00debug_file_release,
226 static struct dentry *rt2x00debug_create_file_driver(const char *name,
227 struct rt2x00debug_intf
228 *intf,
229 struct debugfs_blob_wrapper
230 *blob)
232 char *data;
234 data = kzalloc(3 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
235 if (!data)
236 return NULL;
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);
244 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
247 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
248 struct rt2x00debug_intf
249 *intf,
250 struct
251 debugfs_blob_wrapper
252 *blob)
254 const struct rt2x00debug *debug = intf->debug;
255 char *data;
257 data = kzalloc(4 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
258 if (!data)
259 return NULL;
261 blob->data = data;
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 blob->size = strlen(blob->data);
268 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
271 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
273 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
274 struct rt2x00debug_intf *intf;
276 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
277 if (!intf) {
278 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
279 return;
282 intf->debug = debug;
283 intf->rt2x00dev = rt2x00dev;
284 rt2x00dev->debugfs_intf = intf;
286 intf->driver_folder =
287 debugfs_create_dir(intf->rt2x00dev->ops->name,
288 rt2x00dev->hw->wiphy->debugfsdir);
289 if (IS_ERR(intf->driver_folder))
290 goto exit;
292 intf->driver_entry =
293 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
294 if (IS_ERR(intf->driver_entry))
295 goto exit;
297 intf->chipset_entry =
298 rt2x00debug_create_file_chipset("chipset",
299 intf, &intf->chipset_blob);
300 if (IS_ERR(intf->chipset_entry))
301 goto exit;
303 intf->dev_flags = debugfs_create_file("dev_flags", S_IRUGO,
304 intf->driver_folder, intf,
305 &rt2x00debug_fop_dev_flags);
306 if (IS_ERR(intf->dev_flags))
307 goto exit;
309 #define RT2X00DEBUGFS_CREATE_ENTRY(__intf, __name) \
310 ({ \
311 (__intf)->__name##_off_entry = \
312 debugfs_create_u32(__stringify(__name) "_offset", \
313 S_IRUGO | S_IWUSR, \
314 (__intf)->driver_folder, \
315 &(__intf)->offset_##__name); \
316 if (IS_ERR((__intf)->__name##_off_entry)) \
317 goto exit; \
319 (__intf)->__name##_val_entry = \
320 debugfs_create_file(__stringify(__name) "_value", \
321 S_IRUGO | S_IWUSR, \
322 (__intf)->driver_folder, \
323 (__intf), &rt2x00debug_fop_##__name);\
324 if (IS_ERR((__intf)->__name##_val_entry)) \
325 goto exit; \
328 RT2X00DEBUGFS_CREATE_ENTRY(intf, csr);
329 RT2X00DEBUGFS_CREATE_ENTRY(intf, eeprom);
330 RT2X00DEBUGFS_CREATE_ENTRY(intf, bbp);
331 RT2X00DEBUGFS_CREATE_ENTRY(intf, rf);
333 #undef RT2X00DEBUGFS_CREATE_ENTRY
335 return;
337 exit:
338 rt2x00debug_deregister(rt2x00dev);
339 ERROR(rt2x00dev, "Failed to register debug handler.\n");
341 return;
344 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
346 const struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
348 if (unlikely(!intf))
349 return;
351 debugfs_remove(intf->rf_val_entry);
352 debugfs_remove(intf->rf_off_entry);
353 debugfs_remove(intf->bbp_val_entry);
354 debugfs_remove(intf->bbp_off_entry);
355 debugfs_remove(intf->eeprom_val_entry);
356 debugfs_remove(intf->eeprom_off_entry);
357 debugfs_remove(intf->csr_val_entry);
358 debugfs_remove(intf->csr_off_entry);
359 debugfs_remove(intf->dev_flags);
360 debugfs_remove(intf->chipset_entry);
361 debugfs_remove(intf->driver_entry);
362 debugfs_remove(intf->driver_folder);
363 kfree(intf->chipset_blob.data);
364 kfree(intf->driver_blob.data);
365 kfree(intf);
367 rt2x00dev->debugfs_intf = NULL;