Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *      uvc_debugfs.c --  USB Video Class driver - Debugging support
0004  *
0005  *      Copyright (C) 2011
0006  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/debugfs.h>
0011 #include <linux/slab.h>
0012 #include <linux/usb.h>
0013 
0014 #include "uvcvideo.h"
0015 
0016 /* -----------------------------------------------------------------------------
0017  * Statistics
0018  */
0019 
0020 #define UVC_DEBUGFS_BUF_SIZE    1024
0021 
0022 struct uvc_debugfs_buffer {
0023     size_t count;
0024     char data[UVC_DEBUGFS_BUF_SIZE];
0025 };
0026 
0027 static int uvc_debugfs_stats_open(struct inode *inode, struct file *file)
0028 {
0029     struct uvc_streaming *stream = inode->i_private;
0030     struct uvc_debugfs_buffer *buf;
0031 
0032     buf = kmalloc(sizeof(*buf), GFP_KERNEL);
0033     if (buf == NULL)
0034         return -ENOMEM;
0035 
0036     buf->count = uvc_video_stats_dump(stream, buf->data, sizeof(buf->data));
0037 
0038     file->private_data = buf;
0039     return 0;
0040 }
0041 
0042 static ssize_t uvc_debugfs_stats_read(struct file *file, char __user *user_buf,
0043                       size_t nbytes, loff_t *ppos)
0044 {
0045     struct uvc_debugfs_buffer *buf = file->private_data;
0046 
0047     return simple_read_from_buffer(user_buf, nbytes, ppos, buf->data,
0048                        buf->count);
0049 }
0050 
0051 static int uvc_debugfs_stats_release(struct inode *inode, struct file *file)
0052 {
0053     kfree(file->private_data);
0054     file->private_data = NULL;
0055 
0056     return 0;
0057 }
0058 
0059 static const struct file_operations uvc_debugfs_stats_fops = {
0060     .owner = THIS_MODULE,
0061     .open = uvc_debugfs_stats_open,
0062     .llseek = no_llseek,
0063     .read = uvc_debugfs_stats_read,
0064     .release = uvc_debugfs_stats_release,
0065 };
0066 
0067 /* -----------------------------------------------------------------------------
0068  * Global and stream initialization/cleanup
0069  */
0070 
0071 static struct dentry *uvc_debugfs_root_dir;
0072 
0073 void uvc_debugfs_init_stream(struct uvc_streaming *stream)
0074 {
0075     struct usb_device *udev = stream->dev->udev;
0076     char dir_name[33];
0077 
0078     if (uvc_debugfs_root_dir == NULL)
0079         return;
0080 
0081     snprintf(dir_name, sizeof(dir_name), "%u-%u-%u", udev->bus->busnum,
0082          udev->devnum, stream->intfnum);
0083 
0084     stream->debugfs_dir = debugfs_create_dir(dir_name,
0085                          uvc_debugfs_root_dir);
0086 
0087     debugfs_create_file("stats", 0444, stream->debugfs_dir, stream,
0088                 &uvc_debugfs_stats_fops);
0089 }
0090 
0091 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream)
0092 {
0093     debugfs_remove_recursive(stream->debugfs_dir);
0094     stream->debugfs_dir = NULL;
0095 }
0096 
0097 void uvc_debugfs_init(void)
0098 {
0099     uvc_debugfs_root_dir = debugfs_create_dir("uvcvideo", usb_debug_root);
0100 }
0101 
0102 void uvc_debugfs_cleanup(void)
0103 {
0104     debugfs_remove_recursive(uvc_debugfs_root_dir);
0105 }