rt2x00debug.c 20.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
	Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15
	<http://rt2x00.serialmonkey.com>

 */

/*
	Module: rt2x00lib
	Abstract: rt2x00 debugfs specific routines.
 */

#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/module.h>
16
#include <linux/poll.h>
17
#include <linux/sched.h>
18
#include <linux/slab.h>
19 20 21 22
#include <linux/uaccess.h>

#include "rt2x00.h"
#include "rt2x00lib.h"
23
#include "rt2x00dump.h"
24

25
#define MAX_LINE_LENGTH 64
26

I
Ivo van Doorn 已提交
27 28 29 30 31 32 33
struct rt2x00debug_crypto {
	unsigned long success;
	unsigned long icv_error;
	unsigned long mic_error;
	unsigned long key_error;
};

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
struct rt2x00debug_intf {
	/*
	 * Pointer to driver structure where
	 * this debugfs entry belongs to.
	 */
	struct rt2x00_dev *rt2x00dev;

	/*
	 * Reference to the rt2x00debug structure
	 * which can be used to communicate with
	 * the registers.
	 */
	const struct rt2x00debug *debug;

	/*
	 * Debugfs entries for:
	 * - driver folder
51 52
	 *   - driver file
	 *   - chipset file
I
Ivo van Doorn 已提交
53 54
	 *   - device state flags file
	 *   - device capability flags file
S
Stanislaw Gruszka 已提交
55
	 *   - hardware restart file
56 57 58 59 60
	 *   - register folder
	 *     - csr offset/value files
	 *     - eeprom offset/value files
	 *     - bbp offset/value files
	 *     - rf offset/value files
61
	 *     - rfcsr offset/value files
62
	 *   - queue folder
63
	 *     - frame dump file
64
	 *     - queue stats file
I
Ivo van Doorn 已提交
65
	 *     - crypto stats file
66 67 68 69
	 */
	struct dentry *driver_folder;
	struct dentry *driver_entry;
	struct dentry *chipset_entry;
70
	struct dentry *dev_flags;
I
Ivo van Doorn 已提交
71
	struct dentry *cap_flags;
S
Stanislaw Gruszka 已提交
72
	struct dentry *restart_hw;
73
	struct dentry *register_folder;
74 75 76 77 78 79 80 81
	struct dentry *csr_off_entry;
	struct dentry *csr_val_entry;
	struct dentry *eeprom_off_entry;
	struct dentry *eeprom_val_entry;
	struct dentry *bbp_off_entry;
	struct dentry *bbp_val_entry;
	struct dentry *rf_off_entry;
	struct dentry *rf_val_entry;
82 83
	struct dentry *rfcsr_off_entry;
	struct dentry *rfcsr_val_entry;
84 85 86
	struct dentry *queue_folder;
	struct dentry *queue_frame_dump_entry;
	struct dentry *queue_stats_entry;
I
Ivo van Doorn 已提交
87
	struct dentry *crypto_stats_entry;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	/*
	 * The frame dump file only allows a single reader,
	 * so we need to store the current state here.
	 */
	unsigned long frame_dump_flags;
#define FRAME_DUMP_FILE_OPEN	1

	/*
	 * We queue each frame before dumping it to the user,
	 * per read command we will pass a single skb structure
	 * so we should be prepared to queue multiple sk buffers
	 * before sending it to userspace.
	 */
	struct sk_buff_head frame_dump_skbqueue;
	wait_queue_head_t frame_dump_waitqueue;
104

I
Ivo van Doorn 已提交
105 106
	/*
	 * HW crypto statistics.
D
Daniel Mack 已提交
107
	 * All statistics are stored separately per cipher type.
I
Ivo van Doorn 已提交
108 109 110
	 */
	struct rt2x00debug_crypto crypto_stats[CIPHER_MAX];

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	/*
	 * Driver and chipset files will use a data buffer
	 * that has been created in advance. This will simplify
	 * the code since we can use the debugfs functions.
	 */
	struct debugfs_blob_wrapper driver_blob;
	struct debugfs_blob_wrapper chipset_blob;

	/*
	 * Requested offset for each register type.
	 */
	unsigned int offset_csr;
	unsigned int offset_eeprom;
	unsigned int offset_bbp;
	unsigned int offset_rf;
126
	unsigned int offset_rfcsr;
127 128
};

I
Ivo van Doorn 已提交
129
void rt2x00debug_update_crypto(struct rt2x00_dev *rt2x00dev,
130
			       struct rxdone_entry_desc *rxdesc)
I
Ivo van Doorn 已提交
131 132
{
	struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
133 134
	enum cipher cipher = rxdesc->cipher;
	enum rx_crypto status = rxdesc->cipher_status;
I
Ivo van Doorn 已提交
135 136 137

	if (cipher == CIPHER_TKIP_NO_MIC)
		cipher = CIPHER_TKIP;
138
	if (cipher == CIPHER_NONE || cipher >= CIPHER_MAX)
I
Ivo van Doorn 已提交
139 140 141 142 143 144 145 146 147 148 149
		return;

	/* Remove CIPHER_NONE index */
	cipher--;

	intf->crypto_stats[cipher].success += (status == RX_CRYPTO_SUCCESS);
	intf->crypto_stats[cipher].icv_error += (status == RX_CRYPTO_FAIL_ICV);
	intf->crypto_stats[cipher].mic_error += (status == RX_CRYPTO_FAIL_MIC);
	intf->crypto_stats[cipher].key_error += (status == RX_CRYPTO_FAIL_KEY);
}

150
void rt2x00debug_dump_frame(struct rt2x00_dev *rt2x00dev,
151
			    enum rt2x00_dump_type type, struct queue_entry *entry)
152 153
{
	struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
154
	struct sk_buff *skb = entry->skb;
155
	struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
156 157
	struct sk_buff *skbcopy;
	struct rt2x00dump_hdr *dump_hdr;
158
	struct timespec64 timestamp;
159
	u32 data_len;
160

161
	if (likely(!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)))
162 163
		return;

164
	ktime_get_ts64(&timestamp);
165

166
	if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
167
		rt2x00_dbg(rt2x00dev, "txrx dump queue length exceeded\n");
168 169 170
		return;
	}

171 172 173 174 175
	data_len = skb->len;
	if (skbdesc->flags & SKBDESC_DESC_IN_SKB)
		data_len -= skbdesc->desc_len;

	skbcopy = alloc_skb(sizeof(*dump_hdr) + skbdesc->desc_len + data_len,
176 177
			    GFP_ATOMIC);
	if (!skbcopy) {
178
		rt2x00_dbg(rt2x00dev, "Failed to copy skb for dump\n");
179 180 181
		return;
	}

182
	dump_hdr = skb_put(skbcopy, sizeof(*dump_hdr));
183 184
	dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
	dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
185
	dump_hdr->desc_length = cpu_to_le32(skbdesc->desc_len);
186
	dump_hdr->data_length = cpu_to_le32(data_len);
187 188
	dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
	dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
189
	dump_hdr->chip_rev = cpu_to_le16(rt2x00dev->chip.rev);
190
	dump_hdr->type = cpu_to_le16(type);
191 192
	dump_hdr->queue_index = entry->queue->qid;
	dump_hdr->entry_index = entry->entry_idx;
193
	dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
194 195
	dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_nsec /
					       NSEC_PER_USEC);
196

197
	if (!(skbdesc->flags & SKBDESC_DESC_IN_SKB))
198 199
		skb_put_data(skbcopy, skbdesc->desc, skbdesc->desc_len);
	skb_put_data(skbcopy, skb->data, skb->len);
200 201 202 203 204 205 206 207 208 209

	skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
	wake_up_interruptible(&intf->frame_dump_waitqueue);

	/*
	 * Verify that the file has not been closed while we were working.
	 */
	if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
		skb_queue_purge(&intf->frame_dump_skbqueue);
}
210
EXPORT_SYMBOL_GPL(rt2x00debug_dump_frame);
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
static int rt2x00debug_file_open(struct inode *inode, struct file *file)
{
	struct rt2x00debug_intf *intf = inode->i_private;

	file->private_data = inode->i_private;

	if (!try_module_get(intf->debug->owner))
		return -EBUSY;

	return 0;
}

static int rt2x00debug_file_release(struct inode *inode, struct file *file)
{
	struct rt2x00debug_intf *intf = file->private_data;

	module_put(intf->debug->owner);

	return 0;
}

I
Ivo van Doorn 已提交
233
static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
{
	struct rt2x00debug_intf *intf = inode->i_private;
	int retval;

	retval = rt2x00debug_file_open(inode, file);
	if (retval)
		return retval;

	if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
		rt2x00debug_file_release(inode, file);
		return -EBUSY;
	}

	return 0;
}

I
Ivo van Doorn 已提交
250
static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
251 252 253 254 255 256 257 258 259 260
{
	struct rt2x00debug_intf *intf = inode->i_private;

	skb_queue_purge(&intf->frame_dump_skbqueue);

	clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);

	return rt2x00debug_file_release(inode, file);
}

I
Ivo van Doorn 已提交
261 262 263 264
static ssize_t rt2x00debug_read_queue_dump(struct file *file,
					   char __user *buf,
					   size_t length,
					   loff_t *offset)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
{
	struct rt2x00debug_intf *intf = file->private_data;
	struct sk_buff *skb;
	size_t status;
	int retval;

	if (file->f_flags & O_NONBLOCK)
		return -EAGAIN;

	retval =
	    wait_event_interruptible(intf->frame_dump_waitqueue,
				     (skb =
				     skb_dequeue(&intf->frame_dump_skbqueue)));
	if (retval)
		return retval;

S
Silvan Jegen 已提交
281
	status = min_t(size_t, skb->len, length);
282 283 284 285 286 287 288 289 290 291 292 293 294
	if (copy_to_user(buf, skb->data, status)) {
		status = -EFAULT;
		goto exit;
	}

	*offset += status;

exit:
	kfree_skb(skb);

	return status;
}

295
static __poll_t rt2x00debug_poll_queue_dump(struct file *file,
J
John Daiker 已提交
296
						poll_table *wait)
297 298 299 300 301 302
{
	struct rt2x00debug_intf *intf = file->private_data;

	poll_wait(file, &intf->frame_dump_waitqueue, wait);

	if (!skb_queue_empty(&intf->frame_dump_skbqueue))
303
		return EPOLLOUT | EPOLLWRNORM;
304 305 306 307

	return 0;
}

I
Ivo van Doorn 已提交
308
static const struct file_operations rt2x00debug_fop_queue_dump = {
309
	.owner		= THIS_MODULE,
I
Ivo van Doorn 已提交
310 311 312 313
	.read		= rt2x00debug_read_queue_dump,
	.poll		= rt2x00debug_poll_queue_dump,
	.open		= rt2x00debug_open_queue_dump,
	.release	= rt2x00debug_release_queue_dump,
314
	.llseek		= default_llseek,
315 316
};

317 318 319 320 321 322 323
static ssize_t rt2x00debug_read_queue_stats(struct file *file,
					    char __user *buf,
					    size_t length,
					    loff_t *offset)
{
	struct rt2x00debug_intf *intf = file->private_data;
	struct data_queue *queue;
324
	unsigned long irqflags;
325 326 327 328 329 330 331 332
	unsigned int lines = 1 + intf->rt2x00dev->data_queues;
	size_t size;
	char *data;
	char *temp;

	if (*offset)
		return 0;

333
	data = kcalloc(lines, MAX_LINE_LENGTH, GFP_KERNEL);
334 335 336 337
	if (!data)
		return -ENOMEM;

	temp = data +
338
	    sprintf(data, "qid\tflags\t\tcount\tlimit\tlength\tindex\tdma done\tdone\n");
339 340

	queue_for_each(intf->rt2x00dev, queue) {
341
		spin_lock_irqsave(&queue->index_lock, irqflags);
342

343 344
		temp += sprintf(temp, "%d\t0x%.8x\t%d\t%d\t%d\t%d\t%d\t\t%d\n",
				queue->qid, (unsigned int)queue->flags,
345 346
				queue->count, queue->limit, queue->length,
				queue->index[Q_INDEX],
347
				queue->index[Q_INDEX_DMA_DONE],
348
				queue->index[Q_INDEX_DONE]);
349

350
		spin_unlock_irqrestore(&queue->index_lock, irqflags);
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	}

	size = strlen(data);
	size = min(size, length);

	if (copy_to_user(buf, data, size)) {
		kfree(data);
		return -EFAULT;
	}

	kfree(data);

	*offset += size;
	return size;
}

static const struct file_operations rt2x00debug_fop_queue_stats = {
	.owner		= THIS_MODULE,
	.read		= rt2x00debug_read_queue_stats,
	.open		= rt2x00debug_file_open,
	.release	= rt2x00debug_file_release,
372
	.llseek		= default_llseek,
373 374
};

I
Ivo van Doorn 已提交
375 376 377 378 379 380 381
#ifdef CONFIG_RT2X00_LIB_CRYPTO
static ssize_t rt2x00debug_read_crypto_stats(struct file *file,
					     char __user *buf,
					     size_t length,
					     loff_t *offset)
{
	struct rt2x00debug_intf *intf = file->private_data;
382
	static const char * const name[] = { "WEP64", "WEP128", "TKIP", "AES" };
I
Ivo van Doorn 已提交
383 384 385 386 387 388 389 390
	char *data;
	char *temp;
	size_t size;
	unsigned int i;

	if (*offset)
		return 0;

K
Kees Cook 已提交
391
	data = kcalloc(1 + CIPHER_MAX, MAX_LINE_LENGTH, GFP_KERNEL);
I
Ivo van Doorn 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	if (!data)
		return -ENOMEM;

	temp = data;
	temp += sprintf(data, "cipher\tsuccess\ticv err\tmic err\tkey err\n");

	for (i = 0; i < CIPHER_MAX; i++) {
		temp += sprintf(temp, "%s\t%lu\t%lu\t%lu\t%lu\n", name[i],
				intf->crypto_stats[i].success,
				intf->crypto_stats[i].icv_error,
				intf->crypto_stats[i].mic_error,
				intf->crypto_stats[i].key_error);
	}

	size = strlen(data);
	size = min(size, length);

	if (copy_to_user(buf, data, size)) {
		kfree(data);
		return -EFAULT;
	}

	kfree(data);

	*offset += size;
	return size;
}

static const struct file_operations rt2x00debug_fop_crypto_stats = {
	.owner		= THIS_MODULE,
	.read		= rt2x00debug_read_crypto_stats,
	.open		= rt2x00debug_file_open,
	.release	= rt2x00debug_file_release,
425
	.llseek		= default_llseek,
I
Ivo van Doorn 已提交
426 427 428
};
#endif

429 430 431 432 433 434
#define RT2X00DEBUGFS_OPS_READ(__name, __format, __type)	\
static ssize_t rt2x00debug_read_##__name(struct file *file,	\
					 char __user *buf,	\
					 size_t length,		\
					 loff_t *offset)	\
{								\
435
	struct rt2x00debug_intf *intf = file->private_data;	\
436 437 438
	const struct rt2x00debug *debug = intf->debug;		\
	char line[16];						\
	size_t size;						\
439
	unsigned int index = intf->offset_##__name;		\
440 441 442 443 444
	__type value;						\
								\
	if (*offset)						\
		return 0;					\
								\
445
	if (index >= debug->__name.word_count)			\
446 447
		return -EINVAL;					\
								\
I
Ivo van Doorn 已提交
448 449 450
	index += (debug->__name.word_base /			\
		  debug->__name.word_size);			\
								\
451 452 453
	if (debug->__name.flags & RT2X00DEBUGFS_OFFSET)		\
		index *= debug->__name.word_size;		\
								\
454
	value = debug->__name.read(intf->rt2x00dev, index);	\
455 456 457
								\
	size = sprintf(line, __format, value);			\
								\
458
	return simple_read_from_buffer(buf, length, offset, line, size); \
459 460 461 462 463 464 465 466
}

#define RT2X00DEBUGFS_OPS_WRITE(__name, __type)			\
static ssize_t rt2x00debug_write_##__name(struct file *file,	\
					  const char __user *buf,\
					  size_t length,	\
					  loff_t *offset)	\
{								\
467
	struct rt2x00debug_intf *intf = file->private_data;	\
468
	const struct rt2x00debug *debug = intf->debug;		\
469
	char line[17];						\
470
	size_t size;						\
471
	unsigned int index = intf->offset_##__name;		\
472 473 474 475 476
	__type value;						\
								\
	if (*offset)						\
		return 0;					\
								\
477
	if (index >= debug->__name.word_count)			\
478 479
		return -EINVAL;					\
								\
480 481 482
	if (length > sizeof(line))				\
		return -EINVAL;					\
								\
483 484
	if (copy_from_user(line, buf, length))			\
		return -EFAULT;					\
485 486
	line[16] = 0;						\
						\
487 488 489
	size = strlen(line);					\
	value = simple_strtoul(line, NULL, 0);			\
								\
I
Ivo van Doorn 已提交
490 491 492
	index += (debug->__name.word_base /			\
		  debug->__name.word_size);			\
								\
493 494 495 496
	if (debug->__name.flags & RT2X00DEBUGFS_OFFSET)		\
		index *= debug->__name.word_size;		\
								\
	debug->__name.write(intf->rt2x00dev, index, value);	\
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
								\
	*offset += size;					\
	return size;						\
}

#define RT2X00DEBUGFS_OPS(__name, __format, __type)		\
RT2X00DEBUGFS_OPS_READ(__name, __format, __type);		\
RT2X00DEBUGFS_OPS_WRITE(__name, __type);			\
								\
static const struct file_operations rt2x00debug_fop_##__name = {\
	.owner		= THIS_MODULE,				\
	.read		= rt2x00debug_read_##__name,		\
	.write		= rt2x00debug_write_##__name,		\
	.open		= rt2x00debug_file_open,		\
	.release	= rt2x00debug_file_release,		\
512
	.llseek		= generic_file_llseek,			\
513 514 515 516 517 518
};

RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
519
RT2X00DEBUGFS_OPS(rfcsr, "0x%.2x\n", u8);
520

521 522 523 524 525 526 527 528 529 530 531 532 533 534
static ssize_t rt2x00debug_read_dev_flags(struct file *file,
					  char __user *buf,
					  size_t length,
					  loff_t *offset)
{
	struct rt2x00debug_intf *intf =	file->private_data;
	char line[16];
	size_t size;

	if (*offset)
		return 0;

	size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);

535
	return simple_read_from_buffer(buf, length, offset, line, size);
536 537 538 539 540 541 542
}

static const struct file_operations rt2x00debug_fop_dev_flags = {
	.owner		= THIS_MODULE,
	.read		= rt2x00debug_read_dev_flags,
	.open		= rt2x00debug_file_open,
	.release	= rt2x00debug_file_release,
543
	.llseek		= default_llseek,
544 545
};

I
Ivo van Doorn 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559
static ssize_t rt2x00debug_read_cap_flags(struct file *file,
					  char __user *buf,
					  size_t length,
					  loff_t *offset)
{
	struct rt2x00debug_intf *intf =	file->private_data;
	char line[16];
	size_t size;

	if (*offset)
		return 0;

	size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->cap_flags);

560
	return simple_read_from_buffer(buf, length, offset, line, size);
I
Ivo van Doorn 已提交
561 562 563 564 565 566 567 568 569 570
}

static const struct file_operations rt2x00debug_fop_cap_flags = {
	.owner		= THIS_MODULE,
	.read		= rt2x00debug_read_cap_flags,
	.open		= rt2x00debug_file_open,
	.release	= rt2x00debug_file_release,
	.llseek		= default_llseek,
};

S
Stanislaw Gruszka 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
static ssize_t rt2x00debug_write_restart_hw(struct file *file,
					    const char __user *buf,
					    size_t length,
					    loff_t *offset)
{
	struct rt2x00debug_intf *intf =	file->private_data;
	struct rt2x00_dev *rt2x00dev = intf->rt2x00dev;
	static unsigned long last_reset;

	if (!rt2x00_has_cap_restart_hw(rt2x00dev))
		return -EOPNOTSUPP;

	if (time_before(jiffies, last_reset + msecs_to_jiffies(2000)))
		return -EBUSY;

	last_reset = jiffies;

	ieee80211_restart_hw(rt2x00dev->hw);
	return length;
}

static const struct file_operations rt2x00debug_restart_hw = {
	.owner = THIS_MODULE,
	.write = rt2x00debug_write_restart_hw,
	.open = simple_open,
	.llseek = generic_file_llseek,
};

599 600 601 602 603 604 605 606
static struct dentry *rt2x00debug_create_file_driver(const char *name,
						     struct rt2x00debug_intf
						     *intf,
						     struct debugfs_blob_wrapper
						     *blob)
{
	char *data;

607
	data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
608 609 610 611
	if (!data)
		return NULL;

	blob->data = data;
612 613
	data += sprintf(data, "driver:\t%s\n", intf->rt2x00dev->ops->name);
	data += sprintf(data, "version:\t%s\n", DRV_VERSION);
614 615
	blob->size = strlen(blob->data);

616
	return debugfs_create_blob(name, 0400, intf->driver_folder, blob);
617 618 619 620 621 622 623 624 625 626 627 628
}

static struct dentry *rt2x00debug_create_file_chipset(const char *name,
						      struct rt2x00debug_intf
						      *intf,
						      struct
						      debugfs_blob_wrapper
						      *blob)
{
	const struct rt2x00debug *debug = intf->debug;
	char *data;

629
	data = kzalloc(9 * MAX_LINE_LENGTH, GFP_KERNEL);
630 631 632
	if (!data)
		return NULL;

I
Ivo van Doorn 已提交
633
	blob->data = data;
634 635
	data += sprintf(data, "rt chip:\t%04x\n", intf->rt2x00dev->chip.rt);
	data += sprintf(data, "rf chip:\t%04x\n", intf->rt2x00dev->chip.rf);
636
	data += sprintf(data, "revision:\t%04x\n", intf->rt2x00dev->chip.rev);
637
	data += sprintf(data, "\n");
638
	data += sprintf(data, "register\tbase\twords\twordsize\n");
639 640
#define RT2X00DEBUGFS_SPRINTF_REGISTER(__name)			\
{								\
641
	if (debug->__name.read)					\
642 643 644 645 646 647 648 649 650 651
		data += sprintf(data, __stringify(__name)	\
				"\t%d\t%d\t%d\n",		\
				debug->__name.word_base,	\
				debug->__name.word_count,	\
				debug->__name.word_size);	\
}
	RT2X00DEBUGFS_SPRINTF_REGISTER(csr);
	RT2X00DEBUGFS_SPRINTF_REGISTER(eeprom);
	RT2X00DEBUGFS_SPRINTF_REGISTER(bbp);
	RT2X00DEBUGFS_SPRINTF_REGISTER(rf);
652
	RT2X00DEBUGFS_SPRINTF_REGISTER(rfcsr);
653 654
#undef RT2X00DEBUGFS_SPRINTF_REGISTER

I
Ivo van Doorn 已提交
655
	blob->size = strlen(blob->data);
656

657
	return debugfs_create_blob(name, 0400, intf->driver_folder, blob);
658 659 660 661 662 663 664 665 666
}

void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
{
	const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
	struct rt2x00debug_intf *intf;

	intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
	if (!intf) {
667
		rt2x00_err(rt2x00dev, "Failed to allocate debug handler\n");
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
		return;
	}

	intf->debug = debug;
	intf->rt2x00dev = rt2x00dev;
	rt2x00dev->debugfs_intf = intf;

	intf->driver_folder =
	    debugfs_create_dir(intf->rt2x00dev->ops->name,
			       rt2x00dev->hw->wiphy->debugfsdir);

	intf->driver_entry =
	    rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);

	intf->chipset_entry =
	    rt2x00debug_create_file_chipset("chipset",
					    intf, &intf->chipset_blob);

686
	intf->dev_flags = debugfs_create_file("dev_flags", 0400,
687 688 689
					      intf->driver_folder, intf,
					      &rt2x00debug_fop_dev_flags);

690
	intf->cap_flags = debugfs_create_file("cap_flags", 0400,
I
Ivo van Doorn 已提交
691 692 693
					      intf->driver_folder, intf,
					      &rt2x00debug_fop_cap_flags);

S
Stanislaw Gruszka 已提交
694 695 696 697
	intf->restart_hw = debugfs_create_file("restart_hw", 0200,
					       intf->driver_folder, intf,
					       &rt2x00debug_restart_hw);

698 699 700
	intf->register_folder =
	    debugfs_create_dir("register", intf->driver_folder);

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
#define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name)		\
({									\
	if (debug->__name.read) {					\
		(__intf)->__name##_off_entry =				\
			debugfs_create_u32(__stringify(__name) "_offset", \
					   0600,			\
					   (__intf)->register_folder,	\
					   &(__intf)->offset_##__name);	\
									\
		(__intf)->__name##_val_entry =				\
			debugfs_create_file(__stringify(__name) "_value", \
					    0600,			\
					    (__intf)->register_folder,	\
					    (__intf),			\
					    &rt2x00debug_fop_##__name); \
	}								\
717 718
})

719 720 721 722
	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
723
	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rfcsr);
724

725
#undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
726

727 728
	intf->queue_folder =
	    debugfs_create_dir("queue", intf->driver_folder);
729

730
	intf->queue_frame_dump_entry =
731 732
		debugfs_create_file("dump", 0400, intf->queue_folder,
				    intf, &rt2x00debug_fop_queue_dump);
733 734 735 736

	skb_queue_head_init(&intf->frame_dump_skbqueue);
	init_waitqueue_head(&intf->frame_dump_waitqueue);

737
	intf->queue_stats_entry =
738 739
		debugfs_create_file("queue", 0400, intf->queue_folder,
				    intf, &rt2x00debug_fop_queue_stats);
740

I
Ivo van Doorn 已提交
741
#ifdef CONFIG_RT2X00_LIB_CRYPTO
742
	if (rt2x00_has_cap_hw_crypto(rt2x00dev))
I
Ivo van Doorn 已提交
743
		intf->crypto_stats_entry =
744 745 746
			debugfs_create_file("crypto", 0444, intf->queue_folder,
					    intf,
					    &rt2x00debug_fop_crypto_stats);
I
Ivo van Doorn 已提交
747 748
#endif

749 750 751 752 753
	return;
}

void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
{
754
	struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
755 756 757 758

	if (unlikely(!intf))
		return;

759 760
	skb_queue_purge(&intf->frame_dump_skbqueue);

I
Ivo van Doorn 已提交
761 762 763
#ifdef CONFIG_RT2X00_LIB_CRYPTO
	debugfs_remove(intf->crypto_stats_entry);
#endif
764 765 766
	debugfs_remove(intf->queue_stats_entry);
	debugfs_remove(intf->queue_frame_dump_entry);
	debugfs_remove(intf->queue_folder);
767 768
	debugfs_remove(intf->rfcsr_val_entry);
	debugfs_remove(intf->rfcsr_off_entry);
769 770 771 772 773 774 775 776
	debugfs_remove(intf->rf_val_entry);
	debugfs_remove(intf->rf_off_entry);
	debugfs_remove(intf->bbp_val_entry);
	debugfs_remove(intf->bbp_off_entry);
	debugfs_remove(intf->eeprom_val_entry);
	debugfs_remove(intf->eeprom_off_entry);
	debugfs_remove(intf->csr_val_entry);
	debugfs_remove(intf->csr_off_entry);
777
	debugfs_remove(intf->register_folder);
778
	debugfs_remove(intf->dev_flags);
S
Stanislaw Gruszka 已提交
779
	debugfs_remove(intf->restart_hw);
I
Ivo van Doorn 已提交
780
	debugfs_remove(intf->cap_flags);
781 782 783 784 785 786 787 788 789
	debugfs_remove(intf->chipset_entry);
	debugfs_remove(intf->driver_entry);
	debugfs_remove(intf->driver_folder);
	kfree(intf->chipset_blob.data);
	kfree(intf->driver_blob.data);
	kfree(intf);

	rt2x00dev->debugfs_intf = NULL;
}