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.
24 Supported chipsets: RT2460, RT2560, RT2570,
25 rt2561, rt2561s, rt2661, rt2571W & rt2671.
28 #include <linux/debugfs.h>
30 #include <asm/uaccess.h>
33 #include "rt2x00debug.h"
35 #define PRINT_REG8_STR ( "0x%.2x\n" )
36 #define PRINT_REG16_STR ( "0x%.4x\n" )
37 #define PRINT_REG32_STR ( "0x%.8x\n" )
38 #define PRINT_REG_LEN_MAX ( 16 )
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
64 struct dentry
*driver_folder
;
65 struct dentry
*driver_entry
;
66 struct dentry
*chipset_entry
;
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
;
75 * Driver and chipset files will use a data buffer
76 * that has been created in advance. This will simplify
77 * the code since we can use the debugfs functions.
79 struct debugfs_blob_wrapper driver_blob
;
80 struct debugfs_blob_wrapper chipset_blob
;
83 * Requested offset for each register type.
85 unsigned int offset_csr
;
86 unsigned int offset_eeprom
;
87 unsigned int offset_bbp
;
90 static int rt2x00debug_file_open(struct inode
*inode
, struct file
*file
)
92 struct rt2x00debug_intf
*intf
= inode
->i_private
;
94 file
->private_data
= inode
->i_private
;
96 if (!try_module_get(intf
->debug
->owner
))
102 static int rt2x00debug_file_release(struct inode
*inode
, struct file
*file
)
104 struct rt2x00debug_intf
*intf
= file
->private_data
;
106 module_put(intf
->debug
->owner
);
111 static ssize_t
rt2x00debug_file_read(void *device
, char __user
*buf
,
112 loff_t
*offset
, unsigned int word
, const struct rt2x00debug_reg
*reg
)
121 line
= kzalloc(PRINT_REG_LEN_MAX
, GFP_KERNEL
);
125 reg
->read(device
, word
, &value
);
127 if (reg
->word_size
== sizeof(u8
))
128 size
= sprintf(line
, PRINT_REG8_STR
, (u8
)value
);
129 else if (reg
->word_size
== sizeof(u16
))
130 size
= sprintf(line
, PRINT_REG16_STR
, (u16
)value
);
132 size
= sprintf(line
, PRINT_REG32_STR
, (u32
)value
);
134 if (copy_to_user(buf
, line
, size
))
148 static ssize_t
rt2x00debug_file_write(void *device
, const char __user
*buf
,
149 loff_t
*offset
, unsigned int word
, unsigned int length
,
150 const struct rt2x00debug_reg
*reg
)
156 line
= kzalloc(length
, GFP_KERNEL
);
160 if (copy_from_user(line
, buf
, length
))
164 value
= simple_strtoul(line
, NULL
, 0);
166 reg
->write(device
, word
, &value
);
179 #define RT2X00DEBUGFS_OPS_READ(__name) \
180 static ssize_t rt2x00debug_read_##__name(struct file *file, \
181 char __user *buf, size_t length, loff_t *offset) \
183 struct rt2x00debug_intf *intf = file->private_data; \
184 const struct rt2x00debug *debug = intf->debug; \
185 const struct rt2x00debug_reg *reg = &debug->reg_##__name;\
187 if (intf->offset_##__name > reg->word_count) \
190 return rt2x00debug_file_read(intf->rt2x00dev, buf, \
191 offset, intf->offset_##__name, reg); \
194 RT2X00DEBUGFS_OPS_READ(csr
);
195 RT2X00DEBUGFS_OPS_READ(eeprom
);
196 RT2X00DEBUGFS_OPS_READ(bbp
);
198 #define RT2X00DEBUGFS_OPS_WRITE(__name) \
199 static ssize_t rt2x00debug_write_##__name(struct file *file, \
200 const char __user *buf, size_t length, loff_t *offset) \
202 struct rt2x00debug_intf *intf = file->private_data; \
203 const struct rt2x00debug *debug = intf->debug; \
204 const struct rt2x00debug_reg *reg = &debug->reg_##__name;\
206 if (intf->offset_##__name > reg->word_count) \
209 return rt2x00debug_file_write(intf->rt2x00dev, buf, \
210 offset, intf->offset_##__name, length, reg); \
213 RT2X00DEBUGFS_OPS_WRITE(csr
);
214 RT2X00DEBUGFS_OPS_WRITE(eeprom
);
215 RT2X00DEBUGFS_OPS_WRITE(bbp
);
217 #define RT2X00DEBUGFS_OPS(__name) \
218 static const struct file_operations rt2x00debug_fop_##__name = {\
219 .owner = THIS_MODULE, \
220 .read = rt2x00debug_read_##__name, \
221 .write = rt2x00debug_write_##__name, \
222 .open = rt2x00debug_file_open, \
223 .release = rt2x00debug_file_release, \
226 RT2X00DEBUGFS_OPS(csr
);
227 RT2X00DEBUGFS_OPS(eeprom
);
228 RT2X00DEBUGFS_OPS(bbp
);
230 static struct dentry
*rt2x00debug_create_file_driver(const char *name
,
231 struct rt2x00debug_intf
*intf
, struct debugfs_blob_wrapper
*blob
)
235 data
= kzalloc(3 * PRINT_LINE_LEN_MAX
, GFP_KERNEL
);
240 data
+= sprintf(data
, "driver: %s\n", intf
->rt2x00dev
->ops
->name
);
241 data
+= sprintf(data
, "version: %s\n", DRV_VERSION
);
242 data
+= sprintf(data
, "compiled: %s %s\n", __DATE__
, __TIME__
);
243 blob
->size
= strlen(blob
->data
);
245 return debugfs_create_blob(name
, S_IRUGO
, intf
->driver_folder
, blob
);
248 static struct dentry
*rt2x00debug_create_file_chipset(const char *name
,
249 struct rt2x00debug_intf
*intf
, struct debugfs_blob_wrapper
*blob
)
251 const struct rt2x00debug
*debug
= intf
->debug
;
254 data
= kzalloc(3 * PRINT_LINE_LEN_MAX
, GFP_KERNEL
);
259 data
+= sprintf(data
, "csr length: %d\n", debug
->reg_csr
.word_count
);
260 data
+= sprintf(data
, "eeprom length: %d\n",
261 debug
->reg_eeprom
.word_count
);
262 data
+= sprintf(data
, "bbp length: %d\n", debug
->reg_bbp
.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
= debugfs_create_dir(intf
->rt2x00dev
->ops
->name
,
284 rt2x00dev
->hw
->wiphy
->debugfsdir
);
285 if (IS_ERR(intf
->driver_folder
))
288 intf
->driver_entry
= rt2x00debug_create_file_driver("driver",
289 intf
, &intf
->driver_blob
);
290 if (IS_ERR(intf
->driver_entry
))
293 intf
->chipset_entry
= rt2x00debug_create_file_chipset("chipset",
294 intf
, &intf
->chipset_blob
);
295 if (IS_ERR(intf
->chipset_entry
))
298 intf
->csr_off_entry
= debugfs_create_u32("csr_offset",
299 S_IRUGO
| S_IWUSR
, intf
->driver_folder
, &intf
->offset_csr
);
300 if (IS_ERR(intf
->csr_off_entry
))
303 intf
->csr_val_entry
= debugfs_create_file("csr_value",
304 S_IRUGO
| S_IWUSR
, intf
->driver_folder
, intf
,
305 &rt2x00debug_fop_csr
);
306 if (IS_ERR(intf
->csr_val_entry
))
309 intf
->eeprom_off_entry
= debugfs_create_u32("eeprom_offset",
310 S_IRUGO
| S_IWUSR
, intf
->driver_folder
, &intf
->offset_eeprom
);
311 if (IS_ERR(intf
->eeprom_off_entry
))
314 intf
->eeprom_val_entry
= debugfs_create_file("eeprom_value",
315 S_IRUGO
| S_IWUSR
, intf
->driver_folder
, intf
,
316 &rt2x00debug_fop_eeprom
);
317 if (IS_ERR(intf
->eeprom_val_entry
))
320 intf
->bbp_off_entry
= debugfs_create_u32("bbp_offset",
321 S_IRUGO
| S_IWUSR
, intf
->driver_folder
, &intf
->offset_bbp
);
322 if (IS_ERR(intf
->bbp_off_entry
))
325 intf
->bbp_val_entry
= debugfs_create_file("bbp_value",
326 S_IRUGO
| S_IWUSR
, intf
->driver_folder
, intf
,
327 &rt2x00debug_fop_bbp
);
328 if (IS_ERR(intf
->bbp_val_entry
))
334 rt2x00debug_deregister(rt2x00dev
);
335 ERROR(rt2x00dev
, "Failed to register debug handler.\n");
340 void rt2x00debug_deregister(struct rt2x00_dev
*rt2x00dev
)
342 const struct rt2x00debug_intf
*intf
= rt2x00dev
->debugfs_intf
;
347 debugfs_remove(intf
->bbp_val_entry
);
348 debugfs_remove(intf
->bbp_off_entry
);
349 debugfs_remove(intf
->eeprom_val_entry
);
350 debugfs_remove(intf
->eeprom_off_entry
);
351 debugfs_remove(intf
->csr_val_entry
);
352 debugfs_remove(intf
->csr_off_entry
);
353 debugfs_remove(intf
->chipset_entry
);
354 debugfs_remove(intf
->driver_entry
);
355 debugfs_remove(intf
->driver_folder
);
356 kfree(intf
->chipset_blob
.data
);
357 kfree(intf
->driver_blob
.data
);
360 rt2x00dev
->debugfs_intf
= NULL
;