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