glx: use the loader util lib
[mesa.git] / src / gbm / backends / dri / driver_name.c
1 /*
2 * Copyright © 2011 Intel Corporation
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,
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.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 * Benjamin Franzke <benjaminfranzke@googlemail.com>
27 */
28
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <libudev.h>
33
34 #include "gbm_driint.h"
35 #define DRIVER_MAP_DRI2_ONLY
36 #include "pci_ids/pci_id_driver_map.h"
37
38 char *
39 dri_fd_get_driver_name(int fd)
40 {
41 struct udev *udev;
42 struct udev_device *device, *parent;
43 const char *pci_id;
44 char *driver = NULL;
45 int vendor_id, chip_id, i, j;
46
47 udev = udev_new();
48 device = _gbm_udev_device_new_from_fd(udev, fd);
49 if (device == NULL)
50 return NULL;
51
52 parent = udev_device_get_parent(device);
53 if (parent == NULL) {
54 fprintf(stderr, "gbm: could not get parent device");
55 goto out;
56 }
57
58 pci_id = udev_device_get_property_value(parent, "PCI_ID");
59 if (pci_id == NULL ||
60 sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2) {
61 fprintf(stderr, "gbm: malformed or no PCI ID");
62 goto out;
63 }
64
65 for (i = 0; driver_map[i].driver; i++) {
66 if (vendor_id != driver_map[i].vendor_id)
67 continue;
68 if (driver_map[i].num_chips_ids == -1) {
69 driver = strdup(driver_map[i].driver);
70 _gbm_log("pci id for %d: %04x:%04x, driver %s",
71 fd, vendor_id, chip_id, driver);
72 goto out;
73 }
74
75 for (j = 0; j < driver_map[i].num_chips_ids; j++)
76 if (driver_map[i].chip_ids[j] == chip_id) {
77 driver = strdup(driver_map[i].driver);
78 _gbm_log("pci id for %d: %04x:%04x, driver %s",
79 fd, vendor_id, chip_id, driver);
80 goto out;
81 }
82 }
83
84 out:
85 udev_device_unref(device);
86 udev_unref(udev);
87
88 return driver;
89 }