1/* 2 * Copyright © 2019 Red Hat 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 (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24/* connect to an X server and work out the default device. */ 25 26#include <xcb/xcb.h> 27#include <xcb/dri3.h> 28#include <unistd.h> 29#include <stdlib.h> 30#include <fcntl.h> 31#include <xf86drm.h> 32 33#include "device_select.h" 34static int 35ds_dri3_open(xcb_connection_t *conn, 36 xcb_window_t root, 37 uint32_t provider) 38{ 39 xcb_dri3_open_cookie_t cookie; 40 xcb_dri3_open_reply_t *reply; 41 int fd; 42 43 cookie = xcb_dri3_open(conn, 44 root, 45 provider); 46 47 reply = xcb_dri3_open_reply(conn, cookie, NULL); 48 if (!reply) 49 return -1; 50 51 if (reply->nfd != 1) { 52 free(reply); 53 return -1; 54 } 55 56 fd = xcb_dri3_open_reply_fds(conn, reply)[0]; 57 free(reply); 58 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); 59 60 return fd; 61} 62 63int device_select_find_xcb_pci_default(struct device_pci_info *devices, uint32_t device_count) 64{ 65 const xcb_setup_t *setup; 66 xcb_screen_iterator_t iter; 67 int scrn; 68 xcb_connection_t *conn; 69 int default_idx = -1; 70 drmDevicePtr xdev = NULL; 71 72 conn = xcb_connect(NULL, &scrn); 73 if (!conn) 74 return -1; 75 76 xcb_query_extension_cookie_t dri3_cookie; 77 xcb_query_extension_reply_t *dri3_reply = NULL; 78 79 dri3_cookie = xcb_query_extension(conn, 4, "DRI3"); 80 dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL); 81 82 if (!dri3_reply) 83 goto out; 84 85 if (dri3_reply->present == 0) 86 goto out; 87 88 setup = xcb_get_setup(conn); 89 iter = xcb_setup_roots_iterator(setup); 90 91 xcb_screen_t *screen = iter.data; 92 93 int dri3_fd = ds_dri3_open(conn, screen->root, 0); 94 if (dri3_fd == -1) 95 goto out; 96 97 int ret = drmGetDevice2(dri3_fd, 0, &xdev); 98 close(dri3_fd); 99 if (ret < 0) 100 goto out; 101 102 for (unsigned i = 0; i < device_count; i++) { 103 if (devices[i].has_bus_info) { 104 if (xdev->businfo.pci->domain == devices[i].bus_info.domain && 105 xdev->businfo.pci->bus == devices[i].bus_info.bus && 106 xdev->businfo.pci->dev == devices[i].bus_info.dev && 107 xdev->businfo.pci->func == devices[i].bus_info.func) { 108 default_idx = i; 109 } 110 } else { 111 if (xdev->deviceinfo.pci->vendor_id == devices[i].dev_info.vendor_id && 112 xdev->deviceinfo.pci->device_id == devices[i].dev_info.device_id) 113 default_idx = i; 114 } 115 if (default_idx != -1) 116 break; 117 } 118 119out: 120 free(dri3_reply); 121 drmFreeDevice(&xdev); /* Is NULL pointer safe. */ 122 xcb_disconnect(conn); 123 return default_idx; 124} 125