uvc_debugfs.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <linux/module.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/slab.h>
  11. #include <linux/usb.h>
  12. #include "uvcvideo.h"
  13. /* -----------------------------------------------------------------------------
  14. * Statistics
  15. */
  16. #define UVC_DEBUGFS_BUF_SIZE 1024
  17. struct uvc_debugfs_buffer {
  18. size_t count;
  19. char data[UVC_DEBUGFS_BUF_SIZE];
  20. };
  21. static int uvc_debugfs_stats_open(struct inode *inode, struct file *file)
  22. {
  23. struct uvc_streaming *stream = inode->i_private;
  24. struct uvc_debugfs_buffer *buf;
  25. buf = kmalloc_obj(*buf);
  26. if (buf == NULL)
  27. return -ENOMEM;
  28. buf->count = uvc_video_stats_dump(stream, buf->data, sizeof(buf->data));
  29. file->private_data = buf;
  30. return 0;
  31. }
  32. static ssize_t uvc_debugfs_stats_read(struct file *file, char __user *user_buf,
  33. size_t nbytes, loff_t *ppos)
  34. {
  35. struct uvc_debugfs_buffer *buf = file->private_data;
  36. return simple_read_from_buffer(user_buf, nbytes, ppos, buf->data,
  37. buf->count);
  38. }
  39. static int uvc_debugfs_stats_release(struct inode *inode, struct file *file)
  40. {
  41. kfree(file->private_data);
  42. file->private_data = NULL;
  43. return 0;
  44. }
  45. static const struct file_operations uvc_debugfs_stats_fops = {
  46. .owner = THIS_MODULE,
  47. .open = uvc_debugfs_stats_open,
  48. .read = uvc_debugfs_stats_read,
  49. .release = uvc_debugfs_stats_release,
  50. };
  51. /* -----------------------------------------------------------------------------
  52. * Global and stream initialization/cleanup
  53. */
  54. static struct dentry *uvc_debugfs_root_dir;
  55. void uvc_debugfs_init_stream(struct uvc_streaming *stream)
  56. {
  57. struct usb_device *udev = stream->dev->udev;
  58. char dir_name[33];
  59. if (uvc_debugfs_root_dir == NULL)
  60. return;
  61. snprintf(dir_name, sizeof(dir_name), "%u-%u-%u", udev->bus->busnum,
  62. udev->devnum, stream->intfnum);
  63. stream->debugfs_dir = debugfs_create_dir(dir_name,
  64. uvc_debugfs_root_dir);
  65. debugfs_create_file("stats", 0444, stream->debugfs_dir, stream,
  66. &uvc_debugfs_stats_fops);
  67. }
  68. void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream)
  69. {
  70. debugfs_remove_recursive(stream->debugfs_dir);
  71. stream->debugfs_dir = NULL;
  72. }
  73. void uvc_debugfs_init(void)
  74. {
  75. uvc_debugfs_root_dir = debugfs_create_dir("uvcvideo", usb_debug_root);
  76. }
  77. void uvc_debugfs_cleanup(void)
  78. {
  79. debugfs_remove_recursive(uvc_debugfs_root_dir);
  80. }