kms.c revision d8807b2f
1/*
2 * Copyright 2008 Tungsten Graphics
3 *   Jakob Bornecrantz <jakob@tungstengraphics.com>
4 * Copyright 2008 Intel Corporation
5 *   Jesse Barnes <jesse.barnes@intel.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26/*
27 * This fairly simple test program dumps output in a similar format to the
28 * "xrandr" tool everyone knows & loves.  It's necessarily slightly different
29 * since the kernel separates outputs into encoder and connector structures,
30 * each with their own unique ID.  The program also allows test testing of the
31 * memory management and mode setting APIs by allowing the user to specify a
32 * connector and mode to use for mode setting.  If all works as expected, a
33 * blue background should be painted on the monitor attached to the specified
34 * connector after the selected mode is set.
35 *
36 * TODO: use cairo to write the mode info on the selected output once
37 *       the mode has been programmed, along with possible test patterns.
38 */
39
40#ifdef HAVE_CONFIG_H
41#include "config.h"
42#endif
43
44#include <errno.h>
45#include <stdint.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49
50#include "xf86drm.h"
51#include "xf86drmMode.h"
52
53#include "common.h"
54
55struct type_name {
56	unsigned int type;
57	const char *name;
58};
59
60static const char *util_lookup_type_name(unsigned int type,
61					 const struct type_name *table,
62					 unsigned int count)
63{
64	unsigned int i;
65
66	for (i = 0; i < count; i++)
67		if (table[i].type == type)
68			return table[i].name;
69
70	return NULL;
71}
72
73static const struct type_name encoder_type_names[] = {
74	{ DRM_MODE_ENCODER_NONE, "none" },
75	{ DRM_MODE_ENCODER_DAC, "DAC" },
76	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
77	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
78	{ DRM_MODE_ENCODER_TVDAC, "TVDAC" },
79	{ DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
80	{ DRM_MODE_ENCODER_DSI, "DSI" },
81	{ DRM_MODE_ENCODER_DPMST, "DPMST" },
82	{ DRM_MODE_ENCODER_DPI, "DPI" },
83};
84
85const char *util_lookup_encoder_type_name(unsigned int type)
86{
87	return util_lookup_type_name(type, encoder_type_names,
88				     ARRAY_SIZE(encoder_type_names));
89}
90
91static const struct type_name connector_status_names[] = {
92	{ DRM_MODE_CONNECTED, "connected" },
93	{ DRM_MODE_DISCONNECTED, "disconnected" },
94	{ DRM_MODE_UNKNOWNCONNECTION, "unknown" },
95};
96
97const char *util_lookup_connector_status_name(unsigned int status)
98{
99	return util_lookup_type_name(status, connector_status_names,
100				     ARRAY_SIZE(connector_status_names));
101}
102
103static const struct type_name connector_type_names[] = {
104	{ DRM_MODE_CONNECTOR_Unknown, "unknown" },
105	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
106	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
107	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
108	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
109	{ DRM_MODE_CONNECTOR_Composite, "composite" },
110	{ DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
111	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
112	{ DRM_MODE_CONNECTOR_Component, "component" },
113	{ DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
114	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
115	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
116	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
117	{ DRM_MODE_CONNECTOR_TV, "TV" },
118	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
119	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
120	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
121	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
122};
123
124const char *util_lookup_connector_type_name(unsigned int type)
125{
126	return util_lookup_type_name(type, connector_type_names,
127				     ARRAY_SIZE(connector_type_names));
128}
129
130static const char * const modules[] = {
131	"i915",
132	"amdgpu",
133	"radeon",
134	"nouveau",
135	"vmwgfx",
136	"omapdrm",
137	"exynos",
138	"tilcdc",
139	"msm",
140	"sti",
141	"tegra",
142	"imx-drm",
143	"rockchip",
144	"atmel-hlcdc",
145	"fsl-dcu-drm",
146	"vc4",
147	"virtio_gpu",
148	"mediatek",
149	"meson",
150	"pl111",
151};
152
153int util_open(const char *device, const char *module)
154{
155	int fd;
156
157	if (module) {
158		fd = drmOpen(module, device);
159		if (fd < 0) {
160			fprintf(stderr, "failed to open device '%s': %s\n",
161				module, strerror(errno));
162			return -errno;
163		}
164	} else {
165		unsigned int i;
166
167		for (i = 0; i < ARRAY_SIZE(modules); i++) {
168			printf("trying to open device '%s'...", modules[i]);
169
170			fd = drmOpen(modules[i], device);
171			if (fd < 0) {
172				printf("failed\n");
173			} else {
174				printf("done\n");
175				break;
176			}
177		}
178
179		if (fd < 0) {
180			fprintf(stderr, "no device found\n");
181			return -ENODEV;
182		}
183	}
184
185	return fd;
186}
187