Linux Audio

Check our new training course

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