xref: /openbmc/linux/drivers/net/wireless/ralink/rt2x00/rt2x00debug.c (revision 04eb94d526423ff082efce61f4f26b0369d0bfdd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 	Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
4 	<http://rt2x00.serialmonkey.com>
5 
6  */
7 
8 /*
9 	Module: rt2x00lib
10 	Abstract: rt2x00 debugfs specific routines.
11  */
12 
13 #include <linux/debugfs.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/poll.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
20 
21 #include "rt2x00.h"
22 #include "rt2x00lib.h"
23 #include "rt2x00dump.h"
24 
25 #define MAX_LINE_LENGTH 64
26 
27 struct rt2x00debug_crypto {
28 	unsigned long success;
29 	unsigned long icv_error;
30 	unsigned long mic_error;
31 	unsigned long key_error;
32 };
33 
34 struct rt2x00debug_intf {
35 	/*
36 	 * Pointer to driver structure where
37 	 * this debugfs entry belongs to.
38 	 */
39 	struct rt2x00_dev *rt2x00dev;
40 
41 	/*
42 	 * Reference to the rt2x00debug structure
43 	 * which can be used to communicate with
44 	 * the registers.
45 	 */
46 	const struct rt2x00debug *debug;
47 
48 	/*
49 	 * Debugfs entries for:
50 	 * - driver folder
51 	 *   - driver file
52 	 *   - chipset file
53 	 *   - device state flags file
54 	 *   - device capability flags file
55 	 *   - hardware restart file
56 	 *   - register folder
57 	 *     - csr offset/value files
58 	 *     - eeprom offset/value files
59 	 *     - bbp offset/value files
60 	 *     - rf offset/value files
61 	 *     - rfcsr offset/value files
62 	 *   - queue folder
63 	 *     - frame dump file
64 	 *     - queue stats file
65 	 *     - crypto stats file
66 	 */
67 	struct dentry *driver_folder;
68 	struct dentry *driver_entry;
69 	struct dentry *chipset_entry;
70 	struct dentry *dev_flags;
71 	struct dentry *cap_flags;
72 	struct dentry *restart_hw;
73 	struct dentry *register_folder;
74 	struct dentry *csr_off_entry;
75 	struct dentry *csr_val_entry;
76 	struct dentry *eeprom_off_entry;
77 	struct dentry *eeprom_val_entry;
78 	struct dentry *bbp_off_entry;
79 	struct dentry *bbp_val_entry;
80 	struct dentry *rf_off_entry;
81 	struct dentry *rf_val_entry;
82 	struct dentry *rfcsr_off_entry;
83 	struct dentry *rfcsr_val_entry;
84 	struct dentry *queue_folder;
85 	struct dentry *queue_frame_dump_entry;
86 	struct dentry *queue_stats_entry;
87 	struct dentry *crypto_stats_entry;
88 
89 	/*
90 	 * The frame dump file only allows a single reader,
91 	 * so we need to store the current state here.
92 	 */
93 	unsigned long frame_dump_flags;
94 #define FRAME_DUMP_FILE_OPEN	1
95 
96 	/*
97 	 * We queue each frame before dumping it to the user,
98 	 * per read command we will pass a single skb structure
99 	 * so we should be prepared to queue multiple sk buffers
100 	 * before sending it to userspace.
101 	 */
102 	struct sk_buff_head frame_dump_skbqueue;
103 	wait_queue_head_t frame_dump_waitqueue;
104 
105 	/*
106 	 * HW crypto statistics.
107 	 * All statistics are stored separately per cipher type.
108 	 */
109 	struct rt2x00debug_crypto crypto_stats[CIPHER_MAX];
110 
111 	/*
112 	 * Driver and chipset files will use a data buffer
113 	 * that has been created in advance. This will simplify
114 	 * the code since we can use the debugfs functions.
115 	 */
116 	struct debugfs_blob_wrapper driver_blob;
117 	struct debugfs_blob_wrapper chipset_blob;
118 
119 	/*
120 	 * Requested offset for each register type.
121 	 */
122 	unsigned int offset_csr;
123 	unsigned int offset_eeprom;
124 	unsigned int offset_bbp;
125 	unsigned int offset_rf;
126 	unsigned int offset_rfcsr;
127 };
128 
129 void rt2x00debug_update_crypto(struct rt2x00_dev *rt2x00dev,
130 			       struct rxdone_entry_desc *rxdesc)
131 {
132 	struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
133 	enum cipher cipher = rxdesc->cipher;
134 	enum rx_crypto status = rxdesc->cipher_status;
135 
136 	if (cipher == CIPHER_TKIP_NO_MIC)
137 		cipher = CIPHER_TKIP;
138 	if (cipher == CIPHER_NONE || cipher >= CIPHER_MAX)
139 		return;
140 
141 	/* Remove CIPHER_NONE index */
142 	cipher--;
143 
144 	intf->crypto_stats[cipher].success += (status == RX_CRYPTO_SUCCESS);
145 	intf->crypto_stats[cipher].icv_error += (status == RX_CRYPTO_FAIL_ICV);
146 	intf->crypto_stats[cipher].mic_error += (status == RX_CRYPTO_FAIL_MIC);
147 	intf->crypto_stats[cipher].key_error += (status == RX_CRYPTO_FAIL_KEY);
148 }
149 
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 	struct sk_buff *skbcopy;
157 	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 		return;
163 
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 		return;
169 	}
170 
171 	data_len = skb->len;
172 	if (skbdesc->flags & SKBDESC_DESC_IN_SKB)
173 		data_len -= skbdesc->desc_len;
174 
175 	skbcopy = alloc_skb(sizeof(*dump_hdr) + skbdesc->desc_len + data_len,
176 			    GFP_ATOMIC);
177 	if (!skbcopy) {
178 		rt2x00_dbg(rt2x00dev, "Failed to copy skb for dump\n");
179 		return;
180 	}
181 
182 	dump_hdr = skb_put(skbcopy, sizeof(*dump_hdr));
183 	dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
184 	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 	dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
188 	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 	dump_hdr->queue_index = entry->queue->qid;
192 	dump_hdr->entry_index = entry->entry_idx;
193 	dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
194 	dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_nsec /
195 					       NSEC_PER_USEC);
196 
197 	if (!(skbdesc->flags & SKBDESC_DESC_IN_SKB))
198 		skb_put_data(skbcopy, skbdesc->desc, skbdesc->desc_len);
199 	skb_put_data(skbcopy, skb->data, skb->len);
200 
201 	skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
202 	wake_up_interruptible(&intf->frame_dump_waitqueue);
203 
204 	/*
205 	 * Verify that the file has not been closed while we were working.
206 	 */
207 	if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
208 		skb_queue_purge(&intf->frame_dump_skbqueue);
209 }
210 EXPORT_SYMBOL_GPL(rt2x00debug_dump_frame);
211 
212 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
213 {
214 	struct rt2x00debug_intf *intf = inode->i_private;
215 
216 	file->private_data = inode->i_private;
217 
218 	if (!try_module_get(intf->debug->owner))
219 		return -EBUSY;
220 
221 	return 0;
222 }
223 
224 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
225 {
226 	struct rt2x00debug_intf *intf = file->private_data;
227 
228 	module_put(intf->debug->owner);
229 
230 	return 0;
231 }
232 
233 static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
234 {
235 	struct rt2x00debug_intf *intf = inode->i_private;
236 	int retval;
237 
238 	retval = rt2x00debug_file_open(inode, file);
239 	if (retval)
240 		return retval;
241 
242 	if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
243 		rt2x00debug_file_release(inode, file);
244 		return -EBUSY;
245 	}
246 
247 	return 0;
248 }
249 
250 static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
251 {
252 	struct rt2x00debug_intf *intf = inode->i_private;
253 
254 	skb_queue_purge(&intf->frame_dump_skbqueue);
255 
256 	clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
257 
258 	return rt2x00debug_file_release(inode, file);
259 }
260 
261 static ssize_t rt2x00debug_read_queue_dump(struct file *file,
262 					   char __user *buf,
263 					   size_t length,
264 					   loff_t *offset)
265 {
266 	struct rt2x00debug_intf *intf = file->private_data;
267 	struct sk_buff *skb;
268 	size_t status;
269 	int retval;
270 
271 	if (file->f_flags & O_NONBLOCK)
272 		return -EAGAIN;
273 
274 	retval =
275 	    wait_event_interruptible(intf->frame_dump_waitqueue,
276 				     (skb =
277 				     skb_dequeue(&intf->frame_dump_skbqueue)));
278 	if (retval)
279 		return retval;
280 
281 	status = min_t(size_t, skb->len, length);
282 	if (copy_to_user(buf, skb->data, status)) {
283 		status = -EFAULT;
284 		goto exit;
285 	}
286 
287 	*offset += status;
288 
289 exit:
290 	kfree_skb(skb);
291 
292 	return status;
293 }
294 
295 static __poll_t rt2x00debug_poll_queue_dump(struct file *file,
296 						poll_table *wait)
297 {
298 	struct rt2x00debug_intf *intf = file->private_data;
299 
300 	poll_wait(file, &intf->frame_dump_waitqueue, wait);
301 
302 	if (!skb_queue_empty(&intf->frame_dump_skbqueue))
303 		return EPOLLOUT | EPOLLWRNORM;
304 
305 	return 0;
306 }
307 
308 static const struct file_operations rt2x00debug_fop_queue_dump = {
309 	.owner		= THIS_MODULE,
310 	.read		= rt2x00debug_read_queue_dump,
311 	.poll		= rt2x00debug_poll_queue_dump,
312 	.open		= rt2x00debug_open_queue_dump,
313 	.release	= rt2x00debug_release_queue_dump,
314 	.llseek		= default_llseek,
315 };
316 
317 static ssize_t rt2x00debug_read_queue_stats(struct file *file,
318 					    char __user *buf,
319 					    size_t length,
320 					    loff_t *offset)
321 {
322 	struct rt2x00debug_intf *intf = file->private_data;
323 	struct data_queue *queue;
324 	unsigned long irqflags;
325 	unsigned int lines = 1 + intf->rt2x00dev->data_queues;
326 	size_t size;
327 	char *data;
328 	char *temp;
329 
330 	if (*offset)
331 		return 0;
332 
333 	data = kcalloc(lines, MAX_LINE_LENGTH, GFP_KERNEL);
334 	if (!data)
335 		return -ENOMEM;
336 
337 	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 		temp += sprintf(temp, "%d\t0x%.8x\t%d\t%d\t%d\t%d\t%d\t\t%d\n",
344 				queue->qid, (unsigned int)queue->flags,
345 				queue->count, queue->limit, queue->length,
346 				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 	size = strlen(data);
354 	size = min(size, length);
355 
356 	if (copy_to_user(buf, data, size)) {
357 		kfree(data);
358 		return -EFAULT;
359 	}
360 
361 	kfree(data);
362 
363 	*offset += size;
364 	return size;
365 }
366 
367 static const struct file_operations rt2x00debug_fop_queue_stats = {
368 	.owner		= THIS_MODULE,
369 	.read		= rt2x00debug_read_queue_stats,
370 	.open		= rt2x00debug_file_open,
371 	.release	= rt2x00debug_file_release,
372 	.llseek		= default_llseek,
373 };
374 
375 #ifdef CONFIG_RT2X00_LIB_CRYPTO
376 static ssize_t rt2x00debug_read_crypto_stats(struct file *file,
377 					     char __user *buf,
378 					     size_t length,
379 					     loff_t *offset)
380 {
381 	struct rt2x00debug_intf *intf = file->private_data;
382 	static const char * const name[] = { "WEP64", "WEP128", "TKIP", "AES" };
383 	char *data;
384 	char *temp;
385 	size_t size;
386 	unsigned int i;
387 
388 	if (*offset)
389 		return 0;
390 
391 	data = kcalloc(1 + CIPHER_MAX, MAX_LINE_LENGTH, GFP_KERNEL);
392 	if (!data)
393 		return -ENOMEM;
394 
395 	temp = data;
396 	temp += sprintf(data, "cipher\tsuccess\ticv err\tmic err\tkey err\n");
397 
398 	for (i = 0; i < CIPHER_MAX; i++) {
399 		temp += sprintf(temp, "%s\t%lu\t%lu\t%lu\t%lu\n", name[i],
400 				intf->crypto_stats[i].success,
401 				intf->crypto_stats[i].icv_error,
402 				intf->crypto_stats[i].mic_error,
403 				intf->crypto_stats[i].key_error);
404 	}
405 
406 	size = strlen(data);
407 	size = min(size, length);
408 
409 	if (copy_to_user(buf, data, size)) {
410 		kfree(data);
411 		return -EFAULT;
412 	}
413 
414 	kfree(data);
415 
416 	*offset += size;
417 	return size;
418 }
419 
420 static const struct file_operations rt2x00debug_fop_crypto_stats = {
421 	.owner		= THIS_MODULE,
422 	.read		= rt2x00debug_read_crypto_stats,
423 	.open		= rt2x00debug_file_open,
424 	.release	= rt2x00debug_file_release,
425 	.llseek		= default_llseek,
426 };
427 #endif
428 
429 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type)	\
430 static ssize_t rt2x00debug_read_##__name(struct file *file,	\
431 					 char __user *buf,	\
432 					 size_t length,		\
433 					 loff_t *offset)	\
434 {								\
435 	struct rt2x00debug_intf *intf = file->private_data;	\
436 	const struct rt2x00debug *debug = intf->debug;		\
437 	char line[16];						\
438 	size_t size;						\
439 	unsigned int index = intf->offset_##__name;		\
440 	__type value;						\
441 								\
442 	if (*offset)						\
443 		return 0;					\
444 								\
445 	if (index >= debug->__name.word_count)			\
446 		return -EINVAL;					\
447 								\
448 	index += (debug->__name.word_base /			\
449 		  debug->__name.word_size);			\
450 								\
451 	if (debug->__name.flags & RT2X00DEBUGFS_OFFSET)		\
452 		index *= debug->__name.word_size;		\
453 								\
454 	value = debug->__name.read(intf->rt2x00dev, index);	\
455 								\
456 	size = sprintf(line, __format, value);			\
457 								\
458 	return simple_read_from_buffer(buf, length, offset, line, size); \
459 }
460 
461 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type)			\
462 static ssize_t rt2x00debug_write_##__name(struct file *file,	\
463 					  const char __user *buf,\
464 					  size_t length,	\
465 					  loff_t *offset)	\
466 {								\
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 	__type value;						\
473 								\
474 	if (*offset)						\
475 		return 0;					\
476 								\
477 	if (index >= debug->__name.word_count)			\
478 		return -EINVAL;					\
479 								\
480 	if (length > sizeof(line))				\
481 		return -EINVAL;					\
482 								\
483 	if (copy_from_user(line, buf, length))			\
484 		return -EFAULT;					\
485 	line[16] = 0;						\
486 						\
487 	size = strlen(line);					\
488 	value = simple_strtoul(line, NULL, 0);			\
489 								\
490 	index += (debug->__name.word_base /			\
491 		  debug->__name.word_size);			\
492 								\
493 	if (debug->__name.flags & RT2X00DEBUGFS_OFFSET)		\
494 		index *= debug->__name.word_size;		\
495 								\
496 	debug->__name.write(intf->rt2x00dev, index, value);	\
497 								\
498 	*offset += size;					\
499 	return size;						\
500 }
501 
502 #define RT2X00DEBUGFS_OPS(__name, __format, __type)		\
503 RT2X00DEBUGFS_OPS_READ(__name, __format, __type);		\
504 RT2X00DEBUGFS_OPS_WRITE(__name, __type);			\
505 								\
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,		\
512 	.llseek		= generic_file_llseek,			\
513 };
514 
515 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
516 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
517 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
518 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
519 RT2X00DEBUGFS_OPS(rfcsr, "0x%.2x\n", u8);
520 
521 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
522 					  char __user *buf,
523 					  size_t length,
524 					  loff_t *offset)
525 {
526 	struct rt2x00debug_intf *intf =	file->private_data;
527 	char line[16];
528 	size_t size;
529 
530 	if (*offset)
531 		return 0;
532 
533 	size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
534 
535 	return simple_read_from_buffer(buf, length, offset, line, size);
536 }
537 
538 static const struct file_operations rt2x00debug_fop_dev_flags = {
539 	.owner		= THIS_MODULE,
540 	.read		= rt2x00debug_read_dev_flags,
541 	.open		= rt2x00debug_file_open,
542 	.release	= rt2x00debug_file_release,
543 	.llseek		= default_llseek,
544 };
545 
546 static ssize_t rt2x00debug_read_cap_flags(struct file *file,
547 					  char __user *buf,
548 					  size_t length,
549 					  loff_t *offset)
550 {
551 	struct rt2x00debug_intf *intf =	file->private_data;
552 	char line[16];
553 	size_t size;
554 
555 	if (*offset)
556 		return 0;
557 
558 	size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->cap_flags);
559 
560 	return simple_read_from_buffer(buf, length, offset, line, size);
561 }
562 
563 static const struct file_operations rt2x00debug_fop_cap_flags = {
564 	.owner		= THIS_MODULE,
565 	.read		= rt2x00debug_read_cap_flags,
566 	.open		= rt2x00debug_file_open,
567 	.release	= rt2x00debug_file_release,
568 	.llseek		= default_llseek,
569 };
570 
571 static ssize_t rt2x00debug_write_restart_hw(struct file *file,
572 					    const char __user *buf,
573 					    size_t length,
574 					    loff_t *offset)
575 {
576 	struct rt2x00debug_intf *intf =	file->private_data;
577 	struct rt2x00_dev *rt2x00dev = intf->rt2x00dev;
578 	static unsigned long last_reset;
579 
580 	if (!rt2x00_has_cap_restart_hw(rt2x00dev))
581 		return -EOPNOTSUPP;
582 
583 	if (time_before(jiffies, last_reset + msecs_to_jiffies(2000)))
584 		return -EBUSY;
585 
586 	last_reset = jiffies;
587 
588 	ieee80211_restart_hw(rt2x00dev->hw);
589 	return length;
590 }
591 
592 static const struct file_operations rt2x00debug_restart_hw = {
593 	.owner = THIS_MODULE,
594 	.write = rt2x00debug_write_restart_hw,
595 	.open = simple_open,
596 	.llseek = generic_file_llseek,
597 };
598 
599 static struct dentry *rt2x00debug_create_file_driver(const char *name,
600 						     struct rt2x00debug_intf
601 						     *intf,
602 						     struct debugfs_blob_wrapper
603 						     *blob)
604 {
605 	char *data;
606 
607 	data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
608 	if (!data)
609 		return NULL;
610 
611 	blob->data = data;
612 	data += sprintf(data, "driver:\t%s\n", intf->rt2x00dev->ops->name);
613 	data += sprintf(data, "version:\t%s\n", DRV_VERSION);
614 	blob->size = strlen(blob->data);
615 
616 	return debugfs_create_blob(name, 0400, intf->driver_folder, blob);
617 }
618 
619 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
620 						      struct rt2x00debug_intf
621 						      *intf,
622 						      struct
623 						      debugfs_blob_wrapper
624 						      *blob)
625 {
626 	const struct rt2x00debug *debug = intf->debug;
627 	char *data;
628 
629 	data = kzalloc(9 * MAX_LINE_LENGTH, GFP_KERNEL);
630 	if (!data)
631 		return NULL;
632 
633 	blob->data = data;
634 	data += sprintf(data, "rt chip:\t%04x\n", intf->rt2x00dev->chip.rt);
635 	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 #define RT2X00DEBUGFS_SPRINTF_REGISTER(__name)			\
640 {								\
641 	if (debug->__name.read)					\
642 		data += sprintf(data, __stringify(__name)	\
643 				"\t%d\t%d\t%d\n",		\
644 				debug->__name.word_base,	\
645 				debug->__name.word_count,	\
646 				debug->__name.word_size);	\
647 }
648 	RT2X00DEBUGFS_SPRINTF_REGISTER(csr);
649 	RT2X00DEBUGFS_SPRINTF_REGISTER(eeprom);
650 	RT2X00DEBUGFS_SPRINTF_REGISTER(bbp);
651 	RT2X00DEBUGFS_SPRINTF_REGISTER(rf);
652 	RT2X00DEBUGFS_SPRINTF_REGISTER(rfcsr);
653 #undef RT2X00DEBUGFS_SPRINTF_REGISTER
654 
655 	blob->size = strlen(blob->data);
656 
657 	return debugfs_create_blob(name, 0400, intf->driver_folder, blob);
658 }
659 
660 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
661 {
662 	const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
663 	struct rt2x00debug_intf *intf;
664 
665 	intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
666 	if (!intf) {
667 		rt2x00_err(rt2x00dev, "Failed to allocate debug handler\n");
668 		return;
669 	}
670 
671 	intf->debug = debug;
672 	intf->rt2x00dev = rt2x00dev;
673 	rt2x00dev->debugfs_intf = intf;
674 
675 	intf->driver_folder =
676 	    debugfs_create_dir(intf->rt2x00dev->ops->name,
677 			       rt2x00dev->hw->wiphy->debugfsdir);
678 
679 	intf->driver_entry =
680 	    rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
681 
682 	intf->chipset_entry =
683 	    rt2x00debug_create_file_chipset("chipset",
684 					    intf, &intf->chipset_blob);
685 
686 	intf->dev_flags = debugfs_create_file("dev_flags", 0400,
687 					      intf->driver_folder, intf,
688 					      &rt2x00debug_fop_dev_flags);
689 
690 	intf->cap_flags = debugfs_create_file("cap_flags", 0400,
691 					      intf->driver_folder, intf,
692 					      &rt2x00debug_fop_cap_flags);
693 
694 	intf->restart_hw = debugfs_create_file("restart_hw", 0200,
695 					       intf->driver_folder, intf,
696 					       &rt2x00debug_restart_hw);
697 
698 	intf->register_folder =
699 	    debugfs_create_dir("register", intf->driver_folder);
700 
701 #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name)		\
702 ({									\
703 	if (debug->__name.read) {					\
704 		(__intf)->__name##_off_entry =				\
705 			debugfs_create_u32(__stringify(__name) "_offset", \
706 					   0600,			\
707 					   (__intf)->register_folder,	\
708 					   &(__intf)->offset_##__name);	\
709 									\
710 		(__intf)->__name##_val_entry =				\
711 			debugfs_create_file(__stringify(__name) "_value", \
712 					    0600,			\
713 					    (__intf)->register_folder,	\
714 					    (__intf),			\
715 					    &rt2x00debug_fop_##__name); \
716 	}								\
717 })
718 
719 	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
720 	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
721 	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
722 	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
723 	RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rfcsr);
724 
725 #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
726 
727 	intf->queue_folder =
728 	    debugfs_create_dir("queue", intf->driver_folder);
729 
730 	intf->queue_frame_dump_entry =
731 		debugfs_create_file("dump", 0400, intf->queue_folder,
732 				    intf, &rt2x00debug_fop_queue_dump);
733 
734 	skb_queue_head_init(&intf->frame_dump_skbqueue);
735 	init_waitqueue_head(&intf->frame_dump_waitqueue);
736 
737 	intf->queue_stats_entry =
738 		debugfs_create_file("queue", 0400, intf->queue_folder,
739 				    intf, &rt2x00debug_fop_queue_stats);
740 
741 #ifdef CONFIG_RT2X00_LIB_CRYPTO
742 	if (rt2x00_has_cap_hw_crypto(rt2x00dev))
743 		intf->crypto_stats_entry =
744 			debugfs_create_file("crypto", 0444, intf->queue_folder,
745 					    intf,
746 					    &rt2x00debug_fop_crypto_stats);
747 #endif
748 
749 	return;
750 }
751 
752 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
753 {
754 	struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
755 
756 	if (unlikely(!intf))
757 		return;
758 
759 	skb_queue_purge(&intf->frame_dump_skbqueue);
760 
761 #ifdef CONFIG_RT2X00_LIB_CRYPTO
762 	debugfs_remove(intf->crypto_stats_entry);
763 #endif
764 	debugfs_remove(intf->queue_stats_entry);
765 	debugfs_remove(intf->queue_frame_dump_entry);
766 	debugfs_remove(intf->queue_folder);
767 	debugfs_remove(intf->rfcsr_val_entry);
768 	debugfs_remove(intf->rfcsr_off_entry);
769 	debugfs_remove(intf->rf_val_entry);
770 	debugfs_remove(intf->rf_off_entry);
771 	debugfs_remove(intf->bbp_val_entry);
772 	debugfs_remove(intf->bbp_off_entry);
773 	debugfs_remove(intf->eeprom_val_entry);
774 	debugfs_remove(intf->eeprom_off_entry);
775 	debugfs_remove(intf->csr_val_entry);
776 	debugfs_remove(intf->csr_off_entry);
777 	debugfs_remove(intf->register_folder);
778 	debugfs_remove(intf->dev_flags);
779 	debugfs_remove(intf->restart_hw);
780 	debugfs_remove(intf->cap_flags);
781 	debugfs_remove(intf->chipset_entry);
782 	debugfs_remove(intf->driver_entry);
783 	debugfs_remove(intf->driver_folder);
784 	kfree(intf->chipset_blob.data);
785 	kfree(intf->driver_blob.data);
786 	kfree(intf);
787 
788 	rt2x00dev->debugfs_intf = NULL;
789 }
790