video_device_test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * video_device_test - Video Device Test
  4. *
  5. * Copyright (c) 2016 Shuah Khan <shuahkh@osg.samsung.com>
  6. * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  7. *
  8. */
  9. /*
  10. * This file adds a test for Video Device. This test should not be included
  11. * in the Kselftest run. This test should be run when hardware and driver
  12. * that makes use of V4L2 API is present.
  13. *
  14. * This test opens user specified Video Device and calls video ioctls in a
  15. * loop once every 10 seconds.
  16. *
  17. * Usage:
  18. * sudo ./video_device_test -d /dev/videoX
  19. *
  20. * While test is running, remove the device or unbind the driver and
  21. * ensure there are no use after free errors and other Oops in the
  22. * dmesg.
  23. * When possible, enable KaSan kernel config option for use-after-free
  24. * error detection.
  25. */
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <fcntl.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/stat.h>
  34. #include <time.h>
  35. #include <linux/videodev2.h>
  36. #define PRIORITY_MAX 4
  37. int priority_test(int fd)
  38. {
  39. /* This test will try to update the priority associated with a file descriptor */
  40. enum v4l2_priority old_priority, new_priority, priority_to_compare;
  41. int ret;
  42. int result = 0;
  43. ret = ioctl(fd, VIDIOC_G_PRIORITY, &old_priority);
  44. if (ret < 0) {
  45. printf("Failed to get priority: %s\n", strerror(errno));
  46. return -1;
  47. }
  48. new_priority = (old_priority + 1) % PRIORITY_MAX;
  49. ret = ioctl(fd, VIDIOC_S_PRIORITY, &new_priority);
  50. if (ret < 0) {
  51. printf("Failed to set priority: %s\n", strerror(errno));
  52. return -1;
  53. }
  54. ret = ioctl(fd, VIDIOC_G_PRIORITY, &priority_to_compare);
  55. if (ret < 0) {
  56. printf("Failed to get new priority: %s\n", strerror(errno));
  57. result = -1;
  58. goto cleanup;
  59. }
  60. if (priority_to_compare != new_priority) {
  61. printf("Priority wasn't set - test failed\n");
  62. result = -1;
  63. }
  64. cleanup:
  65. ret = ioctl(fd, VIDIOC_S_PRIORITY, &old_priority);
  66. if (ret < 0) {
  67. printf("Failed to restore priority: %s\n", strerror(errno));
  68. return -1;
  69. }
  70. return result;
  71. }
  72. int loop_test(int fd)
  73. {
  74. int count;
  75. struct v4l2_tuner vtuner;
  76. struct v4l2_capability vcap;
  77. int ret;
  78. /* Generate random number of interations */
  79. srand((unsigned int) time(NULL));
  80. count = rand();
  81. printf("\nNote:\n"
  82. "While test is running, remove the device or unbind\n"
  83. "driver and ensure there are no use after free errors\n"
  84. "and other Oops in the dmesg. When possible, enable KaSan\n"
  85. "kernel config option for use-after-free error detection.\n\n");
  86. while (count > 0) {
  87. ret = ioctl(fd, VIDIOC_QUERYCAP, &vcap);
  88. if (ret < 0)
  89. printf("VIDIOC_QUERYCAP errno %s\n", strerror(errno));
  90. else
  91. printf("Video device driver %s\n", vcap.driver);
  92. ret = ioctl(fd, VIDIOC_G_TUNER, &vtuner);
  93. if (ret < 0)
  94. printf("VIDIOC_G_TUNER, errno %s\n", strerror(errno));
  95. else
  96. printf("type %d rangelow %d rangehigh %d\n",
  97. vtuner.type, vtuner.rangelow, vtuner.rangehigh);
  98. sleep(10);
  99. count--;
  100. }
  101. return 0;
  102. }
  103. int main(int argc, char **argv)
  104. {
  105. int opt;
  106. char video_dev[256];
  107. int fd;
  108. int test_result;
  109. if (argc < 2) {
  110. printf("Usage: %s [-d </dev/videoX>]\n", argv[0]);
  111. exit(-1);
  112. }
  113. /* Process arguments */
  114. while ((opt = getopt(argc, argv, "d:")) != -1) {
  115. switch (opt) {
  116. case 'd':
  117. strncpy(video_dev, optarg, sizeof(video_dev) - 1);
  118. video_dev[sizeof(video_dev)-1] = '\0';
  119. break;
  120. default:
  121. printf("Usage: %s [-d </dev/videoX>]\n", argv[0]);
  122. exit(-1);
  123. }
  124. }
  125. /* Open Video device and keep it open */
  126. fd = open(video_dev, O_RDWR);
  127. if (fd == -1) {
  128. printf("Video Device open errno %s\n", strerror(errno));
  129. exit(-1);
  130. }
  131. test_result = priority_test(fd);
  132. if (!test_result)
  133. printf("Priority test - PASSED\n");
  134. else
  135. printf("Priority test - FAILED\n");
  136. loop_test(fd);
  137. }