Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 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 | |
Emil Velikov | 681fd2a | 2016-07-13 10:01:34 +0100 | [diff] [blame] | 24 | #include <errno.h> |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Emil Velikov | b305238 | 2016-11-30 19:13:04 +0000 | [diff] [blame] | 27 | #include <stdbool.h> |
Emil Velikov | 681fd2a | 2016-07-13 10:01:34 +0100 | [diff] [blame] | 28 | #include <string.h> |
Emil Velikov | 8990ed3 | 2015-09-09 18:13:01 +0100 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 32 | #include <xf86drm.h> |
| 33 | |
| 34 | |
| 35 | static void |
Emil Velikov | b305238 | 2016-11-30 19:13:04 +0000 | [diff] [blame] | 36 | print_device_info(drmDevicePtr device, int i, bool print_revision) |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 37 | { |
| 38 | printf("device[%i]\n", i); |
| 39 | printf("\tavailable_nodes %04x\n", device->available_nodes); |
| 40 | printf("\tnodes\n"); |
| 41 | for (int j = 0; j < DRM_NODE_MAX; j++) |
| 42 | if (device->available_nodes & 1 << j) |
| 43 | printf("\t\tnodes[%d] %s\n", j, device->nodes[j]); |
| 44 | |
| 45 | printf("\tbustype %04x\n", device->bustype); |
| 46 | printf("\tbusinfo\n"); |
| 47 | if (device->bustype == DRM_BUS_PCI) { |
| 48 | printf("\t\tpci\n"); |
| 49 | printf("\t\t\tdomain\t%04x\n",device->businfo.pci->domain); |
Emil Velikov | dd58044 | 2016-07-13 09:43:26 +0100 | [diff] [blame] | 50 | printf("\t\t\tbus\t%02x\n", device->businfo.pci->bus); |
| 51 | printf("\t\t\tdev\t%02x\n", device->businfo.pci->dev); |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 52 | printf("\t\t\tfunc\t%1u\n", device->businfo.pci->func); |
| 53 | |
| 54 | printf("\tdeviceinfo\n"); |
| 55 | printf("\t\tpci\n"); |
| 56 | printf("\t\t\tvendor_id\t%04x\n", device->deviceinfo.pci->vendor_id); |
| 57 | printf("\t\t\tdevice_id\t%04x\n", device->deviceinfo.pci->device_id); |
| 58 | printf("\t\t\tsubvendor_id\t%04x\n", device->deviceinfo.pci->subvendor_id); |
| 59 | printf("\t\t\tsubdevice_id\t%04x\n", device->deviceinfo.pci->subdevice_id); |
Emil Velikov | b305238 | 2016-11-30 19:13:04 +0000 | [diff] [blame] | 60 | if (print_revision) |
| 61 | printf("\t\t\trevision_id\t%02x\n", device->deviceinfo.pci->revision_id); |
| 62 | else |
| 63 | printf("\t\t\trevision_id\tIGNORED\n"); |
| 64 | |
Thierry Reding | 13b99f2 | 2017-01-12 22:07:28 +0100 | [diff] [blame] | 65 | } else if (device->bustype == DRM_BUS_USB) { |
| 66 | printf("\t\tusb\n"); |
| 67 | printf("\t\t\tbus\t%03u\n", device->businfo.usb->bus); |
| 68 | printf("\t\t\tdev\t%03u\n", device->businfo.usb->dev); |
| 69 | |
| 70 | printf("\tdeviceinfo\n"); |
| 71 | printf("\t\tusb\n"); |
| 72 | printf("\t\t\tvendor\t%04x\n", device->deviceinfo.usb->vendor); |
| 73 | printf("\t\t\tproduct\t%04x\n", device->deviceinfo.usb->product); |
| 74 | } else if (device->bustype == DRM_BUS_PLATFORM) { |
| 75 | char **compatible = device->deviceinfo.platform->compatible; |
| 76 | |
| 77 | printf("\t\tplatform\n"); |
| 78 | printf("\t\t\tfullname\t%s\n", device->businfo.platform->fullname); |
| 79 | |
| 80 | printf("\tdeviceinfo\n"); |
| 81 | printf("\t\tplatform\n"); |
| 82 | printf("\t\t\tcompatible\n"); |
| 83 | |
| 84 | while (*compatible) { |
| 85 | printf("\t\t\t\t%s\n", *compatible); |
| 86 | compatible++; |
| 87 | } |
| 88 | } else if (device->bustype == DRM_BUS_HOST1X) { |
| 89 | char **compatible = device->deviceinfo.platform->compatible; |
| 90 | |
| 91 | printf("\t\thost1x\n"); |
| 92 | printf("\t\t\tfullname\t%s\n", device->businfo.host1x->fullname); |
| 93 | |
| 94 | printf("\tdeviceinfo\n"); |
| 95 | printf("\t\tplatform\n"); |
| 96 | printf("\t\t\tcompatible\n"); |
| 97 | |
| 98 | while (*compatible) { |
| 99 | printf("\t\t\t\t%s\n", *compatible); |
| 100 | compatible++; |
| 101 | } |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 102 | } else { |
| 103 | printf("Unknown/unhandled bustype\n"); |
| 104 | } |
| 105 | printf("\n"); |
| 106 | } |
| 107 | |
| 108 | int |
| 109 | main(void) |
| 110 | { |
| 111 | drmDevicePtr *devices; |
Emil Velikov | 8990ed3 | 2015-09-09 18:13:01 +0100 | [diff] [blame] | 112 | drmDevicePtr device; |
| 113 | int fd, ret, max_devices; |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 114 | |
Emil Velikov | b305238 | 2016-11-30 19:13:04 +0000 | [diff] [blame] | 115 | max_devices = drmGetDevices2(0, NULL, 0); |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 116 | |
| 117 | if (max_devices <= 0) { |
Emil Velikov | b305238 | 2016-11-30 19:13:04 +0000 | [diff] [blame] | 118 | printf("drmGetDevices2() has returned %d\n", max_devices); |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | devices = calloc(max_devices, sizeof(drmDevicePtr)); |
| 123 | if (devices == NULL) { |
| 124 | printf("Failed to allocate memory for the drmDevicePtr array\n"); |
| 125 | return -1; |
| 126 | } |
| 127 | |
Emil Velikov | b305238 | 2016-11-30 19:13:04 +0000 | [diff] [blame] | 128 | ret = drmGetDevices2(0, devices, max_devices); |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 129 | if (ret < 0) { |
Emil Velikov | b305238 | 2016-11-30 19:13:04 +0000 | [diff] [blame] | 130 | printf("drmGetDevices2() returned an error %d\n", ret); |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 131 | free(devices); |
| 132 | return -1; |
| 133 | } |
| 134 | |
Emil Velikov | 8990ed3 | 2015-09-09 18:13:01 +0100 | [diff] [blame] | 135 | for (int i = 0; i < ret; i++) { |
Emil Velikov | b305238 | 2016-11-30 19:13:04 +0000 | [diff] [blame] | 136 | print_device_info(devices[i], i, false); |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 137 | |
Emil Velikov | 8990ed3 | 2015-09-09 18:13:01 +0100 | [diff] [blame] | 138 | for (int j = 0; j < DRM_NODE_MAX; j++) { |
| 139 | if (devices[i]->available_nodes & 1 << j) { |
Emil Velikov | 681fd2a | 2016-07-13 10:01:34 +0100 | [diff] [blame] | 140 | printf("Opening device %d node %s\n", i, devices[i]->nodes[j]); |
Emil Velikov | 8990ed3 | 2015-09-09 18:13:01 +0100 | [diff] [blame] | 141 | fd = open(devices[i]->nodes[j], O_RDONLY | O_CLOEXEC, 0); |
Emil Velikov | 681fd2a | 2016-07-13 10:01:34 +0100 | [diff] [blame] | 142 | if (fd < 0) { |
| 143 | printf("Failed - %s (%d)\n", strerror(errno), errno); |
Emil Velikov | 8990ed3 | 2015-09-09 18:13:01 +0100 | [diff] [blame] | 144 | continue; |
Emil Velikov | 681fd2a | 2016-07-13 10:01:34 +0100 | [diff] [blame] | 145 | } |
Emil Velikov | 8990ed3 | 2015-09-09 18:13:01 +0100 | [diff] [blame] | 146 | |
Emil Velikov | b305238 | 2016-11-30 19:13:04 +0000 | [diff] [blame] | 147 | if (drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, &device) == 0) { |
| 148 | print_device_info(device, i, true); |
Emil Velikov | 8990ed3 | 2015-09-09 18:13:01 +0100 | [diff] [blame] | 149 | drmFreeDevice(&device); |
| 150 | } |
| 151 | close(fd); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Emil Velikov | f098d1c | 2015-09-09 16:48:29 +0100 | [diff] [blame] | 156 | drmFreeDevices(devices, ret); |
| 157 | free(devices); |
| 158 | return 0; |
| 159 | } |