xref: /third_party/libdrm/tests/drmdevice.c (revision d722e3fb)
1d722e3fbSopenharmony_ci/*
2d722e3fbSopenharmony_ci * Copyright (c) 2015 Emil Velikov <emil.l.velikov@gmail.com>
3d722e3fbSopenharmony_ci *
4d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation
7d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10d722e3fbSopenharmony_ci *
11d722e3fbSopenharmony_ci * The above copyright notice and this permission notice shall be included in
12d722e3fbSopenharmony_ci * all copies or substantial portions of the Software.
13d722e3fbSopenharmony_ci *
14d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17d722e3fbSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18d722e3fbSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19d722e3fbSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20d722e3fbSopenharmony_ci * IN THE SOFTWARE.
21d722e3fbSopenharmony_ci *
22d722e3fbSopenharmony_ci */
23d722e3fbSopenharmony_ci
24d722e3fbSopenharmony_ci#include <errno.h>
25d722e3fbSopenharmony_ci#include <stdio.h>
26d722e3fbSopenharmony_ci#include <stdlib.h>
27d722e3fbSopenharmony_ci#include <stdbool.h>
28d722e3fbSopenharmony_ci#include <string.h>
29d722e3fbSopenharmony_ci#include <sys/stat.h>
30d722e3fbSopenharmony_ci#include <fcntl.h>
31d722e3fbSopenharmony_ci#include <unistd.h>
32d722e3fbSopenharmony_ci#include <xf86drm.h>
33d722e3fbSopenharmony_ci
34d722e3fbSopenharmony_ci
35d722e3fbSopenharmony_cistatic void
36d722e3fbSopenharmony_ciprint_device_info(drmDevicePtr device, int i, bool print_revision)
37d722e3fbSopenharmony_ci{
38d722e3fbSopenharmony_ci    printf("device[%i]\n", i);
39d722e3fbSopenharmony_ci    printf("+-> available_nodes %#04x\n", device->available_nodes);
40d722e3fbSopenharmony_ci    printf("+-> nodes\n");
41d722e3fbSopenharmony_ci    for (int j = 0; j < DRM_NODE_MAX; j++)
42d722e3fbSopenharmony_ci        if (device->available_nodes & 1 << j)
43d722e3fbSopenharmony_ci            printf("|   +-> nodes[%d] %s\n", j, device->nodes[j]);
44d722e3fbSopenharmony_ci
45d722e3fbSopenharmony_ci    printf("+-> bustype %04x\n", device->bustype);
46d722e3fbSopenharmony_ci    if (device->bustype == DRM_BUS_PCI) {
47d722e3fbSopenharmony_ci        printf("|   +-> pci\n");
48d722e3fbSopenharmony_ci        printf("|       +-> domain %04x\n",device->businfo.pci->domain);
49d722e3fbSopenharmony_ci        printf("|       +-> bus    %02x\n", device->businfo.pci->bus);
50d722e3fbSopenharmony_ci        printf("|       +-> dev    %02x\n", device->businfo.pci->dev);
51d722e3fbSopenharmony_ci        printf("|       +-> func   %1u\n", device->businfo.pci->func);
52d722e3fbSopenharmony_ci
53d722e3fbSopenharmony_ci        printf("+-> deviceinfo\n");
54d722e3fbSopenharmony_ci        printf("    +-> pci\n");
55d722e3fbSopenharmony_ci        printf("        +-> vendor_id     %04x\n", device->deviceinfo.pci->vendor_id);
56d722e3fbSopenharmony_ci        printf("        +-> device_id     %04x\n", device->deviceinfo.pci->device_id);
57d722e3fbSopenharmony_ci        printf("        +-> subvendor_id  %04x\n", device->deviceinfo.pci->subvendor_id);
58d722e3fbSopenharmony_ci        printf("        +-> subdevice_id  %04x\n", device->deviceinfo.pci->subdevice_id);
59d722e3fbSopenharmony_ci        if (print_revision)
60d722e3fbSopenharmony_ci            printf("        +-> revision_id   %02x\n", device->deviceinfo.pci->revision_id);
61d722e3fbSopenharmony_ci        else
62d722e3fbSopenharmony_ci            printf("        +-> revision_id   IGNORED\n");
63d722e3fbSopenharmony_ci
64d722e3fbSopenharmony_ci    } else if (device->bustype == DRM_BUS_USB) {
65d722e3fbSopenharmony_ci        printf("|   +-> usb\n");
66d722e3fbSopenharmony_ci        printf("|       +-> bus %03u\n", device->businfo.usb->bus);
67d722e3fbSopenharmony_ci        printf("|       +-> dev %03u\n", device->businfo.usb->dev);
68d722e3fbSopenharmony_ci
69d722e3fbSopenharmony_ci        printf("+-> deviceinfo\n");
70d722e3fbSopenharmony_ci        printf("    +-> usb\n");
71d722e3fbSopenharmony_ci        printf("        +-> vendor  %04x\n", device->deviceinfo.usb->vendor);
72d722e3fbSopenharmony_ci        printf("        +-> product %04x\n", device->deviceinfo.usb->product);
73d722e3fbSopenharmony_ci    } else if (device->bustype == DRM_BUS_PLATFORM) {
74d722e3fbSopenharmony_ci        char **compatible = device->deviceinfo.platform->compatible;
75d722e3fbSopenharmony_ci
76d722e3fbSopenharmony_ci        printf("|   +-> platform\n");
77d722e3fbSopenharmony_ci        printf("|       +-> fullname\t%s\n", device->businfo.platform->fullname);
78d722e3fbSopenharmony_ci
79d722e3fbSopenharmony_ci        printf("+-> deviceinfo\n");
80d722e3fbSopenharmony_ci        printf("    +-> platform\n");
81d722e3fbSopenharmony_ci        printf("        +-> compatible\n");
82d722e3fbSopenharmony_ci
83d722e3fbSopenharmony_ci        while (*compatible) {
84d722e3fbSopenharmony_ci            printf("                    %s\n", *compatible);
85d722e3fbSopenharmony_ci            compatible++;
86d722e3fbSopenharmony_ci        }
87d722e3fbSopenharmony_ci    } else if (device->bustype == DRM_BUS_HOST1X) {
88d722e3fbSopenharmony_ci        char **compatible = device->deviceinfo.host1x->compatible;
89d722e3fbSopenharmony_ci
90d722e3fbSopenharmony_ci        printf("|   +-> host1x\n");
91d722e3fbSopenharmony_ci        printf("|       +-> fullname\t%s\n", device->businfo.host1x->fullname);
92d722e3fbSopenharmony_ci
93d722e3fbSopenharmony_ci        printf("+-> deviceinfo\n");
94d722e3fbSopenharmony_ci        printf("    +-> host1x\n");
95d722e3fbSopenharmony_ci        printf("        +-> compatible\n");
96d722e3fbSopenharmony_ci
97d722e3fbSopenharmony_ci        while (*compatible) {
98d722e3fbSopenharmony_ci            printf("                    %s\n", *compatible);
99d722e3fbSopenharmony_ci            compatible++;
100d722e3fbSopenharmony_ci        }
101d722e3fbSopenharmony_ci    } else {
102d722e3fbSopenharmony_ci        printf("Unknown/unhandled bustype\n");
103d722e3fbSopenharmony_ci    }
104d722e3fbSopenharmony_ci    printf("\n");
105d722e3fbSopenharmony_ci}
106d722e3fbSopenharmony_ci
107d722e3fbSopenharmony_ciint
108d722e3fbSopenharmony_cimain(void)
109d722e3fbSopenharmony_ci{
110d722e3fbSopenharmony_ci    drmDevicePtr *devices;
111d722e3fbSopenharmony_ci    drmDevicePtr device;
112d722e3fbSopenharmony_ci    int fd, ret, max_devices;
113d722e3fbSopenharmony_ci
114d722e3fbSopenharmony_ci    printf("--- Checking the number of DRM device available ---\n");
115d722e3fbSopenharmony_ci    max_devices = drmGetDevices2(0, NULL, 0);
116d722e3fbSopenharmony_ci
117d722e3fbSopenharmony_ci    if (max_devices <= 0) {
118d722e3fbSopenharmony_ci        printf("drmGetDevices2() has not found any devices (errno=%d)\n",
119d722e3fbSopenharmony_ci               -max_devices);
120d722e3fbSopenharmony_ci        return 77;
121d722e3fbSopenharmony_ci    }
122d722e3fbSopenharmony_ci    printf("--- Devices reported %d ---\n", max_devices);
123d722e3fbSopenharmony_ci
124d722e3fbSopenharmony_ci
125d722e3fbSopenharmony_ci    devices = calloc(max_devices, sizeof(drmDevicePtr));
126d722e3fbSopenharmony_ci    if (devices == NULL) {
127d722e3fbSopenharmony_ci        printf("Failed to allocate memory for the drmDevicePtr array\n");
128d722e3fbSopenharmony_ci        return -1;
129d722e3fbSopenharmony_ci    }
130d722e3fbSopenharmony_ci
131d722e3fbSopenharmony_ci    printf("--- Retrieving devices information (PCI device revision is ignored) ---\n");
132d722e3fbSopenharmony_ci    ret = drmGetDevices2(0, devices, max_devices);
133d722e3fbSopenharmony_ci    if (ret < 0) {
134d722e3fbSopenharmony_ci        printf("drmGetDevices2() returned an error %d\n", ret);
135d722e3fbSopenharmony_ci        free(devices);
136d722e3fbSopenharmony_ci        return -1;
137d722e3fbSopenharmony_ci    }
138d722e3fbSopenharmony_ci
139d722e3fbSopenharmony_ci    for (int i = 0; i < ret; i++) {
140d722e3fbSopenharmony_ci        print_device_info(devices[i], i, false);
141d722e3fbSopenharmony_ci
142d722e3fbSopenharmony_ci        for (int j = 0; j < DRM_NODE_MAX; j++) {
143d722e3fbSopenharmony_ci            if (devices[i]->available_nodes & 1 << j) {
144d722e3fbSopenharmony_ci                printf("--- Opening device node %s ---\n", devices[i]->nodes[j]);
145d722e3fbSopenharmony_ci                fd = open(devices[i]->nodes[j], O_RDONLY | O_CLOEXEC, 0);
146d722e3fbSopenharmony_ci                if (fd < 0) {
147d722e3fbSopenharmony_ci                    printf("Failed - %s (%d)\n", strerror(errno), errno);
148d722e3fbSopenharmony_ci                    continue;
149d722e3fbSopenharmony_ci                }
150d722e3fbSopenharmony_ci
151d722e3fbSopenharmony_ci                printf("--- Retrieving device info, for node %s ---\n", devices[i]->nodes[j]);
152d722e3fbSopenharmony_ci                if (drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, &device) == 0) {
153d722e3fbSopenharmony_ci                    print_device_info(device, i, true);
154d722e3fbSopenharmony_ci                    drmFreeDevice(&device);
155d722e3fbSopenharmony_ci                }
156d722e3fbSopenharmony_ci                close(fd);
157d722e3fbSopenharmony_ci            }
158d722e3fbSopenharmony_ci        }
159d722e3fbSopenharmony_ci    }
160d722e3fbSopenharmony_ci
161d722e3fbSopenharmony_ci    drmFreeDevices(devices, ret);
162d722e3fbSopenharmony_ci    free(devices);
163d722e3fbSopenharmony_ci    return 0;
164d722e3fbSopenharmony_ci}
165