xref: /linux/drivers/media/usb/uvc/uvc_debugfs.c (revision b7019ac550eb3916f34d79db583e9b7ea2524afa)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      uvc_debugfs.c --  USB Video Class driver - Debugging support
4  *
5  *      Copyright (C) 2011
6  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  */
8 
9 #include <linux/module.h>
10 #include <linux/debugfs.h>
11 #include <linux/slab.h>
12 #include <linux/usb.h>
13 
14 #include "uvcvideo.h"
15 
16 /* -----------------------------------------------------------------------------
17  * Statistics
18  */
19 
20 #define UVC_DEBUGFS_BUF_SIZE	1024
21 
22 struct uvc_debugfs_buffer {
23 	size_t count;
24 	char data[UVC_DEBUGFS_BUF_SIZE];
25 };
26 
27 static int uvc_debugfs_stats_open(struct inode *inode, struct file *file)
28 {
29 	struct uvc_streaming *stream = inode->i_private;
30 	struct uvc_debugfs_buffer *buf;
31 
32 	buf = kmalloc(sizeof(*buf), GFP_KERNEL);
33 	if (buf == NULL)
34 		return -ENOMEM;
35 
36 	buf->count = uvc_video_stats_dump(stream, buf->data, sizeof(buf->data));
37 
38 	file->private_data = buf;
39 	return 0;
40 }
41 
42 static ssize_t uvc_debugfs_stats_read(struct file *file, char __user *user_buf,
43 				      size_t nbytes, loff_t *ppos)
44 {
45 	struct uvc_debugfs_buffer *buf = file->private_data;
46 
47 	return simple_read_from_buffer(user_buf, nbytes, ppos, buf->data,
48 				       buf->count);
49 }
50 
51 static int uvc_debugfs_stats_release(struct inode *inode, struct file *file)
52 {
53 	kfree(file->private_data);
54 	file->private_data = NULL;
55 
56 	return 0;
57 }
58 
59 static const struct file_operations uvc_debugfs_stats_fops = {
60 	.owner = THIS_MODULE,
61 	.open = uvc_debugfs_stats_open,
62 	.llseek = no_llseek,
63 	.read = uvc_debugfs_stats_read,
64 	.release = uvc_debugfs_stats_release,
65 };
66 
67 /* -----------------------------------------------------------------------------
68  * Global and stream initialization/cleanup
69  */
70 
71 static struct dentry *uvc_debugfs_root_dir;
72 
73 void uvc_debugfs_init_stream(struct uvc_streaming *stream)
74 {
75 	struct usb_device *udev = stream->dev->udev;
76 	struct dentry *dent;
77 	char dir_name[32];
78 
79 	if (uvc_debugfs_root_dir == NULL)
80 		return;
81 
82 	sprintf(dir_name, "%u-%u", udev->bus->busnum, udev->devnum);
83 
84 	dent = debugfs_create_dir(dir_name, uvc_debugfs_root_dir);
85 	if (IS_ERR_OR_NULL(dent)) {
86 		uvc_printk(KERN_INFO, "Unable to create debugfs %s "
87 			   "directory.\n", dir_name);
88 		return;
89 	}
90 
91 	stream->debugfs_dir = dent;
92 
93 	dent = debugfs_create_file("stats", 0444, stream->debugfs_dir,
94 				   stream, &uvc_debugfs_stats_fops);
95 	if (IS_ERR_OR_NULL(dent)) {
96 		uvc_printk(KERN_INFO, "Unable to create debugfs stats file.\n");
97 		uvc_debugfs_cleanup_stream(stream);
98 		return;
99 	}
100 }
101 
102 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream)
103 {
104 	debugfs_remove_recursive(stream->debugfs_dir);
105 	stream->debugfs_dir = NULL;
106 }
107 
108 void uvc_debugfs_init(void)
109 {
110 	struct dentry *dir;
111 
112 	dir = debugfs_create_dir("uvcvideo", usb_debug_root);
113 	if (IS_ERR_OR_NULL(dir)) {
114 		uvc_printk(KERN_INFO, "Unable to create debugfs directory\n");
115 		return;
116 	}
117 
118 	uvc_debugfs_root_dir = dir;
119 }
120 
121 void uvc_debugfs_cleanup(void)
122 {
123 	debugfs_remove_recursive(uvc_debugfs_root_dir);
124 }
125