2 Copyright (C) 2004 - 2009 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.
26 #include <linux/debugfs.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/poll.h>
30 #include <linux/uaccess.h>
33 #include "rt2x00lib.h"
34 #include "rt2x00dump.h"
36 #define MAX_LINE_LENGTH 64
38 struct rt2x00debug_crypto {
39 unsigned long success;
40 unsigned long icv_error;
41 unsigned long mic_error;
42 unsigned long key_error;
45 struct rt2x00debug_intf {
47 * Pointer to driver structure where
48 * this debugfs entry belongs to.
50 struct rt2x00_dev *rt2x00dev;
53 * Reference to the rt2x00debug structure
54 * which can be used to communicate with
57 const struct rt2x00debug *debug;
60 * Debugfs entries for:
66 * - csr offset/value files
67 * - eeprom offset/value files
68 * - bbp offset/value files
69 * - rf offset/value files
75 struct dentry *driver_folder;
76 struct dentry *driver_entry;
77 struct dentry *chipset_entry;
78 struct dentry *dev_flags;
79 struct dentry *register_folder;
80 struct dentry *csr_off_entry;
81 struct dentry *csr_val_entry;
82 struct dentry *eeprom_off_entry;
83 struct dentry *eeprom_val_entry;
84 struct dentry *bbp_off_entry;
85 struct dentry *bbp_val_entry;
86 struct dentry *rf_off_entry;
87 struct dentry *rf_val_entry;
88 struct dentry *queue_folder;
89 struct dentry *queue_frame_dump_entry;
90 struct dentry *queue_stats_entry;
91 struct dentry *crypto_stats_entry;
94 * The frame dump file only allows a single reader,
95 * so we need to store the current state here.
97 unsigned long frame_dump_flags;
98 #define FRAME_DUMP_FILE_OPEN 1
101 * We queue each frame before dumping it to the user,
102 * per read command we will pass a single skb structure
103 * so we should be prepared to queue multiple sk buffers
104 * before sending it to userspace.
106 struct sk_buff_head frame_dump_skbqueue;
107 wait_queue_head_t frame_dump_waitqueue;
110 * HW crypto statistics.
111 * All statistics are stored seperately per cipher type.
113 struct rt2x00debug_crypto crypto_stats[CIPHER_MAX];
116 * Driver and chipset files will use a data buffer
117 * that has been created in advance. This will simplify
118 * the code since we can use the debugfs functions.
120 struct debugfs_blob_wrapper driver_blob;
121 struct debugfs_blob_wrapper chipset_blob;
124 * Requested offset for each register type.
126 unsigned int offset_csr;
127 unsigned int offset_eeprom;
128 unsigned int offset_bbp;
129 unsigned int offset_rf;
132 void rt2x00debug_update_crypto(struct rt2x00_dev *rt2x00dev,
133 struct rxdone_entry_desc *rxdesc)
135 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
136 enum cipher cipher = rxdesc->cipher;
137 enum rx_crypto status = rxdesc->cipher_status;
139 if (cipher == CIPHER_TKIP_NO_MIC)
140 cipher = CIPHER_TKIP;
141 if (cipher == CIPHER_NONE || cipher > CIPHER_MAX)
144 /* Remove CIPHER_NONE index */
147 intf->crypto_stats[cipher].success += (status == RX_CRYPTO_SUCCESS);
148 intf->crypto_stats[cipher].icv_error += (status == RX_CRYPTO_FAIL_ICV);
149 intf->crypto_stats[cipher].mic_error += (status == RX_CRYPTO_FAIL_MIC);
150 intf->crypto_stats[cipher].key_error += (status == RX_CRYPTO_FAIL_KEY);
153 void rt2x00debug_dump_frame(struct rt2x00_dev *rt2x00dev,
154 enum rt2x00_dump_type type, struct sk_buff *skb)
156 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
157 struct skb_frame_desc *desc = get_skb_frame_desc(skb);
158 struct sk_buff *skbcopy;
159 struct rt2x00dump_hdr *dump_hdr;
160 struct timeval timestamp;
162 do_gettimeofday(×tamp);
164 if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
167 if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
168 DEBUG(rt2x00dev, "txrx dump queue length exceeded.\n");
172 skbcopy = alloc_skb(sizeof(*dump_hdr) + desc->desc_len + skb->len,
175 DEBUG(rt2x00dev, "Failed to copy skb for dump.\n");
179 dump_hdr = (struct rt2x00dump_hdr *)skb_put(skbcopy, sizeof(*dump_hdr));
180 dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
181 dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
182 dump_hdr->desc_length = cpu_to_le32(desc->desc_len);
183 dump_hdr->data_length = cpu_to_le32(skb->len);
184 dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
185 dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
186 dump_hdr->chip_rev = cpu_to_le32(rt2x00dev->chip.rev);
187 dump_hdr->type = cpu_to_le16(type);
188 dump_hdr->queue_index = desc->entry->queue->qid;
189 dump_hdr->entry_index = desc->entry->entry_idx;
190 dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
191 dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_usec);
193 memcpy(skb_put(skbcopy, desc->desc_len), desc->desc, desc->desc_len);
194 memcpy(skb_put(skbcopy, skb->len), skb->data, skb->len);
196 skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
197 wake_up_interruptible(&intf->frame_dump_waitqueue);
200 * Verify that the file has not been closed while we were working.
202 if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
203 skb_queue_purge(&intf->frame_dump_skbqueue);
206 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
208 struct rt2x00debug_intf *intf = inode->i_private;
210 file->private_data = inode->i_private;
212 if (!try_module_get(intf->debug->owner))
218 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
220 struct rt2x00debug_intf *intf = file->private_data;
222 module_put(intf->debug->owner);
227 static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
229 struct rt2x00debug_intf *intf = inode->i_private;
232 retval = rt2x00debug_file_open(inode, file);
236 if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
237 rt2x00debug_file_release(inode, file);
244 static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
246 struct rt2x00debug_intf *intf = inode->i_private;
248 skb_queue_purge(&intf->frame_dump_skbqueue);
250 clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
252 return rt2x00debug_file_release(inode, file);
255 static ssize_t rt2x00debug_read_queue_dump(struct file *file,
260 struct rt2x00debug_intf *intf = file->private_data;
265 if (file->f_flags & O_NONBLOCK)
269 wait_event_interruptible(intf->frame_dump_waitqueue,
271 skb_dequeue(&intf->frame_dump_skbqueue)));
275 status = min((size_t)skb->len, length);
276 if (copy_to_user(buf, skb->data, status)) {
289 static unsigned int rt2x00debug_poll_queue_dump(struct file *file,
292 struct rt2x00debug_intf *intf = file->private_data;
294 poll_wait(file, &intf->frame_dump_waitqueue, wait);
296 if (!skb_queue_empty(&intf->frame_dump_skbqueue))
297 return POLLOUT | POLLWRNORM;
302 static const struct file_operations rt2x00debug_fop_queue_dump = {
303 .owner = THIS_MODULE,
304 .read = rt2x00debug_read_queue_dump,
305 .poll = rt2x00debug_poll_queue_dump,
306 .open = rt2x00debug_open_queue_dump,
307 .release = rt2x00debug_release_queue_dump,
310 static ssize_t rt2x00debug_read_queue_stats(struct file *file,
315 struct rt2x00debug_intf *intf = file->private_data;
316 struct data_queue *queue;
317 unsigned long irqflags;
318 unsigned int lines = 1 + intf->rt2x00dev->data_queues;
326 data = kzalloc(lines * MAX_LINE_LENGTH, GFP_KERNEL);
331 sprintf(data, "qid\tcount\tlimit\tlength\tindex\tdone\tcrypto\n");
333 queue_for_each(intf->rt2x00dev, queue) {
334 spin_lock_irqsave(&queue->lock, irqflags);
336 temp += sprintf(temp, "%d\t%d\t%d\t%d\t%d\t%d\t%d\n", queue->qid,
337 queue->count, queue->limit, queue->length,
338 queue->index[Q_INDEX],
339 queue->index[Q_INDEX_DONE],
340 queue->index[Q_INDEX_CRYPTO]);
342 spin_unlock_irqrestore(&queue->lock, irqflags);
346 size = min(size, length);
348 if (copy_to_user(buf, data, size)) {
359 static const struct file_operations rt2x00debug_fop_queue_stats = {
360 .owner = THIS_MODULE,
361 .read = rt2x00debug_read_queue_stats,
362 .open = rt2x00debug_file_open,
363 .release = rt2x00debug_file_release,
366 #ifdef CONFIG_RT2X00_LIB_CRYPTO
367 static ssize_t rt2x00debug_read_crypto_stats(struct file *file,
372 struct rt2x00debug_intf *intf = file->private_data;
373 char *name[] = { "WEP64", "WEP128", "TKIP", "AES" };
382 data = kzalloc((1 + CIPHER_MAX) * MAX_LINE_LENGTH, GFP_KERNEL);
387 temp += sprintf(data, "cipher\tsuccess\ticv err\tmic err\tkey err\n");
389 for (i = 0; i < CIPHER_MAX; i++) {
390 temp += sprintf(temp, "%s\t%lu\t%lu\t%lu\t%lu\n", name[i],
391 intf->crypto_stats[i].success,
392 intf->crypto_stats[i].icv_error,
393 intf->crypto_stats[i].mic_error,
394 intf->crypto_stats[i].key_error);
398 size = min(size, length);
400 if (copy_to_user(buf, data, size)) {
411 static const struct file_operations rt2x00debug_fop_crypto_stats = {
412 .owner = THIS_MODULE,
413 .read = rt2x00debug_read_crypto_stats,
414 .open = rt2x00debug_file_open,
415 .release = rt2x00debug_file_release,
419 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
420 static ssize_t rt2x00debug_read_##__name(struct file *file, \
425 struct rt2x00debug_intf *intf = file->private_data; \
426 const struct rt2x00debug *debug = intf->debug; \
429 unsigned int index = intf->offset_##__name; \
435 if (index >= debug->__name.word_count) \
438 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
439 index *= debug->__name.word_size; \
441 index += debug->__name.word_base; \
443 debug->__name.read(intf->rt2x00dev, index, &value); \
445 size = sprintf(line, __format, value); \
447 if (copy_to_user(buf, line, size)) \
454 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
455 static ssize_t rt2x00debug_write_##__name(struct file *file, \
456 const char __user *buf,\
460 struct rt2x00debug_intf *intf = file->private_data; \
461 const struct rt2x00debug *debug = intf->debug; \
464 unsigned int index = intf->offset_##__name; \
470 if (index >= debug->__name.word_count) \
473 if (copy_from_user(line, buf, length)) \
476 size = strlen(line); \
477 value = simple_strtoul(line, NULL, 0); \
479 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
480 index *= debug->__name.word_size; \
482 index += debug->__name.word_base; \
484 debug->__name.write(intf->rt2x00dev, index, value); \
490 #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
491 RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
492 RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
494 static const struct file_operations rt2x00debug_fop_##__name = {\
495 .owner = THIS_MODULE, \
496 .read = rt2x00debug_read_##__name, \
497 .write = rt2x00debug_write_##__name, \
498 .open = rt2x00debug_file_open, \
499 .release = rt2x00debug_file_release, \
502 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
503 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
504 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
505 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
507 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
512 struct rt2x00debug_intf *intf = file->private_data;
519 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
521 if (copy_to_user(buf, line, size))
528 static const struct file_operations rt2x00debug_fop_dev_flags = {
529 .owner = THIS_MODULE,
530 .read = rt2x00debug_read_dev_flags,
531 .open = rt2x00debug_file_open,
532 .release = rt2x00debug_file_release,
535 static struct dentry *rt2x00debug_create_file_driver(const char *name,
536 struct rt2x00debug_intf
538 struct debugfs_blob_wrapper
543 data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
548 data += sprintf(data, "driver: %s\n", intf->rt2x00dev->ops->name);
549 data += sprintf(data, "version: %s\n", DRV_VERSION);
550 data += sprintf(data, "compiled: %s %s\n", __DATE__, __TIME__);
551 blob->size = strlen(blob->data);
553 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
556 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
557 struct rt2x00debug_intf
563 const struct rt2x00debug *debug = intf->debug;
566 data = kzalloc(8 * MAX_LINE_LENGTH, GFP_KERNEL);
571 data += sprintf(data, "rt chip: %04x\n", intf->rt2x00dev->chip.rt);
572 data += sprintf(data, "rf chip: %04x\n", intf->rt2x00dev->chip.rf);
573 data += sprintf(data, "revision:%08x\n", intf->rt2x00dev->chip.rev);
574 data += sprintf(data, "\n");
575 data += sprintf(data, "csr length: %d\n", debug->csr.word_count);
576 data += sprintf(data, "eeprom length: %d\n", debug->eeprom.word_count);
577 data += sprintf(data, "bbp length: %d\n", debug->bbp.word_count);
578 data += sprintf(data, "rf length: %d\n", debug->rf.word_count);
579 blob->size = strlen(blob->data);
581 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
584 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
586 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
587 struct rt2x00debug_intf *intf;
589 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
591 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
596 intf->rt2x00dev = rt2x00dev;
597 rt2x00dev->debugfs_intf = intf;
599 intf->driver_folder =
600 debugfs_create_dir(intf->rt2x00dev->ops->name,
601 rt2x00dev->hw->wiphy->debugfsdir);
602 if (IS_ERR(intf->driver_folder) || !intf->driver_folder)
606 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
607 if (IS_ERR(intf->driver_entry) || !intf->driver_entry)
610 intf->chipset_entry =
611 rt2x00debug_create_file_chipset("chipset",
612 intf, &intf->chipset_blob);
613 if (IS_ERR(intf->chipset_entry) || !intf->chipset_entry)
616 intf->dev_flags = debugfs_create_file("dev_flags", S_IRUSR,
617 intf->driver_folder, intf,
618 &rt2x00debug_fop_dev_flags);
619 if (IS_ERR(intf->dev_flags) || !intf->dev_flags)
622 intf->register_folder =
623 debugfs_create_dir("register", intf->driver_folder);
624 if (IS_ERR(intf->register_folder) || !intf->register_folder)
627 #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
629 (__intf)->__name##_off_entry = \
630 debugfs_create_u32(__stringify(__name) "_offset", \
632 (__intf)->register_folder, \
633 &(__intf)->offset_##__name); \
634 if (IS_ERR((__intf)->__name##_off_entry) \
635 || !(__intf)->__name##_off_entry) \
638 (__intf)->__name##_val_entry = \
639 debugfs_create_file(__stringify(__name) "_value", \
641 (__intf)->register_folder, \
642 (__intf), &rt2x00debug_fop_##__name);\
643 if (IS_ERR((__intf)->__name##_val_entry) \
644 || !(__intf)->__name##_val_entry) \
648 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
649 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
650 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
651 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
653 #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
656 debugfs_create_dir("queue", intf->driver_folder);
657 if (IS_ERR(intf->queue_folder) || !intf->queue_folder)
660 intf->queue_frame_dump_entry =
661 debugfs_create_file("dump", S_IRUSR, intf->queue_folder,
662 intf, &rt2x00debug_fop_queue_dump);
663 if (IS_ERR(intf->queue_frame_dump_entry)
664 || !intf->queue_frame_dump_entry)
667 skb_queue_head_init(&intf->frame_dump_skbqueue);
668 init_waitqueue_head(&intf->frame_dump_waitqueue);
670 intf->queue_stats_entry =
671 debugfs_create_file("queue", S_IRUSR, intf->queue_folder,
672 intf, &rt2x00debug_fop_queue_stats);
674 #ifdef CONFIG_RT2X00_LIB_CRYPTO
675 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
676 intf->crypto_stats_entry =
677 debugfs_create_file("crypto", S_IRUGO, intf->queue_folder,
678 intf, &rt2x00debug_fop_crypto_stats);
684 rt2x00debug_deregister(rt2x00dev);
685 ERROR(rt2x00dev, "Failed to register debug handler.\n");
690 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
692 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
697 skb_queue_purge(&intf->frame_dump_skbqueue);
699 #ifdef CONFIG_RT2X00_LIB_CRYPTO
700 debugfs_remove(intf->crypto_stats_entry);
702 debugfs_remove(intf->queue_stats_entry);
703 debugfs_remove(intf->queue_frame_dump_entry);
704 debugfs_remove(intf->queue_folder);
705 debugfs_remove(intf->rf_val_entry);
706 debugfs_remove(intf->rf_off_entry);
707 debugfs_remove(intf->bbp_val_entry);
708 debugfs_remove(intf->bbp_off_entry);
709 debugfs_remove(intf->eeprom_val_entry);
710 debugfs_remove(intf->eeprom_off_entry);
711 debugfs_remove(intf->csr_val_entry);
712 debugfs_remove(intf->csr_off_entry);
713 debugfs_remove(intf->register_folder);
714 debugfs_remove(intf->dev_flags);
715 debugfs_remove(intf->chipset_entry);
716 debugfs_remove(intf->driver_entry);
717 debugfs_remove(intf->driver_folder);
718 kfree(intf->chipset_blob.data);
719 kfree(intf->driver_blob.data);
722 rt2x00dev->debugfs_intf = NULL;