Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * The USB Monitor, inspired by Dave Harding's USBMon. |
| 3 | * |
| 4 | * This is the 's' or 'stat' reader which debugs usbmon itself. |
| 5 | * Note that this code blows through locks, so make sure that |
| 6 | * /dbg/usbmon/0s is well protected from non-root users. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/usb.h> |
Akinobu Mita | 518386c | 2008-06-09 16:39:57 -0700 | [diff] [blame] | 12 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/uaccess.h> |
| 14 | |
| 15 | #include "usb_mon.h" |
| 16 | |
| 17 | #define STAT_BUF_SIZE 80 |
| 18 | |
| 19 | struct snap { |
| 20 | int slen; |
| 21 | char str[STAT_BUF_SIZE]; |
| 22 | }; |
| 23 | |
| 24 | static int mon_stat_open(struct inode *inode, struct file *file) |
| 25 | { |
| 26 | struct mon_bus *mbus; |
| 27 | struct snap *sp; |
| 28 | |
| 29 | if ((sp = kmalloc(sizeof(struct snap), GFP_KERNEL)) == NULL) |
| 30 | return -ENOMEM; |
| 31 | |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 32 | mbus = inode->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | sp->slen = snprintf(sp->str, STAT_BUF_SIZE, |
Pete Zaitcev | 5b1c674 | 2006-06-09 20:10:10 -0700 | [diff] [blame] | 35 | "nreaders %d events %u text_lost %u\n", |
| 36 | mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | file->private_data = sp; |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static ssize_t mon_stat_read(struct file *file, char __user *buf, |
| 43 | size_t nbytes, loff_t *ppos) |
| 44 | { |
| 45 | struct snap *sp = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Akinobu Mita | 518386c | 2008-06-09 16:39:57 -0700 | [diff] [blame] | 47 | return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static int mon_stat_release(struct inode *inode, struct file *file) |
| 51 | { |
Ming Lei | d4062fc | 2008-04-14 21:27:00 +0800 | [diff] [blame] | 52 | struct snap *sp = file->private_data; |
| 53 | file->private_data = NULL; |
| 54 | kfree(sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
Luiz Fernando N. Capitulino | 066202d | 2006-08-05 20:37:11 -0300 | [diff] [blame] | 58 | const struct file_operations mon_fops_stat = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | .owner = THIS_MODULE, |
| 60 | .open = mon_stat_open, |
| 61 | .llseek = no_llseek, |
| 62 | .read = mon_stat_read, |
| 63 | /* .write = mon_stat_write, */ |
| 64 | /* .poll = mon_stat_poll, */ |
| 65 | /* .ioctl = mon_stat_ioctl, */ |
| 66 | .release = mon_stat_release, |
| 67 | }; |