fix issues with multiple ppp links (noticed by Stefano Rivera)
[openwrt.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 #include "rt2x00debug.h"
34
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 )
40
41 struct rt2x00debug_intf {
42 /*
43 * Pointer to driver structure where
44 * this debugfs entry belongs to.
45 */
46 struct rt2x00_dev *rt2x00dev;
47
48 /*
49 * Reference to the rt2x00debug structure
50 * which can be used to communicate with
51 * the registers.
52 */
53 const struct rt2x00debug *debug;
54
55 /*
56 * Debugfs entries for:
57 * - driver folder
58 * - driver file
59 * - chipset file
60 * - register offset/value files
61 * - eeprom offset/value files
62 * - bbp offset/value files
63 */
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;
73
74 /*
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.
78 */
79 struct debugfs_blob_wrapper driver_blob;
80 struct debugfs_blob_wrapper chipset_blob;
81
82 /*
83 * Requested offset for each register type.
84 */
85 unsigned int offset_csr;
86 unsigned int offset_eeprom;
87 unsigned int offset_bbp;
88 };
89
90 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
91 {
92 struct rt2x00debug_intf *intf = inode->i_private;
93
94 file->private_data = inode->i_private;
95
96 if (!try_module_get(intf->debug->owner))
97 return -EBUSY;
98
99 return 0;
100 }
101
102 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
103 {
104 struct rt2x00debug_intf *intf = file->private_data;
105
106 module_put(intf->debug->owner);
107
108 return 0;
109 }
110
111 static ssize_t rt2x00debug_file_read(void *device, char __user *buf,
112 loff_t *offset, unsigned int word, const struct rt2x00debug_reg *reg)
113 {
114 unsigned long value;
115 unsigned int size;
116 char *line;
117
118 if (*offset)
119 return 0;
120
121 line = kzalloc(PRINT_REG_LEN_MAX, GFP_KERNEL);
122 if (!line)
123 return -ENOMEM;
124
125 reg->read(device, word, &value);
126
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);
131 else
132 size = sprintf(line, PRINT_REG32_STR, (u32)value);
133
134 if (copy_to_user(buf, line, size))
135 goto exit;
136
137 kfree(line);
138
139 *offset += size;
140 return size;
141
142 exit:
143 kfree(line);
144
145 return -EFAULT;
146 }
147
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)
151 {
152 unsigned long value;
153 int size;
154 char *line;
155
156 line = kzalloc(length, GFP_KERNEL);
157 if (!line)
158 return -ENOMEM;
159
160 if (copy_from_user(line, buf, length))
161 goto exit;
162
163 size = strlen(line);
164 value = simple_strtoul(line, NULL, 0);
165
166 reg->write(device, word, &value);
167
168 kfree(line);
169
170 *offset += size;
171 return size;
172
173 exit:
174 kfree(line);
175
176 return -EFAULT;
177 }
178
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) \
182 { \
183 struct rt2x00debug_intf *intf = file->private_data; \
184 const struct rt2x00debug *debug = intf->debug; \
185 const struct rt2x00debug_reg *reg = &debug->reg_##__name;\
186 \
187 if (intf->offset_##__name > reg->word_count) \
188 return -EINVAL; \
189 \
190 return rt2x00debug_file_read(intf->rt2x00dev, buf, \
191 offset, intf->offset_##__name, reg); \
192 }
193
194 RT2X00DEBUGFS_OPS_READ(csr);
195 RT2X00DEBUGFS_OPS_READ(eeprom);
196 RT2X00DEBUGFS_OPS_READ(bbp);
197
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) \
201 { \
202 struct rt2x00debug_intf *intf = file->private_data; \
203 const struct rt2x00debug *debug = intf->debug; \
204 const struct rt2x00debug_reg *reg = &debug->reg_##__name;\
205 \
206 if (intf->offset_##__name > reg->word_count) \
207 return -EINVAL; \
208 \
209 return rt2x00debug_file_write(intf->rt2x00dev, buf, \
210 offset, intf->offset_##__name, length, reg); \
211 }
212
213 RT2X00DEBUGFS_OPS_WRITE(csr);
214 RT2X00DEBUGFS_OPS_WRITE(eeprom);
215 RT2X00DEBUGFS_OPS_WRITE(bbp);
216
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, \
224 };
225
226 RT2X00DEBUGFS_OPS(csr);
227 RT2X00DEBUGFS_OPS(eeprom);
228 RT2X00DEBUGFS_OPS(bbp);
229
230 static struct dentry *rt2x00debug_create_file_driver(const char *name,
231 struct rt2x00debug_intf *intf, struct debugfs_blob_wrapper *blob)
232 {
233 char *data;
234
235 data = kzalloc(3 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
236 if (!data)
237 return NULL;
238
239 blob->data = data;
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);
244
245 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
246 }
247
248 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
249 struct rt2x00debug_intf *intf, struct debugfs_blob_wrapper *blob)
250 {
251 const struct rt2x00debug *debug = intf->debug;
252 char *data;
253
254 data = kzalloc(3 * PRINT_LINE_LEN_MAX, GFP_KERNEL);
255 if (!data)
256 return NULL;
257
258 blob->data = data;
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);
264
265 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
266 }
267
268 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
269 {
270 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
271 struct rt2x00debug_intf *intf;
272
273 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
274 if (!intf) {
275 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
276 return;
277 }
278
279 intf->debug = debug;
280 intf->rt2x00dev = rt2x00dev;
281 rt2x00dev->debugfs_intf = intf;
282
283 intf->driver_folder = debugfs_create_dir(intf->rt2x00dev->ops->name,
284 rt2x00dev->hw->wiphy->debugfsdir);
285 if (IS_ERR(intf->driver_folder))
286 goto exit;
287
288 intf->driver_entry = rt2x00debug_create_file_driver("driver",
289 intf, &intf->driver_blob);
290 if (IS_ERR(intf->driver_entry))
291 goto exit;
292
293 intf->chipset_entry = rt2x00debug_create_file_chipset("chipset",
294 intf, &intf->chipset_blob);
295 if (IS_ERR(intf->chipset_entry))
296 goto exit;
297
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))
301 goto exit;
302
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))
307 goto exit;
308
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))
312 goto exit;
313
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))
318 goto exit;
319
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))
323 goto exit;
324
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))
329 goto exit;
330
331 return;
332
333 exit:
334 rt2x00debug_deregister(rt2x00dev);
335 ERROR(rt2x00dev, "Failed to register debug handler.\n");
336
337 return;
338 }
339
340 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
341 {
342 const struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
343
344 if (unlikely(!intf))
345 return;
346
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);
358 kfree(intf);
359
360 rt2x00dev->debugfs_intf = NULL;
361 }
This page took 0.056943 seconds and 5 git commands to generate.