Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00debug.c
1 /*
2         Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
3         <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
26 #include <linux/debugfs.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/poll.h>
30 #include <linux/uaccess.h>
31
32 #include "rt2x00.h"
33 #include "rt2x00lib.h"
34 #include "rt2x00dump.h"
35
36 #define MAX_LINE_LENGTH 64
37
38 struct rt2x00debug_intf {
39         /*
40          * Pointer to driver structure where
41          * this debugfs entry belongs to.
42          */
43         struct rt2x00_dev *rt2x00dev;
44
45         /*
46          * Reference to the rt2x00debug structure
47          * which can be used to communicate with
48          * the registers.
49          */
50         const struct rt2x00debug *debug;
51
52         /*
53          * Debugfs entries for:
54          * - driver folder
55          *   - driver file
56          *   - chipset file
57          *   - device flags file
58          *   - register folder
59          *     - csr offset/value files
60          *     - eeprom offset/value files
61          *     - bbp offset/value files
62          *     - rf offset/value files
63          *   - queue folder
64          *     - frame dump file
65          *     - queue 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 *register_folder;
72         struct dentry *csr_off_entry;
73         struct dentry *csr_val_entry;
74         struct dentry *eeprom_off_entry;
75         struct dentry *eeprom_val_entry;
76         struct dentry *bbp_off_entry;
77         struct dentry *bbp_val_entry;
78         struct dentry *rf_off_entry;
79         struct dentry *rf_val_entry;
80         struct dentry *queue_folder;
81         struct dentry *queue_frame_dump_entry;
82         struct dentry *queue_stats_entry;
83
84         /*
85          * The frame dump file only allows a single reader,
86          * so we need to store the current state here.
87          */
88         unsigned long frame_dump_flags;
89 #define FRAME_DUMP_FILE_OPEN    1
90
91         /*
92          * We queue each frame before dumping it to the user,
93          * per read command we will pass a single skb structure
94          * so we should be prepared to queue multiple sk buffers
95          * before sending it to userspace.
96          */
97         struct sk_buff_head frame_dump_skbqueue;
98         wait_queue_head_t frame_dump_waitqueue;
99
100         /*
101          * Driver and chipset files will use a data buffer
102          * that has been created in advance. This will simplify
103          * the code since we can use the debugfs functions.
104          */
105         struct debugfs_blob_wrapper driver_blob;
106         struct debugfs_blob_wrapper chipset_blob;
107
108         /*
109          * Requested offset for each register type.
110          */
111         unsigned int offset_csr;
112         unsigned int offset_eeprom;
113         unsigned int offset_bbp;
114         unsigned int offset_rf;
115 };
116
117 void rt2x00debug_dump_frame(struct rt2x00_dev *rt2x00dev,
118                             enum rt2x00_dump_type type, struct sk_buff *skb)
119 {
120         struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
121         struct skb_frame_desc *desc = get_skb_frame_desc(skb);
122         struct sk_buff *skbcopy;
123         struct rt2x00dump_hdr *dump_hdr;
124         struct timeval timestamp;
125
126         do_gettimeofday(&timestamp);
127
128         if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
129                 return;
130
131         if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
132                 DEBUG(rt2x00dev, "txrx dump queue length exceeded.\n");
133                 return;
134         }
135
136         skbcopy = alloc_skb(sizeof(*dump_hdr) + desc->desc_len + skb->len,
137                             GFP_ATOMIC);
138         if (!skbcopy) {
139                 DEBUG(rt2x00dev, "Failed to copy skb for dump.\n");
140                 return;
141         }
142
143         dump_hdr = (struct rt2x00dump_hdr *)skb_put(skbcopy, sizeof(*dump_hdr));
144         dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
145         dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
146         dump_hdr->desc_length = cpu_to_le32(desc->desc_len);
147         dump_hdr->data_length = cpu_to_le32(skb->len);
148         dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
149         dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
150         dump_hdr->chip_rev = cpu_to_le32(rt2x00dev->chip.rev);
151         dump_hdr->type = cpu_to_le16(type);
152         dump_hdr->queue_index = desc->entry->queue->qid;
153         dump_hdr->entry_index = desc->entry->entry_idx;
154         dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
155         dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_usec);
156
157         memcpy(skb_put(skbcopy, desc->desc_len), desc->desc, desc->desc_len);
158         memcpy(skb_put(skbcopy, skb->len), skb->data, skb->len);
159
160         skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
161         wake_up_interruptible(&intf->frame_dump_waitqueue);
162
163         /*
164          * Verify that the file has not been closed while we were working.
165          */
166         if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
167                 skb_queue_purge(&intf->frame_dump_skbqueue);
168 }
169
170 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
171 {
172         struct rt2x00debug_intf *intf = inode->i_private;
173
174         file->private_data = inode->i_private;
175
176         if (!try_module_get(intf->debug->owner))
177                 return -EBUSY;
178
179         return 0;
180 }
181
182 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
183 {
184         struct rt2x00debug_intf *intf = file->private_data;
185
186         module_put(intf->debug->owner);
187
188         return 0;
189 }
190
191 static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
192 {
193         struct rt2x00debug_intf *intf = inode->i_private;
194         int retval;
195
196         retval = rt2x00debug_file_open(inode, file);
197         if (retval)
198                 return retval;
199
200         if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
201                 rt2x00debug_file_release(inode, file);
202                 return -EBUSY;
203         }
204
205         return 0;
206 }
207
208 static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
209 {
210         struct rt2x00debug_intf *intf = inode->i_private;
211
212         skb_queue_purge(&intf->frame_dump_skbqueue);
213
214         clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
215
216         return rt2x00debug_file_release(inode, file);
217 }
218
219 static ssize_t rt2x00debug_read_queue_dump(struct file *file,
220                                            char __user *buf,
221                                            size_t length,
222                                            loff_t *offset)
223 {
224         struct rt2x00debug_intf *intf = file->private_data;
225         struct sk_buff *skb;
226         size_t status;
227         int retval;
228
229         if (file->f_flags & O_NONBLOCK)
230                 return -EAGAIN;
231
232         retval =
233             wait_event_interruptible(intf->frame_dump_waitqueue,
234                                      (skb =
235                                      skb_dequeue(&intf->frame_dump_skbqueue)));
236         if (retval)
237                 return retval;
238
239         status = min((size_t)skb->len, length);
240         if (copy_to_user(buf, skb->data, status)) {
241                 status = -EFAULT;
242                 goto exit;
243         }
244
245         *offset += status;
246
247 exit:
248         kfree_skb(skb);
249
250         return status;
251 }
252
253 static unsigned int rt2x00debug_poll_queue_dump(struct file *file,
254                                                 poll_table *wait)
255 {
256         struct rt2x00debug_intf *intf = file->private_data;
257
258         poll_wait(file, &intf->frame_dump_waitqueue, wait);
259
260         if (!skb_queue_empty(&intf->frame_dump_skbqueue))
261                 return POLLOUT | POLLWRNORM;
262
263         return 0;
264 }
265
266 static const struct file_operations rt2x00debug_fop_queue_dump = {
267         .owner          = THIS_MODULE,
268         .read           = rt2x00debug_read_queue_dump,
269         .poll           = rt2x00debug_poll_queue_dump,
270         .open           = rt2x00debug_open_queue_dump,
271         .release        = rt2x00debug_release_queue_dump,
272 };
273
274 static ssize_t rt2x00debug_read_queue_stats(struct file *file,
275                                             char __user *buf,
276                                             size_t length,
277                                             loff_t *offset)
278 {
279         struct rt2x00debug_intf *intf = file->private_data;
280         struct data_queue *queue;
281         unsigned long irqflags;
282         unsigned int lines = 1 + intf->rt2x00dev->data_queues;
283         size_t size;
284         char *data;
285         char *temp;
286
287         if (*offset)
288                 return 0;
289
290         data = kzalloc(lines * MAX_LINE_LENGTH, GFP_KERNEL);
291         if (!data)
292                 return -ENOMEM;
293
294         temp = data +
295             sprintf(data, "qid\tcount\tlimit\tlength\tindex\tdone\tcrypto\n");
296
297         queue_for_each(intf->rt2x00dev, queue) {
298                 spin_lock_irqsave(&queue->lock, irqflags);
299
300                 temp += sprintf(temp, "%d\t%d\t%d\t%d\t%d\t%d\t%d\n", queue->qid,
301                                 queue->count, queue->limit, queue->length,
302                                 queue->index[Q_INDEX],
303                                 queue->index[Q_INDEX_DONE],
304                                 queue->index[Q_INDEX_CRYPTO]);
305
306                 spin_unlock_irqrestore(&queue->lock, irqflags);
307         }
308
309         size = strlen(data);
310         size = min(size, length);
311
312         if (copy_to_user(buf, data, size)) {
313                 kfree(data);
314                 return -EFAULT;
315         }
316
317         kfree(data);
318
319         *offset += size;
320         return size;
321 }
322
323 static const struct file_operations rt2x00debug_fop_queue_stats = {
324         .owner          = THIS_MODULE,
325         .read           = rt2x00debug_read_queue_stats,
326         .open           = rt2x00debug_file_open,
327         .release        = rt2x00debug_file_release,
328 };
329
330 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type)        \
331 static ssize_t rt2x00debug_read_##__name(struct file *file,     \
332                                          char __user *buf,      \
333                                          size_t length,         \
334                                          loff_t *offset)        \
335 {                                                               \
336         struct rt2x00debug_intf *intf = file->private_data;     \
337         const struct rt2x00debug *debug = intf->debug;          \
338         char line[16];                                          \
339         size_t size;                                            \
340         __type value;                                           \
341                                                                 \
342         if (*offset)                                            \
343                 return 0;                                       \
344                                                                 \
345         if (intf->offset_##__name >= debug->__name.word_count)  \
346                 return -EINVAL;                                 \
347                                                                 \
348         debug->__name.read(intf->rt2x00dev,                     \
349                            intf->offset_##__name, &value);      \
350                                                                 \
351         size = sprintf(line, __format, value);                  \
352                                                                 \
353         if (copy_to_user(buf, line, size))                      \
354                 return -EFAULT;                                 \
355                                                                 \
356         *offset += size;                                        \
357         return size;                                            \
358 }
359
360 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type)                 \
361 static ssize_t rt2x00debug_write_##__name(struct file *file,    \
362                                           const char __user *buf,\
363                                           size_t length,        \
364                                           loff_t *offset)       \
365 {                                                               \
366         struct rt2x00debug_intf *intf = file->private_data;     \
367         const struct rt2x00debug *debug = intf->debug;          \
368         char line[16];                                          \
369         size_t size;                                            \
370         __type value;                                           \
371                                                                 \
372         if (*offset)                                            \
373                 return 0;                                       \
374                                                                 \
375         if (intf->offset_##__name >= debug->__name.word_count)  \
376                 return -EINVAL;                                 \
377                                                                 \
378         if (copy_from_user(line, buf, length))                  \
379                 return -EFAULT;                                 \
380                                                                 \
381         size = strlen(line);                                    \
382         value = simple_strtoul(line, NULL, 0);                  \
383                                                                 \
384         debug->__name.write(intf->rt2x00dev,                    \
385                             intf->offset_##__name, value);      \
386                                                                 \
387         *offset += size;                                        \
388         return size;                                            \
389 }
390
391 #define RT2X00DEBUGFS_OPS(__name, __format, __type)             \
392 RT2X00DEBUGFS_OPS_READ(__name, __format, __type);               \
393 RT2X00DEBUGFS_OPS_WRITE(__name, __type);                        \
394                                                                 \
395 static const struct file_operations rt2x00debug_fop_##__name = {\
396         .owner          = THIS_MODULE,                          \
397         .read           = rt2x00debug_read_##__name,            \
398         .write          = rt2x00debug_write_##__name,           \
399         .open           = rt2x00debug_file_open,                \
400         .release        = rt2x00debug_file_release,             \
401 };
402
403 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
404 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
405 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
406 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
407
408 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
409                                           char __user *buf,
410                                           size_t length,
411                                           loff_t *offset)
412 {
413         struct rt2x00debug_intf *intf = file->private_data;
414         char line[16];
415         size_t size;
416
417         if (*offset)
418                 return 0;
419
420         size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
421
422         if (copy_to_user(buf, line, size))
423                 return -EFAULT;
424
425         *offset += size;
426         return size;
427 }
428
429 static const struct file_operations rt2x00debug_fop_dev_flags = {
430         .owner          = THIS_MODULE,
431         .read           = rt2x00debug_read_dev_flags,
432         .open           = rt2x00debug_file_open,
433         .release        = rt2x00debug_file_release,
434 };
435
436 static struct dentry *rt2x00debug_create_file_driver(const char *name,
437                                                      struct rt2x00debug_intf
438                                                      *intf,
439                                                      struct debugfs_blob_wrapper
440                                                      *blob)
441 {
442         char *data;
443
444         data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
445         if (!data)
446                 return NULL;
447
448         blob->data = data;
449         data += sprintf(data, "driver: %s\n", intf->rt2x00dev->ops->name);
450         data += sprintf(data, "version: %s\n", DRV_VERSION);
451         data += sprintf(data, "compiled: %s %s\n", __DATE__, __TIME__);
452         blob->size = strlen(blob->data);
453
454         return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
455 }
456
457 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
458                                                       struct rt2x00debug_intf
459                                                       *intf,
460                                                       struct
461                                                       debugfs_blob_wrapper
462                                                       *blob)
463 {
464         const struct rt2x00debug *debug = intf->debug;
465         char *data;
466
467         data = kzalloc(8 * MAX_LINE_LENGTH, GFP_KERNEL);
468         if (!data)
469                 return NULL;
470
471         blob->data = data;
472         data += sprintf(data, "rt chip: %04x\n", intf->rt2x00dev->chip.rt);
473         data += sprintf(data, "rf chip: %04x\n", intf->rt2x00dev->chip.rf);
474         data += sprintf(data, "revision:%08x\n", intf->rt2x00dev->chip.rev);
475         data += sprintf(data, "\n");
476         data += sprintf(data, "csr length: %d\n", debug->csr.word_count);
477         data += sprintf(data, "eeprom length: %d\n", debug->eeprom.word_count);
478         data += sprintf(data, "bbp length: %d\n", debug->bbp.word_count);
479         data += sprintf(data, "rf length: %d\n", debug->rf.word_count);
480         blob->size = strlen(blob->data);
481
482         return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
483 }
484
485 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
486 {
487         const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
488         struct rt2x00debug_intf *intf;
489
490         intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
491         if (!intf) {
492                 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
493                 return;
494         }
495
496         intf->debug = debug;
497         intf->rt2x00dev = rt2x00dev;
498         rt2x00dev->debugfs_intf = intf;
499
500         intf->driver_folder =
501             debugfs_create_dir(intf->rt2x00dev->ops->name,
502                                rt2x00dev->hw->wiphy->debugfsdir);
503         if (IS_ERR(intf->driver_folder))
504                 goto exit;
505
506         intf->driver_entry =
507             rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
508         if (IS_ERR(intf->driver_entry))
509                 goto exit;
510
511         intf->chipset_entry =
512             rt2x00debug_create_file_chipset("chipset",
513                                             intf, &intf->chipset_blob);
514         if (IS_ERR(intf->chipset_entry))
515                 goto exit;
516
517         intf->dev_flags = debugfs_create_file("dev_flags", S_IRUSR,
518                                               intf->driver_folder, intf,
519                                               &rt2x00debug_fop_dev_flags);
520         if (IS_ERR(intf->dev_flags))
521                 goto exit;
522
523         intf->register_folder =
524             debugfs_create_dir("register", intf->driver_folder);
525         if (IS_ERR(intf->register_folder))
526                 goto exit;
527
528 #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name)     \
529 ({                                                              \
530         (__intf)->__name##_off_entry =                          \
531             debugfs_create_u32(__stringify(__name) "_offset",   \
532                                S_IRUSR | S_IWUSR,               \
533                                (__intf)->register_folder,       \
534                                &(__intf)->offset_##__name);     \
535         if (IS_ERR((__intf)->__name##_off_entry))               \
536                 goto exit;                                      \
537                                                                 \
538         (__intf)->__name##_val_entry =                          \
539             debugfs_create_file(__stringify(__name) "_value",   \
540                                 S_IRUSR | S_IWUSR,              \
541                                 (__intf)->register_folder,      \
542                                 (__intf), &rt2x00debug_fop_##__name);\
543         if (IS_ERR((__intf)->__name##_val_entry))               \
544                 goto exit;                                      \
545 })
546
547         RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
548         RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
549         RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
550         RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
551
552 #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
553
554         intf->queue_folder =
555             debugfs_create_dir("queue", intf->driver_folder);
556         if (IS_ERR(intf->queue_folder))
557                 goto exit;
558
559         intf->queue_frame_dump_entry =
560             debugfs_create_file("dump", S_IRUSR, intf->queue_folder,
561                                 intf, &rt2x00debug_fop_queue_dump);
562         if (IS_ERR(intf->queue_frame_dump_entry))
563                 goto exit;
564
565         skb_queue_head_init(&intf->frame_dump_skbqueue);
566         init_waitqueue_head(&intf->frame_dump_waitqueue);
567
568         intf->queue_stats_entry =
569             debugfs_create_file("queue", S_IRUSR, intf->queue_folder,
570                                 intf, &rt2x00debug_fop_queue_stats);
571
572         return;
573
574 exit:
575         rt2x00debug_deregister(rt2x00dev);
576         ERROR(rt2x00dev, "Failed to register debug handler.\n");
577
578         return;
579 }
580
581 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
582 {
583         struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
584
585         if (unlikely(!intf))
586                 return;
587
588         skb_queue_purge(&intf->frame_dump_skbqueue);
589
590         debugfs_remove(intf->queue_stats_entry);
591         debugfs_remove(intf->queue_frame_dump_entry);
592         debugfs_remove(intf->queue_folder);
593         debugfs_remove(intf->rf_val_entry);
594         debugfs_remove(intf->rf_off_entry);
595         debugfs_remove(intf->bbp_val_entry);
596         debugfs_remove(intf->bbp_off_entry);
597         debugfs_remove(intf->eeprom_val_entry);
598         debugfs_remove(intf->eeprom_off_entry);
599         debugfs_remove(intf->csr_val_entry);
600         debugfs_remove(intf->csr_off_entry);
601         debugfs_remove(intf->register_folder);
602         debugfs_remove(intf->dev_flags);
603         debugfs_remove(intf->chipset_entry);
604         debugfs_remove(intf->driver_entry);
605         debugfs_remove(intf->driver_folder);
606         kfree(intf->chipset_blob.data);
607         kfree(intf->driver_blob.data);
608         kfree(intf);
609
610         rt2x00dev->debugfs_intf = NULL;
611 }