drmdevice.c revision 3f012e29
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>
273f012e29Smrg#include <string.h>
283f012e29Smrg#include <sys/stat.h>
293f012e29Smrg#include <fcntl.h>
303f012e29Smrg#include <unistd.h>
313f012e29Smrg#include <xf86drm.h>
323f012e29Smrg
333f012e29Smrg
343f012e29Smrgstatic void
353f012e29Smrgprint_device_info(drmDevicePtr device, int i)
363f012e29Smrg{
373f012e29Smrg    printf("device[%i]\n", i);
383f012e29Smrg    printf("\tavailable_nodes %04x\n", device->available_nodes);
393f012e29Smrg    printf("\tnodes\n");
403f012e29Smrg    for (int j = 0; j < DRM_NODE_MAX; j++)
413f012e29Smrg        if (device->available_nodes & 1 << j)
423f012e29Smrg            printf("\t\tnodes[%d] %s\n", j, device->nodes[j]);
433f012e29Smrg
443f012e29Smrg    printf("\tbustype %04x\n", device->bustype);
453f012e29Smrg    printf("\tbusinfo\n");
463f012e29Smrg    if (device->bustype == DRM_BUS_PCI) {
473f012e29Smrg        printf("\t\tpci\n");
483f012e29Smrg        printf("\t\t\tdomain\t%04x\n",device->businfo.pci->domain);
493f012e29Smrg        printf("\t\t\tbus\t%02x\n", device->businfo.pci->bus);
503f012e29Smrg        printf("\t\t\tdev\t%02x\n", device->businfo.pci->dev);
513f012e29Smrg        printf("\t\t\tfunc\t%1u\n", device->businfo.pci->func);
523f012e29Smrg
533f012e29Smrg        printf("\tdeviceinfo\n");
543f012e29Smrg        printf("\t\tpci\n");
553f012e29Smrg        printf("\t\t\tvendor_id\t%04x\n", device->deviceinfo.pci->vendor_id);
563f012e29Smrg        printf("\t\t\tdevice_id\t%04x\n", device->deviceinfo.pci->device_id);
573f012e29Smrg        printf("\t\t\tsubvendor_id\t%04x\n", device->deviceinfo.pci->subvendor_id);
583f012e29Smrg        printf("\t\t\tsubdevice_id\t%04x\n", device->deviceinfo.pci->subdevice_id);
593f012e29Smrg        printf("\t\t\trevision_id\t%02x\n", device->deviceinfo.pci->revision_id);
603f012e29Smrg    } else {
613f012e29Smrg        printf("Unknown/unhandled bustype\n");
623f012e29Smrg    }
633f012e29Smrg    printf("\n");
643f012e29Smrg}
653f012e29Smrg
663f012e29Smrgint
673f012e29Smrgmain(void)
683f012e29Smrg{
693f012e29Smrg    drmDevicePtr *devices;
703f012e29Smrg    drmDevicePtr device;
713f012e29Smrg    int fd, ret, max_devices;
723f012e29Smrg
733f012e29Smrg    max_devices = drmGetDevices(NULL, 0);
743f012e29Smrg
753f012e29Smrg    if (max_devices <= 0) {
763f012e29Smrg        printf("drmGetDevices() has returned %d\n", max_devices);
773f012e29Smrg        return -1;
783f012e29Smrg    }
793f012e29Smrg
803f012e29Smrg    devices = calloc(max_devices, sizeof(drmDevicePtr));
813f012e29Smrg    if (devices == NULL) {
823f012e29Smrg        printf("Failed to allocate memory for the drmDevicePtr array\n");
833f012e29Smrg        return -1;
843f012e29Smrg    }
853f012e29Smrg
863f012e29Smrg    ret = drmGetDevices(devices, max_devices);
873f012e29Smrg    if (ret < 0) {
883f012e29Smrg        printf("drmGetDevices() returned an error %d\n", ret);
893f012e29Smrg        free(devices);
903f012e29Smrg        return -1;
913f012e29Smrg    }
923f012e29Smrg
933f012e29Smrg    for (int i = 0; i < ret; i++) {
943f012e29Smrg        print_device_info(devices[i], i);
953f012e29Smrg
963f012e29Smrg        for (int j = 0; j < DRM_NODE_MAX; j++) {
973f012e29Smrg            if (devices[i]->available_nodes & 1 << j) {
983f012e29Smrg                printf("Opening device %d node %s\n", i, devices[i]->nodes[j]);
993f012e29Smrg                fd = open(devices[i]->nodes[j], O_RDONLY | O_CLOEXEC, 0);
1003f012e29Smrg                if (fd < 0) {
1013f012e29Smrg                    printf("Failed - %s (%d)\n", strerror(errno), errno);
1023f012e29Smrg                    continue;
1033f012e29Smrg                }
1043f012e29Smrg
1053f012e29Smrg                if (drmGetDevice(fd, &device) == 0) {
1063f012e29Smrg                    print_device_info(device, i);
1073f012e29Smrg                    drmFreeDevice(&device);
1083f012e29Smrg                }
1093f012e29Smrg                close(fd);
1103f012e29Smrg            }
1113f012e29Smrg        }
1123f012e29Smrg    }
1133f012e29Smrg
1143f012e29Smrg    drmFreeDevices(devices, ret);
1153f012e29Smrg    free(devices);
1163f012e29Smrg    return 0;
1173f012e29Smrg}
118