aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakob Bornecrantz2010-01-07 20:51:04 -0600
committerJakob Bornecrantz2010-01-07 21:05:00 -0600
commit9042d72109d8ae448b8e0f23b93067cc37deff23 (patch)
tree3b23755f0161f3c40531ffee33bfe84afc9bcef8 /libkms/linux.c
parent04f90a44709a48fb932ea954011cb551659bf246 (diff)
downloadexternal-libdrm-9042d72109d8ae448b8e0f23b93067cc37deff23.tar.gz
external-libdrm-9042d72109d8ae448b8e0f23b93067cc37deff23.tar.xz
external-libdrm-9042d72109d8ae448b8e0f23b93067cc37deff23.zip
libkms: Pick driver backend via pci id that we get from udev
Diffstat (limited to 'libkms/linux.c')
-rw-r--r--libkms/linux.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/libkms/linux.c b/libkms/linux.c
new file mode 100644
index 00000000..2b08b854
--- /dev/null
+++ b/libkms/linux.c
@@ -0,0 +1,88 @@
1/**************************************************************************
2 *
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Thanks to krh and jcristau for the tips on
29 * going from fd to pci id via fstat and udev.
30 */
31
32
33#include <errno.h>
34#include <stdio.h>
35#include <xf86drm.h>
36#include <sys/stat.h>
37
38#include "internal.h"
39
40#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
41#include <libudev.h>
42
43int linux_get_pciid_from_fd(int fd, unsigned *vendor_id, unsigned *chip_id)
44{
45 struct udev *udev;
46 struct udev_device *device;
47 struct udev_device *parent;
48 const char *pci_id;
49 struct stat buffer;
50 int ret;
51
52 ret = fstat(fd, &buffer);
53 if (ret)
54 return -EINVAL;
55
56 if (!S_ISCHR(buffer.st_mode))
57 return -EINVAL;
58
59 udev = udev_new();
60 if (!udev)
61 return -ENOMEM;
62
63 device = udev_device_new_from_devnum(udev, 'c', buffer.st_rdev);
64 if (!device)
65 goto err_free_udev;
66
67 parent = udev_device_get_parent(device);
68 if (!parent)
69 goto err_free_device;
70
71 pci_id = udev_device_get_property_value(parent, "PCI_ID");
72 if (!pci_id)
73 goto err_free_device;
74
75 if (sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2)
76 goto err_free_device;
77
78 udev_device_unref(device);
79 udev_unref(udev);
80
81 return 0;
82
83err_free_device:
84 udev_device_unref(device);
85err_free_udev:
86 udev_unref(udev);
87 return -EINVAL;
88}