18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * The USB Monitor, inspired by Dave Harding's USBMon. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This is the 's' or 'stat' reader which debugs usbmon itself. 68c2ecf20Sopenharmony_ci * Note that this code blows through locks, so make sure that 78c2ecf20Sopenharmony_ci * /dbg/usbmon/0s is well protected from non-root users. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include <linux/export.h> 148c2ecf20Sopenharmony_ci#include <linux/usb.h> 158c2ecf20Sopenharmony_ci#include <linux/fs.h> 168c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include "usb_mon.h" 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define STAT_BUF_SIZE 80 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistruct snap { 238c2ecf20Sopenharmony_ci int slen; 248c2ecf20Sopenharmony_ci char str[STAT_BUF_SIZE]; 258c2ecf20Sopenharmony_ci}; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic int mon_stat_open(struct inode *inode, struct file *file) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci struct mon_bus *mbus; 308c2ecf20Sopenharmony_ci struct snap *sp; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci sp = kmalloc(sizeof(struct snap), GFP_KERNEL); 338c2ecf20Sopenharmony_ci if (sp == NULL) 348c2ecf20Sopenharmony_ci return -ENOMEM; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci mbus = inode->i_private; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci sp->slen = snprintf(sp->str, STAT_BUF_SIZE, 398c2ecf20Sopenharmony_ci "nreaders %d events %u text_lost %u\n", 408c2ecf20Sopenharmony_ci mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost); 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci file->private_data = sp; 438c2ecf20Sopenharmony_ci return 0; 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic ssize_t mon_stat_read(struct file *file, char __user *buf, 478c2ecf20Sopenharmony_ci size_t nbytes, loff_t *ppos) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci struct snap *sp = file->private_data; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen); 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistatic int mon_stat_release(struct inode *inode, struct file *file) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci struct snap *sp = file->private_data; 578c2ecf20Sopenharmony_ci file->private_data = NULL; 588c2ecf20Sopenharmony_ci kfree(sp); 598c2ecf20Sopenharmony_ci return 0; 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ciconst struct file_operations mon_fops_stat = { 638c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 648c2ecf20Sopenharmony_ci .open = mon_stat_open, 658c2ecf20Sopenharmony_ci .llseek = no_llseek, 668c2ecf20Sopenharmony_ci .read = mon_stat_read, 678c2ecf20Sopenharmony_ci /* .write = mon_stat_write, */ 688c2ecf20Sopenharmony_ci /* .poll = mon_stat_poll, */ 698c2ecf20Sopenharmony_ci /* .unlocked_ioctl = mon_stat_ioctl, */ 708c2ecf20Sopenharmony_ci .release = mon_stat_release, 718c2ecf20Sopenharmony_ci}; 72