2 * Copyright © 2011 Intel Corporation
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:
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
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kristian Høgsberg <krh@bitplanet.net>
26 * Benjamin Franzke <benjaminfranzke@googlemail.com>
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
35 #include "gbm_gallium_drmint.h"
36 #include "pipe_loader.h"
37 #define DRIVER_MAP_GALLIUM_ONLY
38 #include "pci_ids/pci_id_driver_map.h"
40 static struct pipe_module pipe_modules[16];
43 loader_strdup(const char *str)
45 return mem_dup(str, strlen(str) + 1);
49 drm_fd_get_screen_name(int fd)
52 struct udev_device *device, *parent;
55 int vendor_id, chip_id, i, j;
58 device = _gbm_udev_device_new_from_fd(udev, fd);
62 parent = udev_device_get_parent(device);
64 fprintf(stderr, "gbm: could not get parent device");
68 pci_id = udev_device_get_property_value(parent, "PCI_ID");
70 sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2) {
71 fprintf(stderr, "gbm: malformed or no PCI ID");
75 for (i = 0; driver_map[i].driver; i++) {
76 if (vendor_id != driver_map[i].vendor_id)
78 if (driver_map[i].num_chips_ids == -1) {
79 driver = loader_strdup(driver_map[i].driver);
80 _gbm_log("pci id for %d: %04x:%04x, driver %s",
81 fd, vendor_id, chip_id, driver);
85 for (j = 0; j < driver_map[i].num_chips_ids; j++)
86 if (driver_map[i].chip_ids[j] == chip_id) {
87 driver = loader_strdup(driver_map[i].driver);
88 _gbm_log("pci id for %d: %04x:%04x, driver %s",
89 fd, vendor_id, chip_id, driver);
95 udev_device_unref(device);
102 find_pipe_module(struct pipe_module *pmod, const char *name)
104 char *search_paths, *end, *next, *p;
109 if (geteuid() == getuid()) {
110 /* don't allow setuid apps to use GBM_BACKENDS_PATH */
111 search_paths = getenv("GBM_BACKENDS_PATH");
113 if (search_paths == NULL)
114 search_paths = GBM_BACKEND_SEARCH_DIR;
116 end = search_paths + strlen(search_paths);
117 for (p = search_paths; p < end && pmod->lib == NULL; p = next + 1) {
119 next = strchr(p, ':');
126 ret = util_snprintf(path, sizeof(path),
127 "%.*s/" PIPE_PREFIX "%s" UTIL_DL_EXT, len, p, pmod->name);
130 ret = util_snprintf(path, sizeof(path),
131 PIPE_PREFIX "%s" UTIL_DL_EXT, pmod->name);
133 if (ret > 0 && ret < sizeof(path)) {
134 pmod->lib = util_dl_open(path);
135 debug_printf("loaded %s\n", path);
142 load_pipe_module(struct pipe_module *pmod, const char *name)
144 pmod->name = loader_strdup(name);
148 find_pipe_module(pmod, name);
151 pmod->drmdd = (const struct drm_driver_descriptor *)
152 util_dl_get_proc_address(pmod->lib, "driver_descriptor");
154 /* sanity check on the name */
155 if (pmod->drmdd && strcmp(pmod->drmdd->name, pmod->name) != 0)
159 util_dl_close(pmod->lib);
164 return (pmod->drmdd != NULL);
168 get_pipe_module(const char *name)
170 struct pipe_module *pmod = NULL;
176 for (i = 0; i < Elements(pipe_modules); i++) {
177 if (!pipe_modules[i].initialized ||
178 strcmp(pipe_modules[i].name, name) == 0) {
179 pmod = &pipe_modules[i];
186 if (!pmod->initialized) {
187 load_pipe_module(pmod, name);
188 pmod->initialized = TRUE;