OSDN Git Service

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