Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (c) 2015 Emil Velikov <emil.l.velikov (at) 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 
     24 #include <stdio.h>
     25 #include <stdlib.h>
     26 #include <sys/stat.h>
     27 #include <fcntl.h>
     28 #include <unistd.h>
     29 #include <xf86drm.h>
     30 
     31 
     32 static void
     33 print_device_info(drmDevicePtr device, int i)
     34 {
     35     printf("device[%i]\n", i);
     36     printf("\tavailable_nodes %04x\n", device->available_nodes);
     37     printf("\tnodes\n");
     38     for (int j = 0; j < DRM_NODE_MAX; j++)
     39         if (device->available_nodes & 1 << j)
     40             printf("\t\tnodes[%d] %s\n", j, device->nodes[j]);
     41 
     42     printf("\tbustype %04x\n", device->bustype);
     43     printf("\tbusinfo\n");
     44     if (device->bustype == DRM_BUS_PCI) {
     45         printf("\t\tpci\n");
     46         printf("\t\t\tdomain\t%04x\n",device->businfo.pci->domain);
     47         printf("\t\t\tbu\t%02x\n", device->businfo.pci->bus);
     48         printf("\t\t\tde\t%02x\n", device->businfo.pci->dev);
     49         printf("\t\t\tfunc\t%1u\n", device->businfo.pci->func);
     50 
     51         printf("\tdeviceinfo\n");
     52         printf("\t\tpci\n");
     53         printf("\t\t\tvendor_id\t%04x\n", device->deviceinfo.pci->vendor_id);
     54         printf("\t\t\tdevice_id\t%04x\n", device->deviceinfo.pci->device_id);
     55         printf("\t\t\tsubvendor_id\t%04x\n", device->deviceinfo.pci->subvendor_id);
     56         printf("\t\t\tsubdevice_id\t%04x\n", device->deviceinfo.pci->subdevice_id);
     57         printf("\t\t\trevision_id\t%02x\n", device->deviceinfo.pci->revision_id);
     58     } else {
     59         printf("Unknown/unhandled bustype\n");
     60     }
     61     printf("\n");
     62 }
     63 
     64 int
     65 main(void)
     66 {
     67     drmDevicePtr *devices;
     68     drmDevicePtr device;
     69     int fd, ret, max_devices;
     70 
     71     max_devices = drmGetDevices(NULL, 0);
     72 
     73     if (max_devices <= 0) {
     74         printf("drmGetDevices() has returned %d\n", max_devices);
     75         return -1;
     76     }
     77 
     78     devices = calloc(max_devices, sizeof(drmDevicePtr));
     79     if (devices == NULL) {
     80         printf("Failed to allocate memory for the drmDevicePtr array\n");
     81         return -1;
     82     }
     83 
     84     ret = drmGetDevices(devices, max_devices);
     85     if (ret < 0) {
     86         printf("drmGetDevices() returned an error %d\n", ret);
     87         free(devices);
     88         return -1;
     89     }
     90 
     91     for (int i = 0; i < ret; i++) {
     92         print_device_info(devices[i], i);
     93 
     94         for (int j = 0; j < DRM_NODE_MAX; j++) {
     95             if (devices[i]->available_nodes & 1 << j) {
     96                 fd = open(devices[i]->nodes[j], O_RDONLY | O_CLOEXEC, 0);
     97                 if (fd < 0)
     98                     continue;
     99 
    100                 if (drmGetDevice(fd, &device) == 0) {
    101                     print_device_info(device, -1);
    102                     drmFreeDevice(&device);
    103                 }
    104                 close(fd);
    105             }
    106         }
    107     }
    108 
    109     drmFreeDevices(devices, ret);
    110     free(devices);
    111     return 0;
    112 }
    113