]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/libdrm.git/blob - libkms/linux.c
libkms: add dumb support
[glsdk/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  */
33 #include "config.h"
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <xf86drm.h>
38 #include <string.h>
39 #include <unistd.h>
41 #include <sys/stat.h>
43 #include "internal.h"
45 #define PATH_SIZE 512
47 static int
48 linux_name_from_sysfs(int fd, char **out)
49 {
50         char path[PATH_SIZE+1] = ""; /* initialize to please valgrind */
51         char link[PATH_SIZE+1] = "";
52         struct stat buffer;
53         unsigned maj, min;
54         char* slash_name;
55         int ret;
57         /* 
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.
61          *
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.
65          *
66          * Thanks to Ray Strode of Plymouth for the code.
67          */
69         ret = fstat(fd, &buffer);
70         if (ret)
71                 return ret;
73         if (!S_ISCHR(buffer.st_mode))
74                 return -EINVAL;
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)
82                 return -EINVAL;
84         /* link looks something like this: ../../../bus/pci/drivers/intel */
85         slash_name = strrchr(link, '/');
86         if (!slash_name)
87                 return -EINVAL;
89         /* copy name and at the same time remove the slash */
90         *out = strdup(slash_name + 1);
91         return 0;
92 }
94 static int
95 linux_from_sysfs(int fd, struct kms_driver **out)
96 {
97         char *name;
98         int ret;
100         ret = linux_name_from_sysfs(fd, &name);
101         if (ret)
102                 return ret;
104         if (!strcmp(name, "intel"))
105                 ret = intel_create(fd, out);
106 #ifdef HAVE_VMWGFX
107         else if (!strcmp(name, "vmwgfx"))
108                 ret = vmwgfx_create(fd, out);
109 #endif
110 #ifdef HAVE_NOUVEAU
111         else if (!strcmp(name, "nouveau"))
112                 ret = nouveau_create(fd, out);
113 #endif
114 #ifdef HAVE_RADEON
115         else if (!strcmp(name, "radeon"))
116                 ret = radeon_create(fd, out);
117 #endif
118         else
119                 ret = -ENOSYS;
121         free(name);
122         return ret;
125 #if 0
126 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
127 #include <libudev.h>
129 struct create_record
131         unsigned vendor;
132         unsigned chip;
133         int (*func)(int fd, struct kms_driver **out);
134 };
136 static struct create_record table[] = {
137         { 0x8086, 0x2a42, intel_create }, /* i965 */
138 #ifdef HAVE_VMWGFX
139         { 0x15ad, 0x0405, vmwgfx_create }, /* VMware vGPU */
140 #endif
141         { 0, 0, NULL },
142 };
144 static int
145 linux_get_pciid_from_fd(int fd, unsigned *vendor_id, unsigned *chip_id)
147         struct udev *udev;
148         struct udev_device *device;
149         struct udev_device *parent;
150         const char *pci_id;
151         struct stat buffer;
152         int ret;
154         ret = fstat(fd, &buffer);
155         if (ret)
156                 return -EINVAL;
158         if (!S_ISCHR(buffer.st_mode))
159                 return -EINVAL;
161         udev = udev_new();
162         if (!udev)
163                 return -ENOMEM;
165         device = udev_device_new_from_devnum(udev, 'c', buffer.st_rdev);
166         if (!device)
167                 goto err_free_udev;
169         parent = udev_device_get_parent(device);
170         if (!parent)
171                 goto err_free_device;
173         pci_id = udev_device_get_property_value(parent, "PCI_ID");
174         if (!pci_id)
175                 goto err_free_device;
177         if (sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2)
178                 goto err_free_device;
180         udev_device_unref(device);
181         udev_unref(udev);
183         return 0;
185 err_free_device:
186         udev_device_unref(device);
187 err_free_udev:
188         udev_unref(udev);
189         return -EINVAL;
192 static int
193 linux_from_udev(int fd, struct kms_driver **out)
195         unsigned vendor_id, chip_id;
196         int ret, i;
198         ret = linux_get_pciid_from_fd(fd, &vendor_id, &chip_id);
199         if (ret)
200                 return ret;
202         for (i = 0; table[i].func; i++)
203                 if (table[i].vendor == vendor_id && table[i].chip == chip_id)
204                         return table[i].func(fd, out);
206         return -ENOSYS;
208 #else
209 static int
210 linux_from_udev(int fd, struct kms_driver **out)
212         return -ENOSYS;
214 #endif
216 int
217 linux_create(int fd, struct kms_driver **out)
219         if (!dumb_create(fd, out))
220                 return 0;
222         if (!linux_from_udev(fd, out))
223                 return 0;
225         return linux_from_sysfs(fd, out);