13f012e29Smrg/*
23f012e29Smrg * Copyright (c) 2015 Emil Velikov <emil.l.velikov@gmail.com>
33f012e29Smrg *
43f012e29Smrg * Permission is hereby granted, free of charge, to any person obtaining a
53f012e29Smrg * copy of this software and associated documentation files (the "Software"),
63f012e29Smrg * to deal in the Software without restriction, including without limitation
73f012e29Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83f012e29Smrg * and/or sell copies of the Software, and to permit persons to whom the
93f012e29Smrg * Software is furnished to do so, subject to the following conditions:
103f012e29Smrg *
113f012e29Smrg * The above copyright notice and this permission notice shall be included in
123f012e29Smrg * all copies or substantial portions of the Software.
133f012e29Smrg *
143f012e29Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
153f012e29Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
163f012e29Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
173f012e29Smrg * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
183f012e29Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
193f012e29Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
203f012e29Smrg * IN THE SOFTWARE.
213f012e29Smrg *
223f012e29Smrg */
233f012e29Smrg
243f012e29Smrg#include <errno.h>
253f012e29Smrg#include <stdio.h>
263f012e29Smrg#include <stdlib.h>
27037b3c26Smrg#include <stdbool.h>
283f012e29Smrg#include <string.h>
293f012e29Smrg#include <sys/stat.h>
303f012e29Smrg#include <fcntl.h>
313f012e29Smrg#include <unistd.h>
323f012e29Smrg#include <xf86drm.h>
333f012e29Smrg
343f012e29Smrg
353f012e29Smrgstatic void
36037b3c26Smrgprint_device_info(drmDevicePtr device, int i, bool print_revision)
373f012e29Smrg{
383f012e29Smrg    printf("device[%i]\n", i);
397cdc0497Smrg    printf("+-> available_nodes %#04x\n", device->available_nodes);
407cdc0497Smrg    printf("+-> nodes\n");
413f012e29Smrg    for (int j = 0; j < DRM_NODE_MAX; j++)
423f012e29Smrg        if (device->available_nodes & 1 << j)
437cdc0497Smrg            printf("|   +-> nodes[%d] %s\n", j, device->nodes[j]);
443f012e29Smrg
457cdc0497Smrg    printf("+-> bustype %04x\n", device->bustype);
463f012e29Smrg    if (device->bustype == DRM_BUS_PCI) {
477cdc0497Smrg        printf("|   +-> pci\n");
487cdc0497Smrg        printf("|       +-> domain %04x\n",device->businfo.pci->domain);
497cdc0497Smrg        printf("|       +-> bus    %02x\n", device->businfo.pci->bus);
507cdc0497Smrg        printf("|       +-> dev    %02x\n", device->businfo.pci->dev);
517cdc0497Smrg        printf("|       +-> func   %1u\n", device->businfo.pci->func);
527cdc0497Smrg
537cdc0497Smrg        printf("+-> deviceinfo\n");
547cdc0497Smrg        printf("    +-> pci\n");
557cdc0497Smrg        printf("        +-> vendor_id     %04x\n", device->deviceinfo.pci->vendor_id);
567cdc0497Smrg        printf("        +-> device_id     %04x\n", device->deviceinfo.pci->device_id);
577cdc0497Smrg        printf("        +-> subvendor_id  %04x\n", device->deviceinfo.pci->subvendor_id);
587cdc0497Smrg        printf("        +-> subdevice_id  %04x\n", device->deviceinfo.pci->subdevice_id);
59037b3c26Smrg        if (print_revision)
607cdc0497Smrg            printf("        +-> revision_id   %02x\n", device->deviceinfo.pci->revision_id);
61037b3c26Smrg        else
627cdc0497Smrg            printf("        +-> revision_id   IGNORED\n");
63037b3c26Smrg
64037b3c26Smrg    } else if (device->bustype == DRM_BUS_USB) {
657cdc0497Smrg        printf("|   +-> usb\n");
667cdc0497Smrg        printf("|       +-> bus %03u\n", device->businfo.usb->bus);
677cdc0497Smrg        printf("|       +-> dev %03u\n", device->businfo.usb->dev);
687cdc0497Smrg
697cdc0497Smrg        printf("+-> deviceinfo\n");
707cdc0497Smrg        printf("    +-> usb\n");
717cdc0497Smrg        printf("        +-> vendor  %04x\n", device->deviceinfo.usb->vendor);
727cdc0497Smrg        printf("        +-> product %04x\n", device->deviceinfo.usb->product);
73037b3c26Smrg    } else if (device->bustype == DRM_BUS_PLATFORM) {
74037b3c26Smrg        char **compatible = device->deviceinfo.platform->compatible;
75037b3c26Smrg
767cdc0497Smrg        printf("|   +-> platform\n");
777cdc0497Smrg        printf("|       +-> fullname\t%s\n", device->businfo.platform->fullname);
78037b3c26Smrg
797cdc0497Smrg        printf("+-> deviceinfo\n");
807cdc0497Smrg        printf("    +-> platform\n");
817cdc0497Smrg        printf("        +-> compatible\n");
82037b3c26Smrg
83037b3c26Smrg        while (*compatible) {
847cdc0497Smrg            printf("                    %s\n", *compatible);
85037b3c26Smrg            compatible++;
86037b3c26Smrg        }
87037b3c26Smrg    } else if (device->bustype == DRM_BUS_HOST1X) {
887cdc0497Smrg        char **compatible = device->deviceinfo.host1x->compatible;
89037b3c26Smrg
907cdc0497Smrg        printf("|   +-> host1x\n");
917cdc0497Smrg        printf("|       +-> fullname\t%s\n", device->businfo.host1x->fullname);
92037b3c26Smrg
937cdc0497Smrg        printf("+-> deviceinfo\n");
947cdc0497Smrg        printf("    +-> host1x\n");
957cdc0497Smrg        printf("        +-> compatible\n");
96037b3c26Smrg
97037b3c26Smrg        while (*compatible) {
987cdc0497Smrg            printf("                    %s\n", *compatible);
99037b3c26Smrg            compatible++;
100037b3c26Smrg        }
1013f012e29Smrg    } else {
1023f012e29Smrg        printf("Unknown/unhandled bustype\n");
1033f012e29Smrg    }
1043f012e29Smrg    printf("\n");
1053f012e29Smrg}
1063f012e29Smrg
1073f012e29Smrgint
1083f012e29Smrgmain(void)
1093f012e29Smrg{
1103f012e29Smrg    drmDevicePtr *devices;
1113f012e29Smrg    drmDevicePtr device;
1123f012e29Smrg    int fd, ret, max_devices;
1133f012e29Smrg
1147cdc0497Smrg    printf("--- Checking the number of DRM device available ---\n");
115037b3c26Smrg    max_devices = drmGetDevices2(0, NULL, 0);
1163f012e29Smrg
1173f012e29Smrg    if (max_devices <= 0) {
1186532f28eSmrg        printf("drmGetDevices2() has not found any devices (errno=%d)\n",
1196532f28eSmrg               -max_devices);
1206532f28eSmrg        return 77;
1213f012e29Smrg    }
1227cdc0497Smrg    printf("--- Devices reported %d ---\n", max_devices);
1237cdc0497Smrg
1243f012e29Smrg
1253f012e29Smrg    devices = calloc(max_devices, sizeof(drmDevicePtr));
1263f012e29Smrg    if (devices == NULL) {
1273f012e29Smrg        printf("Failed to allocate memory for the drmDevicePtr array\n");
1283f012e29Smrg        return -1;
1293f012e29Smrg    }
1303f012e29Smrg
1317cdc0497Smrg    printf("--- Retrieving devices information (PCI device revision is ignored) ---\n");
132037b3c26Smrg    ret = drmGetDevices2(0, devices, max_devices);
1333f012e29Smrg    if (ret < 0) {
134037b3c26Smrg        printf("drmGetDevices2() returned an error %d\n", ret);
1353f012e29Smrg        free(devices);
1363f012e29Smrg        return -1;
1373f012e29Smrg    }
1383f012e29Smrg
1393f012e29Smrg    for (int i = 0; i < ret; i++) {
140037b3c26Smrg        print_device_info(devices[i], i, false);
1413f012e29Smrg
1423f012e29Smrg        for (int j = 0; j < DRM_NODE_MAX; j++) {
1433f012e29Smrg            if (devices[i]->available_nodes & 1 << j) {
1447cdc0497Smrg                printf("--- Opening device node %s ---\n", devices[i]->nodes[j]);
145b0ab5608Smrg                fd = open(devices[i]->nodes[j], O_RDONLY | O_CLOEXEC);
1463f012e29Smrg                if (fd < 0) {
1473f012e29Smrg                    printf("Failed - %s (%d)\n", strerror(errno), errno);
1483f012e29Smrg                    continue;
1493f012e29Smrg                }
1503f012e29Smrg
1517cdc0497Smrg                printf("--- Retrieving device info, for node %s ---\n", devices[i]->nodes[j]);
152037b3c26Smrg                if (drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, &device) == 0) {
153037b3c26Smrg                    print_device_info(device, i, true);
1543f012e29Smrg                    drmFreeDevice(&device);
1553f012e29Smrg                }
1563f012e29Smrg                close(fd);
1573f012e29Smrg            }
1583f012e29Smrg        }
1593f012e29Smrg    }
1603f012e29Smrg
1613f012e29Smrg    drmFreeDevices(devices, ret);
1623f012e29Smrg    free(devices);
1633f012e29Smrg    return 0;
1643f012e29Smrg}
165