blob: fe7a7f63a9edc2cf75c0a2caa2f6b8ffe02234aa [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01002 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <http://rt2x00.serialmonkey.com>
4
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.
9
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.
14
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.
19 */
20
21/*
22 Module: rt2x00lib
23 Abstract: rt2x00 debugfs specific routines.
24 */
25
Ivo van Doorn95ea3622007-09-25 17:57:13 -070026#include <linux/debugfs.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010029#include <linux/poll.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040030#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070032#include <linux/uaccess.h>
33
34#include "rt2x00.h"
35#include "rt2x00lib.h"
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010036#include "rt2x00dump.h"
Ivo van Doorn95ea3622007-09-25 17:57:13 -070037
Ivo van Doorn68598d292008-02-10 22:50:28 +010038#define MAX_LINE_LENGTH 64
Ivo van Doorn95ea3622007-09-25 17:57:13 -070039
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020040struct rt2x00debug_crypto {
41 unsigned long success;
42 unsigned long icv_error;
43 unsigned long mic_error;
44 unsigned long key_error;
45};
46
Ivo van Doorn95ea3622007-09-25 17:57:13 -070047struct rt2x00debug_intf {
48 /*
49 * Pointer to driver structure where
50 * this debugfs entry belongs to.
51 */
52 struct rt2x00_dev *rt2x00dev;
53
54 /*
55 * Reference to the rt2x00debug structure
56 * which can be used to communicate with
57 * the registers.
58 */
59 const struct rt2x00debug *debug;
60
61 /*
62 * Debugfs entries for:
63 * - driver folder
Ivo van Doorn91921a42007-11-27 21:48:16 +010064 * - driver file
65 * - chipset file
Ivo van Doorn7dab73b2011-04-18 15:27:06 +020066 * - device state flags file
67 * - device capability flags file
Ivo van Doorn91921a42007-11-27 21:48:16 +010068 * - register folder
69 * - csr offset/value files
70 * - eeprom offset/value files
71 * - bbp offset/value files
72 * - rf offset/value files
Anisse Astierf2bd7f12012-04-19 15:53:10 +020073 * - rfcsr offset/value files
Ivo van Doorn68598d292008-02-10 22:50:28 +010074 * - queue folder
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010075 * - frame dump file
Ivo van Doorn68598d292008-02-10 22:50:28 +010076 * - queue stats file
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020077 * - crypto stats file
Ivo van Doorn95ea3622007-09-25 17:57:13 -070078 */
79 struct dentry *driver_folder;
80 struct dentry *driver_entry;
81 struct dentry *chipset_entry;
Ivo van Doorn643b2522007-09-25 20:13:51 +020082 struct dentry *dev_flags;
Ivo van Doorn7dab73b2011-04-18 15:27:06 +020083 struct dentry *cap_flags;
Ivo van Doorn91921a42007-11-27 21:48:16 +010084 struct dentry *register_folder;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070085 struct dentry *csr_off_entry;
86 struct dentry *csr_val_entry;
87 struct dentry *eeprom_off_entry;
88 struct dentry *eeprom_val_entry;
89 struct dentry *bbp_off_entry;
90 struct dentry *bbp_val_entry;
91 struct dentry *rf_off_entry;
92 struct dentry *rf_val_entry;
Anisse Astierf2bd7f12012-04-19 15:53:10 +020093 struct dentry *rfcsr_off_entry;
94 struct dentry *rfcsr_val_entry;
Ivo van Doorn68598d292008-02-10 22:50:28 +010095 struct dentry *queue_folder;
96 struct dentry *queue_frame_dump_entry;
97 struct dentry *queue_stats_entry;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020098 struct dentry *crypto_stats_entry;
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010099
100 /*
101 * The frame dump file only allows a single reader,
102 * so we need to store the current state here.
103 */
104 unsigned long frame_dump_flags;
105#define FRAME_DUMP_FILE_OPEN 1
106
107 /*
108 * We queue each frame before dumping it to the user,
109 * per read command we will pass a single skb structure
110 * so we should be prepared to queue multiple sk buffers
111 * before sending it to userspace.
112 */
113 struct sk_buff_head frame_dump_skbqueue;
114 wait_queue_head_t frame_dump_waitqueue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700115
116 /*
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200117 * HW crypto statistics.
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800118 * All statistics are stored separately per cipher type.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200119 */
120 struct rt2x00debug_crypto crypto_stats[CIPHER_MAX];
121
122 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700123 * Driver and chipset files will use a data buffer
124 * that has been created in advance. This will simplify
125 * the code since we can use the debugfs functions.
126 */
127 struct debugfs_blob_wrapper driver_blob;
128 struct debugfs_blob_wrapper chipset_blob;
129
130 /*
131 * Requested offset for each register type.
132 */
133 unsigned int offset_csr;
134 unsigned int offset_eeprom;
135 unsigned int offset_bbp;
136 unsigned int offset_rf;
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200137 unsigned int offset_rfcsr;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700138};
139
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200140void rt2x00debug_update_crypto(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn84e31962008-12-20 10:53:29 +0100141 struct rxdone_entry_desc *rxdesc)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200142{
143 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100144 enum cipher cipher = rxdesc->cipher;
145 enum rx_crypto status = rxdesc->cipher_status;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200146
147 if (cipher == CIPHER_TKIP_NO_MIC)
148 cipher = CIPHER_TKIP;
Roel Kluina6c67332009-05-20 02:12:56 +0200149 if (cipher == CIPHER_NONE || cipher >= CIPHER_MAX)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200150 return;
151
152 /* Remove CIPHER_NONE index */
153 cipher--;
154
155 intf->crypto_stats[cipher].success += (status == RX_CRYPTO_SUCCESS);
156 intf->crypto_stats[cipher].icv_error += (status == RX_CRYPTO_FAIL_ICV);
157 intf->crypto_stats[cipher].mic_error += (status == RX_CRYPTO_FAIL_MIC);
158 intf->crypto_stats[cipher].key_error += (status == RX_CRYPTO_FAIL_KEY);
159}
160
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100161void rt2x00debug_dump_frame(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn5a6e5992008-05-10 13:41:32 +0200162 enum rt2x00_dump_type type, struct sk_buff *skb)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100163{
164 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
Gertjan van Wingerde878f7042010-05-11 23:51:37 +0200165 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100166 struct sk_buff *skbcopy;
167 struct rt2x00dump_hdr *dump_hdr;
168 struct timeval timestamp;
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200169 u32 data_len;
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100170
Helmut Schaae2f8c872010-11-04 20:38:56 +0100171 if (likely(!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)))
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100172 return;
173
Helmut Schaae2f8c872010-11-04 20:38:56 +0100174 do_gettimeofday(&timestamp);
175
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100176 if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700177 rt2x00_dbg(rt2x00dev, "txrx dump queue length exceeded\n");
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100178 return;
179 }
180
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200181 data_len = skb->len;
182 if (skbdesc->flags & SKBDESC_DESC_IN_SKB)
183 data_len -= skbdesc->desc_len;
184
185 skbcopy = alloc_skb(sizeof(*dump_hdr) + skbdesc->desc_len + data_len,
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100186 GFP_ATOMIC);
187 if (!skbcopy) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700188 rt2x00_dbg(rt2x00dev, "Failed to copy skb for dump\n");
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100189 return;
190 }
191
192 dump_hdr = (struct rt2x00dump_hdr *)skb_put(skbcopy, sizeof(*dump_hdr));
193 dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
194 dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
Gertjan van Wingerde878f7042010-05-11 23:51:37 +0200195 dump_hdr->desc_length = cpu_to_le32(skbdesc->desc_len);
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200196 dump_hdr->data_length = cpu_to_le32(data_len);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100197 dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
198 dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +0100199 dump_hdr->chip_rev = cpu_to_le16(rt2x00dev->chip.rev);
Ivo van Doorn5a6e5992008-05-10 13:41:32 +0200200 dump_hdr->type = cpu_to_le16(type);
Gertjan van Wingerde878f7042010-05-11 23:51:37 +0200201 dump_hdr->queue_index = skbdesc->entry->queue->qid;
202 dump_hdr->entry_index = skbdesc->entry->entry_idx;
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100203 dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
204 dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_usec);
205
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200206 if (!(skbdesc->flags & SKBDESC_DESC_IN_SKB))
207 memcpy(skb_put(skbcopy, skbdesc->desc_len), skbdesc->desc,
208 skbdesc->desc_len);
Gertjan van Wingerded56d4532008-06-06 22:54:08 +0200209 memcpy(skb_put(skbcopy, skb->len), skb->data, skb->len);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100210
211 skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
212 wake_up_interruptible(&intf->frame_dump_waitqueue);
213
214 /*
215 * Verify that the file has not been closed while we were working.
216 */
217 if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
218 skb_queue_purge(&intf->frame_dump_skbqueue);
219}
Gertjan van Wingerdeb4df4702010-05-13 13:16:36 +0200220EXPORT_SYMBOL_GPL(rt2x00debug_dump_frame);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100221
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700222static int rt2x00debug_file_open(struct inode *inode, struct file *file)
223{
224 struct rt2x00debug_intf *intf = inode->i_private;
225
226 file->private_data = inode->i_private;
227
228 if (!try_module_get(intf->debug->owner))
229 return -EBUSY;
230
231 return 0;
232}
233
234static int rt2x00debug_file_release(struct inode *inode, struct file *file)
235{
236 struct rt2x00debug_intf *intf = file->private_data;
237
238 module_put(intf->debug->owner);
239
240 return 0;
241}
242
Ivo van Doorn181d6902008-02-05 16:42:23 -0500243static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100244{
245 struct rt2x00debug_intf *intf = inode->i_private;
246 int retval;
247
248 retval = rt2x00debug_file_open(inode, file);
249 if (retval)
250 return retval;
251
252 if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
253 rt2x00debug_file_release(inode, file);
254 return -EBUSY;
255 }
256
257 return 0;
258}
259
Ivo van Doorn181d6902008-02-05 16:42:23 -0500260static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100261{
262 struct rt2x00debug_intf *intf = inode->i_private;
263
264 skb_queue_purge(&intf->frame_dump_skbqueue);
265
266 clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
267
268 return rt2x00debug_file_release(inode, file);
269}
270
Ivo van Doorn181d6902008-02-05 16:42:23 -0500271static ssize_t rt2x00debug_read_queue_dump(struct file *file,
272 char __user *buf,
273 size_t length,
274 loff_t *offset)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100275{
276 struct rt2x00debug_intf *intf = file->private_data;
277 struct sk_buff *skb;
278 size_t status;
279 int retval;
280
281 if (file->f_flags & O_NONBLOCK)
282 return -EAGAIN;
283
284 retval =
285 wait_event_interruptible(intf->frame_dump_waitqueue,
286 (skb =
287 skb_dequeue(&intf->frame_dump_skbqueue)));
288 if (retval)
289 return retval;
290
291 status = min((size_t)skb->len, length);
292 if (copy_to_user(buf, skb->data, status)) {
293 status = -EFAULT;
294 goto exit;
295 }
296
297 *offset += status;
298
299exit:
300 kfree_skb(skb);
301
302 return status;
303}
304
Ivo van Doorn181d6902008-02-05 16:42:23 -0500305static unsigned int rt2x00debug_poll_queue_dump(struct file *file,
John Daiker55887512008-10-17 12:16:17 -0700306 poll_table *wait)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100307{
308 struct rt2x00debug_intf *intf = file->private_data;
309
310 poll_wait(file, &intf->frame_dump_waitqueue, wait);
311
312 if (!skb_queue_empty(&intf->frame_dump_skbqueue))
313 return POLLOUT | POLLWRNORM;
314
315 return 0;
316}
317
Ivo van Doorn181d6902008-02-05 16:42:23 -0500318static const struct file_operations rt2x00debug_fop_queue_dump = {
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100319 .owner = THIS_MODULE,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500320 .read = rt2x00debug_read_queue_dump,
321 .poll = rt2x00debug_poll_queue_dump,
322 .open = rt2x00debug_open_queue_dump,
323 .release = rt2x00debug_release_queue_dump,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200324 .llseek = default_llseek,
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100325};
326
Ivo van Doorn68598d292008-02-10 22:50:28 +0100327static ssize_t rt2x00debug_read_queue_stats(struct file *file,
328 char __user *buf,
329 size_t length,
330 loff_t *offset)
331{
332 struct rt2x00debug_intf *intf = file->private_data;
333 struct data_queue *queue;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100334 unsigned long irqflags;
Ivo van Doorn68598d292008-02-10 22:50:28 +0100335 unsigned int lines = 1 + intf->rt2x00dev->data_queues;
336 size_t size;
337 char *data;
338 char *temp;
339
340 if (*offset)
341 return 0;
342
Joe Perchesbaeb2ff2010-08-11 07:02:48 +0000343 data = kcalloc(lines, MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100344 if (!data)
345 return -ENOMEM;
346
347 temp = data +
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100348 sprintf(data, "qid\tflags\t\tcount\tlimit\tlength\tindex\tdma done\tdone\n");
Ivo van Doorn68598d292008-02-10 22:50:28 +0100349
350 queue_for_each(intf->rt2x00dev, queue) {
Ivo van Doorn813f0332010-11-06 15:48:05 +0100351 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100352
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100353 temp += sprintf(temp, "%d\t0x%.8x\t%d\t%d\t%d\t%d\t%d\t\t%d\n",
354 queue->qid, (unsigned int)queue->flags,
Ivo van Doorn68598d292008-02-10 22:50:28 +0100355 queue->count, queue->limit, queue->length,
356 queue->index[Q_INDEX],
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200357 queue->index[Q_INDEX_DMA_DONE],
Ivo van Doorn54e34fb2010-08-23 19:54:41 +0200358 queue->index[Q_INDEX_DONE]);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100359
Ivo van Doorn813f0332010-11-06 15:48:05 +0100360 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100361 }
362
363 size = strlen(data);
364 size = min(size, length);
365
366 if (copy_to_user(buf, data, size)) {
367 kfree(data);
368 return -EFAULT;
369 }
370
371 kfree(data);
372
373 *offset += size;
374 return size;
375}
376
377static const struct file_operations rt2x00debug_fop_queue_stats = {
378 .owner = THIS_MODULE,
379 .read = rt2x00debug_read_queue_stats,
380 .open = rt2x00debug_file_open,
381 .release = rt2x00debug_file_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200382 .llseek = default_llseek,
Ivo van Doorn68598d292008-02-10 22:50:28 +0100383};
384
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200385#ifdef CONFIG_RT2X00_LIB_CRYPTO
386static ssize_t rt2x00debug_read_crypto_stats(struct file *file,
387 char __user *buf,
388 size_t length,
389 loff_t *offset)
390{
391 struct rt2x00debug_intf *intf = file->private_data;
Joe Perches030bda02010-09-13 18:23:55 +0000392 static const char * const name[] = { "WEP64", "WEP128", "TKIP", "AES" };
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200393 char *data;
394 char *temp;
395 size_t size;
396 unsigned int i;
397
398 if (*offset)
399 return 0;
400
John Daiker55887512008-10-17 12:16:17 -0700401 data = kzalloc((1 + CIPHER_MAX) * MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200402 if (!data)
403 return -ENOMEM;
404
405 temp = data;
406 temp += sprintf(data, "cipher\tsuccess\ticv err\tmic err\tkey err\n");
407
408 for (i = 0; i < CIPHER_MAX; i++) {
409 temp += sprintf(temp, "%s\t%lu\t%lu\t%lu\t%lu\n", name[i],
410 intf->crypto_stats[i].success,
411 intf->crypto_stats[i].icv_error,
412 intf->crypto_stats[i].mic_error,
413 intf->crypto_stats[i].key_error);
414 }
415
416 size = strlen(data);
417 size = min(size, length);
418
419 if (copy_to_user(buf, data, size)) {
420 kfree(data);
421 return -EFAULT;
422 }
423
424 kfree(data);
425
426 *offset += size;
427 return size;
428}
429
430static const struct file_operations rt2x00debug_fop_crypto_stats = {
431 .owner = THIS_MODULE,
432 .read = rt2x00debug_read_crypto_stats,
433 .open = rt2x00debug_file_open,
434 .release = rt2x00debug_file_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200435 .llseek = default_llseek,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200436};
437#endif
438
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700439#define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
440static ssize_t rt2x00debug_read_##__name(struct file *file, \
441 char __user *buf, \
442 size_t length, \
443 loff_t *offset) \
444{ \
Ivo van Doorn91921a42007-11-27 21:48:16 +0100445 struct rt2x00debug_intf *intf = file->private_data; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700446 const struct rt2x00debug *debug = intf->debug; \
447 char line[16]; \
448 size_t size; \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100449 unsigned int index = intf->offset_##__name; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700450 __type value; \
451 \
452 if (*offset) \
453 return 0; \
454 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100455 if (index >= debug->__name.word_count) \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700456 return -EINVAL; \
457 \
Ivo van Doornabd2fdb2009-02-17 14:04:29 +0100458 index += (debug->__name.word_base / \
459 debug->__name.word_size); \
460 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100461 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
462 index *= debug->__name.word_size; \
463 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100464 debug->__name.read(intf->rt2x00dev, index, &value); \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700465 \
466 size = sprintf(line, __format, value); \
467 \
468 if (copy_to_user(buf, line, size)) \
469 return -EFAULT; \
470 \
471 *offset += size; \
472 return size; \
473}
474
475#define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
476static ssize_t rt2x00debug_write_##__name(struct file *file, \
477 const char __user *buf,\
478 size_t length, \
479 loff_t *offset) \
480{ \
Ivo van Doorn91921a42007-11-27 21:48:16 +0100481 struct rt2x00debug_intf *intf = file->private_data; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700482 const struct rt2x00debug *debug = intf->debug; \
483 char line[16]; \
484 size_t size; \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100485 unsigned int index = intf->offset_##__name; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700486 __type value; \
487 \
488 if (*offset) \
489 return 0; \
490 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100491 if (index >= debug->__name.word_count) \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700492 return -EINVAL; \
493 \
Arnaud Patard (Rtp)f8d8b7a2010-08-23 23:02:22 +0200494 if (length > sizeof(line)) \
495 return -EINVAL; \
496 \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700497 if (copy_from_user(line, buf, length)) \
498 return -EFAULT; \
499 \
500 size = strlen(line); \
501 value = simple_strtoul(line, NULL, 0); \
502 \
Ivo van Doornabd2fdb2009-02-17 14:04:29 +0100503 index += (debug->__name.word_base / \
504 debug->__name.word_size); \
505 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100506 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
507 index *= debug->__name.word_size; \
508 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100509 debug->__name.write(intf->rt2x00dev, index, value); \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700510 \
511 *offset += size; \
512 return size; \
513}
514
515#define RT2X00DEBUGFS_OPS(__name, __format, __type) \
516RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
517RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
518 \
519static const struct file_operations rt2x00debug_fop_##__name = {\
520 .owner = THIS_MODULE, \
521 .read = rt2x00debug_read_##__name, \
522 .write = rt2x00debug_write_##__name, \
523 .open = rt2x00debug_file_open, \
524 .release = rt2x00debug_file_release, \
Arnd Bergmann2b18ab362010-07-06 19:05:31 +0200525 .llseek = generic_file_llseek, \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700526};
527
528RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
529RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
530RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
531RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200532RT2X00DEBUGFS_OPS(rfcsr, "0x%.2x\n", u8);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700533
Ivo van Doorn643b2522007-09-25 20:13:51 +0200534static ssize_t rt2x00debug_read_dev_flags(struct file *file,
535 char __user *buf,
536 size_t length,
537 loff_t *offset)
538{
539 struct rt2x00debug_intf *intf = file->private_data;
540 char line[16];
541 size_t size;
542
543 if (*offset)
544 return 0;
545
546 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
547
548 if (copy_to_user(buf, line, size))
549 return -EFAULT;
550
551 *offset += size;
552 return size;
553}
554
555static const struct file_operations rt2x00debug_fop_dev_flags = {
556 .owner = THIS_MODULE,
557 .read = rt2x00debug_read_dev_flags,
558 .open = rt2x00debug_file_open,
559 .release = rt2x00debug_file_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200560 .llseek = default_llseek,
Ivo van Doorn643b2522007-09-25 20:13:51 +0200561};
562
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200563static ssize_t rt2x00debug_read_cap_flags(struct file *file,
564 char __user *buf,
565 size_t length,
566 loff_t *offset)
567{
568 struct rt2x00debug_intf *intf = file->private_data;
569 char line[16];
570 size_t size;
571
572 if (*offset)
573 return 0;
574
575 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->cap_flags);
576
577 if (copy_to_user(buf, line, size))
578 return -EFAULT;
579
580 *offset += size;
581 return size;
582}
583
584static const struct file_operations rt2x00debug_fop_cap_flags = {
585 .owner = THIS_MODULE,
586 .read = rt2x00debug_read_cap_flags,
587 .open = rt2x00debug_file_open,
588 .release = rt2x00debug_file_release,
589 .llseek = default_llseek,
590};
591
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700592static struct dentry *rt2x00debug_create_file_driver(const char *name,
593 struct rt2x00debug_intf
594 *intf,
595 struct debugfs_blob_wrapper
596 *blob)
597{
598 char *data;
599
Ivo van Doorn68598d292008-02-10 22:50:28 +0100600 data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700601 if (!data)
602 return NULL;
603
604 blob->data = data;
Ivo van Doorn860559f2009-03-03 18:14:18 +0100605 data += sprintf(data, "driver:\t%s\n", intf->rt2x00dev->ops->name);
606 data += sprintf(data, "version:\t%s\n", DRV_VERSION);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700607 blob->size = strlen(blob->data);
608
Ivo van Doornd2b69072008-07-27 15:06:05 +0200609 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700610}
611
612static struct dentry *rt2x00debug_create_file_chipset(const char *name,
613 struct rt2x00debug_intf
614 *intf,
615 struct
616 debugfs_blob_wrapper
617 *blob)
618{
619 const struct rt2x00debug *debug = intf->debug;
620 char *data;
621
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200622 data = kzalloc(9 * MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700623 if (!data)
624 return NULL;
625
Ivo van Doorn3e34c6d2008-01-06 23:38:10 +0100626 blob->data = data;
Ivo van Doorn860559f2009-03-03 18:14:18 +0100627 data += sprintf(data, "rt chip:\t%04x\n", intf->rt2x00dev->chip.rt);
628 data += sprintf(data, "rf chip:\t%04x\n", intf->rt2x00dev->chip.rf);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +0100629 data += sprintf(data, "revision:\t%04x\n", intf->rt2x00dev->chip.rev);
Ivo van Doorn22c96c22007-11-27 21:48:37 +0100630 data += sprintf(data, "\n");
Ivo van Doorn860559f2009-03-03 18:14:18 +0100631 data += sprintf(data, "register\tbase\twords\twordsize\n");
Anisse Astierf2efd202012-04-19 15:04:52 +0200632#define RT2X00DEBUGFS_SPRINTF_REGISTER(__name) \
633{ \
634 if(debug->__name.read) \
635 data += sprintf(data, __stringify(__name) \
636 "\t%d\t%d\t%d\n", \
637 debug->__name.word_base, \
638 debug->__name.word_count, \
639 debug->__name.word_size); \
640}
641 RT2X00DEBUGFS_SPRINTF_REGISTER(csr);
642 RT2X00DEBUGFS_SPRINTF_REGISTER(eeprom);
643 RT2X00DEBUGFS_SPRINTF_REGISTER(bbp);
644 RT2X00DEBUGFS_SPRINTF_REGISTER(rf);
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200645 RT2X00DEBUGFS_SPRINTF_REGISTER(rfcsr);
Anisse Astierf2efd202012-04-19 15:04:52 +0200646#undef RT2X00DEBUGFS_SPRINTF_REGISTER
647
Ivo van Doorn3e34c6d2008-01-06 23:38:10 +0100648 blob->size = strlen(blob->data);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700649
Ivo van Doornd2b69072008-07-27 15:06:05 +0200650 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700651}
652
653void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
654{
655 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
656 struct rt2x00debug_intf *intf;
657
658 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
659 if (!intf) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700660 rt2x00_err(rt2x00dev, "Failed to allocate debug handler\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700661 return;
662 }
663
664 intf->debug = debug;
665 intf->rt2x00dev = rt2x00dev;
666 rt2x00dev->debugfs_intf = intf;
667
668 intf->driver_folder =
669 debugfs_create_dir(intf->rt2x00dev->ops->name,
670 rt2x00dev->hw->wiphy->debugfsdir);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800671 if (IS_ERR(intf->driver_folder) || !intf->driver_folder)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700672 goto exit;
673
674 intf->driver_entry =
675 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800676 if (IS_ERR(intf->driver_entry) || !intf->driver_entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700677 goto exit;
678
679 intf->chipset_entry =
680 rt2x00debug_create_file_chipset("chipset",
681 intf, &intf->chipset_blob);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800682 if (IS_ERR(intf->chipset_entry) || !intf->chipset_entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700683 goto exit;
684
Ivo van Doornd2b69072008-07-27 15:06:05 +0200685 intf->dev_flags = debugfs_create_file("dev_flags", S_IRUSR,
Ivo van Doorn643b2522007-09-25 20:13:51 +0200686 intf->driver_folder, intf,
687 &rt2x00debug_fop_dev_flags);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800688 if (IS_ERR(intf->dev_flags) || !intf->dev_flags)
Ivo van Doorn643b2522007-09-25 20:13:51 +0200689 goto exit;
690
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200691 intf->cap_flags = debugfs_create_file("cap_flags", S_IRUSR,
692 intf->driver_folder, intf,
693 &rt2x00debug_fop_cap_flags);
694 if (IS_ERR(intf->cap_flags) || !intf->cap_flags)
695 goto exit;
696
Ivo van Doorn91921a42007-11-27 21:48:16 +0100697 intf->register_folder =
698 debugfs_create_dir("register", intf->driver_folder);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800699 if (IS_ERR(intf->register_folder) || !intf->register_folder)
Ivo van Doorn91921a42007-11-27 21:48:16 +0100700 goto exit;
701
Anisse Astierf2efd202012-04-19 15:04:52 +0200702#define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
703({ \
704 if(debug->__name.read) { \
705 (__intf)->__name##_off_entry = \
706 debugfs_create_u32(__stringify(__name) "_offset", \
707 S_IRUSR | S_IWUSR, \
708 (__intf)->register_folder, \
709 &(__intf)->offset_##__name); \
710 if (IS_ERR((__intf)->__name##_off_entry) \
711 || !(__intf)->__name##_off_entry) \
712 goto exit; \
713 \
714 (__intf)->__name##_val_entry = \
715 debugfs_create_file(__stringify(__name) "_value", \
716 S_IRUSR | S_IWUSR, \
717 (__intf)->register_folder, \
718 (__intf), &rt2x00debug_fop_##__name); \
719 if (IS_ERR((__intf)->__name##_val_entry) \
720 || !(__intf)->__name##_val_entry) \
721 goto exit; \
722 } \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700723})
724
Ivo van Doorn91921a42007-11-27 21:48:16 +0100725 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
726 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
727 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
728 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200729 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rfcsr);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700730
Ivo van Doorn91921a42007-11-27 21:48:16 +0100731#undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700732
Ivo van Doorn68598d292008-02-10 22:50:28 +0100733 intf->queue_folder =
734 debugfs_create_dir("queue", intf->driver_folder);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800735 if (IS_ERR(intf->queue_folder) || !intf->queue_folder)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100736 goto exit;
737
Ivo van Doorn68598d292008-02-10 22:50:28 +0100738 intf->queue_frame_dump_entry =
Ivo van Doornd2b69072008-07-27 15:06:05 +0200739 debugfs_create_file("dump", S_IRUSR, intf->queue_folder,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500740 intf, &rt2x00debug_fop_queue_dump);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800741 if (IS_ERR(intf->queue_frame_dump_entry)
742 || !intf->queue_frame_dump_entry)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100743 goto exit;
744
745 skb_queue_head_init(&intf->frame_dump_skbqueue);
746 init_waitqueue_head(&intf->frame_dump_waitqueue);
747
Ivo van Doorn68598d292008-02-10 22:50:28 +0100748 intf->queue_stats_entry =
Ivo van Doornd2b69072008-07-27 15:06:05 +0200749 debugfs_create_file("queue", S_IRUSR, intf->queue_folder,
Ivo van Doorn68598d292008-02-10 22:50:28 +0100750 intf, &rt2x00debug_fop_queue_stats);
751
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200752#ifdef CONFIG_RT2X00_LIB_CRYPTO
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200753 if (test_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags))
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200754 intf->crypto_stats_entry =
755 debugfs_create_file("crypto", S_IRUGO, intf->queue_folder,
756 intf, &rt2x00debug_fop_crypto_stats);
757#endif
758
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700759 return;
760
761exit:
762 rt2x00debug_deregister(rt2x00dev);
Joe Perchesec9c4982013-04-19 08:33:40 -0700763 rt2x00_err(rt2x00dev, "Failed to register debug handler\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700764}
765
766void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
767{
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100768 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700769
770 if (unlikely(!intf))
771 return;
772
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100773 skb_queue_purge(&intf->frame_dump_skbqueue);
774
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200775#ifdef CONFIG_RT2X00_LIB_CRYPTO
776 debugfs_remove(intf->crypto_stats_entry);
777#endif
Ivo van Doorn68598d292008-02-10 22:50:28 +0100778 debugfs_remove(intf->queue_stats_entry);
779 debugfs_remove(intf->queue_frame_dump_entry);
780 debugfs_remove(intf->queue_folder);
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200781 debugfs_remove(intf->rfcsr_val_entry);
782 debugfs_remove(intf->rfcsr_off_entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700783 debugfs_remove(intf->rf_val_entry);
784 debugfs_remove(intf->rf_off_entry);
785 debugfs_remove(intf->bbp_val_entry);
786 debugfs_remove(intf->bbp_off_entry);
787 debugfs_remove(intf->eeprom_val_entry);
788 debugfs_remove(intf->eeprom_off_entry);
789 debugfs_remove(intf->csr_val_entry);
790 debugfs_remove(intf->csr_off_entry);
Ivo van Doorn91921a42007-11-27 21:48:16 +0100791 debugfs_remove(intf->register_folder);
Ivo van Doorn643b2522007-09-25 20:13:51 +0200792 debugfs_remove(intf->dev_flags);
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200793 debugfs_remove(intf->cap_flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700794 debugfs_remove(intf->chipset_entry);
795 debugfs_remove(intf->driver_entry);
796 debugfs_remove(intf->driver_folder);
797 kfree(intf->chipset_blob.data);
798 kfree(intf->driver_blob.data);
799 kfree(intf);
800
801 rt2x00dev->debugfs_intf = NULL;
802}