2 Copyright (C) 2004 - 2008 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_intf {
40 * Pointer to driver structure where
41 * this debugfs entry belongs to.
43 struct rt2x00_dev *rt2x00dev;
46 * Reference to the rt2x00debug structure
47 * which can be used to communicate with
50 const struct rt2x00debug *debug;
53 * Debugfs entries for:
59 * - csr offset/value files
60 * - eeprom offset/value files
61 * - bbp offset/value files
62 * - rf offset/value files
67 struct dentry *driver_folder;
68 struct dentry *driver_entry;
69 struct dentry *chipset_entry;
70 struct dentry *dev_flags;
71 struct dentry *register_folder;
72 struct dentry *csr_off_entry;
73 struct dentry *csr_val_entry;
74 struct dentry *eeprom_off_entry;
75 struct dentry *eeprom_val_entry;
76 struct dentry *bbp_off_entry;
77 struct dentry *bbp_val_entry;
78 struct dentry *rf_off_entry;
79 struct dentry *rf_val_entry;
80 struct dentry *queue_folder;
81 struct dentry *queue_frame_dump_entry;
82 struct dentry *queue_stats_entry;
85 * The frame dump file only allows a single reader,
86 * so we need to store the current state here.
88 unsigned long frame_dump_flags;
89 #define FRAME_DUMP_FILE_OPEN 1
92 * We queue each frame before dumping it to the user,
93 * per read command we will pass a single skb structure
94 * so we should be prepared to queue multiple sk buffers
95 * before sending it to userspace.
97 struct sk_buff_head frame_dump_skbqueue;
98 wait_queue_head_t frame_dump_waitqueue;
101 * Driver and chipset files will use a data buffer
102 * that has been created in advance. This will simplify
103 * the code since we can use the debugfs functions.
105 struct debugfs_blob_wrapper driver_blob;
106 struct debugfs_blob_wrapper chipset_blob;
109 * Requested offset for each register type.
111 unsigned int offset_csr;
112 unsigned int offset_eeprom;
113 unsigned int offset_bbp;
114 unsigned int offset_rf;
117 void rt2x00debug_dump_frame(struct rt2x00_dev *rt2x00dev,
118 enum rt2x00_dump_type type, struct sk_buff *skb)
120 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
121 struct skb_frame_desc *desc = get_skb_frame_desc(skb);
122 struct sk_buff *skbcopy;
123 struct rt2x00dump_hdr *dump_hdr;
124 struct timeval timestamp;
126 do_gettimeofday(×tamp);
128 if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
131 if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
132 DEBUG(rt2x00dev, "txrx dump queue length exceeded.\n");
136 skbcopy = alloc_skb(sizeof(*dump_hdr) + desc->desc_len + skb->len,
139 DEBUG(rt2x00dev, "Failed to copy skb for dump.\n");
143 dump_hdr = (struct rt2x00dump_hdr *)skb_put(skbcopy, sizeof(*dump_hdr));
144 dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
145 dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
146 dump_hdr->desc_length = cpu_to_le32(desc->desc_len);
147 dump_hdr->data_length = cpu_to_le32(skb->len);
148 dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
149 dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
150 dump_hdr->chip_rev = cpu_to_le32(rt2x00dev->chip.rev);
151 dump_hdr->type = cpu_to_le16(type);
152 dump_hdr->queue_index = desc->entry->queue->qid;
153 dump_hdr->entry_index = desc->entry->entry_idx;
154 dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
155 dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_usec);
157 memcpy(skb_put(skbcopy, desc->desc_len), desc->desc, desc->desc_len);
158 memcpy(skb_put(skbcopy, skb->len), skb->data, skb->len);
160 skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
161 wake_up_interruptible(&intf->frame_dump_waitqueue);
164 * Verify that the file has not been closed while we were working.
166 if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
167 skb_queue_purge(&intf->frame_dump_skbqueue);
170 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
172 struct rt2x00debug_intf *intf = inode->i_private;
174 file->private_data = inode->i_private;
176 if (!try_module_get(intf->debug->owner))
182 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
184 struct rt2x00debug_intf *intf = file->private_data;
186 module_put(intf->debug->owner);
191 static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
193 struct rt2x00debug_intf *intf = inode->i_private;
196 retval = rt2x00debug_file_open(inode, file);
200 if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
201 rt2x00debug_file_release(inode, file);
208 static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
210 struct rt2x00debug_intf *intf = inode->i_private;
212 skb_queue_purge(&intf->frame_dump_skbqueue);
214 clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
216 return rt2x00debug_file_release(inode, file);
219 static ssize_t rt2x00debug_read_queue_dump(struct file *file,
224 struct rt2x00debug_intf *intf = file->private_data;
229 if (file->f_flags & O_NONBLOCK)
233 wait_event_interruptible(intf->frame_dump_waitqueue,
235 skb_dequeue(&intf->frame_dump_skbqueue)));
239 status = min((size_t)skb->len, length);
240 if (copy_to_user(buf, skb->data, status)) {
253 static unsigned int rt2x00debug_poll_queue_dump(struct file *file,
256 struct rt2x00debug_intf *intf = file->private_data;
258 poll_wait(file, &intf->frame_dump_waitqueue, wait);
260 if (!skb_queue_empty(&intf->frame_dump_skbqueue))
261 return POLLOUT | POLLWRNORM;
266 static const struct file_operations rt2x00debug_fop_queue_dump = {
267 .owner = THIS_MODULE,
268 .read = rt2x00debug_read_queue_dump,
269 .poll = rt2x00debug_poll_queue_dump,
270 .open = rt2x00debug_open_queue_dump,
271 .release = rt2x00debug_release_queue_dump,
274 static ssize_t rt2x00debug_read_queue_stats(struct file *file,
279 struct rt2x00debug_intf *intf = file->private_data;
280 struct data_queue *queue;
281 unsigned long irqflags;
282 unsigned int lines = 1 + intf->rt2x00dev->data_queues;
290 data = kzalloc(lines * MAX_LINE_LENGTH, GFP_KERNEL);
295 sprintf(data, "qid\tcount\tlimit\tlength\tindex\tdone\tcrypto\n");
297 queue_for_each(intf->rt2x00dev, queue) {
298 spin_lock_irqsave(&queue->lock, irqflags);
300 temp += sprintf(temp, "%d\t%d\t%d\t%d\t%d\t%d\t%d\n", queue->qid,
301 queue->count, queue->limit, queue->length,
302 queue->index[Q_INDEX],
303 queue->index[Q_INDEX_DONE],
304 queue->index[Q_INDEX_CRYPTO]);
306 spin_unlock_irqrestore(&queue->lock, irqflags);
310 size = min(size, length);
312 if (copy_to_user(buf, data, size)) {
323 static const struct file_operations rt2x00debug_fop_queue_stats = {
324 .owner = THIS_MODULE,
325 .read = rt2x00debug_read_queue_stats,
326 .open = rt2x00debug_file_open,
327 .release = rt2x00debug_file_release,
330 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
331 static ssize_t rt2x00debug_read_##__name(struct file *file, \
336 struct rt2x00debug_intf *intf = file->private_data; \
337 const struct rt2x00debug *debug = intf->debug; \
345 if (intf->offset_##__name >= debug->__name.word_count) \
348 debug->__name.read(intf->rt2x00dev, \
349 intf->offset_##__name, &value); \
351 size = sprintf(line, __format, value); \
353 if (copy_to_user(buf, line, size)) \
360 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
361 static ssize_t rt2x00debug_write_##__name(struct file *file, \
362 const char __user *buf,\
366 struct rt2x00debug_intf *intf = file->private_data; \
367 const struct rt2x00debug *debug = intf->debug; \
375 if (intf->offset_##__name >= debug->__name.word_count) \
378 if (copy_from_user(line, buf, length)) \
381 size = strlen(line); \
382 value = simple_strtoul(line, NULL, 0); \
384 debug->__name.write(intf->rt2x00dev, \
385 intf->offset_##__name, value); \
391 #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
392 RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
393 RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
395 static const struct file_operations rt2x00debug_fop_##__name = {\
396 .owner = THIS_MODULE, \
397 .read = rt2x00debug_read_##__name, \
398 .write = rt2x00debug_write_##__name, \
399 .open = rt2x00debug_file_open, \
400 .release = rt2x00debug_file_release, \
403 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
404 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
405 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
406 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
408 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
413 struct rt2x00debug_intf *intf = file->private_data;
420 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
422 if (copy_to_user(buf, line, size))
429 static const struct file_operations rt2x00debug_fop_dev_flags = {
430 .owner = THIS_MODULE,
431 .read = rt2x00debug_read_dev_flags,
432 .open = rt2x00debug_file_open,
433 .release = rt2x00debug_file_release,
436 static struct dentry *rt2x00debug_create_file_driver(const char *name,
437 struct rt2x00debug_intf
439 struct debugfs_blob_wrapper
444 data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
449 data += sprintf(data, "driver: %s\n", intf->rt2x00dev->ops->name);
450 data += sprintf(data, "version: %s\n", DRV_VERSION);
451 data += sprintf(data, "compiled: %s %s\n", __DATE__, __TIME__);
452 blob->size = strlen(blob->data);
454 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
457 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
458 struct rt2x00debug_intf
464 const struct rt2x00debug *debug = intf->debug;
467 data = kzalloc(8 * MAX_LINE_LENGTH, GFP_KERNEL);
472 data += sprintf(data, "rt chip: %04x\n", intf->rt2x00dev->chip.rt);
473 data += sprintf(data, "rf chip: %04x\n", intf->rt2x00dev->chip.rf);
474 data += sprintf(data, "revision:%08x\n", intf->rt2x00dev->chip.rev);
475 data += sprintf(data, "\n");
476 data += sprintf(data, "csr length: %d\n", debug->csr.word_count);
477 data += sprintf(data, "eeprom length: %d\n", debug->eeprom.word_count);
478 data += sprintf(data, "bbp length: %d\n", debug->bbp.word_count);
479 data += sprintf(data, "rf length: %d\n", debug->rf.word_count);
480 blob->size = strlen(blob->data);
482 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
485 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
487 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
488 struct rt2x00debug_intf *intf;
490 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
492 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
497 intf->rt2x00dev = rt2x00dev;
498 rt2x00dev->debugfs_intf = intf;
500 intf->driver_folder =
501 debugfs_create_dir(intf->rt2x00dev->ops->name,
502 rt2x00dev->hw->wiphy->debugfsdir);
503 if (IS_ERR(intf->driver_folder))
507 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
508 if (IS_ERR(intf->driver_entry))
511 intf->chipset_entry =
512 rt2x00debug_create_file_chipset("chipset",
513 intf, &intf->chipset_blob);
514 if (IS_ERR(intf->chipset_entry))
517 intf->dev_flags = debugfs_create_file("dev_flags", S_IRUSR,
518 intf->driver_folder, intf,
519 &rt2x00debug_fop_dev_flags);
520 if (IS_ERR(intf->dev_flags))
523 intf->register_folder =
524 debugfs_create_dir("register", intf->driver_folder);
525 if (IS_ERR(intf->register_folder))
528 #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
530 (__intf)->__name##_off_entry = \
531 debugfs_create_u32(__stringify(__name) "_offset", \
533 (__intf)->register_folder, \
534 &(__intf)->offset_##__name); \
535 if (IS_ERR((__intf)->__name##_off_entry)) \
538 (__intf)->__name##_val_entry = \
539 debugfs_create_file(__stringify(__name) "_value", \
541 (__intf)->register_folder, \
542 (__intf), &rt2x00debug_fop_##__name);\
543 if (IS_ERR((__intf)->__name##_val_entry)) \
547 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
548 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
549 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
550 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
552 #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
555 debugfs_create_dir("queue", intf->driver_folder);
556 if (IS_ERR(intf->queue_folder))
559 intf->queue_frame_dump_entry =
560 debugfs_create_file("dump", S_IRUSR, intf->queue_folder,
561 intf, &rt2x00debug_fop_queue_dump);
562 if (IS_ERR(intf->queue_frame_dump_entry))
565 skb_queue_head_init(&intf->frame_dump_skbqueue);
566 init_waitqueue_head(&intf->frame_dump_waitqueue);
568 intf->queue_stats_entry =
569 debugfs_create_file("queue", S_IRUSR, intf->queue_folder,
570 intf, &rt2x00debug_fop_queue_stats);
575 rt2x00debug_deregister(rt2x00dev);
576 ERROR(rt2x00dev, "Failed to register debug handler.\n");
581 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
583 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
588 skb_queue_purge(&intf->frame_dump_skbqueue);
590 debugfs_remove(intf->queue_stats_entry);
591 debugfs_remove(intf->queue_frame_dump_entry);
592 debugfs_remove(intf->queue_folder);
593 debugfs_remove(intf->rf_val_entry);
594 debugfs_remove(intf->rf_off_entry);
595 debugfs_remove(intf->bbp_val_entry);
596 debugfs_remove(intf->bbp_off_entry);
597 debugfs_remove(intf->eeprom_val_entry);
598 debugfs_remove(intf->eeprom_off_entry);
599 debugfs_remove(intf->csr_val_entry);
600 debugfs_remove(intf->csr_off_entry);
601 debugfs_remove(intf->register_folder);
602 debugfs_remove(intf->dev_flags);
603 debugfs_remove(intf->chipset_entry);
604 debugfs_remove(intf->driver_entry);
605 debugfs_remove(intf->driver_folder);
606 kfree(intf->chipset_blob.data);
607 kfree(intf->driver_blob.data);
610 rt2x00dev->debugfs_intf = NULL;