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