proptest.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright © 2012 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Paulo Zanoni <paulo.r.zanoni@intel.com>
  25. *
  26. */
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <getopt.h>
  30. #include <inttypes.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include "xf86drm.h"
  35. #include "xf86drmMode.h"
  36. #include "util/common.h"
  37. #include "util/kms.h"
  38. static inline int64_t U642I64(uint64_t val)
  39. {
  40. return (int64_t)*((int64_t *)&val);
  41. }
  42. int fd;
  43. drmModeResPtr res = NULL;
  44. /* dump_blob and dump_prop shamelessly copied from ../modetest/modetest.c */
  45. static void
  46. dump_blob(uint32_t blob_id)
  47. {
  48. uint32_t i;
  49. unsigned char *blob_data;
  50. drmModePropertyBlobPtr blob;
  51. blob = drmModeGetPropertyBlob(fd, blob_id);
  52. if (!blob) {
  53. printf("\n");
  54. return;
  55. }
  56. blob_data = blob->data;
  57. for (i = 0; i < blob->length; i++) {
  58. if (i % 16 == 0)
  59. printf("\n\t\t\t");
  60. printf("%.2hhx", blob_data[i]);
  61. }
  62. printf("\n");
  63. drmModeFreePropertyBlob(blob);
  64. }
  65. static void
  66. dump_prop(uint32_t prop_id, uint64_t value)
  67. {
  68. int i;
  69. drmModePropertyPtr prop;
  70. prop = drmModeGetProperty(fd, prop_id);
  71. printf("\t%d", prop_id);
  72. if (!prop) {
  73. printf("\n");
  74. return;
  75. }
  76. printf(" %s:\n", prop->name);
  77. printf("\t\tflags:");
  78. if (prop->flags & DRM_MODE_PROP_PENDING)
  79. printf(" pending");
  80. if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
  81. printf(" immutable");
  82. if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
  83. printf(" signed range");
  84. if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
  85. printf(" range");
  86. if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
  87. printf(" enum");
  88. if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
  89. printf(" bitmask");
  90. if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
  91. printf(" blob");
  92. if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
  93. printf(" object");
  94. printf("\n");
  95. if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
  96. printf("\t\tvalues:");
  97. for (i = 0; i < prop->count_values; i++)
  98. printf(" %"PRId64, U642I64(prop->values[i]));
  99. printf("\n");
  100. }
  101. if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
  102. printf("\t\tvalues:");
  103. for (i = 0; i < prop->count_values; i++)
  104. printf(" %"PRIu64, prop->values[i]);
  105. printf("\n");
  106. }
  107. if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
  108. printf("\t\tenums:");
  109. for (i = 0; i < prop->count_enums; i++)
  110. printf(" %s=%"PRIu64, prop->enums[i].name,
  111. (uint64_t)prop->enums[i].value);
  112. printf("\n");
  113. } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
  114. printf("\t\tvalues:");
  115. for (i = 0; i < prop->count_enums; i++)
  116. printf(" %s=0x%llx", prop->enums[i].name,
  117. (1LL << prop->enums[i].value));
  118. printf("\n");
  119. } else {
  120. assert(prop->count_enums == 0);
  121. }
  122. if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
  123. printf("\t\tblobs:\n");
  124. for (i = 0; i < prop->count_blobs; i++)
  125. dump_blob(prop->blob_ids[i]);
  126. printf("\n");
  127. } else {
  128. assert(prop->count_blobs == 0);
  129. }
  130. printf("\t\tvalue:");
  131. if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
  132. dump_blob(value);
  133. else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
  134. printf(" %"PRId64"\n", value);
  135. else
  136. printf(" %"PRIu64"\n", value);
  137. drmModeFreeProperty(prop);
  138. }
  139. static void listObjectProperties(uint32_t id, uint32_t type)
  140. {
  141. unsigned int i;
  142. drmModeObjectPropertiesPtr props;
  143. props = drmModeObjectGetProperties(fd, id, type);
  144. if (!props) {
  145. printf("\tNo properties: %s.\n", strerror(errno));
  146. return;
  147. }
  148. for (i = 0; i < props->count_props; i++)
  149. dump_prop(props->props[i], props->prop_values[i]);
  150. drmModeFreeObjectProperties(props);
  151. }
  152. static void listConnectorProperties(void)
  153. {
  154. int i;
  155. drmModeConnectorPtr c;
  156. for (i = 0; i < res->count_connectors; i++) {
  157. c = drmModeGetConnector(fd, res->connectors[i]);
  158. if (!c) {
  159. fprintf(stderr, "Could not get connector %u: %s\n",
  160. res->connectors[i], strerror(errno));
  161. continue;
  162. }
  163. printf("Connector %u (%s-%u)\n", c->connector_id,
  164. drmModeGetConnectorTypeName(c->connector_type),
  165. c->connector_type_id);
  166. listObjectProperties(c->connector_id,
  167. DRM_MODE_OBJECT_CONNECTOR);
  168. drmModeFreeConnector(c);
  169. }
  170. }
  171. static void listCrtcProperties(void)
  172. {
  173. int i;
  174. drmModeCrtcPtr c;
  175. for (i = 0; i < res->count_crtcs; i++) {
  176. c = drmModeGetCrtc(fd, res->crtcs[i]);
  177. if (!c) {
  178. fprintf(stderr, "Could not get crtc %u: %s\n",
  179. res->crtcs[i], strerror(errno));
  180. continue;
  181. }
  182. printf("CRTC %u\n", c->crtc_id);
  183. listObjectProperties(c->crtc_id, DRM_MODE_OBJECT_CRTC);
  184. drmModeFreeCrtc(c);
  185. }
  186. }
  187. static void listAllProperties(void)
  188. {
  189. listConnectorProperties();
  190. listCrtcProperties();
  191. }
  192. static int setProperty(char *argv[])
  193. {
  194. uint32_t obj_id, obj_type, prop_id;
  195. uint64_t value;
  196. obj_id = atoi(argv[0]);
  197. if (!strcmp(argv[1], "connector")) {
  198. obj_type = DRM_MODE_OBJECT_CONNECTOR;
  199. } else if (!strcmp(argv[1], "crtc")) {
  200. obj_type = DRM_MODE_OBJECT_CRTC;
  201. } else {
  202. fprintf(stderr, "Invalid object type.\n");
  203. return 1;
  204. }
  205. prop_id = atoi(argv[2]);
  206. value = atoll(argv[3]);
  207. return drmModeObjectSetProperty(fd, obj_id, obj_type, prop_id, value);
  208. }
  209. static void usage(const char *program)
  210. {
  211. printf("Usage:\n"
  212. " %s [options]\n"
  213. " %s [options] [obj id] [obj type] [prop id] [value]\n"
  214. "\n"
  215. "options:\n"
  216. " -D DEVICE use the given device\n"
  217. " -M MODULE use the given driver\n"
  218. "\n"
  219. "The first form just prints all the existing properties. The second one is\n"
  220. "used to set the value of a specified property. The object type can be one of\n"
  221. "the following strings:\n"
  222. " connector crtc\n"
  223. "\n"
  224. "Example:\n"
  225. " proptest 7 connector 2 1\n"
  226. "will set property 2 of connector 7 to 1\n", program, program);
  227. }
  228. int main(int argc, char *argv[])
  229. {
  230. static const char optstr[] = "D:M:";
  231. int c, args, ret = 0;
  232. char *device = NULL;
  233. char *module = NULL;
  234. while ((c = getopt(argc, argv, optstr)) != -1) {
  235. switch (c) {
  236. case 'D':
  237. device = optarg;
  238. break;
  239. case 'M':
  240. module = optarg;
  241. break;
  242. default:
  243. usage(argv[0]);
  244. break;
  245. }
  246. }
  247. args = argc - optind;
  248. fd = util_open(device, module);
  249. if (fd < 0)
  250. return 1;
  251. res = drmModeGetResources(fd);
  252. if (!res) {
  253. fprintf(stderr, "Failed to get resources: %s\n",
  254. strerror(errno));
  255. ret = 1;
  256. goto done;
  257. }
  258. if (args < 1) {
  259. listAllProperties();
  260. } else if (args == 4) {
  261. ret = setProperty(&argv[optind]);
  262. } else {
  263. usage(argv[0]);
  264. ret = 1;
  265. }
  266. drmModeFreeResources(res);
  267. done:
  268. drmClose(fd);
  269. return ret;
  270. }