1 /*
2  * media_device_open.c - Media Controller Device Open 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, closes the file, and exits.
19  *
20  * Usage:
21  *	sudo ./media_device_open -d /dev/mediaX
22  *
23  *	Run this test is a loop and run bind/unbind on the driver.
24 */
25 
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
33 #include <sys/stat.h>
34 #include <linux/media.h>
35 
36 int main(int argc, char **argv)
37 {
38 	int opt;
39 	char media_device[256];
40 	int count = 0;
41 	struct media_device_info mdi;
42 	int ret;
43 	int fd;
44 
45 	if (argc < 2) {
46 		printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
47 		exit(-1);
48 	}
49 
50 	/* Process arguments */
51 	while ((opt = getopt(argc, argv, "d:")) != -1) {
52 		switch (opt) {
53 		case 'd':
54 			strncpy(media_device, optarg, sizeof(media_device) - 1);
55 			media_device[sizeof(media_device)-1] = '\0';
56 			break;
57 		default:
58 			printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
59 			exit(-1);
60 		}
61 	}
62 
63 	if (getuid() != 0) {
64 		printf("Please run the test as root - Exiting.\n");
65 		exit(-1);
66 	}
67 
68 	/* Open Media device and keep it open */
69 	fd = open(media_device, O_RDWR);
70 	if (fd == -1) {
71 		printf("Media Device open errno %s\n", strerror(errno));
72 		exit(-1);
73 	}
74 
75 	ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
76 	if (ret < 0)
77 		printf("Media Device Info errno %s\n", strerror(errno));
78 	else
79 		printf("Media device model %s driver %s\n",
80 			mdi.model, mdi.driver);
81 }
82