Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[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                             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 + desc->data_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(desc->data_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(desc->frame_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, desc->data_len), desc->data, desc->data_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 (!capable(CAP_NET_ADMIN))                            \
376                 return -EPERM;                                  \
377                                                                 \
378         if (intf->offset_##__name >= debug->__name.word_count)  \
379                 return -EINVAL;                                 \
380                                                                 \
381         if (copy_from_user(line, buf, length))                  \
382                 return -EFAULT;                                 \
383                                                                 \
384         size = strlen(line);                                    \
385         value = simple_strtoul(line, NULL, 0);                  \
386                                                                 \
387         debug->__name.write(intf->rt2x00dev,                    \
388                             intf->offset_##__name, value);      \
389                                                                 \
390         *offset += size;                                        \
391         return size;                                            \
392 }
393
394 #define RT2X00DEBUGFS_OPS(__name, __format, __type)             \
395 RT2X00DEBUGFS_OPS_READ(__name, __format, __type);               \
396 RT2X00DEBUGFS_OPS_WRITE(__name, __type);                        \
397                                                                 \
398 static const struct file_operations rt2x00debug_fop_##__name = {\
399         .owner          = THIS_MODULE,                          \
400         .read           = rt2x00debug_read_##__name,            \
401         .write          = rt2x00debug_write_##__name,           \
402         .open           = rt2x00debug_file_open,                \
403         .release        = rt2x00debug_file_release,             \
404 };
405
406 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
407 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
408 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
409 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
410
411 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
412                                           char __user *buf,
413                                           size_t length,
414                                           loff_t *offset)
415 {
416         struct rt2x00debug_intf *intf = file->private_data;
417         char line[16];
418         size_t size;
419
420         if (*offset)
421                 return 0;
422
423         size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
424
425         if (copy_to_user(buf, line, size))
426                 return -EFAULT;
427
428         *offset += size;
429         return size;
430 }
431
432 static const struct file_operations rt2x00debug_fop_dev_flags = {
433         .owner          = THIS_MODULE,
434         .read           = rt2x00debug_read_dev_flags,
435         .open           = rt2x00debug_file_open,
436         .release        = rt2x00debug_file_release,
437 };
438
439 static struct dentry *rt2x00debug_create_file_driver(const char *name,
440                                                      struct rt2x00debug_intf
441                                                      *intf,
442                                                      struct debugfs_blob_wrapper
443                                                      *blob)
444 {
445         char *data;
446
447         data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
448         if (!data)
449                 return NULL;
450
451         blob->data = data;
452         data += sprintf(data, "driver: %s\n", intf->rt2x00dev->ops->name);
453         data += sprintf(data, "version: %s\n", DRV_VERSION);
454         data += sprintf(data, "compiled: %s %s\n", __DATE__, __TIME__);
455         blob->size = strlen(blob->data);
456
457         return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
458 }
459
460 static struct dentry *rt2x00debug_create_file_chipset(const char *name,
461                                                       struct rt2x00debug_intf
462                                                       *intf,
463                                                       struct
464                                                       debugfs_blob_wrapper
465                                                       *blob)
466 {
467         const struct rt2x00debug *debug = intf->debug;
468         char *data;
469
470         data = kzalloc(8 * MAX_LINE_LENGTH, GFP_KERNEL);
471         if (!data)
472                 return NULL;
473
474         blob->data = data;
475         data += sprintf(data, "rt chip: %04x\n", intf->rt2x00dev->chip.rt);
476         data += sprintf(data, "rf chip: %04x\n", intf->rt2x00dev->chip.rf);
477         data += sprintf(data, "revision:%08x\n", intf->rt2x00dev->chip.rev);
478         data += sprintf(data, "\n");
479         data += sprintf(data, "csr length: %d\n", debug->csr.word_count);
480         data += sprintf(data, "eeprom length: %d\n", debug->eeprom.word_count);
481         data += sprintf(data, "bbp length: %d\n", debug->bbp.word_count);
482         data += sprintf(data, "rf length: %d\n", debug->rf.word_count);
483         blob->size = strlen(blob->data);
484
485         return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob);
486 }
487
488 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
489 {
490         const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
491         struct rt2x00debug_intf *intf;
492
493         intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
494         if (!intf) {
495                 ERROR(rt2x00dev, "Failed to allocate debug handler.\n");
496                 return;
497         }
498
499         intf->debug = debug;
500         intf->rt2x00dev = rt2x00dev;
501         rt2x00dev->debugfs_intf = intf;
502
503         intf->driver_folder =
504             debugfs_create_dir(intf->rt2x00dev->ops->name,
505                                rt2x00dev->hw->wiphy->debugfsdir);
506         if (IS_ERR(intf->driver_folder))
507                 goto exit;
508
509         intf->driver_entry =
510             rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
511         if (IS_ERR(intf->driver_entry))
512                 goto exit;
513
514         intf->chipset_entry =
515             rt2x00debug_create_file_chipset("chipset",
516                                             intf, &intf->chipset_blob);
517         if (IS_ERR(intf->chipset_entry))
518                 goto exit;
519
520         intf->dev_flags = debugfs_create_file("dev_flags", S_IRUGO,
521                                               intf->driver_folder, intf,
522                                               &rt2x00debug_fop_dev_flags);
523         if (IS_ERR(intf->dev_flags))
524                 goto exit;
525
526         intf->register_folder =
527             debugfs_create_dir("register", intf->driver_folder);
528         if (IS_ERR(intf->register_folder))
529                 goto exit;
530
531 #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name)     \
532 ({                                                              \
533         (__intf)->__name##_off_entry =                          \
534             debugfs_create_u32(__stringify(__name) "_offset",   \
535                                S_IRUGO | S_IWUSR,               \
536                                (__intf)->register_folder,       \
537                                &(__intf)->offset_##__name);     \
538         if (IS_ERR((__intf)->__name##_off_entry))               \
539                 goto exit;                                      \
540                                                                 \
541         (__intf)->__name##_val_entry =                          \
542             debugfs_create_file(__stringify(__name) "_value",   \
543                                 S_IRUGO | S_IWUSR,              \
544                                 (__intf)->register_folder,      \
545                                 (__intf), &rt2x00debug_fop_##__name);\
546         if (IS_ERR((__intf)->__name##_val_entry))               \
547                 goto exit;                                      \
548 })
549
550         RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
551         RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
552         RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
553         RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
554
555 #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
556
557         intf->queue_folder =
558             debugfs_create_dir("queue", intf->driver_folder);
559         if (IS_ERR(intf->queue_folder))
560                 goto exit;
561
562         intf->queue_frame_dump_entry =
563             debugfs_create_file("dump", S_IRUGO, intf->queue_folder,
564                                 intf, &rt2x00debug_fop_queue_dump);
565         if (IS_ERR(intf->queue_frame_dump_entry))
566                 goto exit;
567
568         skb_queue_head_init(&intf->frame_dump_skbqueue);
569         init_waitqueue_head(&intf->frame_dump_waitqueue);
570
571         intf->queue_stats_entry =
572             debugfs_create_file("queue", S_IRUGO, intf->queue_folder,
573                                 intf, &rt2x00debug_fop_queue_stats);
574
575         return;
576
577 exit:
578         rt2x00debug_deregister(rt2x00dev);
579         ERROR(rt2x00dev, "Failed to register debug handler.\n");
580
581         return;
582 }
583
584 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
585 {
586         struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
587
588         if (unlikely(!intf))
589                 return;
590
591         skb_queue_purge(&intf->frame_dump_skbqueue);
592
593         debugfs_remove(intf->queue_stats_entry);
594         debugfs_remove(intf->queue_frame_dump_entry);
595         debugfs_remove(intf->queue_folder);
596         debugfs_remove(intf->rf_val_entry);
597         debugfs_remove(intf->rf_off_entry);
598         debugfs_remove(intf->bbp_val_entry);
599         debugfs_remove(intf->bbp_off_entry);
600         debugfs_remove(intf->eeprom_val_entry);
601         debugfs_remove(intf->eeprom_off_entry);
602         debugfs_remove(intf->csr_val_entry);
603         debugfs_remove(intf->csr_off_entry);
604         debugfs_remove(intf->register_folder);
605         debugfs_remove(intf->dev_flags);
606         debugfs_remove(intf->chipset_entry);
607         debugfs_remove(intf->driver_entry);
608         debugfs_remove(intf->driver_folder);
609         kfree(intf->chipset_blob.data);
610         kfree(intf->driver_blob.data);
611         kfree(intf);
612
613         rt2x00dev->debugfs_intf = NULL;
614 }