kms.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <fcntl.h>
  44. #include <unistd.h>
  45. #include "xf86drm.h"
  46. #include "xf86drmMode.h"
  47. #include "common.h"
  48. struct type_name {
  49. unsigned int type;
  50. const char *name;
  51. };
  52. static const char *util_lookup_type_name(unsigned int type,
  53. const struct type_name *table,
  54. unsigned int count)
  55. {
  56. unsigned int i;
  57. for (i = 0; i < count; i++)
  58. if (table[i].type == type)
  59. return table[i].name;
  60. return NULL;
  61. }
  62. static const struct type_name encoder_type_names[] = {
  63. { DRM_MODE_ENCODER_NONE, "none" },
  64. { DRM_MODE_ENCODER_DAC, "DAC" },
  65. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  66. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  67. { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
  68. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  69. { DRM_MODE_ENCODER_DSI, "DSI" },
  70. { DRM_MODE_ENCODER_DPMST, "DPMST" },
  71. { DRM_MODE_ENCODER_DPI, "DPI" },
  72. };
  73. const char *util_lookup_encoder_type_name(unsigned int type)
  74. {
  75. return util_lookup_type_name(type, encoder_type_names,
  76. ARRAY_SIZE(encoder_type_names));
  77. }
  78. static const struct type_name connector_status_names[] = {
  79. { DRM_MODE_CONNECTED, "connected" },
  80. { DRM_MODE_DISCONNECTED, "disconnected" },
  81. { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
  82. };
  83. const char *util_lookup_connector_status_name(unsigned int status)
  84. {
  85. return util_lookup_type_name(status, connector_status_names,
  86. ARRAY_SIZE(connector_status_names));
  87. }
  88. int util_open(const char *device, const char *module)
  89. {
  90. int fd = -1;
  91. drmVersionPtr version;
  92. if (module || device) {
  93. fd = drmOpen(module, device);
  94. if (fd < 0) {
  95. fprintf(stderr, "failed to open device '%s' with busid '%s': %s\n",
  96. module, device, strerror(errno));
  97. return -errno;
  98. }
  99. } else {
  100. unsigned int i;
  101. drmDevicePtr devices[64];
  102. int num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
  103. if (num_devices < 0) {
  104. fprintf(stderr, "drmGetDevices2() failed with %s\n", strerror(num_devices));
  105. return num_devices;
  106. }
  107. for (i = 0; i < num_devices; i++) {
  108. drmDevicePtr device = devices[i];
  109. // Select only primary nodes
  110. if ((device->available_nodes & 1 << DRM_NODE_PRIMARY) == 0)
  111. continue;
  112. printf("trying to open device '%s'... ", device->nodes[DRM_NODE_PRIMARY]);
  113. fd = open(device->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC);
  114. if (fd < 0) {
  115. printf("failed\n");
  116. } else if (!drmIsKMS(fd)) {
  117. printf("is not a KMS device\n");
  118. close(fd);
  119. fd = -1;
  120. } else {
  121. printf("done\n");
  122. break;
  123. }
  124. }
  125. if (fd < 0) {
  126. fprintf(stderr, "no device found\n");
  127. return -ENODEV;
  128. }
  129. }
  130. version = drmGetVersion(fd);
  131. printf("opened device `%s` on driver `%s` (version %d.%d.%d at %s)\n",
  132. version->desc,
  133. version->name,
  134. version->version_major,
  135. version->version_minor,
  136. version->version_patchlevel,
  137. version->date);
  138. drmFreeVersion(version);
  139. return fd;
  140. }