Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /*
0004  * video_device_test - Video Device Test
0005  *
0006  * Copyright (c) 2016 Shuah Khan <shuahkh@osg.samsung.com>
0007  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
0008  *
0009  */
0010 
0011 /*
0012  * This file adds a test for Video Device. This test should not be included
0013  * in the Kselftest run. This test should be run when hardware and driver
0014  * that makes use of V4L2 API is present.
0015  *
0016  * This test opens user specified Video Device and calls video ioctls in a
0017  * loop once every 10 seconds.
0018  *
0019  * Usage:
0020  *  sudo ./video_device_test -d /dev/videoX
0021  *
0022  *  While test is running, remove the device or unbind the driver and
0023  *  ensure there are no use after free errors and other Oops in the
0024  *  dmesg.
0025  *  When possible, enable KaSan kernel config option for use-after-free
0026  *  error detection.
0027 */
0028 
0029 #include <stdio.h>
0030 #include <unistd.h>
0031 #include <stdlib.h>
0032 #include <errno.h>
0033 #include <string.h>
0034 #include <fcntl.h>
0035 #include <sys/ioctl.h>
0036 #include <sys/stat.h>
0037 #include <time.h>
0038 #include <linux/videodev2.h>
0039 
0040 int main(int argc, char **argv)
0041 {
0042     int opt;
0043     char video_dev[256];
0044     int count;
0045     struct v4l2_tuner vtuner;
0046     struct v4l2_capability vcap;
0047     int ret;
0048     int fd;
0049 
0050     if (argc < 2) {
0051         printf("Usage: %s [-d </dev/videoX>]\n", argv[0]);
0052         exit(-1);
0053     }
0054 
0055     /* Process arguments */
0056     while ((opt = getopt(argc, argv, "d:")) != -1) {
0057         switch (opt) {
0058         case 'd':
0059             strncpy(video_dev, optarg, sizeof(video_dev) - 1);
0060             video_dev[sizeof(video_dev)-1] = '\0';
0061             break;
0062         default:
0063             printf("Usage: %s [-d </dev/videoX>]\n", argv[0]);
0064             exit(-1);
0065         }
0066     }
0067 
0068     /* Generate random number of interations */
0069     srand((unsigned int) time(NULL));
0070     count = rand();
0071 
0072     /* Open Video device and keep it open */
0073     fd = open(video_dev, O_RDWR);
0074     if (fd == -1) {
0075         printf("Video Device open errno %s\n", strerror(errno));
0076         exit(-1);
0077     }
0078 
0079     printf("\nNote:\n"
0080            "While test is running, remove the device or unbind\n"
0081            "driver and ensure there are no use after free errors\n"
0082            "and other Oops in the dmesg. When possible, enable KaSan\n"
0083            "kernel config option for use-after-free error detection.\n\n");
0084 
0085     while (count > 0) {
0086         ret = ioctl(fd, VIDIOC_QUERYCAP, &vcap);
0087         if (ret < 0)
0088             printf("VIDIOC_QUERYCAP errno %s\n", strerror(errno));
0089         else
0090             printf("Video device driver %s\n", vcap.driver);
0091 
0092         ret = ioctl(fd, VIDIOC_G_TUNER, &vtuner);
0093         if (ret < 0)
0094             printf("VIDIOC_G_TUNER, errno %s\n", strerror(errno));
0095         else
0096             printf("type %d rangelow %d rangehigh %d\n",
0097                 vtuner.type, vtuner.rangelow, vtuner.rangehigh);
0098         sleep(10);
0099         count--;
0100     }
0101 }