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,
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 + desc->data_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(desc->data_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(desc->frame_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, desc->data_len), desc->data, desc->data_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 (!capable(CAP_NET_ADMIN)) \
378 if (intf->offset_##__name >= debug->__name.word_count) \
381 if (copy_from_user(line, buf, length)) \
384 size = strlen(line); \
385 value = simple_strtoul(line, NULL, 0); \
387 debug->__name.write(intf->rt2x00dev, \
388 intf->offset_##__name, value); \
394 #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
395 RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
396 RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
398 static const struct file_operations rt2x00debug_fop_##__name = {\
399 .owner = THIS_MODULE, \
400 .read = rt2x00debug_read_##__name, \
401 .write = rt2x00debug_write_##__name, \
402 .open = rt2x00debug_file_open, \
403 .release = rt2x00debug_file_release, \
406 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
407 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
408 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
409 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
411 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
416 struct rt2x00debug_intf *intf = file->private_data;
423 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
425 if (copy_to_user(buf, line, size))
432 static const struct file_operations rt2x00debug_fop_dev_flags = {
433 .owner = THIS_MODULE,
434 .read = rt2x00debug_read_dev_flags,
435 .open = rt2x00debug_file_open,
436 .release = rt2x00debug_file_release,
439 static struct dentry *rt2x00debug_create_file_driver(const char *name,
440 struct rt2x00debug_intf
442 struct debugfs_blob_wrapper
447 data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
452 data += sprintf(data, "driver: %s\n", intf->rt2x00dev->ops->name);
453 data += sprintf(data, "version: %s\n", DRV_VERSION);
454 data += sprintf(data, "compiled: %s %s\n", __DATE__, __TIME__);
455 blob->size = strlen(blob->data);
457 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
460 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
461 struct rt2x00debug_intf
467 const struct rt2x00debug *debug = intf->debug;
470 data = kzalloc(8 * MAX_LINE_LENGTH, GFP_KERNEL);
475 data += sprintf(data, "rt chip: %04x\n", intf->rt2x00dev->chip.rt);
476 data += sprintf(data, "rf chip: %04x\n", intf->rt2x00dev->chip.rf);
477 data += sprintf(data, "revision:%08x\n", intf->rt2x00dev->chip.rev);
478 data += sprintf(data, "\n");
479 data += sprintf(data, "csr length: %d\n", debug->csr.word_count);
480 data += sprintf(data, "eeprom length: %d\n", debug->eeprom.word_count);
481 data += sprintf(data, "bbp length: %d\n", debug->bbp.word_count);
482 data += sprintf(data, "rf length: %d\n", debug->rf.word_count);
483 blob->size = strlen(blob->data);
485 return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
488 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
490 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
491 struct rt2x00debug_intf *intf;
493 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
495 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
500 intf->rt2x00dev = rt2x00dev;
501 rt2x00dev->debugfs_intf = intf;
503 intf->driver_folder =
504 debugfs_create_dir(intf->rt2x00dev->ops->name,
505 rt2x00dev->hw->wiphy->debugfsdir);
506 if (IS_ERR(intf->driver_folder))
510 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
511 if (IS_ERR(intf->driver_entry))
514 intf->chipset_entry =
515 rt2x00debug_create_file_chipset("chipset",
516 intf, &intf->chipset_blob);
517 if (IS_ERR(intf->chipset_entry))
520 intf->dev_flags = debugfs_create_file("dev_flags", S_IRUGO,
521 intf->driver_folder, intf,
522 &rt2x00debug_fop_dev_flags);
523 if (IS_ERR(intf->dev_flags))
526 intf->register_folder =
527 debugfs_create_dir("register", intf->driver_folder);
528 if (IS_ERR(intf->register_folder))
531 #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
533 (__intf)->__name##_off_entry = \
534 debugfs_create_u32(__stringify(__name) "_offset", \
536 (__intf)->register_folder, \
537 &(__intf)->offset_##__name); \
538 if (IS_ERR((__intf)->__name##_off_entry)) \
541 (__intf)->__name##_val_entry = \
542 debugfs_create_file(__stringify(__name) "_value", \
544 (__intf)->register_folder, \
545 (__intf), &rt2x00debug_fop_##__name);\
546 if (IS_ERR((__intf)->__name##_val_entry)) \
550 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
551 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
552 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
553 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
555 #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
558 debugfs_create_dir("queue", intf->driver_folder);
559 if (IS_ERR(intf->queue_folder))
562 intf->queue_frame_dump_entry =
563 debugfs_create_file("dump", S_IRUGO, intf->queue_folder,
564 intf, &rt2x00debug_fop_queue_dump);
565 if (IS_ERR(intf->queue_frame_dump_entry))
568 skb_queue_head_init(&intf->frame_dump_skbqueue);
569 init_waitqueue_head(&intf->frame_dump_waitqueue);
571 intf->queue_stats_entry =
572 debugfs_create_file("queue", S_IRUGO, intf->queue_folder,
573 intf, &rt2x00debug_fop_queue_stats);
578 rt2x00debug_deregister(rt2x00dev);
579 ERROR(rt2x00dev, "Failed to register debug handler.\n");
584 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
586 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
591 skb_queue_purge(&intf->frame_dump_skbqueue);
593 debugfs_remove(intf->queue_stats_entry);
594 debugfs_remove(intf->queue_frame_dump_entry);
595 debugfs_remove(intf->queue_folder);
596 debugfs_remove(intf->rf_val_entry);
597 debugfs_remove(intf->rf_off_entry);
598 debugfs_remove(intf->bbp_val_entry);
599 debugfs_remove(intf->bbp_off_entry);
600 debugfs_remove(intf->eeprom_val_entry);
601 debugfs_remove(intf->eeprom_off_entry);
602 debugfs_remove(intf->csr_val_entry);
603 debugfs_remove(intf->csr_off_entry);
604 debugfs_remove(intf->register_folder);
605 debugfs_remove(intf->dev_flags);
606 debugfs_remove(intf->chipset_entry);
607 debugfs_remove(intf->driver_entry);
608 debugfs_remove(intf->driver_folder);
609 kfree(intf->chipset_blob.data);
610 kfree(intf->driver_blob.data);
613 rt2x00dev->debugfs_intf = NULL;