blob: c1710b27ba70c702fc789120c08ea06cac9ba15a [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01002 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <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 */
25
Ivo van Doorn95ea3622007-09-25 17:57:13 -070026#include <linux/debugfs.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010029#include <linux/poll.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040030#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070032#include <linux/uaccess.h>
33
34#include "rt2x00.h"
35#include "rt2x00lib.h"
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010036#include "rt2x00dump.h"
Ivo van Doorn95ea3622007-09-25 17:57:13 -070037
Ivo van Doorn68598d292008-02-10 22:50:28 +010038#define MAX_LINE_LENGTH 64
Ivo van Doorn95ea3622007-09-25 17:57:13 -070039
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020040struct rt2x00debug_crypto {
41 unsigned long success;
42 unsigned long icv_error;
43 unsigned long mic_error;
44 unsigned long key_error;
45};
46
Ivo van Doorn95ea3622007-09-25 17:57:13 -070047struct rt2x00debug_intf {
48 /*
49 * Pointer to driver structure where
50 * this debugfs entry belongs to.
51 */
52 struct rt2x00_dev *rt2x00dev;
53
54 /*
55 * Reference to the rt2x00debug structure
56 * which can be used to communicate with
57 * the registers.
58 */
59 const struct rt2x00debug *debug;
60
61 /*
62 * Debugfs entries for:
63 * - driver folder
Ivo van Doorn91921a42007-11-27 21:48:16 +010064 * - driver file
65 * - chipset file
66 * - device flags file
67 * - register folder
68 * - csr offset/value files
69 * - eeprom offset/value files
70 * - bbp offset/value files
71 * - rf offset/value files
Ivo van Doorn68598d292008-02-10 22:50:28 +010072 * - queue folder
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010073 * - frame dump file
Ivo van Doorn68598d292008-02-10 22:50:28 +010074 * - queue stats file
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020075 * - crypto stats file
Ivo van Doorn95ea3622007-09-25 17:57:13 -070076 */
77 struct dentry *driver_folder;
78 struct dentry *driver_entry;
79 struct dentry *chipset_entry;
Ivo van Doorn643b2522007-09-25 20:13:51 +020080 struct dentry *dev_flags;
Ivo van Doorn91921a42007-11-27 21:48:16 +010081 struct dentry *register_folder;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070082 struct dentry *csr_off_entry;
83 struct dentry *csr_val_entry;
84 struct dentry *eeprom_off_entry;
85 struct dentry *eeprom_val_entry;
86 struct dentry *bbp_off_entry;
87 struct dentry *bbp_val_entry;
88 struct dentry *rf_off_entry;
89 struct dentry *rf_val_entry;
Ivo van Doorn68598d292008-02-10 22:50:28 +010090 struct dentry *queue_folder;
91 struct dentry *queue_frame_dump_entry;
92 struct dentry *queue_stats_entry;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020093 struct dentry *crypto_stats_entry;
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010094
95 /*
96 * The frame dump file only allows a single reader,
97 * so we need to store the current state here.
98 */
99 unsigned long frame_dump_flags;
100#define FRAME_DUMP_FILE_OPEN 1
101
102 /*
103 * We queue each frame before dumping it to the user,
104 * per read command we will pass a single skb structure
105 * so we should be prepared to queue multiple sk buffers
106 * before sending it to userspace.
107 */
108 struct sk_buff_head frame_dump_skbqueue;
109 wait_queue_head_t frame_dump_waitqueue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700110
111 /*
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200112 * HW crypto statistics.
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800113 * All statistics are stored separately per cipher type.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200114 */
115 struct rt2x00debug_crypto crypto_stats[CIPHER_MAX];
116
117 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700118 * Driver and chipset files will use a data buffer
119 * that has been created in advance. This will simplify
120 * the code since we can use the debugfs functions.
121 */
122 struct debugfs_blob_wrapper driver_blob;
123 struct debugfs_blob_wrapper chipset_blob;
124
125 /*
126 * Requested offset for each register type.
127 */
128 unsigned int offset_csr;
129 unsigned int offset_eeprom;
130 unsigned int offset_bbp;
131 unsigned int offset_rf;
132};
133
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200134void rt2x00debug_update_crypto(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn84e31962008-12-20 10:53:29 +0100135 struct rxdone_entry_desc *rxdesc)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200136{
137 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100138 enum cipher cipher = rxdesc->cipher;
139 enum rx_crypto status = rxdesc->cipher_status;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200140
141 if (cipher == CIPHER_TKIP_NO_MIC)
142 cipher = CIPHER_TKIP;
Roel Kluina6c67332009-05-20 02:12:56 +0200143 if (cipher == CIPHER_NONE || cipher >= CIPHER_MAX)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200144 return;
145
146 /* Remove CIPHER_NONE index */
147 cipher--;
148
149 intf->crypto_stats[cipher].success += (status == RX_CRYPTO_SUCCESS);
150 intf->crypto_stats[cipher].icv_error += (status == RX_CRYPTO_FAIL_ICV);
151 intf->crypto_stats[cipher].mic_error += (status == RX_CRYPTO_FAIL_MIC);
152 intf->crypto_stats[cipher].key_error += (status == RX_CRYPTO_FAIL_KEY);
153}
154
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100155void rt2x00debug_dump_frame(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn5a6e5992008-05-10 13:41:32 +0200156 enum rt2x00_dump_type type, struct sk_buff *skb)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100157{
158 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
Gertjan van Wingerde878f7042010-05-11 23:51:37 +0200159 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100160 struct sk_buff *skbcopy;
161 struct rt2x00dump_hdr *dump_hdr;
162 struct timeval timestamp;
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200163 u32 data_len;
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100164
165 do_gettimeofday(&timestamp);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100166
167 if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
168 return;
169
170 if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
171 DEBUG(rt2x00dev, "txrx dump queue length exceeded.\n");
172 return;
173 }
174
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200175 data_len = skb->len;
176 if (skbdesc->flags & SKBDESC_DESC_IN_SKB)
177 data_len -= skbdesc->desc_len;
178
179 skbcopy = alloc_skb(sizeof(*dump_hdr) + skbdesc->desc_len + data_len,
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100180 GFP_ATOMIC);
181 if (!skbcopy) {
182 DEBUG(rt2x00dev, "Failed to copy skb for dump.\n");
183 return;
184 }
185
186 dump_hdr = (struct rt2x00dump_hdr *)skb_put(skbcopy, sizeof(*dump_hdr));
187 dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
188 dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
Gertjan van Wingerde878f7042010-05-11 23:51:37 +0200189 dump_hdr->desc_length = cpu_to_le32(skbdesc->desc_len);
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200190 dump_hdr->data_length = cpu_to_le32(data_len);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100191 dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
192 dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +0100193 dump_hdr->chip_rev = cpu_to_le16(rt2x00dev->chip.rev);
Ivo van Doorn5a6e5992008-05-10 13:41:32 +0200194 dump_hdr->type = cpu_to_le16(type);
Gertjan van Wingerde878f7042010-05-11 23:51:37 +0200195 dump_hdr->queue_index = skbdesc->entry->queue->qid;
196 dump_hdr->entry_index = skbdesc->entry->entry_idx;
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100197 dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
198 dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_usec);
199
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200200 if (!(skbdesc->flags & SKBDESC_DESC_IN_SKB))
201 memcpy(skb_put(skbcopy, skbdesc->desc_len), skbdesc->desc,
202 skbdesc->desc_len);
Gertjan van Wingerded56d4532008-06-06 22:54:08 +0200203 memcpy(skb_put(skbcopy, skb->len), skb->data, skb->len);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100204
205 skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
206 wake_up_interruptible(&intf->frame_dump_waitqueue);
207
208 /*
209 * Verify that the file has not been closed while we were working.
210 */
211 if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
212 skb_queue_purge(&intf->frame_dump_skbqueue);
213}
Gertjan van Wingerdeb4df4702010-05-13 13:16:36 +0200214EXPORT_SYMBOL_GPL(rt2x00debug_dump_frame);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100215
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700216static int rt2x00debug_file_open(struct inode *inode, struct file *file)
217{
218 struct rt2x00debug_intf *intf = inode->i_private;
219
220 file->private_data = inode->i_private;
221
222 if (!try_module_get(intf->debug->owner))
223 return -EBUSY;
224
225 return 0;
226}
227
228static int rt2x00debug_file_release(struct inode *inode, struct file *file)
229{
230 struct rt2x00debug_intf *intf = file->private_data;
231
232 module_put(intf->debug->owner);
233
234 return 0;
235}
236
Ivo van Doorn181d6902008-02-05 16:42:23 -0500237static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100238{
239 struct rt2x00debug_intf *intf = inode->i_private;
240 int retval;
241
242 retval = rt2x00debug_file_open(inode, file);
243 if (retval)
244 return retval;
245
246 if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
247 rt2x00debug_file_release(inode, file);
248 return -EBUSY;
249 }
250
251 return 0;
252}
253
Ivo van Doorn181d6902008-02-05 16:42:23 -0500254static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100255{
256 struct rt2x00debug_intf *intf = inode->i_private;
257
258 skb_queue_purge(&intf->frame_dump_skbqueue);
259
260 clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
261
262 return rt2x00debug_file_release(inode, file);
263}
264
Ivo van Doorn181d6902008-02-05 16:42:23 -0500265static ssize_t rt2x00debug_read_queue_dump(struct file *file,
266 char __user *buf,
267 size_t length,
268 loff_t *offset)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100269{
270 struct rt2x00debug_intf *intf = file->private_data;
271 struct sk_buff *skb;
272 size_t status;
273 int retval;
274
275 if (file->f_flags & O_NONBLOCK)
276 return -EAGAIN;
277
278 retval =
279 wait_event_interruptible(intf->frame_dump_waitqueue,
280 (skb =
281 skb_dequeue(&intf->frame_dump_skbqueue)));
282 if (retval)
283 return retval;
284
285 status = min((size_t)skb->len, length);
286 if (copy_to_user(buf, skb->data, status)) {
287 status = -EFAULT;
288 goto exit;
289 }
290
291 *offset += status;
292
293exit:
294 kfree_skb(skb);
295
296 return status;
297}
298
Ivo van Doorn181d6902008-02-05 16:42:23 -0500299static unsigned int rt2x00debug_poll_queue_dump(struct file *file,
John Daiker55887512008-10-17 12:16:17 -0700300 poll_table *wait)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100301{
302 struct rt2x00debug_intf *intf = file->private_data;
303
304 poll_wait(file, &intf->frame_dump_waitqueue, wait);
305
306 if (!skb_queue_empty(&intf->frame_dump_skbqueue))
307 return POLLOUT | POLLWRNORM;
308
309 return 0;
310}
311
Ivo van Doorn181d6902008-02-05 16:42:23 -0500312static const struct file_operations rt2x00debug_fop_queue_dump = {
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100313 .owner = THIS_MODULE,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500314 .read = rt2x00debug_read_queue_dump,
315 .poll = rt2x00debug_poll_queue_dump,
316 .open = rt2x00debug_open_queue_dump,
317 .release = rt2x00debug_release_queue_dump,
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100318};
319
Ivo van Doorn68598d292008-02-10 22:50:28 +0100320static ssize_t rt2x00debug_read_queue_stats(struct file *file,
321 char __user *buf,
322 size_t length,
323 loff_t *offset)
324{
325 struct rt2x00debug_intf *intf = file->private_data;
326 struct data_queue *queue;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100327 unsigned long irqflags;
Ivo van Doorn68598d292008-02-10 22:50:28 +0100328 unsigned int lines = 1 + intf->rt2x00dev->data_queues;
329 size_t size;
330 char *data;
331 char *temp;
332
333 if (*offset)
334 return 0;
335
Joe Perchesbaeb2ff2010-08-11 07:02:48 +0000336 data = kcalloc(lines, MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100337 if (!data)
338 return -ENOMEM;
339
340 temp = data +
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200341 sprintf(data, "qid\tcount\tlimit\tlength\tindex\tdma done\tdone\n");
Ivo van Doorn68598d292008-02-10 22:50:28 +0100342
343 queue_for_each(intf->rt2x00dev, queue) {
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100344 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100345
346 temp += sprintf(temp, "%d\t%d\t%d\t%d\t%d\t%d\t%d\n", queue->qid,
347 queue->count, queue->limit, queue->length,
348 queue->index[Q_INDEX],
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200349 queue->index[Q_INDEX_DMA_DONE],
Ivo van Doorn54e34fb2010-08-23 19:54:41 +0200350 queue->index[Q_INDEX_DONE]);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100351
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100352 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100353 }
354
355 size = strlen(data);
356 size = min(size, length);
357
358 if (copy_to_user(buf, data, size)) {
359 kfree(data);
360 return -EFAULT;
361 }
362
363 kfree(data);
364
365 *offset += size;
366 return size;
367}
368
369static const struct file_operations rt2x00debug_fop_queue_stats = {
370 .owner = THIS_MODULE,
371 .read = rt2x00debug_read_queue_stats,
372 .open = rt2x00debug_file_open,
373 .release = rt2x00debug_file_release,
374};
375
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200376#ifdef CONFIG_RT2X00_LIB_CRYPTO
377static ssize_t rt2x00debug_read_crypto_stats(struct file *file,
378 char __user *buf,
379 size_t length,
380 loff_t *offset)
381{
382 struct rt2x00debug_intf *intf = file->private_data;
Joe Perches030bda02010-09-13 18:23:55 +0000383 static const char * const name[] = { "WEP64", "WEP128", "TKIP", "AES" };
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200384 char *data;
385 char *temp;
386 size_t size;
387 unsigned int i;
388
389 if (*offset)
390 return 0;
391
John Daiker55887512008-10-17 12:16:17 -0700392 data = kzalloc((1 + CIPHER_MAX) * MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200393 if (!data)
394 return -ENOMEM;
395
396 temp = data;
397 temp += sprintf(data, "cipher\tsuccess\ticv err\tmic err\tkey err\n");
398
399 for (i = 0; i < CIPHER_MAX; i++) {
400 temp += sprintf(temp, "%s\t%lu\t%lu\t%lu\t%lu\n", name[i],
401 intf->crypto_stats[i].success,
402 intf->crypto_stats[i].icv_error,
403 intf->crypto_stats[i].mic_error,
404 intf->crypto_stats[i].key_error);
405 }
406
407 size = strlen(data);
408 size = min(size, length);
409
410 if (copy_to_user(buf, data, size)) {
411 kfree(data);
412 return -EFAULT;
413 }
414
415 kfree(data);
416
417 *offset += size;
418 return size;
419}
420
421static const struct file_operations rt2x00debug_fop_crypto_stats = {
422 .owner = THIS_MODULE,
423 .read = rt2x00debug_read_crypto_stats,
424 .open = rt2x00debug_file_open,
425 .release = rt2x00debug_file_release,
426};
427#endif
428
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700429#define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
430static ssize_t rt2x00debug_read_##__name(struct file *file, \
431 char __user *buf, \
432 size_t length, \
433 loff_t *offset) \
434{ \
Ivo van Doorn91921a42007-11-27 21:48:16 +0100435 struct rt2x00debug_intf *intf = file->private_data; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700436 const struct rt2x00debug *debug = intf->debug; \
437 char line[16]; \
438 size_t size; \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100439 unsigned int index = intf->offset_##__name; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700440 __type value; \
441 \
442 if (*offset) \
443 return 0; \
444 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100445 if (index >= debug->__name.word_count) \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700446 return -EINVAL; \
447 \
Ivo van Doornabd2fdb2009-02-17 14:04:29 +0100448 index += (debug->__name.word_base / \
449 debug->__name.word_size); \
450 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100451 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
452 index *= debug->__name.word_size; \
453 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100454 debug->__name.read(intf->rt2x00dev, index, &value); \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700455 \
456 size = sprintf(line, __format, value); \
457 \
458 if (copy_to_user(buf, line, size)) \
459 return -EFAULT; \
460 \
461 *offset += size; \
462 return size; \
463}
464
465#define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
466static ssize_t rt2x00debug_write_##__name(struct file *file, \
467 const char __user *buf,\
468 size_t length, \
469 loff_t *offset) \
470{ \
Ivo van Doorn91921a42007-11-27 21:48:16 +0100471 struct rt2x00debug_intf *intf = file->private_data; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700472 const struct rt2x00debug *debug = intf->debug; \
473 char line[16]; \
474 size_t size; \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100475 unsigned int index = intf->offset_##__name; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700476 __type value; \
477 \
478 if (*offset) \
479 return 0; \
480 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100481 if (index >= debug->__name.word_count) \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700482 return -EINVAL; \
483 \
Arnaud Patard (Rtp)f8d8b7a2010-08-23 23:02:22 +0200484 if (length > sizeof(line)) \
485 return -EINVAL; \
486 \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700487 if (copy_from_user(line, buf, length)) \
488 return -EFAULT; \
489 \
490 size = strlen(line); \
491 value = simple_strtoul(line, NULL, 0); \
492 \
Ivo van Doornabd2fdb2009-02-17 14:04:29 +0100493 index += (debug->__name.word_base / \
494 debug->__name.word_size); \
495 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100496 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
497 index *= debug->__name.word_size; \
498 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100499 debug->__name.write(intf->rt2x00dev, index, value); \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700500 \
501 *offset += size; \
502 return size; \
503}
504
505#define RT2X00DEBUGFS_OPS(__name, __format, __type) \
506RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
507RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
508 \
509static const struct file_operations rt2x00debug_fop_##__name = {\
510 .owner = THIS_MODULE, \
511 .read = rt2x00debug_read_##__name, \
512 .write = rt2x00debug_write_##__name, \
513 .open = rt2x00debug_file_open, \
514 .release = rt2x00debug_file_release, \
515};
516
517RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
518RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
519RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
520RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
521
Ivo van Doorn643b2522007-09-25 20:13:51 +0200522static ssize_t rt2x00debug_read_dev_flags(struct file *file,
523 char __user *buf,
524 size_t length,
525 loff_t *offset)
526{
527 struct rt2x00debug_intf *intf = file->private_data;
528 char line[16];
529 size_t size;
530
531 if (*offset)
532 return 0;
533
534 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
535
536 if (copy_to_user(buf, line, size))
537 return -EFAULT;
538
539 *offset += size;
540 return size;
541}
542
543static const struct file_operations rt2x00debug_fop_dev_flags = {
544 .owner = THIS_MODULE,
545 .read = rt2x00debug_read_dev_flags,
546 .open = rt2x00debug_file_open,
547 .release = rt2x00debug_file_release,
548};
549
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700550static struct dentry *rt2x00debug_create_file_driver(const char *name,
551 struct rt2x00debug_intf
552 *intf,
553 struct debugfs_blob_wrapper
554 *blob)
555{
556 char *data;
557
Ivo van Doorn68598d292008-02-10 22:50:28 +0100558 data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700559 if (!data)
560 return NULL;
561
562 blob->data = data;
Ivo van Doorn860559f2009-03-03 18:14:18 +0100563 data += sprintf(data, "driver:\t%s\n", intf->rt2x00dev->ops->name);
564 data += sprintf(data, "version:\t%s\n", DRV_VERSION);
565 data += sprintf(data, "compiled:\t%s %s\n", __DATE__, __TIME__);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700566 blob->size = strlen(blob->data);
567
Ivo van Doornd2b69072008-07-27 15:06:05 +0200568 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700569}
570
571static struct dentry *rt2x00debug_create_file_chipset(const char *name,
572 struct rt2x00debug_intf
573 *intf,
574 struct
575 debugfs_blob_wrapper
576 *blob)
577{
578 const struct rt2x00debug *debug = intf->debug;
579 char *data;
580
Ivo van Doorn68598d292008-02-10 22:50:28 +0100581 data = kzalloc(8 * MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700582 if (!data)
583 return NULL;
584
Ivo van Doorn3e34c6d2008-01-06 23:38:10 +0100585 blob->data = data;
Ivo van Doorn860559f2009-03-03 18:14:18 +0100586 data += sprintf(data, "rt chip:\t%04x\n", intf->rt2x00dev->chip.rt);
587 data += sprintf(data, "rf chip:\t%04x\n", intf->rt2x00dev->chip.rf);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +0100588 data += sprintf(data, "revision:\t%04x\n", intf->rt2x00dev->chip.rev);
Ivo van Doorn22c96c22007-11-27 21:48:37 +0100589 data += sprintf(data, "\n");
Ivo van Doorn860559f2009-03-03 18:14:18 +0100590 data += sprintf(data, "register\tbase\twords\twordsize\n");
591 data += sprintf(data, "csr\t%d\t%d\t%d\n",
592 debug->csr.word_base,
593 debug->csr.word_count,
594 debug->csr.word_size);
595 data += sprintf(data, "eeprom\t%d\t%d\t%d\n",
596 debug->eeprom.word_base,
597 debug->eeprom.word_count,
598 debug->eeprom.word_size);
599 data += sprintf(data, "bbp\t%d\t%d\t%d\n",
600 debug->bbp.word_base,
601 debug->bbp.word_count,
602 debug->bbp.word_size);
603 data += sprintf(data, "rf\t%d\t%d\t%d\n",
604 debug->rf.word_base,
605 debug->rf.word_count,
606 debug->rf.word_size);
Ivo van Doorn3e34c6d2008-01-06 23:38:10 +0100607 blob->size = strlen(blob->data);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700608
Ivo van Doornd2b69072008-07-27 15:06:05 +0200609 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700610}
611
612void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
613{
614 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
615 struct rt2x00debug_intf *intf;
616
617 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
618 if (!intf) {
619 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
620 return;
621 }
622
623 intf->debug = debug;
624 intf->rt2x00dev = rt2x00dev;
625 rt2x00dev->debugfs_intf = intf;
626
627 intf->driver_folder =
628 debugfs_create_dir(intf->rt2x00dev->ops->name,
629 rt2x00dev->hw->wiphy->debugfsdir);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800630 if (IS_ERR(intf->driver_folder) || !intf->driver_folder)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700631 goto exit;
632
633 intf->driver_entry =
634 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800635 if (IS_ERR(intf->driver_entry) || !intf->driver_entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700636 goto exit;
637
638 intf->chipset_entry =
639 rt2x00debug_create_file_chipset("chipset",
640 intf, &intf->chipset_blob);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800641 if (IS_ERR(intf->chipset_entry) || !intf->chipset_entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700642 goto exit;
643
Ivo van Doornd2b69072008-07-27 15:06:05 +0200644 intf->dev_flags = debugfs_create_file("dev_flags", S_IRUSR,
Ivo van Doorn643b2522007-09-25 20:13:51 +0200645 intf->driver_folder, intf,
646 &rt2x00debug_fop_dev_flags);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800647 if (IS_ERR(intf->dev_flags) || !intf->dev_flags)
Ivo van Doorn643b2522007-09-25 20:13:51 +0200648 goto exit;
649
Ivo van Doorn91921a42007-11-27 21:48:16 +0100650 intf->register_folder =
651 debugfs_create_dir("register", intf->driver_folder);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800652 if (IS_ERR(intf->register_folder) || !intf->register_folder)
Ivo van Doorn91921a42007-11-27 21:48:16 +0100653 goto exit;
654
655#define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700656({ \
657 (__intf)->__name##_off_entry = \
658 debugfs_create_u32(__stringify(__name) "_offset", \
Ivo van Doornd2b69072008-07-27 15:06:05 +0200659 S_IRUSR | S_IWUSR, \
Ivo van Doorn91921a42007-11-27 21:48:16 +0100660 (__intf)->register_folder, \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700661 &(__intf)->offset_##__name); \
Zhaoleicfa3fa42008-10-22 17:07:25 +0800662 if (IS_ERR((__intf)->__name##_off_entry) \
663 || !(__intf)->__name##_off_entry) \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700664 goto exit; \
665 \
666 (__intf)->__name##_val_entry = \
667 debugfs_create_file(__stringify(__name) "_value", \
Ivo van Doornd2b69072008-07-27 15:06:05 +0200668 S_IRUSR | S_IWUSR, \
Ivo van Doorn91921a42007-11-27 21:48:16 +0100669 (__intf)->register_folder, \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700670 (__intf), &rt2x00debug_fop_##__name);\
Zhaoleicfa3fa42008-10-22 17:07:25 +0800671 if (IS_ERR((__intf)->__name##_val_entry) \
672 || !(__intf)->__name##_val_entry) \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700673 goto exit; \
674})
675
Ivo van Doorn91921a42007-11-27 21:48:16 +0100676 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
677 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
678 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
679 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700680
Ivo van Doorn91921a42007-11-27 21:48:16 +0100681#undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700682
Ivo van Doorn68598d292008-02-10 22:50:28 +0100683 intf->queue_folder =
684 debugfs_create_dir("queue", intf->driver_folder);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800685 if (IS_ERR(intf->queue_folder) || !intf->queue_folder)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100686 goto exit;
687
Ivo van Doorn68598d292008-02-10 22:50:28 +0100688 intf->queue_frame_dump_entry =
Ivo van Doornd2b69072008-07-27 15:06:05 +0200689 debugfs_create_file("dump", S_IRUSR, intf->queue_folder,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500690 intf, &rt2x00debug_fop_queue_dump);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800691 if (IS_ERR(intf->queue_frame_dump_entry)
692 || !intf->queue_frame_dump_entry)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100693 goto exit;
694
695 skb_queue_head_init(&intf->frame_dump_skbqueue);
696 init_waitqueue_head(&intf->frame_dump_waitqueue);
697
Ivo van Doorn68598d292008-02-10 22:50:28 +0100698 intf->queue_stats_entry =
Ivo van Doornd2b69072008-07-27 15:06:05 +0200699 debugfs_create_file("queue", S_IRUSR, intf->queue_folder,
Ivo van Doorn68598d292008-02-10 22:50:28 +0100700 intf, &rt2x00debug_fop_queue_stats);
701
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200702#ifdef CONFIG_RT2X00_LIB_CRYPTO
703 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
704 intf->crypto_stats_entry =
705 debugfs_create_file("crypto", S_IRUGO, intf->queue_folder,
706 intf, &rt2x00debug_fop_crypto_stats);
707#endif
708
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700709 return;
710
711exit:
712 rt2x00debug_deregister(rt2x00dev);
713 ERROR(rt2x00dev, "Failed to register debug handler.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700714}
715
716void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
717{
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100718 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700719
720 if (unlikely(!intf))
721 return;
722
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100723 skb_queue_purge(&intf->frame_dump_skbqueue);
724
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200725#ifdef CONFIG_RT2X00_LIB_CRYPTO
726 debugfs_remove(intf->crypto_stats_entry);
727#endif
Ivo van Doorn68598d292008-02-10 22:50:28 +0100728 debugfs_remove(intf->queue_stats_entry);
729 debugfs_remove(intf->queue_frame_dump_entry);
730 debugfs_remove(intf->queue_folder);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700731 debugfs_remove(intf->rf_val_entry);
732 debugfs_remove(intf->rf_off_entry);
733 debugfs_remove(intf->bbp_val_entry);
734 debugfs_remove(intf->bbp_off_entry);
735 debugfs_remove(intf->eeprom_val_entry);
736 debugfs_remove(intf->eeprom_off_entry);
737 debugfs_remove(intf->csr_val_entry);
738 debugfs_remove(intf->csr_off_entry);
Ivo van Doorn91921a42007-11-27 21:48:16 +0100739 debugfs_remove(intf->register_folder);
Ivo van Doorn643b2522007-09-25 20:13:51 +0200740 debugfs_remove(intf->dev_flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700741 debugfs_remove(intf->chipset_entry);
742 debugfs_remove(intf->driver_entry);
743 debugfs_remove(intf->driver_folder);
744 kfree(intf->chipset_blob.data);
745 kfree(intf->driver_blob.data);
746 kfree(intf);
747
748 rt2x00dev->debugfs_intf = NULL;
749}