Merge remote-tracking branches 'regmap/topic/doc' and 'regmap/topic/flat' into regmap...
[cascardo/linux.git] / tools / testing / selftests / media_tests / media_device_test.c
1 /*
2  * media_devkref_test.c - Media Controller Device Kref API 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 <linux/media.h>
39
40 int main(int argc, char **argv)
41 {
42         int opt;
43         char media_device[256];
44         int count = 0;
45         struct media_device_info mdi;
46         int ret;
47         int fd;
48
49         if (argc < 2) {
50                 printf("Usage: %s [-d </dev/mediaX>]\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(media_device, optarg, sizeof(media_device) - 1);
59                         media_device[sizeof(media_device)-1] = '\0';
60                         break;
61                 default:
62                         printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
63                         exit(-1);
64                 }
65         }
66
67         if (getuid() != 0) {
68                 printf("Please run the test as root - Exiting.\n");
69                 exit(-1);
70         }
71
72         /* Open Media device and keep it open */
73         fd = open(media_device, O_RDWR);
74         if (fd == -1) {
75                 printf("Media Device open errno %s\n", strerror(errno));
76                 exit(-1);
77         }
78
79         printf("\nNote:\n"
80                "While test is running, remove the device and\n"
81                "ensure there are no use after free errors and\n"
82                "other Oops in the dmesg. Enable KaSan kernel\n"
83                "config option for use-after-free error detection.\n\n");
84
85         while (count < 100) {
86                 ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
87                 if (ret < 0)
88                         printf("Media Device Info errno %s\n", strerror(errno));
89                 else
90                         printf("Media device model %s driver %s\n",
91                                 mdi.model, mdi.driver);
92                 sleep(10);
93                 count++;
94         }
95 }