| 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> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> | 
| Paul Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 12 | #include <linux/export.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/usb.h> | 
| Akinobu Mita | 518386c | 2008-06-09 16:39:57 -0700 | [diff] [blame] | 14 | #include <linux/fs.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <asm/uaccess.h> | 
|  | 16 |  | 
|  | 17 | #include "usb_mon.h" | 
|  | 18 |  | 
|  | 19 | #define STAT_BUF_SIZE  80 | 
|  | 20 |  | 
|  | 21 | struct snap { | 
|  | 22 | int slen; | 
|  | 23 | char str[STAT_BUF_SIZE]; | 
|  | 24 | }; | 
|  | 25 |  | 
|  | 26 | static int mon_stat_open(struct inode *inode, struct file *file) | 
|  | 27 | { | 
|  | 28 | struct mon_bus *mbus; | 
|  | 29 | struct snap *sp; | 
|  | 30 |  | 
|  | 31 | if ((sp = kmalloc(sizeof(struct snap), GFP_KERNEL)) == NULL) | 
|  | 32 | return -ENOMEM; | 
|  | 33 |  | 
| Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 34 | mbus = inode->i_private; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | sp->slen = snprintf(sp->str, STAT_BUF_SIZE, | 
| Pete Zaitcev | 5b1c674 | 2006-06-09 20:10:10 -0700 | [diff] [blame] | 37 | "nreaders %d events %u text_lost %u\n", | 
|  | 38 | mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
|  | 40 | file->private_data = sp; | 
|  | 41 | return 0; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | static ssize_t mon_stat_read(struct file *file, char __user *buf, | 
|  | 45 | size_t nbytes, loff_t *ppos) | 
|  | 46 | { | 
|  | 47 | struct snap *sp = file->private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 |  | 
| Akinobu Mita | 518386c | 2008-06-09 16:39:57 -0700 | [diff] [blame] | 49 | return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
|  | 52 | static int mon_stat_release(struct inode *inode, struct file *file) | 
|  | 53 | { | 
| Ming Lei | d4062fc | 2008-04-14 21:27:00 +0800 | [diff] [blame] | 54 | struct snap *sp = file->private_data; | 
|  | 55 | file->private_data = NULL; | 
|  | 56 | kfree(sp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | return 0; | 
|  | 58 | } | 
|  | 59 |  | 
| Luiz Fernando N. Capitulino | 066202d | 2006-08-05 20:37:11 -0300 | [diff] [blame] | 60 | const struct file_operations mon_fops_stat = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | .owner =	THIS_MODULE, | 
|  | 62 | .open =		mon_stat_open, | 
|  | 63 | .llseek =	no_llseek, | 
|  | 64 | .read =		mon_stat_read, | 
|  | 65 | /* .write =	mon_stat_write, */ | 
|  | 66 | /* .poll =		mon_stat_poll, */ | 
| Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 67 | /* .unlocked_ioctl =	mon_stat_ioctl, */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | .release =	mon_stat_release, | 
|  | 69 | }; |