kms.c revision 0ed5401b
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#include <errno.h> 41#include <stdint.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45 46#include "xf86drm.h" 47#include "xf86drmMode.h" 48 49#include "common.h" 50 51struct type_name { 52 unsigned int type; 53 const char *name; 54}; 55 56static const char *util_lookup_type_name(unsigned int type, 57 const struct type_name *table, 58 unsigned int count) 59{ 60 unsigned int i; 61 62 for (i = 0; i < count; i++) 63 if (table[i].type == type) 64 return table[i].name; 65 66 return NULL; 67} 68 69static const struct type_name encoder_type_names[] = { 70 { DRM_MODE_ENCODER_NONE, "none" }, 71 { DRM_MODE_ENCODER_DAC, "DAC" }, 72 { DRM_MODE_ENCODER_TMDS, "TMDS" }, 73 { DRM_MODE_ENCODER_LVDS, "LVDS" }, 74 { DRM_MODE_ENCODER_TVDAC, "TVDAC" }, 75 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" }, 76 { DRM_MODE_ENCODER_DSI, "DSI" }, 77 { DRM_MODE_ENCODER_DPMST, "DPMST" }, 78 { DRM_MODE_ENCODER_DPI, "DPI" }, 79}; 80 81const char *util_lookup_encoder_type_name(unsigned int type) 82{ 83 return util_lookup_type_name(type, encoder_type_names, 84 ARRAY_SIZE(encoder_type_names)); 85} 86 87static const struct type_name connector_status_names[] = { 88 { DRM_MODE_CONNECTED, "connected" }, 89 { DRM_MODE_DISCONNECTED, "disconnected" }, 90 { DRM_MODE_UNKNOWNCONNECTION, "unknown" }, 91}; 92 93const char *util_lookup_connector_status_name(unsigned int status) 94{ 95 return util_lookup_type_name(status, connector_status_names, 96 ARRAY_SIZE(connector_status_names)); 97} 98 99static const char * const modules[] = { 100 "i915", 101 "amdgpu", 102 "radeon", 103 "nouveau", 104 "vmwgfx", 105 "omapdrm", 106 "exynos", 107 "tilcdc", 108 "msm", 109 "sti", 110 "tegra", 111 "imx-drm", 112 "rockchip", 113 "atmel-hlcdc", 114 "fsl-dcu-drm", 115 "vc4", 116 "virtio_gpu", 117 "mediatek", 118 "meson", 119 "pl111", 120 "stm", 121 "sun4i-drm", 122 "armada-drm", 123 "komeda", 124 "imx-dcss", 125 "mxsfb-drm", 126}; 127 128int util_open(const char *device, const char *module) 129{ 130 int fd; 131 132 if (module) { 133 fd = drmOpen(module, device); 134 if (fd < 0) { 135 fprintf(stderr, "failed to open device '%s': %s\n", 136 module, strerror(errno)); 137 return -errno; 138 } 139 } else { 140 unsigned int i; 141 142 for (i = 0; i < ARRAY_SIZE(modules); i++) { 143 printf("trying to open device '%s'...", modules[i]); 144 145 fd = drmOpen(modules[i], device); 146 if (fd < 0) { 147 printf("failed\n"); 148 } else { 149 printf("done\n"); 150 break; 151 } 152 } 153 154 if (fd < 0) { 155 fprintf(stderr, "no device found\n"); 156 return -ENODEV; 157 } 158 } 159 160 return fd; 161} 162