uvc_debugfs.c 3.2 KB

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