1 /**************************************************************************
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Thanks to krh and jcristau for the tips on
29 * going from fd to pci id via fstat and udev.
48 linux_name_from_sysfs(int fd, char **out)
50 char path[PATH_SIZE+1] = ""; /* initialize to please valgrind */
51 char link[PATH_SIZE+1] = "";
58 * Inside the sysfs directory for the device there is a symlink
59 * to the directory representing the driver module, that path
60 * happens to hold the name of the driver.
62 * So lets get the symlink for the drm device. Then read the link
63 * and filter out the last directory which happens to be the name
64 * of the driver, which we can use to load the correct interface.
66 * Thanks to Ray Strode of Plymouth for the code.
69 ret = fstat(fd, &buffer);
73 if (!S_ISCHR(buffer.st_mode))
76 maj = major(buffer.st_rdev);
77 min = minor(buffer.st_rdev);
79 snprintf(path, PATH_SIZE, "/sys/dev/char/%d:%d/device/driver", maj, min);
81 if (readlink(path, link, PATH_SIZE) < 0)
84 /* link looks something like this: ../../../bus/pci/drivers/intel */
85 slash_name = strrchr(link, '/');
89 /* copy name and at the same time remove the slash */
90 *out = strdup(slash_name + 1);
95 linux_from_sysfs(int fd, struct kms_driver **out)
100 ret = linux_name_from_sysfs(fd, &name);
104 if (!strcmp(name, "intel"))
105 ret = intel_create(fd, out);
107 else if (!strcmp(name, "vmwgfx"))
108 ret = vmwgfx_create(fd, out);
111 else if (!strcmp(name, "nouveau"))
112 ret = nouveau_create(fd, out);
115 else if (!strcmp(name, "radeon"))
116 ret = radeon_create(fd, out);
119 else if (!strcmp(name, "exynos"))
120 ret = exynos_create(fd, out);
130 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
137 int (*func)(int fd, struct kms_driver **out);
140 static struct create_record table[] = {
141 { 0x8086, 0x2a42, intel_create }, /* i965 */
143 { 0x15ad, 0x0405, vmwgfx_create }, /* VMware vGPU */
149 linux_get_pciid_from_fd(int fd, unsigned *vendor_id, unsigned *chip_id)
152 struct udev_device *device;
153 struct udev_device *parent;
158 ret = fstat(fd, &buffer);
162 if (!S_ISCHR(buffer.st_mode))
169 device = udev_device_new_from_devnum(udev, 'c', buffer.st_rdev);
173 parent = udev_device_get_parent(device);
175 goto err_free_device;
177 pci_id = udev_device_get_property_value(parent, "PCI_ID");
179 goto err_free_device;
181 if (sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2)
182 goto err_free_device;
184 udev_device_unref(device);
190 udev_device_unref(device);
197 linux_from_udev(int fd, struct kms_driver **out)
199 unsigned vendor_id, chip_id;
202 ret = linux_get_pciid_from_fd(fd, &vendor_id, &chip_id);
206 for (i = 0; table[i].func; i++)
207 if (table[i].vendor == vendor_id && table[i].chip == chip_id)
208 return table[i].func(fd, out);
214 linux_from_udev(int fd, struct kms_driver **out)
221 linux_create(int fd, struct kms_driver **out)
223 if (!dumb_create(fd, out))
226 if (!linux_from_udev(fd, out))
229 return linux_from_sysfs(fd, out);