xref: /linux/drivers/usb/mon/mon_stat.c (revision 3fd6c59042dbba50391e30862beac979491145fe)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * The USB Monitor, inspired by Dave Harding's USBMon.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This is the 's' or 'stat' reader which debugs usbmon itself.
61da177e4SLinus Torvalds  * Note that this code blows through locks, so make sure that
71da177e4SLinus Torvalds  * /dbg/usbmon/0s is well protected from non-root users.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/kernel.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
13f940fcd8SPaul Gortmaker #include <linux/export.h>
141da177e4SLinus Torvalds #include <linux/usb.h>
15518386c7SAkinobu Mita #include <linux/fs.h>
167c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include "usb_mon.h"
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #define STAT_BUF_SIZE  80
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds struct snap {
231da177e4SLinus Torvalds 	int slen;
241da177e4SLinus Torvalds 	char str[STAT_BUF_SIZE];
251da177e4SLinus Torvalds };
261da177e4SLinus Torvalds 
mon_stat_open(struct inode * inode,struct file * file)271da177e4SLinus Torvalds static int mon_stat_open(struct inode *inode, struct file *file)
281da177e4SLinus Torvalds {
291da177e4SLinus Torvalds 	struct mon_bus *mbus;
301da177e4SLinus Torvalds 	struct snap *sp;
311da177e4SLinus Torvalds 
324c08ccf0SGreg Kroah-Hartman 	sp = kmalloc(sizeof(struct snap), GFP_KERNEL);
334c08ccf0SGreg Kroah-Hartman 	if (sp == NULL)
341da177e4SLinus Torvalds 		return -ENOMEM;
351da177e4SLinus Torvalds 
368e18e294STheodore Ts'o 	mbus = inode->i_private;
371da177e4SLinus Torvalds 
38*79632569SLee Jones 	sp->slen = scnprintf(sp->str, STAT_BUF_SIZE,
395b1c674dSPete Zaitcev 			     "nreaders %d events %u text_lost %u\n",
405b1c674dSPete Zaitcev 			     mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost);
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	file->private_data = sp;
431da177e4SLinus Torvalds 	return 0;
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds 
mon_stat_read(struct file * file,char __user * buf,size_t nbytes,loff_t * ppos)461da177e4SLinus Torvalds static ssize_t mon_stat_read(struct file *file, char __user *buf,
471da177e4SLinus Torvalds 				size_t nbytes, loff_t *ppos)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	struct snap *sp = file->private_data;
501da177e4SLinus Torvalds 
51518386c7SAkinobu Mita 	return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen);
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds 
mon_stat_release(struct inode * inode,struct file * file)541da177e4SLinus Torvalds static int mon_stat_release(struct inode *inode, struct file *file)
551da177e4SLinus Torvalds {
56d4062fcbSMing Lei 	struct snap *sp = file->private_data;
57d4062fcbSMing Lei 	file->private_data = NULL;
58d4062fcbSMing Lei 	kfree(sp);
591da177e4SLinus Torvalds 	return 0;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
62066202ddSLuiz Fernando N. Capitulino const struct file_operations mon_fops_stat = {
631da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
641da177e4SLinus Torvalds 	.open =		mon_stat_open,
651da177e4SLinus Torvalds 	.read =		mon_stat_read,
661da177e4SLinus Torvalds 	/* .write =	mon_stat_write, */
671da177e4SLinus Torvalds 	/* .poll =		mon_stat_poll, */
6855929332SArnd Bergmann 	/* .unlocked_ioctl =	mon_stat_ioctl, */
691da177e4SLinus Torvalds 	.release =	mon_stat_release,
701da177e4SLinus Torvalds };
71