Linux-libre 5.4.49-gnu
[librecmc/linux-libre.git] / tools / testing / selftests / media_tests / video_device_test.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * video_device_test - Video Device Test
5  *
6  * Copyright (c) 2016 Shuah Khan <shuahkh@osg.samsung.com>
7  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
8  *
9  */
10
11 /*
12  * This file adds a test for Video Device. This test should not be included
13  * in the Kselftest run. This test should be run when hardware and driver
14  * that makes use of V4L2 API is present.
15  *
16  * This test opens user specified Video Device and calls video ioctls in a
17  * loop once every 10 seconds.
18  *
19  * Usage:
20  *      sudo ./video_device_test -d /dev/videoX
21  *
22  *      While test is running, remove the device or unbind the driver and
23  *      ensure there are no use after free errors and other Oops in the
24  *      dmesg.
25  *      When possible, enable KaSan kernel config option for use-after-free
26  *      error detection.
27 */
28
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 #include <sys/stat.h>
37 #include <time.h>
38 #include <linux/videodev2.h>
39
40 int main(int argc, char **argv)
41 {
42         int opt;
43         char video_dev[256];
44         int count;
45         struct v4l2_tuner vtuner;
46         struct v4l2_capability vcap;
47         int ret;
48         int fd;
49
50         if (argc < 2) {
51                 printf("Usage: %s [-d </dev/videoX>]\n", argv[0]);
52                 exit(-1);
53         }
54
55         /* Process arguments */
56         while ((opt = getopt(argc, argv, "d:")) != -1) {
57                 switch (opt) {
58                 case 'd':
59                         strncpy(video_dev, optarg, sizeof(video_dev) - 1);
60                         video_dev[sizeof(video_dev)-1] = '\0';
61                         break;
62                 default:
63                         printf("Usage: %s [-d </dev/videoX>]\n", argv[0]);
64                         exit(-1);
65                 }
66         }
67
68         /* Generate random number of interations */
69         srand((unsigned int) time(NULL));
70         count = rand();
71
72         /* Open Video device and keep it open */
73         fd = open(video_dev, O_RDWR);
74         if (fd == -1) {
75                 printf("Video Device open errno %s\n", strerror(errno));
76                 exit(-1);
77         }
78
79         printf("\nNote:\n"
80                "While test is running, remove the device or unbind\n"
81                "driver and ensure there are no use after free errors\n"
82                "and other Oops in the dmesg. When possible, enable KaSan\n"
83                "kernel config option for use-after-free error detection.\n\n");
84
85         while (count > 0) {
86                 ret = ioctl(fd, VIDIOC_QUERYCAP, &vcap);
87                 if (ret < 0)
88                         printf("VIDIOC_QUERYCAP errno %s\n", strerror(errno));
89                 else
90                         printf("Video device driver %s\n", vcap.driver);
91
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 }