1 /*
2  * Copyright (c) 2014 Jean-Sebastien Pedron <dumbbell@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 
29 #include <dirent.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include <libdevq.h>
37 
38 #define	DRIDEV_DIR "/dev/dri"
39 
40 int
print_drm_info(int fd)41 print_drm_info(int fd)
42 {
43 	int ret;
44 	int vendor_id, device_id, subvendor_id, subdevice_id, revision_id;
45 	char *device_path, *driver_name;
46 	size_t device_path_len, driver_name_len;
47 
48 	ret = devq_device_get_devpath_from_fd(fd,
49 	    NULL, &device_path_len);
50 	if (ret < 0) {
51 		fprintf(stderr, "Warning: unable to device path\n");
52 		return (-1);
53 	}
54 
55 	device_path = malloc(device_path_len);
56 	ret = devq_device_get_devpath_from_fd(fd,
57 	    device_path, &device_path_len);
58 	if (ret < 0) {
59 		fprintf(stderr, "Warning: Unable to device path\n");
60 		return (-1);
61 	}
62 
63 	printf("%.*s:\n", (int)device_path_len, device_path);
64 	free(device_path);
65 	device_path = NULL;
66 
67 	ret = devq_device_drm_get_drvname_from_fd(fd, NULL, &driver_name_len);
68 	if (ret < 0) {
69 		printf("    Driver name:   Unknown\n");
70 		return (-1);
71 	}
72 
73 	driver_name = malloc(driver_name_len);
74 	ret = devq_device_drm_get_drvname_from_fd(fd,
75 	    driver_name, &driver_name_len);
76 	if (ret < 0) {
77 		fprintf(stderr, "Warning: Unable to determine driver name\n");
78 		return (-1);
79 	}
80 
81 	printf("    Driver name:   %.*s\n", (int)driver_name_len, driver_name);
82 	free(driver_name);
83 	driver_name = NULL;
84 
85 	ret = devq_device_get_pciid_full_from_fd(fd, &vendor_id, &device_id,
86 				&subvendor_id, &subdevice_id, &revision_id);
87 	if (ret < 0) {
88 		fprintf(stderr, "Warning: Unable to determine vendor and device ID\n");
89 		return (-1);
90 	}
91 
92 	printf("    PCI vendor ID: 0x%04x subvendor ID: 0x%04x\n", vendor_id, subvendor_id);
93 	printf("    PCI device ID: 0x%04x subdevice ID: 0x%04x\n", device_id, subdevice_id);
94 	printf("    PCI revision ID: 0x%04x\n", revision_id);
95 
96 	return (0);
97 }
98 
99 int
main(int argc,char * argv[])100 main(int argc, char *argv[])
101 {
102 	int fd;
103 
104 	if (argc >= 2) {
105 		for (int i = 1; i < argc; ++i) {
106 			fd = open(argv[i], O_RDWR);
107 			if (fd < 0) {
108 				fprintf(stderr, "%s\n", argv[i]);
109 				continue;
110 			}
111 
112 			print_drm_info(fd);
113 			close(fd);
114 		}
115 	} else {
116 		DIR *dir;
117 		struct dirent *dp;
118 		char path[256];
119 
120 		dir = opendir(DRIDEV_DIR);
121 		if (dir == NULL)
122 			return (-1);
123 
124 		while ((dp = readdir(dir)) != NULL) {
125 			if (dp->d_name[0] == '.')
126 				continue;
127 
128 			path[0] = '\0';
129 			strcpy(path, DRIDEV_DIR);
130 			strcat(path, "/");
131 			strcat(path, dp->d_name);
132 
133 			fd = open(path, O_RDWR);
134 			if (fd < 0) {
135 				fprintf(stderr, "%s\n", path);
136 				continue;
137 			}
138 
139 			print_drm_info(fd);
140 			close(fd);
141 		}
142 
143 		closedir(dir);
144 	}
145 
146 	return (0);
147 }
148