^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * The USB Monitor, inspired by Dave Harding's USBMon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This is the 's' or 'stat' reader which debugs usbmon itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Note that this code blows through locks, so make sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * /dbg/usbmon/0s is well protected from non-root users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "usb_mon.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define STAT_BUF_SIZE 80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct snap {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) char str[STAT_BUF_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int mon_stat_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct mon_bus *mbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct snap *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) sp = kmalloc(sizeof(struct snap), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (sp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) mbus = inode->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) sp->slen = snprintf(sp->str, STAT_BUF_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) "nreaders %d events %u text_lost %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) file->private_data = sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static ssize_t mon_stat_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) size_t nbytes, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct snap *sp = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int mon_stat_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct snap *sp = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) kfree(sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const struct file_operations mon_fops_stat = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .open = mon_stat_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .read = mon_stat_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* .write = mon_stat_write, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* .poll = mon_stat_poll, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* .unlocked_ioctl = mon_stat_ioctl, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .release = mon_stat_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };