1 /**************************************************************************
3 * Copyright 2011 Intel Corporation
4 * Copyright 2012 Francisco Jerez
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Kristian Høgsberg <krh@bitplanet.net>
29 * Benjamin Franzke <benjaminfranzke@googlemail.com>
31 **************************************************************************/
38 #include "state_tracker/drm_driver.h"
39 #include "pipe_loader_priv.h"
41 #include "util/u_memory.h"
42 #include "util/u_dl.h"
43 #include "util/u_debug.h"
45 #define DRIVER_MAP_GALLIUM_ONLY
46 #include "pci_ids/pci_id_driver_map.h"
48 struct pipe_loader_drm_device
{
49 struct pipe_loader_device base
;
50 struct util_dl_library
*lib
;
54 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
57 find_drm_pci_id(struct pipe_loader_drm_device
*ddev
)
59 struct udev
*udev
= NULL
;
60 struct udev_device
*parent
, *device
= NULL
;
64 if (fstat(ddev
->fd
, &stat
) < 0)
71 device
= udev_device_new_from_devnum(udev
, 'c', stat
.st_rdev
);
75 parent
= udev_device_get_parent(device
);
79 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
81 sscanf(pci_id
, "%x:%x", &ddev
->base
.pci
.vendor_id
,
82 &ddev
->base
.pci
.chip_id
) != 2)
89 udev_device_unref(device
);
93 debug_printf("pci id for fd %d not found\n", ddev
->fd
);
98 find_drm_driver_name(struct pipe_loader_drm_device
*ddev
)
100 struct pipe_loader_device
*dev
= &ddev
->base
;
103 for (i
= 0; driver_map
[i
].driver
; i
++) {
104 if (dev
->pci
.vendor_id
!= driver_map
[i
].vendor_id
)
107 if (driver_map
[i
].num_chips_ids
== -1) {
108 dev
->driver_name
= driver_map
[i
].driver
;
112 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++) {
113 if (dev
->pci
.chip_id
== driver_map
[i
].chip_ids
[j
]) {
114 dev
->driver_name
= driver_map
[i
].driver
;
123 debug_printf("driver for %04x:%04x: %s\n", dev
->pci
.vendor_id
,
124 dev
->pci
.chip_id
, dev
->driver_name
);
128 static struct pipe_loader_ops pipe_loader_drm_ops
;
131 pipe_loader_drm_probe_fd(struct pipe_loader_device
**dev
, int fd
)
133 struct pipe_loader_drm_device
*ddev
= CALLOC_STRUCT(pipe_loader_drm_device
);
135 ddev
->base
.type
= PIPE_LOADER_DEVICE_PCI
;
136 ddev
->base
.ops
= &pipe_loader_drm_ops
;
139 if (!find_drm_pci_id(ddev
))
142 if (!find_drm_driver_name(ddev
))
154 open_drm_minor(int minor
)
157 snprintf(path
, sizeof(path
), DRM_DEV_NAME
, DRM_DIR_NAME
, minor
);
158 return open(path
, O_RDWR
, 0);
162 pipe_loader_drm_probe(struct pipe_loader_device
**devs
, int ndev
)
166 for (i
= 0, j
= 0; i
< DRM_MAX_MINOR
; i
++) {
167 fd
= open_drm_minor(i
);
171 if (j
>= ndev
|| !pipe_loader_drm_probe_fd(&devs
[j
], fd
))
181 pipe_loader_drm_release(struct pipe_loader_device
**dev
)
183 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(*dev
);
186 util_dl_close(ddev
->lib
);
193 static struct pipe_screen
*
194 pipe_loader_drm_create_screen(struct pipe_loader_device
*dev
,
195 const char *library_paths
)
197 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(dev
);
198 const struct drm_driver_descriptor
*dd
;
201 ddev
->lib
= pipe_loader_find_module(dev
, library_paths
);
205 dd
= (const struct drm_driver_descriptor
*)
206 util_dl_get_proc_address(ddev
->lib
, "driver_descriptor");
208 /* sanity check on the name */
209 if (!dd
|| strcmp(dd
->name
, ddev
->base
.driver_name
) != 0)
212 return dd
->create_screen(ddev
->fd
);
215 static struct pipe_loader_ops pipe_loader_drm_ops
= {
216 .create_screen
= pipe_loader_drm_create_screen
,
217 .release
= pipe_loader_drm_release