| 1 |
/* |
| 2 |
* Copyright (c) 2015 Emil Velikov <[email protected]> |
| 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 |
|
| 24 |
#include <errno.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdbool.h> |
| 28 |
#include <string.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <fcntl.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <xf86drm.h> |
| 33 |
|
| 34 |
|
| 35 |
static void |
| 36 |
print_device_info(drmDevicePtr device, int i, bool print_revision) |
| 37 |
{ |
| 38 |
printf("device[%i]\n", i); |
| 39 |
printf("+-> available_nodes %#04x\n", device->available_nodes); |
| 40 |
printf("+-> nodes\n"); |
| 41 |
for (int j = 0; j < DRM_NODE_MAX; j++) |
| 42 |
if (device->available_nodes & 1 << j) |
| 43 |
printf("| +-> nodes[%d] %s\n", j, device->nodes[j]); |
| 44 |
|
| 45 |
printf("+-> bustype %04x\n", device->bustype); |
| 46 |
if (device->bustype == DRM_BUS_PCI) { |
| 47 |
printf("| +-> pci\n"); |
| 48 |
printf("| +-> domain %04x\n",device->businfo.pci->domain); |
| 49 |
printf("| +-> bus %02x\n", device->businfo.pci->bus); |
| 50 |
printf("| +-> dev %02x\n", device->businfo.pci->dev); |
| 51 |
printf("| +-> func %1u\n", device->businfo.pci->func); |
| 52 |
|
| 53 |
printf("+-> deviceinfo\n"); |
| 54 |
printf(" +-> pci\n"); |
| 55 |
printf(" +-> vendor_id %04x\n", device->deviceinfo.pci->vendor_id); |
| 56 |
printf(" +-> device_id %04x\n", device->deviceinfo.pci->device_id); |
| 57 |
printf(" +-> subvendor_id %04x\n", device->deviceinfo.pci->subvendor_id); |
| 58 |
printf(" +-> subdevice_id %04x\n", device->deviceinfo.pci->subdevice_id); |
| 59 |
if (print_revision) |
| 60 |
printf(" +-> revision_id %02x\n", device->deviceinfo.pci->revision_id); |
| 61 |
else |
| 62 |
printf(" +-> revision_id IGNORED\n"); |
| 63 |
|
| 64 |
} else if (device->bustype == DRM_BUS_USB) { |
| 65 |
printf("| +-> usb\n"); |
| 66 |
printf("| +-> bus %03u\n", device->businfo.usb->bus); |
| 67 |
printf("| +-> dev %03u\n", device->businfo.usb->dev); |
| 68 |
|
| 69 |
printf("+-> deviceinfo\n"); |
| 70 |
printf(" +-> usb\n"); |
| 71 |
printf(" +-> vendor %04x\n", device->deviceinfo.usb->vendor); |
| 72 |
printf(" +-> product %04x\n", device->deviceinfo.usb->product); |
| 73 |
} else if (device->bustype == DRM_BUS_PLATFORM) { |
| 74 |
char **compatible = device->deviceinfo.platform->compatible; |
| 75 |
|
| 76 |
printf("| +-> platform\n"); |
| 77 |
printf("| +-> fullname\t%s\n", device->businfo.platform->fullname); |
| 78 |
|
| 79 |
printf("+-> deviceinfo\n"); |
| 80 |
printf(" +-> platform\n"); |
| 81 |
printf(" +-> compatible\n"); |
| 82 |
|
| 83 |
while (*compatible) { |
| 84 |
printf(" %s\n", *compatible); |
| 85 |
compatible++; |
| 86 |
} |
| 87 |
} else if (device->bustype == DRM_BUS_HOST1X) { |
| 88 |
char **compatible = device->deviceinfo.host1x->compatible; |
| 89 |
|
| 90 |
printf("| +-> host1x\n"); |
| 91 |
printf("| +-> fullname\t%s\n", device->businfo.host1x->fullname); |
| 92 |
|
| 93 |
printf("+-> deviceinfo\n"); |
| 94 |
printf(" +-> host1x\n"); |
| 95 |
printf(" +-> compatible\n"); |
| 96 |
|
| 97 |
while (*compatible) { |
| 98 |
printf(" %s\n", *compatible); |
| 99 |
compatible++; |
| 100 |
} |
| 101 |
} else if (device->bustype == DRM_BUS_FAUX) { |
| 102 |
printf("| +-> faux\n"); |
| 103 |
|
| 104 |
printf("+-> businfo\n"); |
| 105 |
printf(" +-> faux\n"); |
| 106 |
printf(" +-> name %s\n", device->businfo.faux->name); |
| 107 |
} else { |
| 108 |
printf("Unknown/unhandled bustype\n"); |
| 109 |
} |
| 110 |
printf("\n"); |
| 111 |
} |
| 112 |
|
| 113 |
int |
| 114 |
main(void) |
| 115 |
{ |
| 116 |
drmDevicePtr *devices; |
| 117 |
drmDevicePtr device; |
| 118 |
int fd, ret, max_devices; |
| 119 |
|
| 120 |
printf("--- Checking the number of DRM device available ---\n"); |
| 121 |
max_devices = drmGetDevices2(0, NULL, 0); |
| 122 |
|
| 123 |
if (max_devices <= 0) { |
| 124 |
printf("drmGetDevices2() has not found any devices (errno=%d)\n", |
| 125 |
-max_devices); |
| 126 |
return 77; |
| 127 |
} |
| 128 |
printf("--- Devices reported %d ---\n", max_devices); |
| 129 |
|
| 130 |
|
| 131 |
devices = calloc(max_devices, sizeof(drmDevicePtr)); |
| 132 |
if (devices == NULL) { |
| 133 |
printf("Failed to allocate memory for the drmDevicePtr array\n"); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
|
| 137 |
printf("--- Retrieving devices information (PCI device revision is ignored) ---\n"); |
| 138 |
ret = drmGetDevices2(0, devices, max_devices); |
| 139 |
if (ret < 0) { |
| 140 |
printf("drmGetDevices2() returned an error %d\n", ret); |
| 141 |
free(devices); |
| 142 |
return -1; |
| 143 |
} |
| 144 |
|
| 145 |
for (int i = 0; i < ret; i++) { |
| 146 |
print_device_info(devices[i], i, false); |
| 147 |
|
| 148 |
for (int j = 0; j < DRM_NODE_MAX; j++) { |
| 149 |
if (devices[i]->available_nodes & 1 << j) { |
| 150 |
printf("--- Opening device node %s ---\n", devices[i]->nodes[j]); |
| 151 |
fd = open(devices[i]->nodes[j], O_RDONLY | O_CLOEXEC); |
| 152 |
if (fd < 0) { |
| 153 |
printf("Failed - %s (%d)\n", strerror(errno), errno); |
| 154 |
continue; |
| 155 |
} |
| 156 |
|
| 157 |
printf("--- Retrieving device info, for node %s ---\n", devices[i]->nodes[j]); |
| 158 |
if (drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, &device) == 0) { |
| 159 |
print_device_info(device, i, true); |
| 160 |
drmFreeDevice(&device); |
| 161 |
} |
| 162 |
close(fd); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
drmFreeDevices(devices, ret); |
| 168 |
free(devices); |
| 169 |
return 0; |
| 170 |
} |