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