main drm.c
 1/* wld: drm.c
 2 *
 3 * Copyright (c) 2013, 2014 Michael Forney
 4 *
 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
 6 * of this software and associated documentation files (the "Software"), to deal
 7 * in the Software without restriction, including without limitation the rights
 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#include "drm.h"
25#include "drm-private.h"
26
27#include <xf86drm.h>
28
29const static struct drm_driver *drivers[] = {
30#if WITH_DRM_INTEL
31	&intel_drm_driver,
32#endif
33#if WITH_DRM_NOUVEAU
34	&nouveau_drm_driver,
35#endif
36	&dumb_drm_driver
37};
38
39static const struct drm_driver *
40find_driver(int fd)
41{
42	drmDevicePtr device = NULL;
43	uint32_t vendor_id, device_id;
44	uint32_t index;
45	const struct drm_driver *driver = NULL;
46
47	if (drmGetDevice2(fd, 0, &device) != 0)
48		return NULL;
49
50	if (device->bustype != DRM_BUS_PCI || !device->deviceinfo.pci)
51		goto out;
52
53	vendor_id = device->deviceinfo.pci->vendor_id;
54	device_id = device->deviceinfo.pci->device_id;
55
56	for (index = 0; index < ARRAY_LENGTH(drivers); ++index) {
57		DEBUG("Trying DRM driver `%s'\n", drivers[index]->name);
58		if (drivers[index]->device_supported(vendor_id, device_id)) {
59			driver = drivers[index];
60			break;
61		}
62	}
63
64out:
65	drmFreeDevice(&device);
66	return driver;
67}
68
69EXPORT
70struct wld_context *
71wld_drm_create_context(int fd)
72{
73	const struct drm_driver *driver;
74	struct wld_context *context;
75
76	if (!getenv("WLD_DRM_DUMB")) {
77		driver = find_driver(fd);
78		if (driver) {
79			context = driver->create_context(fd);
80			if (context)
81				return context;
82		}
83	}
84
85	DEBUG("Falling back to dumb DRM driver\n");
86	return dumb_drm_driver.create_context(fd);
87}
88
89EXPORT
90bool
91wld_drm_is_dumb(struct wld_context *context)
92{
93	return context->impl == dumb_context_impl;
94}