Merge branch 'for-linus-4.8' of git://git.kernel.org/pub/scm/linux/kernel/git/mason...
[cascardo/linux.git] / tools / testing / selftests / media_tests / media_device_test.c
1 /*
2  * media_device_test.c - Media Controller Device ioctl loop 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 Media Controller API.
12  * This test should be run as root and should not be
13  * included in the Kselftest run. This test should be
14  * run when hardware and driver that makes use Media
15  * Controller API are present in the system.
16  *
17  * This test opens user specified Media Device and calls
18  * MEDIA_IOC_DEVICE_INFO ioctl in a loop once every 10
19  * seconds.
20  *
21  * Usage:
22  *      sudo ./media_device_test -d /dev/mediaX
23  *
24  *      While test is running, remove the device and
25  *      ensure there are no use after free errors and
26  *      other Oops in the dmesg. Enable KaSan kernel
27  *      config option for use-after-free error detection.
28 */
29
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/ioctl.h>
37 #include <sys/stat.h>
38 #include <time.h>
39 #include <linux/media.h>
40
41 int main(int argc, char **argv)
42 {
43         int opt;
44         char media_device[256];
45         int count;
46         struct media_device_info mdi;
47         int ret;
48         int fd;
49
50         if (argc < 2) {
51                 printf("Usage: %s [-d </dev/mediaX>]\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(media_device, optarg, sizeof(media_device) - 1);
60                         media_device[sizeof(media_device)-1] = '\0';
61                         break;
62                 default:
63                         printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
64                         exit(-1);
65                 }
66         }
67
68         if (getuid() != 0) {
69                 printf("Please run the test as root - Exiting.\n");
70                 exit(-1);
71         }
72
73         /* Generate random number of interations */
74         srand((unsigned int) time(NULL));
75         count = rand();
76
77         /* Open Media device and keep it open */
78         fd = open(media_device, O_RDWR);
79         if (fd == -1) {
80                 printf("Media Device open errno %s\n", strerror(errno));
81                 exit(-1);
82         }
83
84         printf("\nNote:\n"
85                "While test is running, remove the device and\n"
86                "ensure there are no use after free errors and\n"
87                "other Oops in the dmesg. Enable KaSan kernel\n"
88                "config option for use-after-free error detection.\n\n");
89
90         printf("Running test for %d iternations\n", count);
91
92         while (count > 0) {
93                 ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
94                 if (ret < 0)
95                         printf("Media Device Info errno %s\n", strerror(errno));
96                 else
97                         printf("Media device model %s driver %s - count %d\n",
98                                 mdi.model, mdi.driver, count);
99                 sleep(10);
100                 count--;
101         }
102 }