modeprint.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * \file modedemo.c
  3. * Test program to dump DRM kernel mode setting related information.
  4. * Queries the kernel for all available information and dumps it to stdout.
  5. *
  6. * \author Jakob Bornecrantz <wallbraker@gmail.com>
  7. */
  8. /*
  9. * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
  10. * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  28. * IN THE SOFTWARE.
  29. *
  30. */
  31. #include <assert.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stdint.h>
  35. #include <unistd.h>
  36. #include <string.h>
  37. #include <inttypes.h>
  38. #include "xf86drm.h"
  39. #include "xf86drmMode.h"
  40. #include "util/common.h"
  41. #include "util/kms.h"
  42. int current;
  43. int connectors;
  44. int full_props;
  45. int edid;
  46. int modes;
  47. int full_modes;
  48. int encoders;
  49. int crtcs;
  50. int fbs;
  51. char *module_name;
  52. static int printMode(struct drm_mode_modeinfo *mode)
  53. {
  54. if (full_modes) {
  55. printf("Mode: %s\n", mode->name);
  56. printf("\tclock : %i\n", mode->clock);
  57. printf("\thdisplay : %i\n", mode->hdisplay);
  58. printf("\thsync_start : %i\n", mode->hsync_start);
  59. printf("\thsync_end : %i\n", mode->hsync_end);
  60. printf("\thtotal : %i\n", mode->htotal);
  61. printf("\thskew : %i\n", mode->hskew);
  62. printf("\tvdisplay : %i\n", mode->vdisplay);
  63. printf("\tvsync_start : %i\n", mode->vsync_start);
  64. printf("\tvsync_end : %i\n", mode->vsync_end);
  65. printf("\tvtotal : %i\n", mode->vtotal);
  66. printf("\tvscan : %i\n", mode->vscan);
  67. printf("\tvrefresh : %i\n", mode->vrefresh);
  68. printf("\tflags : %i\n", mode->flags);
  69. } else {
  70. printf("Mode: \"%s\" %ix%i %i\n", mode->name,
  71. mode->hdisplay, mode->vdisplay, mode->vrefresh);
  72. }
  73. return 0;
  74. }
  75. static int printProperty(int fd, drmModeResPtr res, drmModePropertyPtr props, uint64_t value)
  76. {
  77. const char *name = NULL;
  78. int j;
  79. printf("Property: %s\n", props->name);
  80. printf("\tid : %i\n", props->prop_id);
  81. printf("\tflags : %i\n", props->flags);
  82. printf("\tcount_values : %d\n", props->count_values);
  83. if (props->count_values) {
  84. printf("\tvalues :");
  85. for (j = 0; j < props->count_values; j++)
  86. printf(" %" PRIu64, props->values[j]);
  87. printf("\n");
  88. }
  89. printf("\tcount_enums : %d\n", props->count_enums);
  90. if (props->flags & DRM_MODE_PROP_BLOB) {
  91. drmModePropertyBlobPtr blob;
  92. blob = drmModeGetPropertyBlob(fd, value);
  93. if (blob) {
  94. printf("blob is %d length, %08X\n", blob->length, *(uint32_t *)blob->data);
  95. drmModeFreePropertyBlob(blob);
  96. } else {
  97. printf("error getting blob %" PRIu64 "\n", value);
  98. }
  99. } else {
  100. for (j = 0; j < props->count_enums; j++) {
  101. printf("\t\t%" PRIu64" = %s\n", (uint64_t)props->enums[j].value, props->enums[j].name);
  102. if (props->enums[j].value == value)
  103. name = props->enums[j].name;
  104. }
  105. if (props->count_enums && name) {
  106. printf("\tcon_value : %s\n", name);
  107. } else {
  108. printf("\tcon_value : %" PRIu64 "\n", value);
  109. }
  110. }
  111. return 0;
  112. }
  113. static int printConnector(int fd, drmModeResPtr res, drmModeConnectorPtr connector, uint32_t id)
  114. {
  115. int i = 0;
  116. struct drm_mode_modeinfo *mode = NULL;
  117. drmModePropertyPtr props;
  118. const char *connector_type_name = NULL;
  119. connector_type_name = drmModeGetConnectorTypeName(connector->connector_type);
  120. if (connector_type_name)
  121. printf("Connector: %s-%d\n", connector_type_name,
  122. connector->connector_type_id);
  123. else
  124. printf("Connector: %d-%d\n", connector->connector_type,
  125. connector->connector_type_id);
  126. printf("\tid : %i\n", id);
  127. printf("\tencoder id : %i\n", connector->encoder_id);
  128. printf("\tconn : %s\n", util_lookup_connector_status_name(connector->connection));
  129. printf("\tsize : %ix%i (mm)\n", connector->mmWidth, connector->mmHeight);
  130. printf("\tcount_modes : %i\n", connector->count_modes);
  131. printf("\tcount_props : %i\n", connector->count_props);
  132. if (connector->count_props) {
  133. printf("\tprops :");
  134. for (i = 0; i < connector->count_props; i++)
  135. printf(" %i", connector->props[i]);
  136. printf("\n");
  137. }
  138. printf("\tcount_encoders : %i\n", connector->count_encoders);
  139. if (connector->count_encoders) {
  140. printf("\tencoders :");
  141. for (i = 0; i < connector->count_encoders; i++)
  142. printf(" %i", connector->encoders[i]);
  143. printf("\n");
  144. }
  145. if (modes) {
  146. for (i = 0; i < connector->count_modes; i++) {
  147. mode = (struct drm_mode_modeinfo *)&connector->modes[i];
  148. printMode(mode);
  149. }
  150. }
  151. if (full_props) {
  152. for (i = 0; i < connector->count_props; i++) {
  153. props = drmModeGetProperty(fd, connector->props[i]);
  154. if (props) {
  155. printProperty(fd, res, props, connector->prop_values[i]);
  156. drmModeFreeProperty(props);
  157. }
  158. }
  159. }
  160. return 0;
  161. }
  162. static int printEncoder(int fd, drmModeResPtr res, drmModeEncoderPtr encoder, uint32_t id)
  163. {
  164. const char *encoder_name;
  165. encoder_name = util_lookup_encoder_type_name(encoder->encoder_type);
  166. if (encoder_name)
  167. printf("Encoder: %s\n", encoder_name);
  168. else
  169. printf("Encoder\n");
  170. printf("\tid :%i\n", id);
  171. printf("\tcrtc_id :%d\n", encoder->crtc_id);
  172. printf("\ttype :%d\n", encoder->encoder_type);
  173. printf("\tpossible_crtcs :0x%x\n", encoder->possible_crtcs);
  174. printf("\tpossible_clones :0x%x\n", encoder->possible_clones);
  175. return 0;
  176. }
  177. static int printCrtc(int fd, drmModeResPtr res, drmModeCrtcPtr crtc, uint32_t id)
  178. {
  179. printf("Crtc\n");
  180. printf("\tid : %i\n", id);
  181. printf("\tx : %i\n", crtc->x);
  182. printf("\ty : %i\n", crtc->y);
  183. printf("\twidth : %i\n", crtc->width);
  184. printf("\theight : %i\n", crtc->height);
  185. printf("\tmode : %p\n", &crtc->mode);
  186. printf("\tgamma size : %d\n", crtc->gamma_size);
  187. return 0;
  188. }
  189. static int printFrameBuffer(int fd, drmModeResPtr res, drmModeFBPtr fb)
  190. {
  191. printf("Framebuffer\n");
  192. printf("\thandle : %i\n", fb->handle);
  193. printf("\twidth : %i\n", fb->width);
  194. printf("\theight : %i\n", fb->height);
  195. printf("\tpitch : %i\n", fb->pitch);
  196. printf("\tbpp : %i\n", fb->bpp);
  197. printf("\tdepth : %i\n", fb->depth);
  198. printf("\tbuffer_id : %i\n", fb->handle);
  199. return 0;
  200. }
  201. static int printRes(int fd, drmModeResPtr res)
  202. {
  203. int i;
  204. drmModeFBPtr fb;
  205. drmModeCrtcPtr crtc;
  206. drmModeEncoderPtr encoder;
  207. drmModeConnectorPtr connector;
  208. printf("Resources\n\n");
  209. printf("count_connectors : %i\n", res->count_connectors);
  210. printf("count_encoders : %i\n", res->count_encoders);
  211. printf("count_crtcs : %i\n", res->count_crtcs);
  212. printf("count_fbs : %i\n", res->count_fbs);
  213. printf("\n");
  214. if (connectors) {
  215. for (i = 0; i < res->count_connectors; i++) {
  216. connector = (current ? drmModeGetConnectorCurrent : drmModeGetConnector) (fd, res->connectors[i]);
  217. if (!connector)
  218. printf("Could not get connector %i\n", res->connectors[i]);
  219. else {
  220. printConnector(fd, res, connector, res->connectors[i]);
  221. drmModeFreeConnector(connector);
  222. }
  223. }
  224. printf("\n");
  225. }
  226. if (encoders) {
  227. for (i = 0; i < res->count_encoders; i++) {
  228. encoder = drmModeGetEncoder(fd, res->encoders[i]);
  229. if (!encoder)
  230. printf("Could not get encoder %i\n", res->encoders[i]);
  231. else {
  232. printEncoder(fd, res, encoder, res->encoders[i]);
  233. drmModeFreeEncoder(encoder);
  234. }
  235. }
  236. printf("\n");
  237. }
  238. if (crtcs) {
  239. for (i = 0; i < res->count_crtcs; i++) {
  240. crtc = drmModeGetCrtc(fd, res->crtcs[i]);
  241. if (!crtc)
  242. printf("Could not get crtc %i\n", res->crtcs[i]);
  243. else {
  244. printCrtc(fd, res, crtc, res->crtcs[i]);
  245. drmModeFreeCrtc(crtc);
  246. }
  247. }
  248. printf("\n");
  249. }
  250. if (fbs) {
  251. for (i = 0; i < res->count_fbs; i++) {
  252. fb = drmModeGetFB(fd, res->fbs[i]);
  253. if (!fb)
  254. printf("Could not get fb %i\n", res->fbs[i]);
  255. else {
  256. printFrameBuffer(fd, res, fb);
  257. drmModeFreeFB(fb);
  258. }
  259. }
  260. }
  261. return 0;
  262. }
  263. static void args(int argc, char **argv)
  264. {
  265. int defaults = 1;
  266. int i;
  267. fbs = 0;
  268. edid = 0;
  269. crtcs = 0;
  270. modes = 0;
  271. encoders = 0;
  272. full_modes = 0;
  273. full_props = 0;
  274. connectors = 0;
  275. current = 0;
  276. module_name = argv[1];
  277. for (i = 2; i < argc; i++) {
  278. if (strcmp(argv[i], "-fb") == 0) {
  279. fbs = 1;
  280. defaults = 0;
  281. } else if (strcmp(argv[i], "-crtcs") == 0) {
  282. crtcs = 1;
  283. defaults = 0;
  284. } else if (strcmp(argv[i], "-cons") == 0) {
  285. connectors = 1;
  286. modes = 1;
  287. defaults = 0;
  288. } else if (strcmp(argv[i], "-modes") == 0) {
  289. connectors = 1;
  290. modes = 1;
  291. defaults = 0;
  292. } else if (strcmp(argv[i], "-full") == 0) {
  293. connectors = 1;
  294. modes = 1;
  295. full_modes = 1;
  296. defaults = 0;
  297. } else if (strcmp(argv[i], "-props") == 0) {
  298. connectors = 1;
  299. full_props = 1;
  300. defaults = 0;
  301. } else if (strcmp(argv[i], "-edids") == 0) {
  302. connectors = 1;
  303. edid = 1;
  304. defaults = 0;
  305. } else if (strcmp(argv[i], "-encoders") == 0) {
  306. encoders = 1;
  307. defaults = 0;
  308. } else if (strcmp(argv[i], "-v") == 0) {
  309. fbs = 1;
  310. edid = 1;
  311. crtcs = 1;
  312. modes = 1;
  313. encoders = 1;
  314. full_modes = 1;
  315. full_props = 1;
  316. connectors = 1;
  317. defaults = 0;
  318. } else if (strcmp(argv[i], "-current") == 0) {
  319. current = 1;
  320. }
  321. }
  322. if (defaults) {
  323. fbs = 1;
  324. edid = 1;
  325. crtcs = 1;
  326. modes = 1;
  327. encoders = 1;
  328. full_modes = 0;
  329. full_props = 0;
  330. connectors = 1;
  331. }
  332. }
  333. int main(int argc, char **argv)
  334. {
  335. int fd;
  336. drmModeResPtr res;
  337. if (argc == 1) {
  338. printf("Please add modulename as first argument\n");
  339. return 1;
  340. }
  341. args(argc, argv);
  342. printf("Starting test\n");
  343. fd = drmOpen(module_name, NULL);
  344. if (fd < 0) {
  345. printf("Failed to open the card fd (%d)\n",fd);
  346. return 1;
  347. }
  348. res = drmModeGetResources(fd);
  349. if (res == 0) {
  350. printf("Failed to get resources from card\n");
  351. drmClose(fd);
  352. return 1;
  353. }
  354. printRes(fd, res);
  355. drmModeFreeResources(res);
  356. printf("Ok\n");
  357. return 0;
  358. }