Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / usb / uvc / uvc_debugfs.c
blob368f8f8dfcb5c5917e63fbf1cf25bb80f1d4ed50
1 /*
2 * uvc_debugfs.c -- USB Video Class driver - Debugging support
4 * Copyright (C) 2011
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/debugfs.h>
16 #include <linux/slab.h>
17 #include <linux/usb.h>
19 #include "uvcvideo.h"
21 /* -----------------------------------------------------------------------------
22 * Statistics
25 #define UVC_DEBUGFS_BUF_SIZE 1024
27 struct uvc_debugfs_buffer {
28 size_t count;
29 char data[UVC_DEBUGFS_BUF_SIZE];
32 static int uvc_debugfs_stats_open(struct inode *inode, struct file *file)
34 struct uvc_streaming *stream = inode->i_private;
35 struct uvc_debugfs_buffer *buf;
37 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
38 if (buf == NULL)
39 return -ENOMEM;
41 buf->count = uvc_video_stats_dump(stream, buf->data, sizeof(buf->data));
43 file->private_data = buf;
44 return 0;
47 static ssize_t uvc_debugfs_stats_read(struct file *file, char __user *user_buf,
48 size_t nbytes, loff_t *ppos)
50 struct uvc_debugfs_buffer *buf = file->private_data;
52 return simple_read_from_buffer(user_buf, nbytes, ppos, buf->data,
53 buf->count);
56 static int uvc_debugfs_stats_release(struct inode *inode, struct file *file)
58 kfree(file->private_data);
59 file->private_data = NULL;
61 return 0;
64 static const struct file_operations uvc_debugfs_stats_fops = {
65 .owner = THIS_MODULE,
66 .open = uvc_debugfs_stats_open,
67 .llseek = no_llseek,
68 .read = uvc_debugfs_stats_read,
69 .release = uvc_debugfs_stats_release,
72 /* -----------------------------------------------------------------------------
73 * Global and stream initialization/cleanup
76 static struct dentry *uvc_debugfs_root_dir;
78 void uvc_debugfs_init_stream(struct uvc_streaming *stream)
80 struct usb_device *udev = stream->dev->udev;
81 struct dentry *dent;
82 char dir_name[32];
84 if (uvc_debugfs_root_dir == NULL)
85 return;
87 sprintf(dir_name, "%u-%u", udev->bus->busnum, udev->devnum);
89 dent = debugfs_create_dir(dir_name, uvc_debugfs_root_dir);
90 if (IS_ERR_OR_NULL(dent)) {
91 uvc_printk(KERN_INFO, "Unable to create debugfs %s "
92 "directory.\n", dir_name);
93 return;
96 stream->debugfs_dir = dent;
98 dent = debugfs_create_file("stats", 0444, stream->debugfs_dir,
99 stream, &uvc_debugfs_stats_fops);
100 if (IS_ERR_OR_NULL(dent)) {
101 uvc_printk(KERN_INFO, "Unable to create debugfs stats file.\n");
102 uvc_debugfs_cleanup_stream(stream);
103 return;
107 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream)
109 if (stream->debugfs_dir == NULL)
110 return;
112 debugfs_remove_recursive(stream->debugfs_dir);
113 stream->debugfs_dir = NULL;
116 void uvc_debugfs_init(void)
118 struct dentry *dir;
120 dir = debugfs_create_dir("uvcvideo", usb_debug_root);
121 if (IS_ERR_OR_NULL(dir)) {
122 uvc_printk(KERN_INFO, "Unable to create debugfs directory\n");
123 return;
126 uvc_debugfs_root_dir = dir;
129 void uvc_debugfs_cleanup(void)
131 if (uvc_debugfs_root_dir != NULL)
132 debugfs_remove_recursive(uvc_debugfs_root_dir);