2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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.
26 #include <linux/debugfs.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/poll.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/uaccess.h>
35 #include "rt2x00lib.h"
36 #include "rt2x00dump.h"
38 #define MAX_LINE_LENGTH 64
40 struct rt2x00debug_crypto
{
41 unsigned long success
;
42 unsigned long icv_error
;
43 unsigned long mic_error
;
44 unsigned long key_error
;
47 struct rt2x00debug_intf
{
49 * Pointer to driver structure where
50 * this debugfs entry belongs to.
52 struct rt2x00_dev
*rt2x00dev
;
55 * Reference to the rt2x00debug structure
56 * which can be used to communicate with
59 const struct rt2x00debug
*debug
;
62 * Debugfs entries for:
68 * - csr offset/value files
69 * - eeprom offset/value files
70 * - bbp offset/value files
71 * - rf offset/value files
77 struct dentry
*driver_folder
;
78 struct dentry
*driver_entry
;
79 struct dentry
*chipset_entry
;
80 struct dentry
*dev_flags
;
81 struct dentry
*register_folder
;
82 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
;
90 struct dentry
*queue_folder
;
91 struct dentry
*queue_frame_dump_entry
;
92 struct dentry
*queue_stats_entry
;
93 struct dentry
*crypto_stats_entry
;
96 * The frame dump file only allows a single reader,
97 * so we need to store the current state here.
99 unsigned long frame_dump_flags
;
100 #define FRAME_DUMP_FILE_OPEN 1
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.
108 struct sk_buff_head frame_dump_skbqueue
;
109 wait_queue_head_t frame_dump_waitqueue
;
112 * HW crypto statistics.
113 * All statistics are stored separately per cipher type.
115 struct rt2x00debug_crypto crypto_stats
[CIPHER_MAX
];
118 * 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.
122 struct debugfs_blob_wrapper driver_blob
;
123 struct debugfs_blob_wrapper chipset_blob
;
126 * Requested offset for each register type.
128 unsigned int offset_csr
;
129 unsigned int offset_eeprom
;
130 unsigned int offset_bbp
;
131 unsigned int offset_rf
;
134 void rt2x00debug_update_crypto(struct rt2x00_dev
*rt2x00dev
,
135 struct rxdone_entry_desc
*rxdesc
)
137 struct rt2x00debug_intf
*intf
= rt2x00dev
->debugfs_intf
;
138 enum cipher cipher
= rxdesc
->cipher
;
139 enum rx_crypto status
= rxdesc
->cipher_status
;
141 if (cipher
== CIPHER_TKIP_NO_MIC
)
142 cipher
= CIPHER_TKIP
;
143 if (cipher
== CIPHER_NONE
|| cipher
>= CIPHER_MAX
)
146 /* Remove CIPHER_NONE index */
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
);
155 void rt2x00debug_dump_frame(struct rt2x00_dev
*rt2x00dev
,
156 enum rt2x00_dump_type type
, struct sk_buff
*skb
)
158 struct rt2x00debug_intf
*intf
= rt2x00dev
->debugfs_intf
;
159 struct skb_frame_desc
*skbdesc
= get_skb_frame_desc(skb
);
160 struct sk_buff
*skbcopy
;
161 struct rt2x00dump_hdr
*dump_hdr
;
162 struct timeval timestamp
;
165 do_gettimeofday(×tamp
);
167 if (!test_bit(FRAME_DUMP_FILE_OPEN
, &intf
->frame_dump_flags
))
170 if (skb_queue_len(&intf
->frame_dump_skbqueue
) > 20) {
171 DEBUG(rt2x00dev
, "txrx dump queue length exceeded.\n");
176 if (skbdesc
->flags
& SKBDESC_DESC_IN_SKB
)
177 data_len
-= skbdesc
->desc_len
;
179 skbcopy
= alloc_skb(sizeof(*dump_hdr
) + skbdesc
->desc_len
+ data_len
,
182 DEBUG(rt2x00dev
, "Failed to copy skb for dump.\n");
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
));
189 dump_hdr
->desc_length
= cpu_to_le32(skbdesc
->desc_len
);
190 dump_hdr
->data_length
= cpu_to_le32(data_len
);
191 dump_hdr
->chip_rt
= cpu_to_le16(rt2x00dev
->chip
.rt
);
192 dump_hdr
->chip_rf
= cpu_to_le16(rt2x00dev
->chip
.rf
);
193 dump_hdr
->chip_rev
= cpu_to_le16(rt2x00dev
->chip
.rev
);
194 dump_hdr
->type
= cpu_to_le16(type
);
195 dump_hdr
->queue_index
= skbdesc
->entry
->queue
->qid
;
196 dump_hdr
->entry_index
= skbdesc
->entry
->entry_idx
;
197 dump_hdr
->timestamp_sec
= cpu_to_le32(timestamp
.tv_sec
);
198 dump_hdr
->timestamp_usec
= cpu_to_le32(timestamp
.tv_usec
);
200 if (!(skbdesc
->flags
& SKBDESC_DESC_IN_SKB
))
201 memcpy(skb_put(skbcopy
, skbdesc
->desc_len
), skbdesc
->desc
,
203 memcpy(skb_put(skbcopy
, skb
->len
), skb
->data
, skb
->len
);
205 skb_queue_tail(&intf
->frame_dump_skbqueue
, skbcopy
);
206 wake_up_interruptible(&intf
->frame_dump_waitqueue
);
209 * Verify that the file has not been closed while we were working.
211 if (!test_bit(FRAME_DUMP_FILE_OPEN
, &intf
->frame_dump_flags
))
212 skb_queue_purge(&intf
->frame_dump_skbqueue
);
214 EXPORT_SYMBOL_GPL(rt2x00debug_dump_frame
);
216 static int rt2x00debug_file_open(struct inode
*inode
, struct file
*file
)
218 struct rt2x00debug_intf
*intf
= inode
->i_private
;
220 file
->private_data
= inode
->i_private
;
222 if (!try_module_get(intf
->debug
->owner
))
228 static int rt2x00debug_file_release(struct inode
*inode
, struct file
*file
)
230 struct rt2x00debug_intf
*intf
= file
->private_data
;
232 module_put(intf
->debug
->owner
);
237 static int rt2x00debug_open_queue_dump(struct inode
*inode
, struct file
*file
)
239 struct rt2x00debug_intf
*intf
= inode
->i_private
;
242 retval
= rt2x00debug_file_open(inode
, file
);
246 if (test_and_set_bit(FRAME_DUMP_FILE_OPEN
, &intf
->frame_dump_flags
)) {
247 rt2x00debug_file_release(inode
, file
);
254 static int rt2x00debug_release_queue_dump(struct inode
*inode
, struct file
*file
)
256 struct rt2x00debug_intf
*intf
= inode
->i_private
;
258 skb_queue_purge(&intf
->frame_dump_skbqueue
);
260 clear_bit(FRAME_DUMP_FILE_OPEN
, &intf
->frame_dump_flags
);
262 return rt2x00debug_file_release(inode
, file
);
265 static ssize_t
rt2x00debug_read_queue_dump(struct file
*file
,
270 struct rt2x00debug_intf
*intf
= file
->private_data
;
275 if (file
->f_flags
& O_NONBLOCK
)
279 wait_event_interruptible(intf
->frame_dump_waitqueue
,
281 skb_dequeue(&intf
->frame_dump_skbqueue
)));
285 status
= min((size_t)skb
->len
, length
);
286 if (copy_to_user(buf
, skb
->data
, status
)) {
299 static unsigned int rt2x00debug_poll_queue_dump(struct file
*file
,
302 struct rt2x00debug_intf
*intf
= file
->private_data
;
304 poll_wait(file
, &intf
->frame_dump_waitqueue
, wait
);
306 if (!skb_queue_empty(&intf
->frame_dump_skbqueue
))
307 return POLLOUT
| POLLWRNORM
;
312 static const struct file_operations rt2x00debug_fop_queue_dump
= {
313 .owner
= THIS_MODULE
,
314 .read
= rt2x00debug_read_queue_dump
,
315 .poll
= rt2x00debug_poll_queue_dump
,
316 .open
= rt2x00debug_open_queue_dump
,
317 .release
= rt2x00debug_release_queue_dump
,
320 static ssize_t
rt2x00debug_read_queue_stats(struct file
*file
,
325 struct rt2x00debug_intf
*intf
= file
->private_data
;
326 struct data_queue
*queue
;
327 unsigned long irqflags
;
328 unsigned int lines
= 1 + intf
->rt2x00dev
->data_queues
;
336 data
= kzalloc(lines
* MAX_LINE_LENGTH
, GFP_KERNEL
);
341 sprintf(data
, "qid\tcount\tlimit\tlength\tindex\tdone\tcrypto\n");
343 queue_for_each(intf
->rt2x00dev
, queue
) {
344 spin_lock_irqsave(&queue
->lock
, irqflags
);
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
],
349 queue
->index
[Q_INDEX_DONE
],
350 queue
->index
[Q_INDEX_CRYPTO
]);
352 spin_unlock_irqrestore(&queue
->lock
, irqflags
);
356 size
= min(size
, length
);
358 if (copy_to_user(buf
, data
, size
)) {
369 static 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
,
376 #ifdef CONFIG_RT2X00_LIB_CRYPTO
377 static ssize_t
rt2x00debug_read_crypto_stats(struct file
*file
,
382 struct rt2x00debug_intf
*intf
= file
->private_data
;
383 char *name
[] = { "WEP64", "WEP128", "TKIP", "AES" };
392 data
= kzalloc((1 + CIPHER_MAX
) * MAX_LINE_LENGTH
, GFP_KERNEL
);
397 temp
+= sprintf(data
, "cipher\tsuccess\ticv err\tmic err\tkey err\n");
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
);
408 size
= min(size
, length
);
410 if (copy_to_user(buf
, data
, size
)) {
421 static 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
,
429 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
430 static ssize_t rt2x00debug_read_##__name(struct file *file, \
435 struct rt2x00debug_intf *intf = file->private_data; \
436 const struct rt2x00debug *debug = intf->debug; \
439 unsigned int index = intf->offset_##__name; \
445 if (index >= debug->__name.word_count) \
448 index += (debug->__name.word_base / \
449 debug->__name.word_size); \
451 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
452 index *= debug->__name.word_size; \
454 debug->__name.read(intf->rt2x00dev, index, &value); \
456 size = sprintf(line, __format, value); \
458 if (copy_to_user(buf, line, size)) \
465 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
466 static ssize_t rt2x00debug_write_##__name(struct file *file, \
467 const char __user *buf,\
471 struct rt2x00debug_intf *intf = file->private_data; \
472 const struct rt2x00debug *debug = intf->debug; \
475 unsigned int index = intf->offset_##__name; \
481 if (index >= debug->__name.word_count) \
484 if (copy_from_user(line, buf, length)) \
487 size = strlen(line); \
488 value = simple_strtoul(line, NULL, 0); \
490 index += (debug->__name.word_base / \
491 debug->__name.word_size); \
493 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
494 index *= debug->__name.word_size; \
496 debug->__name.write(intf->rt2x00dev, index, value); \
502 #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
503 RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
504 RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
506 static const struct file_operations rt2x00debug_fop_##__name = {\
507 .owner = THIS_MODULE, \
508 .read = rt2x00debug_read_##__name, \
509 .write = rt2x00debug_write_##__name, \
510 .open = rt2x00debug_file_open, \
511 .release = rt2x00debug_file_release, \
514 RT2X00DEBUGFS_OPS(csr
, "0x%.8x\n", u32
);
515 RT2X00DEBUGFS_OPS(eeprom
, "0x%.4x\n", u16
);
516 RT2X00DEBUGFS_OPS(bbp
, "0x%.2x\n", u8
);
517 RT2X00DEBUGFS_OPS(rf
, "0x%.8x\n", u32
);
519 static ssize_t
rt2x00debug_read_dev_flags(struct file
*file
,
524 struct rt2x00debug_intf
*intf
= file
->private_data
;
531 size
= sprintf(line
, "0x%.8x\n", (unsigned int)intf
->rt2x00dev
->flags
);
533 if (copy_to_user(buf
, line
, size
))
540 static const struct file_operations rt2x00debug_fop_dev_flags
= {
541 .owner
= THIS_MODULE
,
542 .read
= rt2x00debug_read_dev_flags
,
543 .open
= rt2x00debug_file_open
,
544 .release
= rt2x00debug_file_release
,
547 static struct dentry
*rt2x00debug_create_file_driver(const char *name
,
548 struct rt2x00debug_intf
550 struct debugfs_blob_wrapper
555 data
= kzalloc(3 * MAX_LINE_LENGTH
, GFP_KERNEL
);
560 data
+= sprintf(data
, "driver:\t%s\n", intf
->rt2x00dev
->ops
->name
);
561 data
+= sprintf(data
, "version:\t%s\n", DRV_VERSION
);
562 data
+= sprintf(data
, "compiled:\t%s %s\n", __DATE__
, __TIME__
);
563 blob
->size
= strlen(blob
->data
);
565 return debugfs_create_blob(name
, S_IRUSR
, intf
->driver_folder
, blob
);
568 static struct dentry
*rt2x00debug_create_file_chipset(const char *name
,
569 struct rt2x00debug_intf
575 const struct rt2x00debug
*debug
= intf
->debug
;
578 data
= kzalloc(8 * MAX_LINE_LENGTH
, GFP_KERNEL
);
583 data
+= sprintf(data
, "rt chip:\t%04x\n", intf
->rt2x00dev
->chip
.rt
);
584 data
+= sprintf(data
, "rf chip:\t%04x\n", intf
->rt2x00dev
->chip
.rf
);
585 data
+= sprintf(data
, "revision:\t%04x\n", intf
->rt2x00dev
->chip
.rev
);
586 data
+= sprintf(data
, "\n");
587 data
+= sprintf(data
, "register\tbase\twords\twordsize\n");
588 data
+= sprintf(data
, "csr\t%d\t%d\t%d\n",
589 debug
->csr
.word_base
,
590 debug
->csr
.word_count
,
591 debug
->csr
.word_size
);
592 data
+= sprintf(data
, "eeprom\t%d\t%d\t%d\n",
593 debug
->eeprom
.word_base
,
594 debug
->eeprom
.word_count
,
595 debug
->eeprom
.word_size
);
596 data
+= sprintf(data
, "bbp\t%d\t%d\t%d\n",
597 debug
->bbp
.word_base
,
598 debug
->bbp
.word_count
,
599 debug
->bbp
.word_size
);
600 data
+= sprintf(data
, "rf\t%d\t%d\t%d\n",
602 debug
->rf
.word_count
,
603 debug
->rf
.word_size
);
604 blob
->size
= strlen(blob
->data
);
606 return debugfs_create_blob(name
, S_IRUSR
, intf
->driver_folder
, blob
);
609 void rt2x00debug_register(struct rt2x00_dev
*rt2x00dev
)
611 const struct rt2x00debug
*debug
= rt2x00dev
->ops
->debugfs
;
612 struct rt2x00debug_intf
*intf
;
614 intf
= kzalloc(sizeof(struct rt2x00debug_intf
), GFP_KERNEL
);
616 ERROR(rt2x00dev
, "Failed to allocate debug handler.\n");
621 intf
->rt2x00dev
= rt2x00dev
;
622 rt2x00dev
->debugfs_intf
= intf
;
624 intf
->driver_folder
=
625 debugfs_create_dir(intf
->rt2x00dev
->ops
->name
,
626 rt2x00dev
->hw
->wiphy
->debugfsdir
);
627 if (IS_ERR(intf
->driver_folder
) || !intf
->driver_folder
)
631 rt2x00debug_create_file_driver("driver", intf
, &intf
->driver_blob
);
632 if (IS_ERR(intf
->driver_entry
) || !intf
->driver_entry
)
635 intf
->chipset_entry
=
636 rt2x00debug_create_file_chipset("chipset",
637 intf
, &intf
->chipset_blob
);
638 if (IS_ERR(intf
->chipset_entry
) || !intf
->chipset_entry
)
641 intf
->dev_flags
= debugfs_create_file("dev_flags", S_IRUSR
,
642 intf
->driver_folder
, intf
,
643 &rt2x00debug_fop_dev_flags
);
644 if (IS_ERR(intf
->dev_flags
) || !intf
->dev_flags
)
647 intf
->register_folder
=
648 debugfs_create_dir("register", intf
->driver_folder
);
649 if (IS_ERR(intf
->register_folder
) || !intf
->register_folder
)
652 #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
654 (__intf)->__name##_off_entry = \
655 debugfs_create_u32(__stringify(__name) "_offset", \
657 (__intf)->register_folder, \
658 &(__intf)->offset_##__name); \
659 if (IS_ERR((__intf)->__name##_off_entry) \
660 || !(__intf)->__name##_off_entry) \
663 (__intf)->__name##_val_entry = \
664 debugfs_create_file(__stringify(__name) "_value", \
666 (__intf)->register_folder, \
667 (__intf), &rt2x00debug_fop_##__name);\
668 if (IS_ERR((__intf)->__name##_val_entry) \
669 || !(__intf)->__name##_val_entry) \
673 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf
, csr
);
674 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf
, eeprom
);
675 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf
, bbp
);
676 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf
, rf
);
678 #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
681 debugfs_create_dir("queue", intf
->driver_folder
);
682 if (IS_ERR(intf
->queue_folder
) || !intf
->queue_folder
)
685 intf
->queue_frame_dump_entry
=
686 debugfs_create_file("dump", S_IRUSR
, intf
->queue_folder
,
687 intf
, &rt2x00debug_fop_queue_dump
);
688 if (IS_ERR(intf
->queue_frame_dump_entry
)
689 || !intf
->queue_frame_dump_entry
)
692 skb_queue_head_init(&intf
->frame_dump_skbqueue
);
693 init_waitqueue_head(&intf
->frame_dump_waitqueue
);
695 intf
->queue_stats_entry
=
696 debugfs_create_file("queue", S_IRUSR
, intf
->queue_folder
,
697 intf
, &rt2x00debug_fop_queue_stats
);
699 #ifdef CONFIG_RT2X00_LIB_CRYPTO
700 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO
, &rt2x00dev
->flags
))
701 intf
->crypto_stats_entry
=
702 debugfs_create_file("crypto", S_IRUGO
, intf
->queue_folder
,
703 intf
, &rt2x00debug_fop_crypto_stats
);
709 rt2x00debug_deregister(rt2x00dev
);
710 ERROR(rt2x00dev
, "Failed to register debug handler.\n");
713 void rt2x00debug_deregister(struct rt2x00_dev
*rt2x00dev
)
715 struct rt2x00debug_intf
*intf
= rt2x00dev
->debugfs_intf
;
720 skb_queue_purge(&intf
->frame_dump_skbqueue
);
722 #ifdef CONFIG_RT2X00_LIB_CRYPTO
723 debugfs_remove(intf
->crypto_stats_entry
);
725 debugfs_remove(intf
->queue_stats_entry
);
726 debugfs_remove(intf
->queue_frame_dump_entry
);
727 debugfs_remove(intf
->queue_folder
);
728 debugfs_remove(intf
->rf_val_entry
);
729 debugfs_remove(intf
->rf_off_entry
);
730 debugfs_remove(intf
->bbp_val_entry
);
731 debugfs_remove(intf
->bbp_off_entry
);
732 debugfs_remove(intf
->eeprom_val_entry
);
733 debugfs_remove(intf
->eeprom_off_entry
);
734 debugfs_remove(intf
->csr_val_entry
);
735 debugfs_remove(intf
->csr_off_entry
);
736 debugfs_remove(intf
->register_folder
);
737 debugfs_remove(intf
->dev_flags
);
738 debugfs_remove(intf
->chipset_entry
);
739 debugfs_remove(intf
->driver_entry
);
740 debugfs_remove(intf
->driver_folder
);
741 kfree(intf
->chipset_blob
.data
);
742 kfree(intf
->driver_blob
.data
);
745 rt2x00dev
->debugfs_intf
= NULL
;