xref: /linux/drivers/usb/mon/mon_stat.c (revision 066202dd48cf3296b6cc22b5fcf89aef33fa0efc)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * The USB Monitor, inspired by Dave Harding's USBMon.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * This is the 's' or 'stat' reader which debugs usbmon itself.
51da177e4SLinus Torvalds  * Note that this code blows through locks, so make sure that
61da177e4SLinus Torvalds  * /dbg/usbmon/0s is well protected from non-root users.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/kernel.h>
111da177e4SLinus Torvalds #include <linux/usb.h>
121da177e4SLinus Torvalds #include <asm/uaccess.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include "usb_mon.h"
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #define STAT_BUF_SIZE  80
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds struct snap {
191da177e4SLinus Torvalds 	int slen;
201da177e4SLinus Torvalds 	char str[STAT_BUF_SIZE];
211da177e4SLinus Torvalds };
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds static int mon_stat_open(struct inode *inode, struct file *file)
241da177e4SLinus Torvalds {
251da177e4SLinus Torvalds 	struct mon_bus *mbus;
261da177e4SLinus Torvalds 	struct snap *sp;
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds 	if ((sp = kmalloc(sizeof(struct snap), GFP_KERNEL)) == NULL)
291da177e4SLinus Torvalds 		return -ENOMEM;
301da177e4SLinus Torvalds 
318e18e294STheodore Ts'o 	mbus = inode->i_private;
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 	sp->slen = snprintf(sp->str, STAT_BUF_SIZE,
345b1c674dSPete Zaitcev 	    "nreaders %d events %u text_lost %u\n",
355b1c674dSPete Zaitcev 	    mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost);
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	file->private_data = sp;
381da177e4SLinus Torvalds 	return 0;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds static ssize_t mon_stat_read(struct file *file, char __user *buf,
421da177e4SLinus Torvalds 				size_t nbytes, loff_t *ppos)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	struct snap *sp = file->private_data;
451da177e4SLinus Torvalds 	loff_t pos = *ppos;
461da177e4SLinus Torvalds 	int cnt;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	if (pos < 0 || pos >= sp->slen)
491da177e4SLinus Torvalds 		return 0;
501da177e4SLinus Torvalds 	if (nbytes == 0)
511da177e4SLinus Torvalds 		return 0;
521da177e4SLinus Torvalds 	if ((cnt = sp->slen - pos) > nbytes)
531da177e4SLinus Torvalds 		cnt = nbytes;
541da177e4SLinus Torvalds 	if (copy_to_user(buf, sp->str + pos, cnt))
551da177e4SLinus Torvalds 		return -EFAULT;
561da177e4SLinus Torvalds 	*ppos = pos + cnt;
571da177e4SLinus Torvalds 	return cnt;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static int mon_stat_release(struct inode *inode, struct file *file)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	return 0;
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
65*066202ddSLuiz Fernando N. Capitulino const struct file_operations mon_fops_stat = {
661da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
671da177e4SLinus Torvalds 	.open =		mon_stat_open,
681da177e4SLinus Torvalds 	.llseek =	no_llseek,
691da177e4SLinus Torvalds 	.read =		mon_stat_read,
701da177e4SLinus Torvalds 	/* .write =	mon_stat_write, */
711da177e4SLinus Torvalds 	/* .poll =		mon_stat_poll, */
721da177e4SLinus Torvalds 	/* .ioctl =	mon_stat_ioctl, */
731da177e4SLinus Torvalds 	.release =	mon_stat_release,
741da177e4SLinus Torvalds };
75