diff options
Diffstat (limited to 'drivers/usb/mon/mon_stat.c')
-rw-r--r-- | drivers/usb/mon/mon_stat.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/drivers/usb/mon/mon_stat.c b/drivers/usb/mon/mon_stat.c new file mode 100644 index 000000000000..6e4b165d070a --- /dev/null +++ b/drivers/usb/mon/mon_stat.c | |||
@@ -0,0 +1,74 @@ | |||
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 | |||
31 | mbus = inode->u.generic_ip; | ||
32 | |||
33 | sp->slen = snprintf(sp->str, STAT_BUF_SIZE, | ||
34 | "nreaders %d text_lost %u\n", | ||
35 | mbus->nreaders, mbus->cnt_text_lost); | ||
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 | |||
65 | struct file_operations mon_fops_stat = { | ||
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 | }; | ||