OSDN Git Service

add timeline signal/transfer ioctls v2
[android-x86/external-libdrm.git] / libkms / linux.c
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 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <xf86drm.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #ifdef MAJOR_IN_MKDEV
41 #include <sys/mkdev.h>
42 #endif
43 #ifdef MAJOR_IN_SYSMACROS
44 #include <sys/sysmacros.h>
45 #endif
46
47 #include "libdrm_macros.h"
48 #include "internal.h"
49
50 #define PATH_SIZE 512
51
52 static int
53 linux_name_from_sysfs(int fd, char **out)
54 {
55         char path[PATH_SIZE+1] = ""; /* initialize to please valgrind */
56         char link[PATH_SIZE+1] = "";
57         struct stat buffer;
58         unsigned maj, min;
59         char* slash_name;
60         int ret;
61
62         /* 
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.
66          *
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.
70          *
71          * Thanks to Ray Strode of Plymouth for the code.
72          */
73
74         ret = fstat(fd, &buffer);
75         if (ret)
76                 return -EINVAL;
77
78         if (!S_ISCHR(buffer.st_mode))
79                 return -EINVAL;
80
81         maj = major(buffer.st_rdev);
82         min = minor(buffer.st_rdev);
83
84         snprintf(path, PATH_SIZE, "/sys/dev/char/%d:%d/device/driver", maj, min);
85
86         if (readlink(path, link, PATH_SIZE) < 0)
87                 return -EINVAL;
88
89         /* link looks something like this: ../../../bus/pci/drivers/intel */
90         slash_name = strrchr(link, '/');
91         if (!slash_name)
92                 return -EINVAL;
93
94         /* copy name and at the same time remove the slash */
95         *out = strdup(slash_name + 1);
96         return 0;
97 }
98
99 static int
100 linux_from_sysfs(int fd, struct kms_driver **out)
101 {
102         char *name;
103         int ret;
104
105         ret = linux_name_from_sysfs(fd, &name);
106         if (ret)
107                 return ret;
108
109 #if HAVE_INTEL
110         if (!strcmp(name, "intel"))
111                 ret = intel_create(fd, out);
112         else
113 #endif
114 #if HAVE_VMWGFX
115         if (!strcmp(name, "vmwgfx"))
116                 ret = vmwgfx_create(fd, out);
117         else
118 #endif
119 #if HAVE_NOUVEAU
120         if (!strcmp(name, "nouveau"))
121                 ret = nouveau_create(fd, out);
122         else
123 #endif
124 #if HAVE_RADEON
125         if (!strcmp(name, "radeon"))
126                 ret = radeon_create(fd, out);
127         else
128 #endif
129 #if HAVE_EXYNOS
130         if (!strcmp(name, "exynos"))
131                 ret = exynos_create(fd, out);
132         else
133 #endif
134                 ret = -ENOSYS;
135
136         free(name);
137         return ret;
138 }
139
140 drm_private int
141 linux_create(int fd, struct kms_driver **out)
142 {
143         if (!dumb_create(fd, out))
144                 return 0;
145
146         return linux_from_sysfs(fd, out);
147 }