1
0

drmdevice.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2015 Emil Velikov <emil.l.velikov@gmail.com>
  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 shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. *
  22. */
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdbool.h>
  27. #include <string.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <xf86drm.h>
  32. static void
  33. print_device_info(drmDevicePtr device, int i, bool print_revision)
  34. {
  35. printf("device[%i]\n", i);
  36. printf("+-> available_nodes %#04x\n", device->available_nodes);
  37. printf("+-> nodes\n");
  38. for (int j = 0; j < DRM_NODE_MAX; j++)
  39. if (device->available_nodes & 1 << j)
  40. printf("| +-> nodes[%d] %s\n", j, device->nodes[j]);
  41. printf("+-> bustype %04x\n", device->bustype);
  42. if (device->bustype == DRM_BUS_PCI) {
  43. printf("| +-> pci\n");
  44. printf("| +-> domain %04x\n",device->businfo.pci->domain);
  45. printf("| +-> bus %02x\n", device->businfo.pci->bus);
  46. printf("| +-> dev %02x\n", device->businfo.pci->dev);
  47. printf("| +-> func %1u\n", device->businfo.pci->func);
  48. printf("+-> deviceinfo\n");
  49. printf(" +-> pci\n");
  50. printf(" +-> vendor_id %04x\n", device->deviceinfo.pci->vendor_id);
  51. printf(" +-> device_id %04x\n", device->deviceinfo.pci->device_id);
  52. printf(" +-> subvendor_id %04x\n", device->deviceinfo.pci->subvendor_id);
  53. printf(" +-> subdevice_id %04x\n", device->deviceinfo.pci->subdevice_id);
  54. if (print_revision)
  55. printf(" +-> revision_id %02x\n", device->deviceinfo.pci->revision_id);
  56. else
  57. printf(" +-> revision_id IGNORED\n");
  58. } else if (device->bustype == DRM_BUS_USB) {
  59. printf("| +-> usb\n");
  60. printf("| +-> bus %03u\n", device->businfo.usb->bus);
  61. printf("| +-> dev %03u\n", device->businfo.usb->dev);
  62. printf("+-> deviceinfo\n");
  63. printf(" +-> usb\n");
  64. printf(" +-> vendor %04x\n", device->deviceinfo.usb->vendor);
  65. printf(" +-> product %04x\n", device->deviceinfo.usb->product);
  66. } else if (device->bustype == DRM_BUS_PLATFORM) {
  67. char **compatible = device->deviceinfo.platform->compatible;
  68. printf("| +-> platform\n");
  69. printf("| +-> fullname\t%s\n", device->businfo.platform->fullname);
  70. printf("+-> deviceinfo\n");
  71. printf(" +-> platform\n");
  72. printf(" +-> compatible\n");
  73. while (*compatible) {
  74. printf(" %s\n", *compatible);
  75. compatible++;
  76. }
  77. } else if (device->bustype == DRM_BUS_HOST1X) {
  78. char **compatible = device->deviceinfo.host1x->compatible;
  79. printf("| +-> host1x\n");
  80. printf("| +-> fullname\t%s\n", device->businfo.host1x->fullname);
  81. printf("+-> deviceinfo\n");
  82. printf(" +-> host1x\n");
  83. printf(" +-> compatible\n");
  84. while (*compatible) {
  85. printf(" %s\n", *compatible);
  86. compatible++;
  87. }
  88. } else if (device->bustype == DRM_BUS_FAUX) {
  89. printf("| +-> faux\n");
  90. printf("+-> businfo\n");
  91. printf(" +-> faux\n");
  92. printf(" +-> name %s\n", device->businfo.faux->name);
  93. } else {
  94. printf("Unknown/unhandled bustype\n");
  95. }
  96. printf("\n");
  97. }
  98. int
  99. main(void)
  100. {
  101. drmDevicePtr *devices;
  102. drmDevicePtr device;
  103. int fd, ret, max_devices;
  104. printf("--- Checking the number of DRM device available ---\n");
  105. max_devices = drmGetDevices2(0, NULL, 0);
  106. if (max_devices <= 0) {
  107. printf("drmGetDevices2() has not found any devices (errno=%d)\n",
  108. -max_devices);
  109. return 77;
  110. }
  111. printf("--- Devices reported %d ---\n", max_devices);
  112. devices = calloc(max_devices, sizeof(drmDevicePtr));
  113. if (devices == NULL) {
  114. printf("Failed to allocate memory for the drmDevicePtr array\n");
  115. return -1;
  116. }
  117. printf("--- Retrieving devices information (PCI device revision is ignored) ---\n");
  118. ret = drmGetDevices2(0, devices, max_devices);
  119. if (ret < 0) {
  120. printf("drmGetDevices2() returned an error %d\n", ret);
  121. free(devices);
  122. return -1;
  123. }
  124. for (int i = 0; i < ret; i++) {
  125. print_device_info(devices[i], i, false);
  126. for (int j = 0; j < DRM_NODE_MAX; j++) {
  127. if (devices[i]->available_nodes & 1 << j) {
  128. printf("--- Opening device node %s ---\n", devices[i]->nodes[j]);
  129. fd = open(devices[i]->nodes[j], O_RDONLY | O_CLOEXEC);
  130. if (fd < 0) {
  131. printf("Failed - %s (%d)\n", strerror(errno), errno);
  132. continue;
  133. }
  134. printf("--- Retrieving device info, for node %s ---\n", devices[i]->nodes[j]);
  135. if (drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, &device) == 0) {
  136. print_device_info(device, i, true);
  137. drmFreeDevice(&device);
  138. }
  139. close(fd);
  140. }
  141. }
  142. }
  143. drmFreeDevices(devices, ret);
  144. free(devices);
  145. return 0;
  146. }