blob: 25ee3cb8e982db287ac269cac97666ad7dd60ec6 [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
Jeff Kirshera05b8c52013-12-06 03:32:11 -080016 along with this program; if not, see <http://www.gnu.org/licenses/>.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070017 */
18
19/*
20 Module: rt2x00lib
21 Abstract: rt2x00 debugfs specific routines.
22 */
23
Ivo van Doorn95ea3622007-09-25 17:57:13 -070024#include <linux/debugfs.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010027#include <linux/poll.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040028#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070030#include <linux/uaccess.h>
31
32#include "rt2x00.h"
33#include "rt2x00lib.h"
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010034#include "rt2x00dump.h"
Ivo van Doorn95ea3622007-09-25 17:57:13 -070035
Ivo van Doorn68598d292008-02-10 22:50:28 +010036#define MAX_LINE_LENGTH 64
Ivo van Doorn95ea3622007-09-25 17:57:13 -070037
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020038struct rt2x00debug_crypto {
39 unsigned long success;
40 unsigned long icv_error;
41 unsigned long mic_error;
42 unsigned long key_error;
43};
44
Ivo van Doorn95ea3622007-09-25 17:57:13 -070045struct rt2x00debug_intf {
46 /*
47 * Pointer to driver structure where
48 * this debugfs entry belongs to.
49 */
50 struct rt2x00_dev *rt2x00dev;
51
52 /*
53 * Reference to the rt2x00debug structure
54 * which can be used to communicate with
55 * the registers.
56 */
57 const struct rt2x00debug *debug;
58
59 /*
60 * Debugfs entries for:
61 * - driver folder
Ivo van Doorn91921a42007-11-27 21:48:16 +010062 * - driver file
63 * - chipset file
Ivo van Doorn7dab73b2011-04-18 15:27:06 +020064 * - device state flags file
65 * - device capability flags file
Ivo van Doorn91921a42007-11-27 21:48:16 +010066 * - register folder
67 * - csr offset/value files
68 * - eeprom offset/value files
69 * - bbp offset/value files
70 * - rf offset/value files
Anisse Astierf2bd7f12012-04-19 15:53:10 +020071 * - rfcsr offset/value files
Ivo van Doorn68598d292008-02-10 22:50:28 +010072 * - queue folder
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010073 * - frame dump file
Ivo van Doorn68598d292008-02-10 22:50:28 +010074 * - queue stats file
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020075 * - crypto stats file
Ivo van Doorn95ea3622007-09-25 17:57:13 -070076 */
77 struct dentry *driver_folder;
78 struct dentry *driver_entry;
79 struct dentry *chipset_entry;
Ivo van Doorn643b2522007-09-25 20:13:51 +020080 struct dentry *dev_flags;
Ivo van Doorn7dab73b2011-04-18 15:27:06 +020081 struct dentry *cap_flags;
Ivo van Doorn91921a42007-11-27 21:48:16 +010082 struct dentry *register_folder;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070083 struct dentry *csr_off_entry;
84 struct dentry *csr_val_entry;
85 struct dentry *eeprom_off_entry;
86 struct dentry *eeprom_val_entry;
87 struct dentry *bbp_off_entry;
88 struct dentry *bbp_val_entry;
89 struct dentry *rf_off_entry;
90 struct dentry *rf_val_entry;
Anisse Astierf2bd7f12012-04-19 15:53:10 +020091 struct dentry *rfcsr_off_entry;
92 struct dentry *rfcsr_val_entry;
Ivo van Doorn68598d292008-02-10 22:50:28 +010093 struct dentry *queue_folder;
94 struct dentry *queue_frame_dump_entry;
95 struct dentry *queue_stats_entry;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020096 struct dentry *crypto_stats_entry;
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010097
98 /*
99 * The frame dump file only allows a single reader,
100 * so we need to store the current state here.
101 */
102 unsigned long frame_dump_flags;
103#define FRAME_DUMP_FILE_OPEN 1
104
105 /*
106 * We queue each frame before dumping it to the user,
107 * per read command we will pass a single skb structure
108 * so we should be prepared to queue multiple sk buffers
109 * before sending it to userspace.
110 */
111 struct sk_buff_head frame_dump_skbqueue;
112 wait_queue_head_t frame_dump_waitqueue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700113
114 /*
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200115 * HW crypto statistics.
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800116 * All statistics are stored separately per cipher type.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200117 */
118 struct rt2x00debug_crypto crypto_stats[CIPHER_MAX];
119
120 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700121 * Driver and chipset files will use a data buffer
122 * that has been created in advance. This will simplify
123 * the code since we can use the debugfs functions.
124 */
125 struct debugfs_blob_wrapper driver_blob;
126 struct debugfs_blob_wrapper chipset_blob;
127
128 /*
129 * Requested offset for each register type.
130 */
131 unsigned int offset_csr;
132 unsigned int offset_eeprom;
133 unsigned int offset_bbp;
134 unsigned int offset_rf;
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200135 unsigned int offset_rfcsr;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700136};
137
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200138void rt2x00debug_update_crypto(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn84e31962008-12-20 10:53:29 +0100139 struct rxdone_entry_desc *rxdesc)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200140{
141 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100142 enum cipher cipher = rxdesc->cipher;
143 enum rx_crypto status = rxdesc->cipher_status;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200144
145 if (cipher == CIPHER_TKIP_NO_MIC)
146 cipher = CIPHER_TKIP;
Roel Kluina6c67332009-05-20 02:12:56 +0200147 if (cipher == CIPHER_NONE || cipher >= CIPHER_MAX)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200148 return;
149
150 /* Remove CIPHER_NONE index */
151 cipher--;
152
153 intf->crypto_stats[cipher].success += (status == RX_CRYPTO_SUCCESS);
154 intf->crypto_stats[cipher].icv_error += (status == RX_CRYPTO_FAIL_ICV);
155 intf->crypto_stats[cipher].mic_error += (status == RX_CRYPTO_FAIL_MIC);
156 intf->crypto_stats[cipher].key_error += (status == RX_CRYPTO_FAIL_KEY);
157}
158
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100159void rt2x00debug_dump_frame(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn5a6e5992008-05-10 13:41:32 +0200160 enum rt2x00_dump_type type, struct sk_buff *skb)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100161{
162 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
Gertjan van Wingerde878f7042010-05-11 23:51:37 +0200163 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100164 struct sk_buff *skbcopy;
165 struct rt2x00dump_hdr *dump_hdr;
166 struct timeval timestamp;
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200167 u32 data_len;
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100168
Helmut Schaae2f8c872010-11-04 20:38:56 +0100169 if (likely(!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)))
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100170 return;
171
Helmut Schaae2f8c872010-11-04 20:38:56 +0100172 do_gettimeofday(&timestamp);
173
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100174 if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700175 rt2x00_dbg(rt2x00dev, "txrx dump queue length exceeded\n");
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100176 return;
177 }
178
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200179 data_len = skb->len;
180 if (skbdesc->flags & SKBDESC_DESC_IN_SKB)
181 data_len -= skbdesc->desc_len;
182
183 skbcopy = alloc_skb(sizeof(*dump_hdr) + skbdesc->desc_len + data_len,
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100184 GFP_ATOMIC);
185 if (!skbcopy) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700186 rt2x00_dbg(rt2x00dev, "Failed to copy skb for dump\n");
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100187 return;
188 }
189
190 dump_hdr = (struct rt2x00dump_hdr *)skb_put(skbcopy, sizeof(*dump_hdr));
191 dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
192 dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
Gertjan van Wingerde878f7042010-05-11 23:51:37 +0200193 dump_hdr->desc_length = cpu_to_le32(skbdesc->desc_len);
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200194 dump_hdr->data_length = cpu_to_le32(data_len);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100195 dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
196 dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +0100197 dump_hdr->chip_rev = cpu_to_le16(rt2x00dev->chip.rev);
Ivo van Doorn5a6e5992008-05-10 13:41:32 +0200198 dump_hdr->type = cpu_to_le16(type);
Gertjan van Wingerde878f7042010-05-11 23:51:37 +0200199 dump_hdr->queue_index = skbdesc->entry->queue->qid;
200 dump_hdr->entry_index = skbdesc->entry->entry_idx;
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100201 dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
202 dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_usec);
203
Gertjan van Wingerdefd76f142010-05-11 23:51:43 +0200204 if (!(skbdesc->flags & SKBDESC_DESC_IN_SKB))
205 memcpy(skb_put(skbcopy, skbdesc->desc_len), skbdesc->desc,
206 skbdesc->desc_len);
Gertjan van Wingerded56d4532008-06-06 22:54:08 +0200207 memcpy(skb_put(skbcopy, skb->len), skb->data, skb->len);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100208
209 skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
210 wake_up_interruptible(&intf->frame_dump_waitqueue);
211
212 /*
213 * Verify that the file has not been closed while we were working.
214 */
215 if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
216 skb_queue_purge(&intf->frame_dump_skbqueue);
217}
Gertjan van Wingerdeb4df4702010-05-13 13:16:36 +0200218EXPORT_SYMBOL_GPL(rt2x00debug_dump_frame);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100219
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700220static int rt2x00debug_file_open(struct inode *inode, struct file *file)
221{
222 struct rt2x00debug_intf *intf = inode->i_private;
223
224 file->private_data = inode->i_private;
225
226 if (!try_module_get(intf->debug->owner))
227 return -EBUSY;
228
229 return 0;
230}
231
232static int rt2x00debug_file_release(struct inode *inode, struct file *file)
233{
234 struct rt2x00debug_intf *intf = file->private_data;
235
236 module_put(intf->debug->owner);
237
238 return 0;
239}
240
Ivo van Doorn181d6902008-02-05 16:42:23 -0500241static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100242{
243 struct rt2x00debug_intf *intf = inode->i_private;
244 int retval;
245
246 retval = rt2x00debug_file_open(inode, file);
247 if (retval)
248 return retval;
249
250 if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
251 rt2x00debug_file_release(inode, file);
252 return -EBUSY;
253 }
254
255 return 0;
256}
257
Ivo van Doorn181d6902008-02-05 16:42:23 -0500258static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100259{
260 struct rt2x00debug_intf *intf = inode->i_private;
261
262 skb_queue_purge(&intf->frame_dump_skbqueue);
263
264 clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
265
266 return rt2x00debug_file_release(inode, file);
267}
268
Ivo van Doorn181d6902008-02-05 16:42:23 -0500269static ssize_t rt2x00debug_read_queue_dump(struct file *file,
270 char __user *buf,
271 size_t length,
272 loff_t *offset)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100273{
274 struct rt2x00debug_intf *intf = file->private_data;
275 struct sk_buff *skb;
276 size_t status;
277 int retval;
278
279 if (file->f_flags & O_NONBLOCK)
280 return -EAGAIN;
281
282 retval =
283 wait_event_interruptible(intf->frame_dump_waitqueue,
284 (skb =
285 skb_dequeue(&intf->frame_dump_skbqueue)));
286 if (retval)
287 return retval;
288
Silvan Jegenc8e49552014-02-25 18:12:52 +0100289 status = min_t(size_t, skb->len, length);
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100290 if (copy_to_user(buf, skb->data, status)) {
291 status = -EFAULT;
292 goto exit;
293 }
294
295 *offset += status;
296
297exit:
298 kfree_skb(skb);
299
300 return status;
301}
302
Ivo van Doorn181d6902008-02-05 16:42:23 -0500303static unsigned int rt2x00debug_poll_queue_dump(struct file *file,
John Daiker55887512008-10-17 12:16:17 -0700304 poll_table *wait)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100305{
306 struct rt2x00debug_intf *intf = file->private_data;
307
308 poll_wait(file, &intf->frame_dump_waitqueue, wait);
309
310 if (!skb_queue_empty(&intf->frame_dump_skbqueue))
311 return POLLOUT | POLLWRNORM;
312
313 return 0;
314}
315
Ivo van Doorn181d6902008-02-05 16:42:23 -0500316static const struct file_operations rt2x00debug_fop_queue_dump = {
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100317 .owner = THIS_MODULE,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500318 .read = rt2x00debug_read_queue_dump,
319 .poll = rt2x00debug_poll_queue_dump,
320 .open = rt2x00debug_open_queue_dump,
321 .release = rt2x00debug_release_queue_dump,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200322 .llseek = default_llseek,
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100323};
324
Ivo van Doorn68598d292008-02-10 22:50:28 +0100325static ssize_t rt2x00debug_read_queue_stats(struct file *file,
326 char __user *buf,
327 size_t length,
328 loff_t *offset)
329{
330 struct rt2x00debug_intf *intf = file->private_data;
331 struct data_queue *queue;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100332 unsigned long irqflags;
Ivo van Doorn68598d292008-02-10 22:50:28 +0100333 unsigned int lines = 1 + intf->rt2x00dev->data_queues;
334 size_t size;
335 char *data;
336 char *temp;
337
338 if (*offset)
339 return 0;
340
Joe Perchesbaeb2ff2010-08-11 07:02:48 +0000341 data = kcalloc(lines, MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100342 if (!data)
343 return -ENOMEM;
344
345 temp = data +
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100346 sprintf(data, "qid\tflags\t\tcount\tlimit\tlength\tindex\tdma done\tdone\n");
Ivo van Doorn68598d292008-02-10 22:50:28 +0100347
348 queue_for_each(intf->rt2x00dev, queue) {
Ivo van Doorn813f0332010-11-06 15:48:05 +0100349 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100350
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100351 temp += sprintf(temp, "%d\t0x%.8x\t%d\t%d\t%d\t%d\t%d\t\t%d\n",
352 queue->qid, (unsigned int)queue->flags,
Ivo van Doorn68598d292008-02-10 22:50:28 +0100353 queue->count, queue->limit, queue->length,
354 queue->index[Q_INDEX],
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200355 queue->index[Q_INDEX_DMA_DONE],
Ivo van Doorn54e34fb2010-08-23 19:54:41 +0200356 queue->index[Q_INDEX_DONE]);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100357
Ivo van Doorn813f0332010-11-06 15:48:05 +0100358 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn68598d292008-02-10 22:50:28 +0100359 }
360
361 size = strlen(data);
362 size = min(size, length);
363
364 if (copy_to_user(buf, data, size)) {
365 kfree(data);
366 return -EFAULT;
367 }
368
369 kfree(data);
370
371 *offset += size;
372 return size;
373}
374
375static const struct file_operations rt2x00debug_fop_queue_stats = {
376 .owner = THIS_MODULE,
377 .read = rt2x00debug_read_queue_stats,
378 .open = rt2x00debug_file_open,
379 .release = rt2x00debug_file_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200380 .llseek = default_llseek,
Ivo van Doorn68598d292008-02-10 22:50:28 +0100381};
382
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200383#ifdef CONFIG_RT2X00_LIB_CRYPTO
384static ssize_t rt2x00debug_read_crypto_stats(struct file *file,
385 char __user *buf,
386 size_t length,
387 loff_t *offset)
388{
389 struct rt2x00debug_intf *intf = file->private_data;
Joe Perches030bda02010-09-13 18:23:55 +0000390 static const char * const name[] = { "WEP64", "WEP128", "TKIP", "AES" };
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200391 char *data;
392 char *temp;
393 size_t size;
394 unsigned int i;
395
396 if (*offset)
397 return 0;
398
John Daiker55887512008-10-17 12:16:17 -0700399 data = kzalloc((1 + CIPHER_MAX) * MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200400 if (!data)
401 return -ENOMEM;
402
403 temp = data;
404 temp += sprintf(data, "cipher\tsuccess\ticv err\tmic err\tkey err\n");
405
406 for (i = 0; i < CIPHER_MAX; i++) {
407 temp += sprintf(temp, "%s\t%lu\t%lu\t%lu\t%lu\n", name[i],
408 intf->crypto_stats[i].success,
409 intf->crypto_stats[i].icv_error,
410 intf->crypto_stats[i].mic_error,
411 intf->crypto_stats[i].key_error);
412 }
413
414 size = strlen(data);
415 size = min(size, length);
416
417 if (copy_to_user(buf, data, size)) {
418 kfree(data);
419 return -EFAULT;
420 }
421
422 kfree(data);
423
424 *offset += size;
425 return size;
426}
427
428static const struct file_operations rt2x00debug_fop_crypto_stats = {
429 .owner = THIS_MODULE,
430 .read = rt2x00debug_read_crypto_stats,
431 .open = rt2x00debug_file_open,
432 .release = rt2x00debug_file_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200433 .llseek = default_llseek,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200434};
435#endif
436
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700437#define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
438static ssize_t rt2x00debug_read_##__name(struct file *file, \
439 char __user *buf, \
440 size_t length, \
441 loff_t *offset) \
442{ \
Ivo van Doorn91921a42007-11-27 21:48:16 +0100443 struct rt2x00debug_intf *intf = file->private_data; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700444 const struct rt2x00debug *debug = intf->debug; \
445 char line[16]; \
446 size_t size; \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100447 unsigned int index = intf->offset_##__name; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700448 __type value; \
449 \
450 if (*offset) \
451 return 0; \
452 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100453 if (index >= debug->__name.word_count) \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700454 return -EINVAL; \
455 \
Ivo van Doornabd2fdb2009-02-17 14:04:29 +0100456 index += (debug->__name.word_base / \
457 debug->__name.word_size); \
458 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100459 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
460 index *= debug->__name.word_size; \
461 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100462 debug->__name.read(intf->rt2x00dev, index, &value); \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700463 \
464 size = sprintf(line, __format, value); \
465 \
466 if (copy_to_user(buf, line, size)) \
467 return -EFAULT; \
468 \
469 *offset += size; \
470 return size; \
471}
472
473#define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
474static ssize_t rt2x00debug_write_##__name(struct file *file, \
475 const char __user *buf,\
476 size_t length, \
477 loff_t *offset) \
478{ \
Ivo van Doorn91921a42007-11-27 21:48:16 +0100479 struct rt2x00debug_intf *intf = file->private_data; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700480 const struct rt2x00debug *debug = intf->debug; \
481 char line[16]; \
482 size_t size; \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100483 unsigned int index = intf->offset_##__name; \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700484 __type value; \
485 \
486 if (*offset) \
487 return 0; \
488 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100489 if (index >= debug->__name.word_count) \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700490 return -EINVAL; \
491 \
Arnaud Patard (Rtp)f8d8b7a2010-08-23 23:02:22 +0200492 if (length > sizeof(line)) \
493 return -EINVAL; \
494 \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700495 if (copy_from_user(line, buf, length)) \
496 return -EFAULT; \
497 \
498 size = strlen(line); \
499 value = simple_strtoul(line, NULL, 0); \
500 \
Ivo van Doornabd2fdb2009-02-17 14:04:29 +0100501 index += (debug->__name.word_base / \
502 debug->__name.word_size); \
503 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100504 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
505 index *= debug->__name.word_size; \
506 \
Ivo van Doorn743b97c2008-10-29 19:41:03 +0100507 debug->__name.write(intf->rt2x00dev, index, value); \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700508 \
509 *offset += size; \
510 return size; \
511}
512
513#define RT2X00DEBUGFS_OPS(__name, __format, __type) \
514RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
515RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
516 \
517static const struct file_operations rt2x00debug_fop_##__name = {\
518 .owner = THIS_MODULE, \
519 .read = rt2x00debug_read_##__name, \
520 .write = rt2x00debug_write_##__name, \
521 .open = rt2x00debug_file_open, \
522 .release = rt2x00debug_file_release, \
Arnd Bergmann2b18ab362010-07-06 19:05:31 +0200523 .llseek = generic_file_llseek, \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700524};
525
526RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
527RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
528RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
529RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200530RT2X00DEBUGFS_OPS(rfcsr, "0x%.2x\n", u8);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700531
Ivo van Doorn643b2522007-09-25 20:13:51 +0200532static ssize_t rt2x00debug_read_dev_flags(struct file *file,
533 char __user *buf,
534 size_t length,
535 loff_t *offset)
536{
537 struct rt2x00debug_intf *intf = file->private_data;
538 char line[16];
539 size_t size;
540
541 if (*offset)
542 return 0;
543
544 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
545
546 if (copy_to_user(buf, line, size))
547 return -EFAULT;
548
549 *offset += size;
550 return size;
551}
552
553static const struct file_operations rt2x00debug_fop_dev_flags = {
554 .owner = THIS_MODULE,
555 .read = rt2x00debug_read_dev_flags,
556 .open = rt2x00debug_file_open,
557 .release = rt2x00debug_file_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200558 .llseek = default_llseek,
Ivo van Doorn643b2522007-09-25 20:13:51 +0200559};
560
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200561static ssize_t rt2x00debug_read_cap_flags(struct file *file,
562 char __user *buf,
563 size_t length,
564 loff_t *offset)
565{
566 struct rt2x00debug_intf *intf = file->private_data;
567 char line[16];
568 size_t size;
569
570 if (*offset)
571 return 0;
572
573 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->cap_flags);
574
575 if (copy_to_user(buf, line, size))
576 return -EFAULT;
577
578 *offset += size;
579 return size;
580}
581
582static const struct file_operations rt2x00debug_fop_cap_flags = {
583 .owner = THIS_MODULE,
584 .read = rt2x00debug_read_cap_flags,
585 .open = rt2x00debug_file_open,
586 .release = rt2x00debug_file_release,
587 .llseek = default_llseek,
588};
589
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700590static struct dentry *rt2x00debug_create_file_driver(const char *name,
591 struct rt2x00debug_intf
592 *intf,
593 struct debugfs_blob_wrapper
594 *blob)
595{
596 char *data;
597
Ivo van Doorn68598d292008-02-10 22:50:28 +0100598 data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700599 if (!data)
600 return NULL;
601
602 blob->data = data;
Ivo van Doorn860559f2009-03-03 18:14:18 +0100603 data += sprintf(data, "driver:\t%s\n", intf->rt2x00dev->ops->name);
604 data += sprintf(data, "version:\t%s\n", DRV_VERSION);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700605 blob->size = strlen(blob->data);
606
Ivo van Doornd2b69072008-07-27 15:06:05 +0200607 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700608}
609
610static struct dentry *rt2x00debug_create_file_chipset(const char *name,
611 struct rt2x00debug_intf
612 *intf,
613 struct
614 debugfs_blob_wrapper
615 *blob)
616{
617 const struct rt2x00debug *debug = intf->debug;
618 char *data;
619
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200620 data = kzalloc(9 * MAX_LINE_LENGTH, GFP_KERNEL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700621 if (!data)
622 return NULL;
623
Ivo van Doorn3e34c6d2008-01-06 23:38:10 +0100624 blob->data = data;
Ivo van Doorn860559f2009-03-03 18:14:18 +0100625 data += sprintf(data, "rt chip:\t%04x\n", intf->rt2x00dev->chip.rt);
626 data += sprintf(data, "rf chip:\t%04x\n", intf->rt2x00dev->chip.rf);
Gertjan van Wingerde49e721e2010-02-13 20:55:49 +0100627 data += sprintf(data, "revision:\t%04x\n", intf->rt2x00dev->chip.rev);
Ivo van Doorn22c96c22007-11-27 21:48:37 +0100628 data += sprintf(data, "\n");
Ivo van Doorn860559f2009-03-03 18:14:18 +0100629 data += sprintf(data, "register\tbase\twords\twordsize\n");
Anisse Astierf2efd202012-04-19 15:04:52 +0200630#define RT2X00DEBUGFS_SPRINTF_REGISTER(__name) \
631{ \
Paul Mcquade5b451712015-10-17 22:11:23 +0100632 if (debug->__name.read) \
Anisse Astierf2efd202012-04-19 15:04:52 +0200633 data += sprintf(data, __stringify(__name) \
634 "\t%d\t%d\t%d\n", \
635 debug->__name.word_base, \
636 debug->__name.word_count, \
637 debug->__name.word_size); \
638}
639 RT2X00DEBUGFS_SPRINTF_REGISTER(csr);
640 RT2X00DEBUGFS_SPRINTF_REGISTER(eeprom);
641 RT2X00DEBUGFS_SPRINTF_REGISTER(bbp);
642 RT2X00DEBUGFS_SPRINTF_REGISTER(rf);
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200643 RT2X00DEBUGFS_SPRINTF_REGISTER(rfcsr);
Anisse Astierf2efd202012-04-19 15:04:52 +0200644#undef RT2X00DEBUGFS_SPRINTF_REGISTER
645
Ivo van Doorn3e34c6d2008-01-06 23:38:10 +0100646 blob->size = strlen(blob->data);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700647
Ivo van Doornd2b69072008-07-27 15:06:05 +0200648 return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700649}
650
651void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
652{
653 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
654 struct rt2x00debug_intf *intf;
655
656 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
657 if (!intf) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700658 rt2x00_err(rt2x00dev, "Failed to allocate debug handler\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700659 return;
660 }
661
662 intf->debug = debug;
663 intf->rt2x00dev = rt2x00dev;
664 rt2x00dev->debugfs_intf = intf;
665
666 intf->driver_folder =
667 debugfs_create_dir(intf->rt2x00dev->ops->name,
668 rt2x00dev->hw->wiphy->debugfsdir);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800669 if (IS_ERR(intf->driver_folder) || !intf->driver_folder)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700670 goto exit;
671
672 intf->driver_entry =
673 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800674 if (IS_ERR(intf->driver_entry) || !intf->driver_entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700675 goto exit;
676
677 intf->chipset_entry =
678 rt2x00debug_create_file_chipset("chipset",
679 intf, &intf->chipset_blob);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800680 if (IS_ERR(intf->chipset_entry) || !intf->chipset_entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700681 goto exit;
682
Ivo van Doornd2b69072008-07-27 15:06:05 +0200683 intf->dev_flags = debugfs_create_file("dev_flags", S_IRUSR,
Ivo van Doorn643b2522007-09-25 20:13:51 +0200684 intf->driver_folder, intf,
685 &rt2x00debug_fop_dev_flags);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800686 if (IS_ERR(intf->dev_flags) || !intf->dev_flags)
Ivo van Doorn643b2522007-09-25 20:13:51 +0200687 goto exit;
688
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200689 intf->cap_flags = debugfs_create_file("cap_flags", S_IRUSR,
690 intf->driver_folder, intf,
691 &rt2x00debug_fop_cap_flags);
692 if (IS_ERR(intf->cap_flags) || !intf->cap_flags)
693 goto exit;
694
Ivo van Doorn91921a42007-11-27 21:48:16 +0100695 intf->register_folder =
696 debugfs_create_dir("register", intf->driver_folder);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800697 if (IS_ERR(intf->register_folder) || !intf->register_folder)
Ivo van Doorn91921a42007-11-27 21:48:16 +0100698 goto exit;
699
Anisse Astierf2efd202012-04-19 15:04:52 +0200700#define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
701({ \
Paul Mcquade5b451712015-10-17 22:11:23 +0100702 if (debug->__name.read) { \
Anisse Astierf2efd202012-04-19 15:04:52 +0200703 (__intf)->__name##_off_entry = \
704 debugfs_create_u32(__stringify(__name) "_offset", \
705 S_IRUSR | S_IWUSR, \
706 (__intf)->register_folder, \
707 &(__intf)->offset_##__name); \
708 if (IS_ERR((__intf)->__name##_off_entry) \
709 || !(__intf)->__name##_off_entry) \
710 goto exit; \
711 \
712 (__intf)->__name##_val_entry = \
713 debugfs_create_file(__stringify(__name) "_value", \
714 S_IRUSR | S_IWUSR, \
715 (__intf)->register_folder, \
716 (__intf), &rt2x00debug_fop_##__name); \
717 if (IS_ERR((__intf)->__name##_val_entry) \
718 || !(__intf)->__name##_val_entry) \
719 goto exit; \
720 } \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700721})
722
Ivo van Doorn91921a42007-11-27 21:48:16 +0100723 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
724 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
725 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
726 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200727 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rfcsr);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700728
Ivo van Doorn91921a42007-11-27 21:48:16 +0100729#undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700730
Ivo van Doorn68598d292008-02-10 22:50:28 +0100731 intf->queue_folder =
732 debugfs_create_dir("queue", intf->driver_folder);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800733 if (IS_ERR(intf->queue_folder) || !intf->queue_folder)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100734 goto exit;
735
Ivo van Doorn68598d292008-02-10 22:50:28 +0100736 intf->queue_frame_dump_entry =
Ivo van Doornd2b69072008-07-27 15:06:05 +0200737 debugfs_create_file("dump", S_IRUSR, intf->queue_folder,
Ivo van Doorn181d6902008-02-05 16:42:23 -0500738 intf, &rt2x00debug_fop_queue_dump);
Zhaoleicfa3fa42008-10-22 17:07:25 +0800739 if (IS_ERR(intf->queue_frame_dump_entry)
740 || !intf->queue_frame_dump_entry)
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100741 goto exit;
742
743 skb_queue_head_init(&intf->frame_dump_skbqueue);
744 init_waitqueue_head(&intf->frame_dump_waitqueue);
745
Ivo van Doorn68598d292008-02-10 22:50:28 +0100746 intf->queue_stats_entry =
Ivo van Doornd2b69072008-07-27 15:06:05 +0200747 debugfs_create_file("queue", S_IRUSR, intf->queue_folder,
Ivo van Doorn68598d292008-02-10 22:50:28 +0100748 intf, &rt2x00debug_fop_queue_stats);
749
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200750#ifdef CONFIG_RT2X00_LIB_CRYPTO
Gabor Juhos7b8a00d2013-10-11 13:18:41 +0200751 if (rt2x00_has_cap_hw_crypto(rt2x00dev))
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200752 intf->crypto_stats_entry =
753 debugfs_create_file("crypto", S_IRUGO, intf->queue_folder,
754 intf, &rt2x00debug_fop_crypto_stats);
755#endif
756
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700757 return;
758
759exit:
760 rt2x00debug_deregister(rt2x00dev);
Joe Perchesec9c4982013-04-19 08:33:40 -0700761 rt2x00_err(rt2x00dev, "Failed to register debug handler\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700762}
763
764void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
765{
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100766 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700767
768 if (unlikely(!intf))
769 return;
770
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100771 skb_queue_purge(&intf->frame_dump_skbqueue);
772
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200773#ifdef CONFIG_RT2X00_LIB_CRYPTO
774 debugfs_remove(intf->crypto_stats_entry);
775#endif
Ivo van Doorn68598d292008-02-10 22:50:28 +0100776 debugfs_remove(intf->queue_stats_entry);
777 debugfs_remove(intf->queue_frame_dump_entry);
778 debugfs_remove(intf->queue_folder);
Anisse Astierf2bd7f12012-04-19 15:53:10 +0200779 debugfs_remove(intf->rfcsr_val_entry);
780 debugfs_remove(intf->rfcsr_off_entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700781 debugfs_remove(intf->rf_val_entry);
782 debugfs_remove(intf->rf_off_entry);
783 debugfs_remove(intf->bbp_val_entry);
784 debugfs_remove(intf->bbp_off_entry);
785 debugfs_remove(intf->eeprom_val_entry);
786 debugfs_remove(intf->eeprom_off_entry);
787 debugfs_remove(intf->csr_val_entry);
788 debugfs_remove(intf->csr_off_entry);
Ivo van Doorn91921a42007-11-27 21:48:16 +0100789 debugfs_remove(intf->register_folder);
Ivo van Doorn643b2522007-09-25 20:13:51 +0200790 debugfs_remove(intf->dev_flags);
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200791 debugfs_remove(intf->cap_flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700792 debugfs_remove(intf->chipset_entry);
793 debugfs_remove(intf->driver_entry);
794 debugfs_remove(intf->driver_folder);
795 kfree(intf->chipset_blob.data);
796 kfree(intf->driver_blob.data);
797 kfree(intf);
798
799 rt2x00dev->debugfs_intf = NULL;
800}