mesa: Use AC_HEADER_MAJOR to include correct header for major().
[mesa.git] / src / loader / loader.c
1 /*
2 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
3 *
4 * This code is derived from the following files.
5 *
6 * * src/glx/dri3_common.c
7 * Copyright © 2013 Keith Packard
8 *
9 * * src/egl/drivers/dri2/common.c
10 * * src/gbm/backends/dri/driver_name.c
11 * Copyright © 2011 Intel Corporation
12 *
13 * Authors:
14 * Kristian Høgsberg <krh@bitplanet.net>
15 * Benjamin Franzke <benjaminfranzke@googlemail.com>
16 *
17 * * src/gallium/targets/egl-static/egl.c
18 * Copyright (C) 2010-2011 LunarG Inc.
19 *
20 * Authors:
21 * Chia-I Wu <olv@lunarg.com>
22 *
23 * * src/gallium/state_trackers/egl/drm/native_drm.c
24 * Copyright (C) 2010 Chia-I Wu <olv@0xlab.org>
25 *
26 * * src/egl/drivers/dri2/platform_android.c
27 *
28 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
29 * Copyright (C) 2010-2011 LunarG Inc.
30 *
31 * Based on platform_x11, which has
32 *
33 * Copyright © 2011 Intel Corporation
34 *
35 * * src/gallium/auxiliary/pipe-loader/pipe_loader_drm.c
36 * Copyright 2011 Intel Corporation
37 * Copyright 2012 Francisco Jerez
38 * All Rights Reserved.
39 *
40 * Authors:
41 * Kristian Høgsberg <krh@bitplanet.net>
42 * Benjamin Franzke <benjaminfranzke@googlemail.com>
43 *
44 * Permission is hereby granted, free of charge, to any person obtaining a
45 * copy of this software and associated documentation files (the "Software"),
46 * to deal in the Software without restriction, including without limitation
47 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
48 * and/or sell copies of the Software, and to permit persons to whom the
49 * Software is furnished to do so, subject to the following conditions:
50 *
51 * The above copyright notice and this permission notice (including the next
52 * paragraph) shall be included in all copies or substantial portions of the
53 * Software.
54 *
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
58 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
61 * SOFTWARE.
62 *
63 * Authors:
64 * Rob Clark <robclark@freedesktop.org>
65 */
66
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <sys/stat.h>
70 #include <stdarg.h>
71 #include <stdio.h>
72 #include <string.h>
73 #ifdef HAVE_LIBUDEV
74 #include <assert.h>
75 #include <dlfcn.h>
76 #include <unistd.h>
77 #include <stdlib.h>
78 #ifdef USE_DRICONF
79 #include "xmlconfig.h"
80 #include "xmlpool.h"
81 #endif
82 #endif
83 #ifdef MAJOR_IN_MKDEV
84 #include <sys/mkdev.h>
85 #endif
86 #ifdef MAJOR_IN_SYSMACROS
87 #include <sys/sysmacros.h>
88 #endif
89 #include "loader.h"
90
91 #ifdef HAVE_LIBDRM
92 #include <xf86drm.h>
93 #endif
94
95 #define __IS_LOADER
96 #include "pci_id_driver_map.h"
97
98 static void default_logger(int level, const char *fmt, ...)
99 {
100 if (level <= _LOADER_WARNING) {
101 va_list args;
102 va_start(args, fmt);
103 vfprintf(stderr, fmt, args);
104 va_end(args);
105 }
106 }
107
108 static void (*log_)(int level, const char *fmt, ...) = default_logger;
109
110 int
111 loader_open_device(const char *device_name)
112 {
113 int fd;
114 #ifdef O_CLOEXEC
115 fd = open(device_name, O_RDWR | O_CLOEXEC);
116 if (fd == -1 && errno == EINVAL)
117 #endif
118 {
119 fd = open(device_name, O_RDWR);
120 if (fd != -1)
121 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
122 }
123 return fd;
124 }
125
126 #ifdef HAVE_LIBUDEV
127 #include <libudev.h>
128
129 static void *udev_handle = NULL;
130
131 static void *
132 udev_dlopen_handle(void)
133 {
134 char name[80];
135 unsigned flags = RTLD_NOLOAD | RTLD_LOCAL | RTLD_LAZY;
136 int version;
137
138 /* libudev.so.1 changed the return types of the two unref functions
139 * from voids to pointers. We don't use those return values, and the
140 * only ABI I've heard that cares about this kind of change (calling
141 * a function with a void * return that actually only returns void)
142 * might be ia64.
143 */
144
145 /* First try opening an already linked libudev, then try loading one */
146 do {
147 for (version = 1; version >= 0; version--) {
148 snprintf(name, sizeof(name), "libudev.so.%d", version);
149 udev_handle = dlopen(name, flags);
150 if (udev_handle)
151 return udev_handle;
152 }
153
154 if ((flags & RTLD_NOLOAD) == 0)
155 break;
156
157 flags &= ~RTLD_NOLOAD;
158 } while (1);
159
160 log_(_LOADER_WARNING,
161 "Couldn't dlopen libudev.so.1 or "
162 "libudev.so.0, driver detection may be broken.\n");
163 return NULL;
164 }
165
166 static int dlsym_failed = 0;
167
168 static void *
169 checked_dlsym(void *dlopen_handle, const char *name)
170 {
171 void *result = dlsym(dlopen_handle, name);
172 if (!result)
173 dlsym_failed = 1;
174 return result;
175 }
176
177 #define UDEV_SYMBOL(ret, name, args) \
178 ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
179
180
181 static inline struct udev_device *
182 udev_device_new_from_fd(struct udev *udev, int fd)
183 {
184 struct udev_device *device;
185 struct stat buf;
186 UDEV_SYMBOL(struct udev_device *, udev_device_new_from_devnum,
187 (struct udev *udev, char type, dev_t devnum));
188
189 if (dlsym_failed)
190 return NULL;
191
192 if (fstat(fd, &buf) < 0) {
193 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
194 return NULL;
195 }
196
197 device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
198 if (device == NULL) {
199 log_(_LOADER_WARNING,
200 "MESA-LOADER: could not create udev device for fd %d\n", fd);
201 return NULL;
202 }
203
204 return device;
205 }
206
207 static int
208 libudev_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
209 {
210 struct udev *udev = NULL;
211 struct udev_device *device = NULL, *parent;
212 const char *pci_id;
213 UDEV_SYMBOL(struct udev *, udev_new, (void));
214 UDEV_SYMBOL(struct udev_device *, udev_device_get_parent,
215 (struct udev_device *));
216 UDEV_SYMBOL(const char *, udev_device_get_property_value,
217 (struct udev_device *, const char *));
218 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
219 (struct udev_device *));
220 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
221
222 *chip_id = -1;
223
224 if (dlsym_failed)
225 return 0;
226
227 udev = udev_new();
228 device = udev_device_new_from_fd(udev, fd);
229 if (!device)
230 goto out;
231
232 parent = udev_device_get_parent(device);
233 if (parent == NULL) {
234 log_(_LOADER_WARNING, "MESA-LOADER: could not get parent device\n");
235 goto out;
236 }
237
238 pci_id = udev_device_get_property_value(parent, "PCI_ID");
239 if (pci_id == NULL) {
240 log_(_LOADER_INFO, "MESA-LOADER: no PCI ID\n");
241 *chip_id = -1;
242 goto out;
243 } else if (sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2) {
244 log_(_LOADER_WARNING, "MESA-LOADER: malformed PCI ID\n");
245 *chip_id = -1;
246 goto out;
247 }
248
249 out:
250 if (device)
251 udev_device_unref(device);
252 if (udev)
253 udev_unref(udev);
254
255 return (*chip_id >= 0);
256 }
257
258 static char *
259 get_render_node_from_id_path_tag(struct udev *udev,
260 char *id_path_tag,
261 char another_tag)
262 {
263 struct udev_device *device;
264 struct udev_enumerate *e;
265 struct udev_list_entry *entry;
266 const char *path, *id_path_tag_tmp;
267 char *path_res;
268 char found = 0;
269 UDEV_SYMBOL(struct udev_enumerate *, udev_enumerate_new,
270 (struct udev *));
271 UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem,
272 (struct udev_enumerate *, const char *));
273 UDEV_SYMBOL(int, udev_enumerate_add_match_sysname,
274 (struct udev_enumerate *, const char *));
275 UDEV_SYMBOL(int, udev_enumerate_scan_devices,
276 (struct udev_enumerate *));
277 UDEV_SYMBOL(struct udev_list_entry *, udev_enumerate_get_list_entry,
278 (struct udev_enumerate *));
279 UDEV_SYMBOL(void, udev_enumerate_unref,
280 (struct udev_enumerate *));
281 UDEV_SYMBOL(struct udev_list_entry *, udev_list_entry_get_next,
282 (struct udev_list_entry *));
283 UDEV_SYMBOL(const char *, udev_list_entry_get_name,
284 (struct udev_list_entry *));
285 UDEV_SYMBOL(struct udev_device *, udev_device_new_from_syspath,
286 (struct udev *, const char *));
287 UDEV_SYMBOL(const char *, udev_device_get_property_value,
288 (struct udev_device *, const char *));
289 UDEV_SYMBOL(const char *, udev_device_get_devnode,
290 (struct udev_device *));
291 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
292 (struct udev_device *));
293
294 e = udev_enumerate_new(udev);
295 udev_enumerate_add_match_subsystem(e, "drm");
296 udev_enumerate_add_match_sysname(e, "render*");
297
298 udev_enumerate_scan_devices(e);
299 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
300 path = udev_list_entry_get_name(entry);
301 device = udev_device_new_from_syspath(udev, path);
302 if (!device)
303 continue;
304 id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
305 if (id_path_tag_tmp) {
306 if ((!another_tag && !strcmp(id_path_tag, id_path_tag_tmp)) ||
307 (another_tag && strcmp(id_path_tag, id_path_tag_tmp))) {
308 found = 1;
309 break;
310 }
311 }
312 udev_device_unref(device);
313 }
314
315 udev_enumerate_unref(e);
316
317 if (found) {
318 path_res = strdup(udev_device_get_devnode(device));
319 udev_device_unref(device);
320 return path_res;
321 }
322 return NULL;
323 }
324
325 static char *
326 get_id_path_tag_from_fd(struct udev *udev, int fd)
327 {
328 struct udev_device *device;
329 const char *id_path_tag_tmp;
330 char *id_path_tag;
331 UDEV_SYMBOL(const char *, udev_device_get_property_value,
332 (struct udev_device *, const char *));
333 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
334 (struct udev_device *));
335
336 device = udev_device_new_from_fd(udev, fd);
337 if (!device)
338 return NULL;
339
340 id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
341 if (!id_path_tag_tmp)
342 return NULL;
343
344 id_path_tag = strdup(id_path_tag_tmp);
345
346 udev_device_unref(device);
347 return id_path_tag;
348 }
349
350 #ifdef USE_DRICONF
351 const char __driConfigOptionsLoader[] =
352 DRI_CONF_BEGIN
353 DRI_CONF_SECTION_INITIALIZATION
354 DRI_CONF_DEVICE_ID_PATH_TAG()
355 DRI_CONF_SECTION_END
356 DRI_CONF_END;
357 #endif
358
359 int loader_get_user_preferred_fd(int default_fd, int *different_device)
360 {
361 struct udev *udev;
362 #ifdef USE_DRICONF
363 driOptionCache defaultInitOptions;
364 driOptionCache userInitOptions;
365 #endif
366 const char *dri_prime = getenv("DRI_PRIME");
367 char *prime = NULL;
368 int is_different_device = 0, fd = default_fd;
369 char *default_device_id_path_tag;
370 char *device_name = NULL;
371 char another_tag = 0;
372 UDEV_SYMBOL(struct udev *, udev_new, (void));
373 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
374
375 if (dri_prime)
376 prime = strdup(dri_prime);
377 #ifdef USE_DRICONF
378 else {
379 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
380 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "loader");
381 if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
382 prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
383 driDestroyOptionCache(&userInitOptions);
384 driDestroyOptionInfo(&defaultInitOptions);
385 }
386 #endif
387
388 if (prime == NULL) {
389 *different_device = 0;
390 return default_fd;
391 }
392
393 udev = udev_new();
394 if (!udev)
395 goto prime_clean;
396
397 default_device_id_path_tag = get_id_path_tag_from_fd(udev, default_fd);
398 if (!default_device_id_path_tag)
399 goto udev_clean;
400
401 is_different_device = 1;
402 /* two format are supported:
403 * "1": choose any other card than the card used by default.
404 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
405 * with this id_path_tag.
406 */
407 if (!strcmp(prime,"1")) {
408 free(prime);
409 prime = strdup(default_device_id_path_tag);
410 /* request a card with a different card than the default card */
411 another_tag = 1;
412 } else if (!strcmp(default_device_id_path_tag, prime))
413 /* we are to get a new fd (render-node) of the same device */
414 is_different_device = 0;
415
416 device_name = get_render_node_from_id_path_tag(udev,
417 prime,
418 another_tag);
419 if (device_name == NULL) {
420 is_different_device = 0;
421 goto default_device_clean;
422 }
423
424 fd = loader_open_device(device_name);
425 if (fd >= 0) {
426 close(default_fd);
427 } else {
428 fd = default_fd;
429 is_different_device = 0;
430 }
431 free(device_name);
432
433 default_device_clean:
434 free(default_device_id_path_tag);
435 udev_clean:
436 udev_unref(udev);
437 prime_clean:
438 free(prime);
439
440 *different_device = is_different_device;
441 return fd;
442 }
443 #else
444 int loader_get_user_preferred_fd(int default_fd, int *different_device)
445 {
446 *different_device = 0;
447 return default_fd;
448 }
449 #endif
450
451 #if defined(HAVE_SYSFS) || defined(HAVE_LIBDRM)
452 static int
453 dev_node_from_fd(int fd, unsigned int *maj, unsigned int *min)
454 {
455 struct stat buf;
456
457 if (fstat(fd, &buf) < 0) {
458 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
459 return -1;
460 }
461
462 if (!S_ISCHR(buf.st_mode)) {
463 log_(_LOADER_WARNING, "MESA-LOADER: fd %d not a character device\n", fd);
464 return -1;
465 }
466
467 *maj = major(buf.st_rdev);
468 *min = minor(buf.st_rdev);
469
470 return 0;
471 }
472 #endif
473
474 #if defined(HAVE_SYSFS)
475 static int
476 sysfs_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
477 {
478 unsigned int maj, min;
479 FILE *f;
480 char buf[0x40];
481
482 if (dev_node_from_fd(fd, &maj, &min) < 0) {
483 *chip_id = -1;
484 return 0;
485 }
486
487 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/vendor", maj, min);
488 if (!(f = fopen(buf, "r"))) {
489 *chip_id = -1;
490 return 0;
491 }
492 if (fscanf(f, "%x", vendor_id) != 1) {
493 *chip_id = -1;
494 fclose(f);
495 return 0;
496 }
497 fclose(f);
498 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/device", maj, min);
499 if (!(f = fopen(buf, "r"))) {
500 *chip_id = -1;
501 return 0;
502 }
503 if (fscanf(f, "%x", chip_id) != 1) {
504 *chip_id = -1;
505 fclose(f);
506 return 0;
507 }
508 fclose(f);
509 return 1;
510 }
511 #endif
512
513 #if defined(HAVE_LIBDRM)
514 /* for i915 */
515 #include <i915_drm.h>
516 /* for radeon */
517 #include <radeon_drm.h>
518
519 static int
520 drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
521 {
522 drmVersionPtr version;
523
524 *chip_id = -1;
525
526 version = drmGetVersion(fd);
527 if (!version) {
528 log_(_LOADER_WARNING, "MESA-LOADER: invalid drm fd\n");
529 return 0;
530 }
531 if (!version->name) {
532 log_(_LOADER_WARNING, "MESA-LOADER: unable to determine the driver name\n");
533 drmFreeVersion(version);
534 return 0;
535 }
536
537 if (strcmp(version->name, "i915") == 0) {
538 struct drm_i915_getparam gp;
539 int ret;
540
541 *vendor_id = 0x8086;
542
543 memset(&gp, 0, sizeof(gp));
544 gp.param = I915_PARAM_CHIPSET_ID;
545 gp.value = chip_id;
546 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
547 if (ret) {
548 log_(_LOADER_WARNING, "MESA-LOADER: failed to get param for i915\n");
549 *chip_id = -1;
550 }
551 }
552 else if (strcmp(version->name, "radeon") == 0) {
553 struct drm_radeon_info info;
554 int ret;
555
556 *vendor_id = 0x1002;
557
558 memset(&info, 0, sizeof(info));
559 info.request = RADEON_INFO_DEVICE_ID;
560 info.value = (unsigned long) chip_id;
561 ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
562 if (ret) {
563 log_(_LOADER_WARNING, "MESA-LOADER: failed to get info for radeon\n");
564 *chip_id = -1;
565 }
566 }
567 else if (strcmp(version->name, "nouveau") == 0) {
568 *vendor_id = 0x10de;
569 /* not used */
570 *chip_id = 0;
571 }
572 else if (strcmp(version->name, "vmwgfx") == 0) {
573 *vendor_id = 0x15ad;
574 /* assume SVGA II */
575 *chip_id = 0x0405;
576 }
577
578 drmFreeVersion(version);
579
580 return (*chip_id >= 0);
581 }
582 #endif
583
584
585 int
586 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
587 {
588 #if HAVE_LIBUDEV
589 if (libudev_get_pci_id_for_fd(fd, vendor_id, chip_id))
590 return 1;
591 #endif
592 #if HAVE_SYSFS
593 if (sysfs_get_pci_id_for_fd(fd, vendor_id, chip_id))
594 return 1;
595 #endif
596 #if HAVE_LIBDRM
597 if (drm_get_pci_id_for_fd(fd, vendor_id, chip_id))
598 return 1;
599 #endif
600 return 0;
601 }
602
603
604 #ifdef HAVE_LIBUDEV
605 static char *
606 libudev_get_device_name_for_fd(int fd)
607 {
608 char *device_name = NULL;
609 struct udev *udev;
610 struct udev_device *device;
611 const char *const_device_name;
612 UDEV_SYMBOL(struct udev *, udev_new, (void));
613 UDEV_SYMBOL(const char *, udev_device_get_devnode,
614 (struct udev_device *));
615 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
616 (struct udev_device *));
617 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
618
619 if (dlsym_failed)
620 return NULL;
621
622 udev = udev_new();
623 device = udev_device_new_from_fd(udev, fd);
624 if (device == NULL)
625 return NULL;
626
627 const_device_name = udev_device_get_devnode(device);
628 if (!const_device_name)
629 goto out;
630 device_name = strdup(const_device_name);
631
632 out:
633 udev_device_unref(device);
634 udev_unref(udev);
635 return device_name;
636 }
637 #endif
638
639
640 #if HAVE_SYSFS
641 static char *
642 sysfs_get_device_name_for_fd(int fd)
643 {
644 char *device_name = NULL;
645 unsigned int maj, min;
646 FILE *f;
647 char buf[0x40];
648 static const char match[9] = "\0DEVNAME=";
649 int expected = 1;
650
651 if (dev_node_from_fd(fd, &maj, &min) < 0)
652 return NULL;
653
654 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
655 if (!(f = fopen(buf, "r")))
656 return NULL;
657
658 while (expected < sizeof(match)) {
659 int c = getc(f);
660
661 if (c == EOF) {
662 fclose(f);
663 return NULL;
664 } else if (c == match[expected] )
665 expected++;
666 else
667 expected = 0;
668 }
669
670 strcpy(buf, "/dev/");
671 if (fgets(buf + 5, sizeof(buf) - 5, f))
672 device_name = strdup(buf);
673
674 fclose(f);
675 return device_name;
676 }
677 #endif
678
679 #if defined(HAVE_LIBDRM)
680 static char *
681 drm_get_device_name_for_fd(int fd)
682 {
683 unsigned int maj, min;
684 char buf[0x40];
685 int n;
686
687 if (dev_node_from_fd(fd, &maj, &min) < 0)
688 return NULL;
689
690 n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, min);
691 if (n == -1 || n >= sizeof(buf))
692 return NULL;
693
694 return strdup(buf);
695 }
696 #endif
697
698 char *
699 loader_get_device_name_for_fd(int fd)
700 {
701 char *result = NULL;
702
703 #if HAVE_LIBUDEV
704 if ((result = libudev_get_device_name_for_fd(fd)))
705 return result;
706 #endif
707 #if HAVE_SYSFS
708 if ((result = sysfs_get_device_name_for_fd(fd)))
709 return result;
710 #endif
711 #if HAVE_LIBDRM
712 if ((result = drm_get_device_name_for_fd(fd)))
713 return result;
714 #endif
715 return result;
716 }
717
718 char *
719 loader_get_driver_for_fd(int fd, unsigned driver_types)
720 {
721 int vendor_id, chip_id, i, j;
722 char *driver = NULL;
723
724 if (!driver_types)
725 driver_types = _LOADER_GALLIUM | _LOADER_DRI;
726
727 if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
728
729 #if HAVE_LIBDRM
730 /* fallback to drmGetVersion(): */
731 drmVersionPtr version = drmGetVersion(fd);
732
733 if (!version) {
734 log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
735 return NULL;
736 }
737
738 driver = strndup(version->name, version->name_len);
739 log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
740
741 drmFreeVersion(version);
742 #endif
743
744 return driver;
745 }
746
747 for (i = 0; driver_map[i].driver; i++) {
748 if (vendor_id != driver_map[i].vendor_id)
749 continue;
750
751 if (!(driver_types & driver_map[i].driver_types))
752 continue;
753
754 if (driver_map[i].predicate && !driver_map[i].predicate(fd))
755 continue;
756
757 if (driver_map[i].num_chips_ids == -1) {
758 driver = strdup(driver_map[i].driver);
759 goto out;
760 }
761
762 for (j = 0; j < driver_map[i].num_chips_ids; j++)
763 if (driver_map[i].chip_ids[j] == chip_id) {
764 driver = strdup(driver_map[i].driver);
765 goto out;
766 }
767 }
768
769 out:
770 log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
771 "pci id for fd %d: %04x:%04x, driver %s\n",
772 fd, vendor_id, chip_id, driver);
773 return driver;
774 }
775
776 void
777 loader_set_logger(void (*logger)(int level, const char *fmt, ...))
778 {
779 log_ = logger;
780 }