Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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 <sys/stat.h>
  68. #include <stdarg.h>
  69. #include <stdio.h>
  70. #include <string.h>
  71. #ifdef HAVE_LIBUDEV
  72. #include <assert.h>
  73. #include <dlfcn.h>
  74. #include <fcntl.h>
  75. #include <unistd.h>
  76. #include <stdlib.h>
  77. #include <errno.h>
  78. #ifdef USE_DRICONF
  79. #include "xmlconfig.h"
  80. #include "xmlpool.h"
  81. #endif
  82. #endif
  83. #ifdef HAVE_SYSFS
  84. #include <sys/types.h>
  85. #endif
  86. #include "loader.h"
  87.  
  88. #ifndef __NOT_HAVE_DRM_H
  89. #include <xf86drm.h>
  90. #endif
  91.  
  92. #define __IS_LOADER
  93. #include "pci_id_driver_map.h"
  94.  
  95. static void default_logger(int level, const char *fmt, ...)
  96. {
  97.    if (level <= _LOADER_WARNING) {
  98.       va_list args;
  99.       va_start(args, fmt);
  100.       vfprintf(stderr, fmt, args);
  101.       va_end(args);
  102.    }
  103. }
  104.  
  105. static void (*log_)(int level, const char *fmt, ...) = default_logger;
  106.  
  107. #ifdef HAVE_LIBUDEV
  108. #include <libudev.h>
  109.  
  110. static void *udev_handle = NULL;
  111.  
  112. static void *
  113. udev_dlopen_handle(void)
  114. {
  115.    if (!udev_handle) {
  116.       udev_handle = dlopen("libudev.so.1", RTLD_LOCAL | RTLD_LAZY);
  117.  
  118.       if (!udev_handle) {
  119.          /* libudev.so.1 changed the return types of the two unref functions
  120.           * from voids to pointers.  We don't use those return values, and the
  121.           * only ABI I've heard that cares about this kind of change (calling
  122.           * a function with a void * return that actually only returns void)
  123.           * might be ia64.
  124.           */
  125.          udev_handle = dlopen("libudev.so.0", RTLD_LOCAL | RTLD_LAZY);
  126.  
  127.          if (!udev_handle) {
  128.             log_(_LOADER_WARNING, "Couldn't dlopen libudev.so.1 or "
  129.                  "libudev.so.0, driver detection may be broken.\n");
  130.          }
  131.       }
  132.    }
  133.  
  134.    return udev_handle;
  135. }
  136.  
  137. static int dlsym_failed = 0;
  138.  
  139. static void *
  140. checked_dlsym(void *dlopen_handle, const char *name)
  141. {
  142.    void *result = dlsym(dlopen_handle, name);
  143.    if (!result)
  144.       dlsym_failed = 1;
  145.    return result;
  146. }
  147.  
  148. #define UDEV_SYMBOL(ret, name, args) \
  149.    ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
  150.  
  151.  
  152. static inline struct udev_device *
  153. udev_device_new_from_fd(struct udev *udev, int fd)
  154. {
  155.    struct udev_device *device;
  156.    struct stat buf;
  157.    UDEV_SYMBOL(struct udev_device *, udev_device_new_from_devnum,
  158.                (struct udev *udev, char type, dev_t devnum));
  159.  
  160.    if (dlsym_failed)
  161.       return NULL;
  162.  
  163.    if (fstat(fd, &buf) < 0) {
  164.       log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
  165.       return NULL;
  166.    }
  167.  
  168.    device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
  169.    if (device == NULL) {
  170.       log_(_LOADER_WARNING,
  171.               "MESA-LOADER: could not create udev device for fd %d\n", fd);
  172.       return NULL;
  173.    }
  174.  
  175.    return device;
  176. }
  177.  
  178. static int
  179. libudev_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
  180. {
  181.    struct udev *udev = NULL;
  182.    struct udev_device *device = NULL, *parent;
  183.    const char *pci_id;
  184.    UDEV_SYMBOL(struct udev *, udev_new, (void));
  185.    UDEV_SYMBOL(struct udev_device *, udev_device_get_parent,
  186.                (struct udev_device *));
  187.    UDEV_SYMBOL(const char *, udev_device_get_property_value,
  188.                (struct udev_device *, const char *));
  189.    UDEV_SYMBOL(struct udev_device *, udev_device_unref,
  190.                (struct udev_device *));
  191.    UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
  192.  
  193.    *chip_id = -1;
  194.  
  195.    if (dlsym_failed)
  196.       return 0;
  197.  
  198.    udev = udev_new();
  199.    device = udev_device_new_from_fd(udev, fd);
  200.    if (!device)
  201.       goto out;
  202.  
  203.    parent = udev_device_get_parent(device);
  204.    if (parent == NULL) {
  205.       log_(_LOADER_WARNING, "MESA-LOADER: could not get parent device\n");
  206.       goto out;
  207.    }
  208.  
  209.    pci_id = udev_device_get_property_value(parent, "PCI_ID");
  210.    if (pci_id == NULL) {
  211.       log_(_LOADER_INFO, "MESA-LOADER: no PCI ID\n");
  212.       *chip_id = -1;
  213.       goto out;
  214.    } else if (sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2) {
  215.       log_(_LOADER_WARNING, "MESA-LOADER: malformed PCI ID\n");
  216.       *chip_id = -1;
  217.       goto out;
  218.    }
  219.  
  220. out:
  221.    if (device)
  222.       udev_device_unref(device);
  223.    if (udev)
  224.       udev_unref(udev);
  225.  
  226.    return (*chip_id >= 0);
  227. }
  228.  
  229. static char *
  230. get_render_node_from_id_path_tag(struct udev *udev,
  231.                                  char *id_path_tag,
  232.                                  char another_tag)
  233. {
  234.    struct udev_device *device;
  235.    struct udev_enumerate *e;
  236.    struct udev_list_entry *entry;
  237.    const char *path, *id_path_tag_tmp;
  238.    char *path_res;
  239.    char found = 0;
  240.    UDEV_SYMBOL(struct udev_enumerate *, udev_enumerate_new,
  241.                (struct udev *));
  242.    UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem,
  243.                (struct udev_enumerate *, const char *));
  244.    UDEV_SYMBOL(int, udev_enumerate_add_match_sysname,
  245.                (struct udev_enumerate *, const char *));
  246.    UDEV_SYMBOL(int, udev_enumerate_scan_devices,
  247.                (struct udev_enumerate *));
  248.    UDEV_SYMBOL(struct udev_list_entry *, udev_enumerate_get_list_entry,
  249.                (struct udev_enumerate *));
  250.    UDEV_SYMBOL(struct udev_list_entry *, udev_list_entry_get_next,
  251.                (struct udev_list_entry *));
  252.    UDEV_SYMBOL(const char *, udev_list_entry_get_name,
  253.                (struct udev_list_entry *));
  254.    UDEV_SYMBOL(struct udev_device *, udev_device_new_from_syspath,
  255.                (struct udev *, const char *));
  256.    UDEV_SYMBOL(const char *, udev_device_get_property_value,
  257.                (struct udev_device *, const char *));
  258.    UDEV_SYMBOL(const char *, udev_device_get_devnode,
  259.                (struct udev_device *));
  260.    UDEV_SYMBOL(struct udev_device *, udev_device_unref,
  261.                (struct udev_device *));
  262.  
  263.    e = udev_enumerate_new(udev);
  264.    udev_enumerate_add_match_subsystem(e, "drm");
  265.    udev_enumerate_add_match_sysname(e, "render*");
  266.  
  267.    udev_enumerate_scan_devices(e);
  268.    udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
  269.       path = udev_list_entry_get_name(entry);
  270.       device = udev_device_new_from_syspath(udev, path);
  271.       if (!device)
  272.          continue;
  273.       id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
  274.       if (id_path_tag_tmp) {
  275.          if ((!another_tag && !strcmp(id_path_tag, id_path_tag_tmp)) ||
  276.              (another_tag && strcmp(id_path_tag, id_path_tag_tmp))) {
  277.             found = 1;
  278.             break;
  279.          }
  280.       }
  281.       udev_device_unref(device);
  282.    }
  283.  
  284.    if (found) {
  285.       path_res = strdup(udev_device_get_devnode(device));
  286.       udev_device_unref(device);
  287.       return path_res;
  288.    }
  289.    return NULL;
  290. }
  291.  
  292. static char *
  293. get_id_path_tag_from_fd(struct udev *udev, int fd)
  294. {
  295.    struct udev_device *device;
  296.    const char *id_path_tag_tmp;
  297.    char *id_path_tag;
  298.    UDEV_SYMBOL(const char *, udev_device_get_property_value,
  299.                (struct udev_device *, const char *));
  300.    UDEV_SYMBOL(struct udev_device *, udev_device_unref,
  301.                (struct udev_device *));
  302.  
  303.    device = udev_device_new_from_fd(udev, fd);
  304.    if (!device)
  305.       return NULL;
  306.  
  307.    id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
  308.    if (!id_path_tag_tmp)
  309.       return NULL;
  310.  
  311.    id_path_tag = strdup(id_path_tag_tmp);
  312.  
  313.    udev_device_unref(device);
  314.    return id_path_tag;
  315. }
  316.  
  317. static int
  318. drm_open_device(const char *device_name)
  319. {
  320.    int fd;
  321. #ifdef O_CLOEXEC
  322.    fd = open(device_name, O_RDWR | O_CLOEXEC);
  323.    if (fd == -1 && errno == EINVAL)
  324. #endif
  325.    {
  326.       fd = open(device_name, O_RDWR);
  327.       if (fd != -1)
  328.          fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
  329.    }
  330.    return fd;
  331. }
  332.  
  333. #ifdef USE_DRICONF
  334. const char __driConfigOptionsLoader[] =
  335. DRI_CONF_BEGIN
  336.     DRI_CONF_SECTION_INITIALIZATION
  337.         DRI_CONF_DEVICE_ID_PATH_TAG()
  338.     DRI_CONF_SECTION_END
  339. DRI_CONF_END;
  340. #endif
  341.  
  342. int loader_get_user_preferred_fd(int default_fd, int *different_device)
  343. {
  344.    struct udev *udev;
  345. #ifdef USE_DRICONF
  346.    driOptionCache defaultInitOptions;
  347.    driOptionCache userInitOptions;
  348. #endif
  349.    const char *dri_prime = getenv("DRI_PRIME");
  350.    char *prime = NULL;
  351.    int is_different_device = 0, fd = default_fd;
  352.    char *default_device_id_path_tag;
  353.    char *device_name = NULL;
  354.    char another_tag = 0;
  355.    UDEV_SYMBOL(struct udev *, udev_new, (void));
  356.    UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
  357.  
  358.    if (dri_prime)
  359.       prime = strdup(dri_prime);
  360. #ifdef USE_DRICONF
  361.    else {
  362.       driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
  363.       driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "loader");
  364.       if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
  365.          prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
  366.       driDestroyOptionCache(&userInitOptions);
  367.       driDestroyOptionInfo(&defaultInitOptions);
  368.    }
  369. #endif
  370.  
  371.    if (prime == NULL) {
  372.       *different_device = 0;
  373.       return default_fd;
  374.    }
  375.  
  376.    udev = udev_new();
  377.    if (!udev)
  378.       goto prime_clean;
  379.  
  380.    default_device_id_path_tag = get_id_path_tag_from_fd(udev, default_fd);
  381.    if (!default_device_id_path_tag)
  382.       goto udev_clean;
  383.  
  384.    is_different_device = 1;
  385.    /* two format are supported:
  386.     * "1": choose any other card than the card used by default.
  387.     * id_path_tag: (for example "pci-0000_02_00_0") choose the card
  388.     * with this id_path_tag.
  389.     */
  390.    if (!strcmp(prime,"1")) {
  391.       free(prime);
  392.       prime = strdup(default_device_id_path_tag);
  393.       /* request a card with a different card than the default card */
  394.       another_tag = 1;
  395.    } else if (!strcmp(default_device_id_path_tag, prime))
  396.       /* we are to get a new fd (render-node) of the same device */
  397.       is_different_device = 0;
  398.  
  399.    device_name = get_render_node_from_id_path_tag(udev,
  400.                                                   prime,
  401.                                                   another_tag);
  402.    if (device_name == NULL) {
  403.       is_different_device = 0;
  404.       goto default_device_clean;
  405.    }
  406.  
  407.    fd = drm_open_device(device_name);
  408.    if (fd >= 0) {
  409.       close(default_fd);
  410.    } else {
  411.       fd = default_fd;
  412.       is_different_device = 0;
  413.    }
  414.    free(device_name);
  415.  
  416.  default_device_clean:
  417.    free(default_device_id_path_tag);
  418.  udev_clean:
  419.    udev_unref(udev);
  420.  prime_clean:
  421.    free(prime);
  422.  
  423.    *different_device = is_different_device;
  424.    return fd;
  425. }
  426. #else
  427. int loader_get_user_preferred_fd(int default_fd, int *different_device)
  428. {
  429.    *different_device = 0;
  430.    return default_fd;
  431. }
  432. #endif
  433.  
  434. #if defined(HAVE_SYSFS)
  435. static int
  436. dev_node_from_fd(int fd, unsigned int *maj, unsigned int *min)
  437. {
  438.    struct stat buf;
  439.  
  440.    if (fstat(fd, &buf) < 0) {
  441.       log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
  442.       return -1;
  443.    }
  444.  
  445.    if (!S_ISCHR(buf.st_mode)) {
  446.       log_(_LOADER_WARNING, "MESA-LOADER: fd %d not a character device\n", fd);
  447.       return -1;
  448.    }
  449.  
  450.    *maj = major(buf.st_rdev);
  451.    *min = minor(buf.st_rdev);
  452.  
  453.    return 0;
  454. }
  455.  
  456. static int
  457. sysfs_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
  458. {
  459.    unsigned int maj, min;
  460.    FILE *f;
  461.    char buf[0x40];
  462.  
  463.    if (dev_node_from_fd(fd, &maj, &min) < 0) {
  464.       *chip_id = -1;
  465.       return 0;
  466.    }
  467.  
  468.    snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/vendor", maj, min);
  469.    if (!(f = fopen(buf, "r"))) {
  470.       *chip_id = -1;
  471.       return 0;
  472.    }
  473.    if (fscanf(f, "%x", vendor_id) != 1) {
  474.       *chip_id = -1;
  475.       fclose(f);
  476.       return 0;
  477.    }
  478.    fclose(f);
  479.    snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/device", maj, min);
  480.    if (!(f = fopen(buf, "r"))) {
  481.       *chip_id = -1;
  482.       return 0;
  483.    }
  484.    if (fscanf(f, "%x", chip_id) != 1) {
  485.       *chip_id = -1;
  486.       fclose(f);
  487.       return 0;
  488.    }
  489.    fclose(f);
  490.    return 1;
  491. }
  492. #endif
  493.  
  494. #if !defined(__NOT_HAVE_DRM_H)
  495. /* for i915 */
  496. #include <i915_drm.h>
  497. /* for radeon */
  498. #include <radeon_drm.h>
  499.  
  500. static int
  501. drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
  502. {
  503.    drmVersionPtr version;
  504.  
  505.    *chip_id = -1;
  506.  
  507.    version = drmGetVersion(fd);
  508.    if (!version) {
  509.       log_(_LOADER_WARNING, "MESA-LOADER: invalid drm fd\n");
  510.       return 0;
  511.    }
  512.    if (!version->name) {
  513.       log_(_LOADER_WARNING, "MESA-LOADER: unable to determine the driver name\n");
  514.       drmFreeVersion(version);
  515.       return 0;
  516.    }
  517.  
  518.    if (strcmp(version->name, "i915") == 0) {
  519.       struct drm_i915_getparam gp;
  520.       int ret;
  521.  
  522.       *vendor_id = 0x8086;
  523.  
  524.       memset(&gp, 0, sizeof(gp));
  525.       gp.param = I915_PARAM_CHIPSET_ID;
  526.       gp.value = chip_id;
  527.       ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
  528.       if (ret) {
  529.          log_(_LOADER_WARNING, "MESA-LOADER: failed to get param for i915\n");
  530.          *chip_id = -1;
  531.       }
  532.    }
  533.    else if (strcmp(version->name, "radeon") == 0) {
  534.       struct drm_radeon_info info;
  535.       int ret;
  536.  
  537.       *vendor_id = 0x1002;
  538.  
  539.       memset(&info, 0, sizeof(info));
  540.       info.request = RADEON_INFO_DEVICE_ID;
  541.       info.value = (unsigned long) chip_id;
  542.       ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
  543.       if (ret) {
  544.          log_(_LOADER_WARNING, "MESA-LOADER: failed to get info for radeon\n");
  545.          *chip_id = -1;
  546.       }
  547.    }
  548.    else if (strcmp(version->name, "nouveau") == 0) {
  549.       *vendor_id = 0x10de;
  550.       /* not used */
  551.       *chip_id = 0;
  552.    }
  553.    else if (strcmp(version->name, "vmwgfx") == 0) {
  554.       *vendor_id = 0x15ad;
  555.       /* assume SVGA II */
  556.       *chip_id = 0x0405;
  557.    }
  558.  
  559.    drmFreeVersion(version);
  560.  
  561.    return (*chip_id >= 0);
  562. }
  563. #endif
  564.  
  565.  
  566. int
  567. loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
  568. {
  569. #if HAVE_LIBUDEV
  570.    if (libudev_get_pci_id_for_fd(fd, vendor_id, chip_id))
  571.       return 1;
  572. #endif
  573. #if HAVE_SYSFS
  574.    if (sysfs_get_pci_id_for_fd(fd, vendor_id, chip_id))
  575.       return 1;
  576. #endif
  577. #if !defined(__NOT_HAVE_DRM_H)
  578.    if (drm_get_pci_id_for_fd(fd, vendor_id, chip_id))
  579.       return 1;
  580. #endif
  581.    return 0;
  582. }
  583.  
  584.  
  585. #ifdef HAVE_LIBUDEV
  586. static char *
  587. libudev_get_device_name_for_fd(int fd)
  588. {
  589.    char *device_name = NULL;
  590.    struct udev *udev;
  591.    struct udev_device *device;
  592.    const char *const_device_name;
  593.    UDEV_SYMBOL(struct udev *, udev_new, (void));
  594.    UDEV_SYMBOL(const char *, udev_device_get_devnode,
  595.                (struct udev_device *));
  596.    UDEV_SYMBOL(struct udev_device *, udev_device_unref,
  597.                (struct udev_device *));
  598.    UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
  599.  
  600.    if (dlsym_failed)
  601.       return NULL;
  602.  
  603.    udev = udev_new();
  604.    device = udev_device_new_from_fd(udev, fd);
  605.    if (device == NULL)
  606.       return NULL;
  607.  
  608.    const_device_name = udev_device_get_devnode(device);
  609.    if (!const_device_name)
  610.       goto out;
  611.    device_name = strdup(const_device_name);
  612.  
  613. out:
  614.    udev_device_unref(device);
  615.    udev_unref(udev);
  616.    return device_name;
  617. }
  618. #endif
  619.  
  620.  
  621. #if HAVE_SYSFS
  622. static char *
  623. sysfs_get_device_name_for_fd(int fd)
  624. {
  625.    char *device_name = NULL;
  626.    unsigned int maj, min;
  627.    FILE *f;
  628.    char buf[0x40];
  629.    static const char match[9] = "\0DEVNAME=";
  630.    int expected = 1;
  631.  
  632.    if (dev_node_from_fd(fd, &maj, &min) < 0)
  633.       return NULL;
  634.  
  635.    snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
  636.    if (!(f = fopen(buf, "r")))
  637.        return NULL;
  638.  
  639.    while (expected < sizeof(match)) {
  640.       int c = getc(f);
  641.  
  642.       if (c == EOF) {
  643.          fclose(f);
  644.          return NULL;
  645.       } else if (c == match[expected] )
  646.          expected++;
  647.       else
  648.          expected = 0;
  649.    }
  650.  
  651.    strcpy(buf, "/dev/");
  652.    if (fgets(buf + 5, sizeof(buf) - 5, f))
  653.       device_name = strdup(buf);
  654.  
  655.    fclose(f);
  656.    return device_name;
  657. }
  658. #endif
  659.  
  660.  
  661. char *
  662. loader_get_device_name_for_fd(int fd)
  663. {
  664.    char *result = NULL;
  665.  
  666. #if HAVE_LIBUDEV
  667.    if ((result = libudev_get_device_name_for_fd(fd)))
  668.       return result;
  669. #endif
  670. #if HAVE_SYSFS
  671.    if ((result = sysfs_get_device_name_for_fd(fd)))
  672.       return result;
  673. #endif
  674.    return result;
  675. }
  676.  
  677. char *
  678. loader_get_driver_for_fd(int fd, unsigned driver_types)
  679. {
  680.    int vendor_id, chip_id, i, j;
  681.    char *driver = NULL;
  682.  
  683.    if (!driver_types)
  684.       driver_types = _LOADER_GALLIUM | _LOADER_DRI;
  685.  
  686.    if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
  687.  
  688. #ifndef __NOT_HAVE_DRM_H
  689.       /* fallback to drmGetVersion(): */
  690.       drmVersionPtr version = drmGetVersion(fd);
  691.  
  692.       if (!version) {
  693.          log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
  694.          return NULL;
  695.       }
  696.  
  697.       driver = strndup(version->name, version->name_len);
  698.       log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
  699.  
  700.       drmFreeVersion(version);
  701. #endif
  702.  
  703.       return driver;
  704.    }
  705.  
  706.    for (i = 0; driver_map[i].driver; i++) {
  707.       if (vendor_id != driver_map[i].vendor_id)
  708.          continue;
  709.  
  710.       if (!(driver_types & driver_map[i].driver_types))
  711.          continue;
  712.  
  713.       if (driver_map[i].predicate && !driver_map[i].predicate(fd))
  714.          continue;
  715.  
  716.       if (driver_map[i].num_chips_ids == -1) {
  717.          driver = strdup(driver_map[i].driver);
  718.          goto out;
  719.       }
  720.  
  721.       for (j = 0; j < driver_map[i].num_chips_ids; j++)
  722.          if (driver_map[i].chip_ids[j] == chip_id) {
  723.             driver = strdup(driver_map[i].driver);
  724.             goto out;
  725.          }
  726.    }
  727.  
  728. out:
  729.    log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
  730.          "pci id for fd %d: %04x:%04x, driver %s\n",
  731.          fd, vendor_id, chip_id, driver);
  732.    return driver;
  733. }
  734.  
  735. void
  736. loader_set_logger(void (*logger)(int level, const char *fmt, ...))
  737. {
  738.    log_ = logger;
  739. }
  740.