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.
39 #include <sys/types.h>
41 #include <sys/mkdev.h>
43 #ifdef MAJOR_IN_SYSMACROS
44 #include <sys/sysmacros.h>
47 #include "libdrm_macros.h"
53 linux_name_from_sysfs(int fd, char **out)
55 char path[PATH_SIZE+1] = ""; /* initialize to please valgrind */
56 char link[PATH_SIZE+1] = "";
63 * Inside the sysfs directory for the device there is a symlink
64 * to the directory representing the driver module, that path
65 * happens to hold the name of the driver.
67 * So lets get the symlink for the drm device. Then read the link
68 * and filter out the last directory which happens to be the name
69 * of the driver, which we can use to load the correct interface.
71 * Thanks to Ray Strode of Plymouth for the code.
74 ret = fstat(fd, &buffer);
78 if (!S_ISCHR(buffer.st_mode))
81 maj = major(buffer.st_rdev);
82 min = minor(buffer.st_rdev);
84 snprintf(path, PATH_SIZE, "/sys/dev/char/%d:%d/device/driver", maj, min);
86 if (readlink(path, link, PATH_SIZE) < 0)
89 /* link looks something like this: ../../../bus/pci/drivers/intel */
90 slash_name = strrchr(link, '/');
94 /* copy name and at the same time remove the slash */
95 *out = strdup(slash_name + 1);
100 linux_from_sysfs(int fd, struct kms_driver **out)
105 ret = linux_name_from_sysfs(fd, &name);
110 if (!strcmp(name, "intel"))
111 ret = intel_create(fd, out);
115 if (!strcmp(name, "vmwgfx"))
116 ret = vmwgfx_create(fd, out);
120 if (!strcmp(name, "nouveau"))
121 ret = nouveau_create(fd, out);
125 if (!strcmp(name, "radeon"))
126 ret = radeon_create(fd, out);
130 if (!strcmp(name, "exynos"))
131 ret = exynos_create(fd, out);
141 linux_create(int fd, struct kms_driver **out)
143 if (!dumb_create(fd, out))
146 return linux_from_sysfs(fd, out);