kms.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * This fairly simple test program dumps output in a similar format to the
  27. * "xrandr" tool everyone knows & loves. It's necessarily slightly different
  28. * since the kernel separates outputs into encoder and connector structures,
  29. * each with their own unique ID. The program also allows test testing of the
  30. * memory management and mode setting APIs by allowing the user to specify a
  31. * connector and mode to use for mode setting. If all works as expected, a
  32. * blue background should be painted on the monitor attached to the specified
  33. * connector after the selected mode is set.
  34. *
  35. * TODO: use cairo to write the mode info on the selected output once
  36. * the mode has been programmed, along with possible test patterns.
  37. */
  38. #include <errno.h>
  39. #include <stdint.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "xf86drm.h"
  44. #include "xf86drmMode.h"
  45. #include "common.h"
  46. struct type_name {
  47. unsigned int type;
  48. const char *name;
  49. };
  50. static const char *util_lookup_type_name(unsigned int type,
  51. const struct type_name *table,
  52. unsigned int count)
  53. {
  54. unsigned int i;
  55. for (i = 0; i < count; i++)
  56. if (table[i].type == type)
  57. return table[i].name;
  58. return NULL;
  59. }
  60. static const struct type_name encoder_type_names[] = {
  61. { DRM_MODE_ENCODER_NONE, "none" },
  62. { DRM_MODE_ENCODER_DAC, "DAC" },
  63. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  64. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  65. { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
  66. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  67. { DRM_MODE_ENCODER_DSI, "DSI" },
  68. { DRM_MODE_ENCODER_DPMST, "DPMST" },
  69. { DRM_MODE_ENCODER_DPI, "DPI" },
  70. };
  71. const char *util_lookup_encoder_type_name(unsigned int type)
  72. {
  73. return util_lookup_type_name(type, encoder_type_names,
  74. ARRAY_SIZE(encoder_type_names));
  75. }
  76. static const struct type_name connector_status_names[] = {
  77. { DRM_MODE_CONNECTED, "connected" },
  78. { DRM_MODE_DISCONNECTED, "disconnected" },
  79. { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
  80. };
  81. const char *util_lookup_connector_status_name(unsigned int status)
  82. {
  83. return util_lookup_type_name(status, connector_status_names,
  84. ARRAY_SIZE(connector_status_names));
  85. }
  86. static const struct type_name connector_type_names[] = {
  87. { DRM_MODE_CONNECTOR_Unknown, "unknown" },
  88. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  89. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  90. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  91. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  92. { DRM_MODE_CONNECTOR_Composite, "composite" },
  93. { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
  94. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  95. { DRM_MODE_CONNECTOR_Component, "component" },
  96. { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
  97. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  98. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  99. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  100. { DRM_MODE_CONNECTOR_TV, "TV" },
  101. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  102. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  103. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  104. { DRM_MODE_CONNECTOR_DPI, "DPI" },
  105. };
  106. const char *util_lookup_connector_type_name(unsigned int type)
  107. {
  108. return util_lookup_type_name(type, connector_type_names,
  109. ARRAY_SIZE(connector_type_names));
  110. }
  111. static const char * const modules[] = {
  112. "i915",
  113. "amdgpu",
  114. "radeon",
  115. "nouveau",
  116. "vmwgfx",
  117. "omapdrm",
  118. "exynos",
  119. "tilcdc",
  120. "msm",
  121. "sti",
  122. "tegra",
  123. "imx-drm",
  124. "rockchip",
  125. "atmel-hlcdc",
  126. "fsl-dcu-drm",
  127. "vc4",
  128. "virtio_gpu",
  129. "mediatek",
  130. "meson",
  131. "pl111",
  132. "stm",
  133. "sun4i-drm",
  134. "armada-drm",
  135. };
  136. int util_open(const char *device, const char *module)
  137. {
  138. int fd;
  139. if (module) {
  140. fd = drmOpen(module, device);
  141. if (fd < 0) {
  142. fprintf(stderr, "failed to open device '%s': %s\n",
  143. module, strerror(errno));
  144. return -errno;
  145. }
  146. } else {
  147. unsigned int i;
  148. for (i = 0; i < ARRAY_SIZE(modules); i++) {
  149. printf("trying to open device '%s'...", modules[i]);
  150. fd = drmOpen(modules[i], device);
  151. if (fd < 0) {
  152. printf("failed\n");
  153. } else {
  154. printf("done\n");
  155. break;
  156. }
  157. }
  158. if (fd < 0) {
  159. fprintf(stderr, "no device found\n");
  160. return -ENODEV;
  161. }
  162. }
  163. return fd;
  164. }